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.
5 #include "sync/sessions/sync_session.h"
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"
23 using syncable::WriteTransaction;
28 class SyncSessionTest : public testing::Test,
29 public SyncSession::Delegate {
31 SyncSessionTest() : controller_invocations_allowed_(false) {}
33 SyncSession* MakeSession() {
34 return SyncSession::Build(context_.get(), this);
37 virtual void SetUp() {
38 extensions_activity_ = new ExtensionsActivity();
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));
50 workers_.push_back(passive_worker);
51 workers_.push_back(ui_worker);
52 workers_.push_back(db_worker);
54 std::vector<ModelSafeWorker*> workers;
58 new SyncSessionContext(
62 extensions_activity_.get(),
63 std::vector<SyncEngineEventListener*>(),
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_);
71 session_.reset(MakeSession());
73 virtual void TearDown() {
78 virtual void OnThrottled(const base::TimeDelta& throttle_duration) OVERRIDE {
79 FailControllerInvocationIfDisabled("OnThrottled");
81 virtual void OnTypesThrottled(
83 const base::TimeDelta& throttle_duration) OVERRIDE {
84 FailControllerInvocationIfDisabled("OnTypesThrottled");
86 virtual bool IsCurrentlyThrottled() OVERRIDE {
87 FailControllerInvocationIfDisabled("IsSyncingCurrentlySilenced");
90 virtual void OnReceivedLongPollIntervalUpdate(
91 const base::TimeDelta& new_interval) OVERRIDE {
92 FailControllerInvocationIfDisabled("OnReceivedLongPollIntervalUpdate");
94 virtual void OnReceivedShortPollIntervalUpdate(
95 const base::TimeDelta& new_interval) OVERRIDE {
96 FailControllerInvocationIfDisabled("OnReceivedShortPollIntervalUpdate");
98 virtual void OnReceivedSessionsCommitDelay(
99 const base::TimeDelta& new_delay) OVERRIDE {
100 FailControllerInvocationIfDisabled("OnReceivedSessionsCommitDelay");
102 virtual void OnReceivedClientInvalidationHintBufferSize(
104 FailControllerInvocationIfDisabled(
105 "OnReceivedClientInvalidationHintBufferSize");
107 virtual void OnShouldStopSyncingPermanently() OVERRIDE {
108 FailControllerInvocationIfDisabled("OnShouldStopSyncingPermanently");
110 virtual void OnSyncProtocolError(
111 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE {
112 FailControllerInvocationIfDisabled("SyncProtocolError");
115 void GetWorkers(std::vector<ModelSafeWorker*>* out) const {
117 for (std::vector<scoped_refptr<ModelSafeWorker> >::const_iterator it =
118 workers_.begin(); it != workers_.end(); ++it) {
119 out->push_back(it->get());
122 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) const {
126 StatusController* status() { return session_->mutable_status_controller(); }
128 void FailControllerInvocationIfDisabled(const std::string& msg) {
129 if (!controller_invocations_allowed_)
133 ModelTypeSet ParamsMeaningAllEnabledTypes() {
134 ModelTypeSet request_params(BOOKMARKS, AUTOFILL);
135 return request_params;
138 ModelTypeSet ParamsMeaningJustOneEnabledType() {
139 return ModelTypeSet(AUTOFILL);
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_;
151 TEST_F(SyncSessionTest, MoreToDownloadIfDownloadFailed) {
152 status()->set_updates_request_types(ParamsMeaningAllEnabledTypes());
154 status()->set_last_download_updates_result(NETWORK_IO_ERROR);
156 // When DownloadUpdatesCommand fails, these should be false.
157 EXPECT_FALSE(status()->ServerSaysNothingMoreToDownload());
158 EXPECT_FALSE(status()->download_updates_succeeded());
161 TEST_F(SyncSessionTest, MoreToDownloadIfGotChangesRemaining) {
162 status()->set_updates_request_types(ParamsMeaningAllEnabledTypes());
164 // When the server returns changes_remaining, that means there's
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());
173 TEST_F(SyncSessionTest, MoreToDownloadIfGotNoChangesRemaining) {
174 status()->set_updates_request_types(ParamsMeaningAllEnabledTypes());
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());
184 } // namespace sessions
185 } // namespace syncer