| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 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/engine/sync_session_job.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/time.h" | |
| 10 #include "sync/internal_api/public/base/model_type_invalidation_map.h" | |
| 11 #include "sync/sessions/sync_session.h" | |
| 12 #include "sync/sessions/sync_session_context.h" | |
| 13 #include "sync/sessions/test_util.h" | |
| 14 #include "sync/test/engine/fake_model_worker.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 using base::TimeTicks; | |
| 19 | |
| 20 namespace syncer { | |
| 21 | |
| 22 using sessions::SyncSession; | |
| 23 | |
| 24 class MockDelegate : public SyncSession::Delegate { | |
| 25 public: | |
| 26 MockDelegate() {} | |
| 27 ~MockDelegate() {} | |
| 28 | |
| 29 MOCK_METHOD0(IsSyncingCurrentlySilenced, bool()); | |
| 30 MOCK_METHOD1(OnReceivedShortPollIntervalUpdate, void(const base::TimeDelta&)); | |
| 31 MOCK_METHOD1(OnReceivedLongPollIntervalUpdate ,void(const base::TimeDelta&)); | |
| 32 MOCK_METHOD1(OnReceivedSessionsCommitDelay, void(const base::TimeDelta&)); | |
| 33 MOCK_METHOD1(OnSyncProtocolError, void(const sessions::SyncSessionSnapshot&)); | |
| 34 MOCK_METHOD0(OnShouldStopSyncingPermanently, void()); | |
| 35 MOCK_METHOD1(OnSilencedUntil, void(const base::TimeTicks&)); | |
| 36 }; | |
| 37 | |
| 38 class SyncSessionJobTest : public testing::Test { | |
| 39 public: | |
| 40 SyncSessionJobTest() : config_params_callback_invoked_(false) {} | |
| 41 virtual void SetUp() { | |
| 42 routes_.clear(); | |
| 43 workers_.clear(); | |
| 44 config_params_callback_invoked_ = false; | |
| 45 routes_[BOOKMARKS] = GROUP_PASSIVE; | |
| 46 scoped_refptr<ModelSafeWorker> passive_worker( | |
| 47 new FakeModelWorker(GROUP_PASSIVE)); | |
| 48 workers_.push_back(passive_worker); | |
| 49 std::vector<ModelSafeWorker*> workers; | |
| 50 GetWorkers(&workers); | |
| 51 context_.reset(new sessions::SyncSessionContext( | |
| 52 NULL, // |connection_manager| | |
| 53 NULL, // |directory| | |
| 54 workers, | |
| 55 NULL, // |extensions_activity_monitor| | |
| 56 NULL, // |throttled_data_type_tracker| | |
| 57 std::vector<SyncEngineEventListener*>(), | |
| 58 NULL, // |debug_info_getter| | |
| 59 NULL, // |traffic_recorder| | |
| 60 true, // |enable keystore encryption| | |
| 61 "fake_invalidator_client_id")); | |
| 62 context_->set_routing_info(routes_); | |
| 63 } | |
| 64 | |
| 65 scoped_ptr<SyncSession> MakeSession() { | |
| 66 sessions::SyncSourceInfo info(MakeSourceInfo()); | |
| 67 std::vector<sessions::SyncSourceInfo> sources_list; | |
| 68 return scoped_ptr<SyncSession>( | |
| 69 new SyncSession(context_.get(), &delegate_, info)); | |
| 70 } | |
| 71 | |
| 72 sessions::SyncSourceInfo MakeSourceInfo() { | |
| 73 sessions::SyncSourceInfo info(sync_pb::GetUpdatesCallerInfo::LOCAL, | |
| 74 ModelTypeInvalidationMap()); | |
| 75 return info; | |
| 76 } | |
| 77 | |
| 78 ModelTypeSet ParamsMeaningAllEnabledTypes() { | |
| 79 ModelTypeSet request_params(BOOKMARKS, AUTOFILL); | |
| 80 return request_params; | |
| 81 } | |
| 82 | |
| 83 ModelTypeSet ParamsMeaningJustOneEnabledType() { | |
| 84 return ModelTypeSet(AUTOFILL); | |
| 85 } | |
| 86 | |
| 87 void GetWorkers(std::vector<ModelSafeWorker*>* out) const { | |
| 88 out->clear(); | |
| 89 for (std::vector<scoped_refptr<ModelSafeWorker> >::const_iterator it = | |
| 90 workers_.begin(); it != workers_.end(); ++it) { | |
| 91 out->push_back(it->get()); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void ConfigurationParamsCallback() { | |
| 96 config_params_callback_invoked_ = true; | |
| 97 } | |
| 98 | |
| 99 bool config_params_callback_invoked() const { | |
| 100 return config_params_callback_invoked_; | |
| 101 } | |
| 102 | |
| 103 sessions::SyncSessionContext* context() { return context_.get(); } | |
| 104 SyncSession::Delegate* delegate() { return &delegate_; } | |
| 105 const ModelSafeRoutingInfo& routes() { return routes_; } | |
| 106 | |
| 107 // Checks that the two jobs are "clones" as defined by SyncSessionJob. | |
| 108 void ExpectClones(SyncSessionJob* job, SyncSessionJob* clone) { | |
| 109 EXPECT_EQ(job->purpose(), clone->purpose()); | |
| 110 EXPECT_EQ(job->scheduled_start(), clone->scheduled_start()); | |
| 111 EXPECT_EQ(job->start_step(), clone->start_step()); | |
| 112 EXPECT_EQ(job->end_step(), clone->end_step()); | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 scoped_ptr<sessions::SyncSessionContext> context_; | |
| 117 std::vector<scoped_refptr<ModelSafeWorker> > workers_; | |
| 118 MockDelegate delegate_; | |
| 119 ModelSafeRoutingInfo routes_; | |
| 120 bool config_params_callback_invoked_; | |
| 121 }; | |
| 122 | |
| 123 TEST_F(SyncSessionJobTest, FinishCallsReadyTask) { | |
| 124 ConfigurationParams params; | |
| 125 params.ready_task = base::Bind( | |
| 126 &SyncSessionJobTest::ConfigurationParamsCallback, | |
| 127 base::Unretained(this)); | |
| 128 | |
| 129 sessions::SyncSourceInfo info( | |
| 130 sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, | |
| 131 ModelTypeInvalidationMap()); | |
| 132 scoped_ptr<SyncSession> session( | |
| 133 new SyncSession(context(), delegate(), info)); | |
| 134 | |
| 135 SyncSessionJob job1( | |
| 136 SyncSessionJob::CONFIGURATION, TimeTicks::Now(), info, params); | |
| 137 sessions::test_util::SimulateSuccess(session.get(), | |
| 138 job1.start_step(), | |
| 139 job1.end_step()); | |
| 140 job1.Finish(false, session.get()); | |
| 141 EXPECT_TRUE(config_params_callback_invoked()); | |
| 142 } | |
| 143 | |
| 144 TEST_F(SyncSessionJobTest, CoalesceSources) { | |
| 145 ModelTypeInvalidationMap one_type = | |
| 146 ModelTypeSetToInvalidationMap( | |
| 147 ParamsMeaningJustOneEnabledType(), | |
| 148 std::string()); | |
| 149 ModelTypeInvalidationMap all_types = | |
| 150 ModelTypeSetToInvalidationMap( | |
| 151 ParamsMeaningAllEnabledTypes(), | |
| 152 std::string()); | |
| 153 sessions::SyncSourceInfo source_one( | |
| 154 sync_pb::GetUpdatesCallerInfo::PERIODIC, one_type); | |
| 155 sessions::SyncSourceInfo source_two( | |
| 156 sync_pb::GetUpdatesCallerInfo::LOCAL, all_types); | |
| 157 | |
| 158 SyncSessionJob job(SyncSessionJob::NUDGE, TimeTicks::Now(), | |
| 159 source_one, ConfigurationParams()); | |
| 160 | |
| 161 job.CoalesceSources(source_two); | |
| 162 | |
| 163 EXPECT_EQ(source_two.updates_source, job.source_info().updates_source); | |
| 164 } | |
| 165 | |
| 166 } // namespace syncer | |
| OLD | NEW |