1// Copyright 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "sync/sessions/sync_session.h"
6
7#include "base/compiler_specific.h"
8#include "base/location.h"
9#include "base/memory/ref_counted.h"
10#include "base/message_loop/message_loop.h"
11#include "sync/engine/syncer_types.h"
12#include "sync/internal_api/public/base/model_type.h"
13#include "sync/sessions/status_controller.h"
14#include "sync/syncable/syncable_id.h"
15#include "sync/syncable/syncable_write_transaction.h"
16#include "sync/test/engine/fake_model_worker.h"
17#include "sync/test/engine/test_directory_setter_upper.h"
18#include "sync/util/extensions_activity.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21namespace syncer {
22
23using syncable::WriteTransaction;
24
25namespace sessions {
26namespace {
27
28class SyncSessionTest : public testing::Test,
29                        public SyncSession::Delegate {
30 public:
31  SyncSessionTest() : controller_invocations_allowed_(false) {}
32
33  SyncSession* MakeSession() {
34    return SyncSession::Build(context_.get(), this);
35  }
36
37  virtual void SetUp() {
38    extensions_activity_ = new ExtensionsActivity();
39
40    routes_.clear();
41    routes_[BOOKMARKS] = GROUP_UI;
42    routes_[AUTOFILL] = GROUP_DB;
43    scoped_refptr<ModelSafeWorker> passive_worker(
44        new FakeModelWorker(GROUP_PASSIVE));
45    scoped_refptr<ModelSafeWorker> ui_worker(
46        new FakeModelWorker(GROUP_UI));
47    scoped_refptr<ModelSafeWorker> db_worker(
48        new FakeModelWorker(GROUP_DB));
49    workers_.clear();
50    workers_.push_back(passive_worker);
51    workers_.push_back(ui_worker);
52    workers_.push_back(db_worker);
53
54    std::vector<ModelSafeWorker*> workers;
55    GetWorkers(&workers);
56
57    context_.reset(
58        new SyncSessionContext(
59            NULL,
60            NULL,
61            workers,
62            extensions_activity_.get(),
63            std::vector<SyncEngineEventListener*>(),
64            NULL,
65            NULL,
66            true,  // enable keystore encryption
67            false,  // force enable pre-commit GU avoidance experiment
68            "fake_invalidator_client_id"));
69    context_->set_routing_info(routes_);
70
71    session_.reset(MakeSession());
72  }
73  virtual void TearDown() {
74    session_.reset();
75    context_.reset();
76  }
77
78  virtual void OnThrottled(const base::TimeDelta& throttle_duration) OVERRIDE {
79    FailControllerInvocationIfDisabled("OnThrottled");
80  }
81  virtual void OnTypesThrottled(
82      ModelTypeSet types,
83      const base::TimeDelta& throttle_duration) OVERRIDE {
84    FailControllerInvocationIfDisabled("OnTypesThrottled");
85  }
86  virtual bool IsCurrentlyThrottled() OVERRIDE {
87    FailControllerInvocationIfDisabled("IsSyncingCurrentlySilenced");
88    return false;
89  }
90  virtual void OnReceivedLongPollIntervalUpdate(
91      const base::TimeDelta& new_interval) OVERRIDE {
92    FailControllerInvocationIfDisabled("OnReceivedLongPollIntervalUpdate");
93  }
94  virtual void OnReceivedShortPollIntervalUpdate(
95      const base::TimeDelta& new_interval) OVERRIDE {
96    FailControllerInvocationIfDisabled("OnReceivedShortPollIntervalUpdate");
97  }
98  virtual void OnReceivedSessionsCommitDelay(
99      const base::TimeDelta& new_delay) OVERRIDE {
100    FailControllerInvocationIfDisabled("OnReceivedSessionsCommitDelay");
101  }
102  virtual void OnReceivedClientInvalidationHintBufferSize(
103      int size) OVERRIDE {
104    FailControllerInvocationIfDisabled(
105        "OnReceivedClientInvalidationHintBufferSize");
106  }
107  virtual void OnSyncProtocolError(
108      const sessions::SyncSessionSnapshot& snapshot) OVERRIDE {
109    FailControllerInvocationIfDisabled("SyncProtocolError");
110  }
111
112  void GetWorkers(std::vector<ModelSafeWorker*>* out) const {
113    out->clear();
114    for (std::vector<scoped_refptr<ModelSafeWorker> >::const_iterator it =
115             workers_.begin(); it != workers_.end(); ++it) {
116      out->push_back(it->get());
117    }
118  }
119  void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) const {
120    *out = routes_;
121  }
122
123  StatusController* status() { return session_->mutable_status_controller(); }
124 protected:
125  void FailControllerInvocationIfDisabled(const std::string& msg) {
126    if (!controller_invocations_allowed_)
127      FAIL() << msg;
128  }
129
130  ModelTypeSet ParamsMeaningAllEnabledTypes() {
131    ModelTypeSet request_params(BOOKMARKS, AUTOFILL);
132    return request_params;
133  }
134
135  ModelTypeSet ParamsMeaningJustOneEnabledType() {
136    return ModelTypeSet(AUTOFILL);
137  }
138
139  base::MessageLoop message_loop_;
140  bool controller_invocations_allowed_;
141  scoped_ptr<SyncSession> session_;
142  scoped_ptr<SyncSessionContext> context_;
143  std::vector<scoped_refptr<ModelSafeWorker> > workers_;
144  ModelSafeRoutingInfo routes_;
145  scoped_refptr<ExtensionsActivity> extensions_activity_;
146};
147
148}  // namespace
149}  // namespace sessions
150}  // namespace syncer
151