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.
5 #ifndef SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_
6 #define SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "sync/engine/model_changing_syncer_command.h"
16 #include "sync/engine/traffic_recorder.h"
17 #include "sync/internal_api/debug_info_event_listener.h"
18 #include "sync/internal_api/public/base/cancelation_signal.h"
19 #include "sync/internal_api/public/engine/model_safe_worker.h"
20 #include "sync/sessions/debug_info_getter.h"
21 #include "sync/sessions/sync_session.h"
22 #include "sync/sessions/sync_session_context.h"
23 #include "sync/syncable/directory.h"
24 #include "sync/test/engine/fake_model_worker.h"
25 #include "sync/test/engine/mock_connection_manager.h"
26 #include "sync/test/engine/test_directory_setter_upper.h"
27 #include "sync/util/extensions_activity.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
33 // A test fixture that simplifies writing unit tests for individual
34 // SyncerCommands, providing convenient access to a test directory
35 // and a syncer session.
36 class SyncerCommandTestBase : public testing::Test,
37 public sessions::SyncSession::Delegate {
39 // SyncSession::Delegate implementation.
40 virtual void OnThrottled(
41 const base::TimeDelta& throttle_duration) OVERRIDE;
42 virtual void OnTypesThrottled(
44 const base::TimeDelta& throttle_duration) OVERRIDE;
45 virtual bool IsCurrentlyThrottled() OVERRIDE;
46 virtual void OnReceivedLongPollIntervalUpdate(
47 const base::TimeDelta& new_interval) OVERRIDE;
48 virtual void OnReceivedShortPollIntervalUpdate(
49 const base::TimeDelta& new_interval) OVERRIDE;
50 virtual void OnReceivedSessionsCommitDelay(
51 const base::TimeDelta& new_delay) OVERRIDE;
52 virtual void OnReceivedClientInvalidationHintBufferSize(int size) OVERRIDE;
53 virtual void OnShouldStopSyncingPermanently() OVERRIDE;
54 virtual void OnSyncProtocolError(
55 const sessions::SyncSessionSnapshot& session) OVERRIDE;
57 std::vector<ModelSafeWorker*> GetWorkers() {
58 std::vector<ModelSafeWorker*> workers;
59 std::vector<scoped_refptr<ModelSafeWorker> >::iterator it;
60 for (it = workers_.begin(); it != workers_.end(); ++it)
61 workers.push_back(it->get());
64 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
65 ModelSafeRoutingInfo copy(routing_info_);
70 SyncerCommandTestBase();
72 virtual ~SyncerCommandTestBase();
74 virtual void TearDown();
76 sessions::SyncSessionContext* context() const { return context_.get(); }
77 sessions::SyncSession::Delegate* delegate() { return this; }
79 // Lazily create a session requesting all datatypes with no state.
80 sessions::SyncSession* session() {
82 session_.reset(sessions::SyncSession::Build(context(), delegate()));
83 return session_.get();
91 context_.reset(new sessions::SyncSessionContext(
92 mock_server_.get(), directory(),
93 GetWorkers(), extensions_activity_.get(),
94 std::vector<SyncEngineEventListener*>(),
95 &debug_info_event_listener_,
97 true, // enable keystore encryption
98 false, // force enable pre-commit GU avoidance experiment
99 "fake_invalidator_client_id"));
100 context_->set_routing_info(routing_info_);
101 context_->set_account_name(directory()->name());
105 // Install a MockServerConnection. Resets the context. By default,
106 // the context does not have a MockServerConnection attached.
107 void ConfigureMockServerConnection() {
108 mock_server_.reset(new MockConnectionManager(directory(),
109 &cancelation_signal_));
113 virtual syncable::Directory* directory() = 0;
115 std::vector<scoped_refptr<ModelSafeWorker> >* workers() {
119 const ModelSafeRoutingInfo& routing_info() { return routing_info_; }
120 ModelSafeRoutingInfo* mutable_routing_info() { return &routing_info_; }
122 MockConnectionManager* mock_server() {
123 return mock_server_.get();
126 DebugInfoEventListener* debug_info_event_listener() {
127 return &debug_info_event_listener_;
129 // Helper functions to check command.GetGroupsToChange().
131 void ExpectNoGroupsToChange(const ModelChangingSyncerCommand& command) {
132 EXPECT_TRUE(command.GetGroupsToChangeForTest(*session()).empty());
135 void ExpectGroupToChange(
136 const ModelChangingSyncerCommand& command, ModelSafeGroup group) {
137 std::set<ModelSafeGroup> expected_groups_to_change;
138 expected_groups_to_change.insert(group);
139 EXPECT_EQ(expected_groups_to_change,
140 command.GetGroupsToChangeForTest(*session()));
143 void ExpectGroupsToChange(
144 const ModelChangingSyncerCommand& command,
145 ModelSafeGroup group1, ModelSafeGroup group2) {
146 std::set<ModelSafeGroup> expected_groups_to_change;
147 expected_groups_to_change.insert(group1);
148 expected_groups_to_change.insert(group2);
149 EXPECT_EQ(expected_groups_to_change,
150 command.GetGroupsToChangeForTest(*session()));
153 void ExpectGroupsToChange(
154 const ModelChangingSyncerCommand& command,
155 ModelSafeGroup group1, ModelSafeGroup group2, ModelSafeGroup group3) {
156 std::set<ModelSafeGroup> expected_groups_to_change;
157 expected_groups_to_change.insert(group1);
158 expected_groups_to_change.insert(group2);
159 expected_groups_to_change.insert(group3);
160 EXPECT_EQ(expected_groups_to_change,
161 command.GetGroupsToChangeForTest(*session()));
165 base::MessageLoop message_loop_;
166 scoped_ptr<sessions::SyncSessionContext> context_;
167 scoped_ptr<MockConnectionManager> mock_server_;
168 scoped_ptr<sessions::SyncSession> session_;
169 std::vector<scoped_refptr<ModelSafeWorker> > workers_;
170 ModelSafeRoutingInfo routing_info_;
171 DebugInfoEventListener debug_info_event_listener_;
172 scoped_refptr<ExtensionsActivity> extensions_activity_;
173 TrafficRecorder traffic_recorder_;
174 CancelationSignal cancelation_signal_;
175 DISALLOW_COPY_AND_ASSIGN(SyncerCommandTestBase);
178 class SyncerCommandTest : public SyncerCommandTestBase {
180 virtual void SetUp() OVERRIDE;
181 virtual void TearDown() OVERRIDE;
182 virtual syncable::Directory* directory() OVERRIDE;
185 TestDirectorySetterUpper dir_maker_;
188 } // namespace syncer
190 #endif // SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_