- add sources.
[platform/framework/web/crosswalk.git] / src / sync / sessions / sync_session_unittest.cc
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
21 namespace syncer {
22
23 using syncable::WriteTransaction;
24
25 namespace sessions {
26 namespace {
27
28 class 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 OnShouldStopSyncingPermanently() OVERRIDE {
108     FailControllerInvocationIfDisabled("OnShouldStopSyncingPermanently");
109   }
110   virtual void OnSyncProtocolError(
111       const sessions::SyncSessionSnapshot& snapshot) OVERRIDE {
112     FailControllerInvocationIfDisabled("SyncProtocolError");
113   }
114
115   void GetWorkers(std::vector<ModelSafeWorker*>* out) const {
116     out->clear();
117     for (std::vector<scoped_refptr<ModelSafeWorker> >::const_iterator it =
118              workers_.begin(); it != workers_.end(); ++it) {
119       out->push_back(it->get());
120     }
121   }
122   void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) const {
123     *out = routes_;
124   }
125
126   StatusController* status() { return session_->mutable_status_controller(); }
127  protected:
128   void FailControllerInvocationIfDisabled(const std::string& msg) {
129     if (!controller_invocations_allowed_)
130       FAIL() << msg;
131   }
132
133   ModelTypeSet ParamsMeaningAllEnabledTypes() {
134     ModelTypeSet request_params(BOOKMARKS, AUTOFILL);
135     return request_params;
136   }
137
138   ModelTypeSet ParamsMeaningJustOneEnabledType() {
139     return ModelTypeSet(AUTOFILL);
140   }
141
142   base::MessageLoop message_loop_;
143   bool controller_invocations_allowed_;
144   scoped_ptr<SyncSession> session_;
145   scoped_ptr<SyncSessionContext> context_;
146   std::vector<scoped_refptr<ModelSafeWorker> > workers_;
147   ModelSafeRoutingInfo routes_;
148   scoped_refptr<ExtensionsActivity> extensions_activity_;
149 };
150
151 TEST_F(SyncSessionTest, MoreToDownloadIfDownloadFailed) {
152   status()->set_updates_request_types(ParamsMeaningAllEnabledTypes());
153
154   status()->set_last_download_updates_result(NETWORK_IO_ERROR);
155
156   // When DownloadUpdatesCommand fails, these should be false.
157   EXPECT_FALSE(status()->ServerSaysNothingMoreToDownload());
158   EXPECT_FALSE(status()->download_updates_succeeded());
159 }
160
161 TEST_F(SyncSessionTest, MoreToDownloadIfGotChangesRemaining) {
162   status()->set_updates_request_types(ParamsMeaningAllEnabledTypes());
163
164   // When the server returns changes_remaining, that means there's
165   // more to download.
166   status()->set_last_download_updates_result(SYNCER_OK);
167   status()->mutable_updates_response()->mutable_get_updates()
168      ->set_changes_remaining(1000L);
169   EXPECT_FALSE(status()->ServerSaysNothingMoreToDownload());
170   EXPECT_TRUE(status()->download_updates_succeeded());
171 }
172
173 TEST_F(SyncSessionTest, MoreToDownloadIfGotNoChangesRemaining) {
174   status()->set_updates_request_types(ParamsMeaningAllEnabledTypes());
175
176   status()->set_last_download_updates_result(SYNCER_OK);
177   status()->mutable_updates_response()->mutable_get_updates()
178       ->set_changes_remaining(0);
179   EXPECT_TRUE(status()->ServerSaysNothingMoreToDownload());
180   EXPECT_TRUE(status()->download_updates_succeeded());
181 }
182
183 }  // namespace
184 }  // namespace sessions
185 }  // namespace syncer