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 #ifndef CHROME_BROWSER_SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_ | |
6 #define CHROME_BROWSER_SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_ | |
7 #pragma once | |
8 | |
9 #include <algorithm> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/compiler_specific.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/message_loop.h" | |
16 #include "chrome/browser/sync/engine/model_changing_syncer_command.h" | |
17 #include "chrome/browser/sync/engine/model_safe_worker.h" | |
18 #include "chrome/browser/sync/sessions/debug_info_getter.h" | |
19 #include "chrome/browser/sync/sessions/sync_session.h" | |
20 #include "chrome/browser/sync/sessions/sync_session_context.h" | |
21 #include "chrome/browser/sync/syncable/syncable_mock.h" | |
22 #include "chrome/browser/sync/test/engine/mock_connection_manager.h" | |
23 #include "chrome/browser/sync/test/engine/fake_model_worker.h" | |
24 #include "chrome/browser/sync/test/engine/test_directory_setter_upper.h" | |
25 #include "chrome/browser/sync/test/fake_extensions_activity_monitor.h" | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 #include "testing/gmock/include/gmock/gmock.h" | |
28 | |
29 using ::testing::NiceMock; | |
30 | |
31 namespace browser_sync { | |
32 | |
33 class MockDebugInfoGetter : public browser_sync::sessions::DebugInfoGetter { | |
34 public: | |
35 MockDebugInfoGetter(); | |
36 virtual ~MockDebugInfoGetter(); | |
37 MOCK_METHOD1(GetAndClearDebugInfo, void(sync_pb::DebugInfo* debug_info)); | |
38 }; | |
39 | |
40 // A test fixture that simplifies writing unit tests for individual | |
41 // SyncerCommands, providing convenient access to a test directory | |
42 // and a syncer session. | |
43 class SyncerCommandTestBase : public testing::Test, | |
44 public sessions::SyncSession::Delegate, | |
45 public ModelSafeWorkerRegistrar { | |
46 public: | |
47 enum UseMockDirectory { | |
48 USE_MOCK_DIRECTORY | |
49 }; | |
50 | |
51 // SyncSession::Delegate implementation. | |
52 virtual void OnSilencedUntil( | |
53 const base::TimeTicks& silenced_until) OVERRIDE { | |
54 FAIL() << "Should not get silenced."; | |
55 } | |
56 virtual bool IsSyncingCurrentlySilenced() OVERRIDE { | |
57 return false; | |
58 } | |
59 virtual void OnReceivedLongPollIntervalUpdate( | |
60 const base::TimeDelta& new_interval) OVERRIDE { | |
61 FAIL() << "Should not get poll interval update."; | |
62 } | |
63 virtual void OnReceivedShortPollIntervalUpdate( | |
64 const base::TimeDelta& new_interval) OVERRIDE { | |
65 FAIL() << "Should not get poll interval update."; | |
66 } | |
67 virtual void OnReceivedSessionsCommitDelay( | |
68 const base::TimeDelta& new_delay) OVERRIDE { | |
69 FAIL() << "Should not get sessions commit delay."; | |
70 } | |
71 virtual void OnShouldStopSyncingPermanently() OVERRIDE { | |
72 FAIL() << "Shouldn't be called."; | |
73 } | |
74 virtual void OnSyncProtocolError( | |
75 const sessions::SyncSessionSnapshot& session) OVERRIDE { | |
76 return; | |
77 } | |
78 | |
79 // ModelSafeWorkerRegistrar implementation. | |
80 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) OVERRIDE { | |
81 std::vector<scoped_refptr<ModelSafeWorker> >::iterator it; | |
82 for (it = workers_.begin(); it != workers_.end(); ++it) | |
83 out->push_back(*it); | |
84 } | |
85 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) OVERRIDE { | |
86 ModelSafeRoutingInfo copy(routing_info_); | |
87 out->swap(copy); | |
88 } | |
89 | |
90 protected: | |
91 SyncerCommandTestBase(); | |
92 | |
93 virtual ~SyncerCommandTestBase(); | |
94 virtual void SetUp(); | |
95 virtual void TearDown(); | |
96 | |
97 sessions::SyncSessionContext* context() const { return context_.get(); } | |
98 sessions::SyncSession::Delegate* delegate() { return this; } | |
99 ModelSafeWorkerRegistrar* registrar() { return this; } | |
100 | |
101 // Lazily create a session requesting all datatypes with no payload. | |
102 sessions::SyncSession* session() { | |
103 syncable::ModelTypePayloadMap types = | |
104 syncable::ModelTypePayloadMapFromRoutingInfo(routing_info_, | |
105 std::string()); | |
106 return session(sessions::SyncSourceInfo(types)); | |
107 } | |
108 | |
109 // Create a session with the provided source. | |
110 sessions::SyncSession* session(const sessions::SyncSourceInfo& source) { | |
111 if (!session_.get()) { | |
112 std::vector<ModelSafeWorker*> workers; | |
113 GetWorkers(&workers); | |
114 session_.reset(new sessions::SyncSession(context(), delegate(), source, | |
115 routing_info_, workers)); | |
116 } | |
117 return session_.get(); | |
118 } | |
119 | |
120 void ClearSession() { | |
121 session_.reset(); | |
122 } | |
123 | |
124 void ResetContext() { | |
125 context_.reset(new sessions::SyncSessionContext( | |
126 mock_server_.get(), directory(), | |
127 registrar(), &extensions_activity_monitor_, | |
128 std::vector<SyncEngineEventListener*>(), | |
129 &mock_debug_info_getter_)); | |
130 context_->set_account_name(directory()->name()); | |
131 ClearSession(); | |
132 } | |
133 | |
134 // Install a MockServerConnection. Resets the context. By default, | |
135 // the context does not have a MockServerConnection attached. | |
136 void ConfigureMockServerConnection() { | |
137 mock_server_.reset(new MockConnectionManager(directory())); | |
138 ResetContext(); | |
139 } | |
140 | |
141 virtual syncable::Directory* directory() = 0; | |
142 | |
143 std::vector<scoped_refptr<ModelSafeWorker> >* workers() { | |
144 return &workers_; | |
145 } | |
146 | |
147 const ModelSafeRoutingInfo& routing_info() { return routing_info_; } | |
148 ModelSafeRoutingInfo* mutable_routing_info() { return &routing_info_; } | |
149 | |
150 MockConnectionManager* mock_server() { | |
151 return mock_server_.get(); | |
152 } | |
153 | |
154 MockDebugInfoGetter* mock_debug_info_getter() { | |
155 return &mock_debug_info_getter_; | |
156 } | |
157 | |
158 // Helper functions to check command.GetGroupsToChange(). | |
159 | |
160 void ExpectNoGroupsToChange(const ModelChangingSyncerCommand& command) { | |
161 EXPECT_TRUE(command.GetGroupsToChangeForTest(*session()).empty()); | |
162 } | |
163 | |
164 void ExpectGroupToChange( | |
165 const ModelChangingSyncerCommand& command, ModelSafeGroup group) { | |
166 std::set<ModelSafeGroup> expected_groups_to_change; | |
167 expected_groups_to_change.insert(group); | |
168 EXPECT_EQ(expected_groups_to_change, | |
169 command.GetGroupsToChangeForTest(*session())); | |
170 } | |
171 | |
172 void ExpectGroupsToChange( | |
173 const ModelChangingSyncerCommand& command, | |
174 ModelSafeGroup group1, ModelSafeGroup group2) { | |
175 std::set<ModelSafeGroup> expected_groups_to_change; | |
176 expected_groups_to_change.insert(group1); | |
177 expected_groups_to_change.insert(group2); | |
178 EXPECT_EQ(expected_groups_to_change, | |
179 command.GetGroupsToChangeForTest(*session())); | |
180 } | |
181 | |
182 void ExpectGroupsToChange( | |
183 const ModelChangingSyncerCommand& command, | |
184 ModelSafeGroup group1, ModelSafeGroup group2, ModelSafeGroup group3) { | |
185 std::set<ModelSafeGroup> expected_groups_to_change; | |
186 expected_groups_to_change.insert(group1); | |
187 expected_groups_to_change.insert(group2); | |
188 expected_groups_to_change.insert(group3); | |
189 EXPECT_EQ(expected_groups_to_change, | |
190 command.GetGroupsToChangeForTest(*session())); | |
191 } | |
192 | |
193 private: | |
194 MessageLoop message_loop_; | |
195 scoped_ptr<sessions::SyncSessionContext> context_; | |
196 scoped_ptr<MockConnectionManager> mock_server_; | |
197 scoped_ptr<sessions::SyncSession> session_; | |
198 std::vector<scoped_refptr<ModelSafeWorker> > workers_; | |
199 ModelSafeRoutingInfo routing_info_; | |
200 NiceMock<MockDebugInfoGetter> mock_debug_info_getter_; | |
201 FakeExtensionsActivityMonitor extensions_activity_monitor_; | |
202 DISALLOW_COPY_AND_ASSIGN(SyncerCommandTestBase); | |
203 }; | |
204 | |
205 class SyncerCommandTest : public SyncerCommandTestBase { | |
206 public: | |
207 virtual void SetUp() OVERRIDE; | |
208 virtual void TearDown() OVERRIDE; | |
209 virtual Directory* directory() OVERRIDE; | |
210 | |
211 private: | |
212 TestDirectorySetterUpper dir_maker_; | |
213 }; | |
214 | |
215 class MockDirectorySyncerCommandTest : public SyncerCommandTestBase { | |
216 public: | |
217 MockDirectorySyncerCommandTest(); | |
218 virtual ~MockDirectorySyncerCommandTest(); | |
219 virtual Directory* directory() OVERRIDE; | |
220 | |
221 MockDirectory* mock_directory() { | |
222 return static_cast<MockDirectory*>(directory()); | |
223 } | |
224 | |
225 virtual void SetUp() OVERRIDE; | |
226 | |
227 TestUnrecoverableErrorHandler handler_; | |
228 MockDirectory mock_directory_; | |
229 }; | |
230 | |
231 } // namespace browser_sync | |
232 | |
233 #endif // CHROME_BROWSER_SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_ | |
OLD | NEW |