OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/callback.h" | |
5 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
6 #include "chrome/browser/sync/glue/fake_data_type_controller.h" | 7 #include "chrome/browser/sync/glue/fake_data_type_controller.h" |
7 #include "chrome/browser/sync/glue/model_association_manager.h" | 8 #include "chrome/browser/sync/glue/model_association_manager.h" |
8 #include "content/test/test_browser_thread.h" | 9 #include "content/test/test_browser_thread.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using ::testing::_; | 13 using ::testing::_; |
13 namespace browser_sync { | 14 namespace browser_sync { |
14 class MockModelAssociationResultProcessor : | 15 class MockModelAssociationResultProcessor : |
15 public ModelAssociationResultProcessor { | 16 public ModelAssociationResultProcessor { |
16 public: | 17 public: |
17 MockModelAssociationResultProcessor() {} | 18 MockModelAssociationResultProcessor() {} |
18 ~MockModelAssociationResultProcessor() {} | 19 ~MockModelAssociationResultProcessor() {} |
19 MOCK_METHOD1(OnModelAssociationDone, void( | 20 MOCK_METHOD1(OnModelAssociationDone, void( |
20 const DataTypeManager::ConfigureResult& result)); | 21 const DataTypeManager::ConfigureResult& result)); |
22 MOCK_METHOD0(OnTypesLoaded, void()); | |
21 }; | 23 }; |
22 | 24 |
23 FakeDataTypeController* GetController( | 25 FakeDataTypeController* GetController( |
24 const DataTypeController::TypeMap& controllers, | 26 const DataTypeController::TypeMap& controllers, |
25 syncable::ModelType model_type) { | 27 syncable::ModelType model_type) { |
26 DataTypeController::TypeMap::const_iterator it = | 28 DataTypeController::TypeMap::const_iterator it = |
27 controllers.find(model_type); | 29 controllers.find(model_type); |
28 if (it == controllers.end()) { | 30 if (it == controllers.end()) { |
29 return NULL; | 31 return NULL; |
30 } | 32 } |
31 return (FakeDataTypeController*)(it->second.get()); | 33 return (FakeDataTypeController*)(it->second.get()); |
32 } | 34 } |
33 | 35 |
34 ACTION_P(VerifyResult, expected_result) { | 36 ACTION_P(VerifyResult, expected_result) { |
35 EXPECT_EQ(arg0.status, expected_result.status); | 37 EXPECT_EQ(arg0.status, expected_result.status); |
36 EXPECT_TRUE(arg0.requested_types.Equals(expected_result.requested_types)); | 38 EXPECT_TRUE(arg0.requested_types.Equals(expected_result.requested_types)); |
37 EXPECT_EQ(arg0.errors.size(), expected_result.errors.size()); | 39 EXPECT_EQ(arg0.failed_data_types.size(), |
40 expected_result.failed_data_types.size()); | |
38 | 41 |
39 if (arg0.errors.size() == expected_result.errors.size()) { | 42 if (arg0.failed_data_types.size() == |
43 expected_result.failed_data_types.size()) { | |
40 std::list<SyncError>::const_iterator it1, it2; | 44 std::list<SyncError>::const_iterator it1, it2; |
41 for (it1 = arg0.errors.begin(), | 45 for (it1 = arg0.failed_data_types.begin(), |
42 it2 = expected_result.errors.begin(); | 46 it2 = expected_result.failed_data_types.begin(); |
43 it1 != arg0.errors.end(); | 47 it1 != arg0.failed_data_types.end(); |
44 ++it1, ++it2) { | 48 ++it1, ++it2) { |
45 EXPECT_EQ((*it1).type(), (*it2).type()); | 49 EXPECT_EQ((*it1).type(), (*it2).type()); |
46 } | 50 } |
47 } | 51 } |
52 | |
53 EXPECT_TRUE(arg0.waiting_to_start.Equals(expected_result.waiting_to_start)); | |
48 } | 54 } |
49 | 55 |
50 class ModelAssociationManagerTest : public testing::Test { | 56 class ModelAssociationManagerTest : public testing::Test { |
51 public: | 57 public: |
52 ModelAssociationManagerTest() : | 58 ModelAssociationManagerTest() : |
53 ui_thread_(content::BrowserThread::UI, &ui_loop_) { | 59 ui_thread_(content::BrowserThread::UI, &ui_loop_) { |
54 } | 60 } |
55 | 61 |
56 protected: | 62 protected: |
57 MessageLoopForUI ui_loop_; | 63 MessageLoopForUI ui_loop_; |
58 content::TestBrowserThread ui_thread_; | 64 content::TestBrowserThread ui_thread_; |
59 MockModelAssociationResultProcessor result_processor_; | 65 MockModelAssociationResultProcessor result_processor_; |
60 DataTypeController::TypeMap controllers_; | 66 DataTypeController::TypeMap controllers_; |
61 }; | 67 }; |
62 | 68 |
63 // Start a type and make sure ModelAssociationManager callst the |Start| | 69 // Start a type and make sure ModelAssociationManager callst the |Start| |
64 // method and calls the callback when it is done. | 70 // method and calls the callback when it is done. |
65 TEST_F(ModelAssociationManagerTest, SimpleModelStart) { | 71 TEST_F(ModelAssociationManagerTest, SimpleModelStart) { |
66 controllers_[syncable::BOOKMARKS] = | 72 controllers_[syncable::BOOKMARKS] = |
67 new FakeDataTypeController(syncable::BOOKMARKS); | 73 new FakeDataTypeController(syncable::BOOKMARKS); |
68 ModelAssociationManager model_association_manager(&controllers_, | 74 ModelAssociationManager model_association_manager(&controllers_, |
69 &result_processor_); | 75 &result_processor_); |
70 syncable::ModelTypeSet types; | 76 syncable::ModelTypeSet types; |
71 types.Put(syncable::BOOKMARKS); | 77 types.Put(syncable::BOOKMARKS); |
72 DataTypeManager::ConfigureResult expected_result( | 78 DataTypeManager::ConfigureResult expected_result( |
73 DataTypeManager::OK, | 79 DataTypeManager::OK, |
74 types, | 80 types, |
75 std::list<SyncError>()); | 81 std::list<SyncError>(), |
82 syncable::ModelTypeSet()); | |
76 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 83 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
77 WillOnce(VerifyResult(expected_result)); | 84 WillOnce(VerifyResult(expected_result)); |
78 | 85 |
79 model_association_manager.Initialize(types); | 86 model_association_manager.Initialize(types); |
80 model_association_manager.StopDisabledTypes(); | 87 model_association_manager.StopDisabledTypes(); |
81 model_association_manager.StartAssociationAsync(); | 88 model_association_manager.StartAssociationAsync(); |
82 | 89 |
83 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 90 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
84 DataTypeController::MODEL_STARTING); | 91 DataTypeController::MODEL_LOADED); |
85 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 92 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
86 DataTypeController::OK); | 93 DataTypeController::OK); |
87 } | 94 } |
88 | 95 |
89 // Start a type and call stop before it finishes associating. | 96 // Start a type and call stop before it finishes associating. |
90 TEST_F(ModelAssociationManagerTest, StopModelBeforeFinish) { | 97 TEST_F(ModelAssociationManagerTest, StopModelBeforeFinish) { |
91 controllers_[syncable::BOOKMARKS] = | 98 controllers_[syncable::BOOKMARKS] = |
92 new FakeDataTypeController(syncable::BOOKMARKS); | 99 new FakeDataTypeController(syncable::BOOKMARKS); |
93 ModelAssociationManager model_association_manager(&controllers_, | 100 ModelAssociationManager model_association_manager(&controllers_, |
94 &result_processor_); | 101 &result_processor_); |
95 | 102 |
96 syncable::ModelTypeSet types; | 103 syncable::ModelTypeSet types; |
97 types.Put(syncable::BOOKMARKS); | 104 types.Put(syncable::BOOKMARKS); |
98 | 105 |
99 DataTypeManager::ConfigureResult expected_result( | 106 DataTypeManager::ConfigureResult expected_result( |
100 DataTypeManager::ABORTED, | 107 DataTypeManager::ABORTED, |
101 types, | 108 types, |
102 std::list<SyncError>()); | 109 std::list<SyncError>(), |
110 syncable::ModelTypeSet()); | |
103 | 111 |
104 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 112 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
105 WillOnce(VerifyResult(expected_result)); | 113 WillOnce(VerifyResult(expected_result)); |
106 | 114 |
107 model_association_manager.Initialize(types); | 115 model_association_manager.Initialize(types); |
108 model_association_manager.StopDisabledTypes(); | 116 model_association_manager.StopDisabledTypes(); |
109 model_association_manager.StartAssociationAsync(); | 117 model_association_manager.StartAssociationAsync(); |
110 | 118 |
111 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 119 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
112 DataTypeController::MODEL_STARTING); | 120 DataTypeController::MODEL_LOADED); |
113 model_association_manager.Stop(); | 121 model_association_manager.Stop(); |
114 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 122 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
115 DataTypeController::NOT_RUNNING); | 123 DataTypeController::NOT_RUNNING); |
116 } | 124 } |
117 | 125 |
118 // Start a type, let it finish and then call stop. | 126 // Start a type, let it finish and then call stop. |
119 TEST_F(ModelAssociationManagerTest, StopAfterFinish) { | 127 TEST_F(ModelAssociationManagerTest, StopAfterFinish) { |
120 controllers_[syncable::BOOKMARKS] = | 128 controllers_[syncable::BOOKMARKS] = |
121 new FakeDataTypeController(syncable::BOOKMARKS); | 129 new FakeDataTypeController(syncable::BOOKMARKS); |
122 ModelAssociationManager model_association_manager(&controllers_, | 130 ModelAssociationManager model_association_manager(&controllers_, |
123 &result_processor_); | 131 &result_processor_); |
124 syncable::ModelTypeSet types; | 132 syncable::ModelTypeSet types; |
125 types.Put(syncable::BOOKMARKS); | 133 types.Put(syncable::BOOKMARKS); |
126 DataTypeManager::ConfigureResult expected_result( | 134 DataTypeManager::ConfigureResult expected_result( |
127 DataTypeManager::OK, | 135 DataTypeManager::OK, |
128 types, | 136 types, |
129 std::list<SyncError>()); | 137 std::list<SyncError>(), |
138 syncable::ModelTypeSet()); | |
130 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 139 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
131 WillOnce(VerifyResult(expected_result)); | 140 WillOnce(VerifyResult(expected_result)); |
132 | 141 |
133 model_association_manager.Initialize(types); | 142 model_association_manager.Initialize(types); |
134 model_association_manager.StopDisabledTypes(); | 143 model_association_manager.StopDisabledTypes(); |
135 model_association_manager.StartAssociationAsync(); | 144 model_association_manager.StartAssociationAsync(); |
136 | 145 |
137 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 146 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
138 DataTypeController::MODEL_STARTING); | 147 DataTypeController::MODEL_LOADED); |
139 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 148 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
140 DataTypeController::OK); | 149 DataTypeController::OK); |
141 | 150 |
142 model_association_manager.Stop(); | 151 model_association_manager.Stop(); |
143 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 152 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
144 DataTypeController::NOT_RUNNING); | 153 DataTypeController::NOT_RUNNING); |
145 } | 154 } |
146 | 155 |
147 // Make a type fail model association and verify correctness. | 156 // Make a type fail model association and verify correctness. |
148 TEST_F(ModelAssociationManagerTest, TypeFailModelAssociation) { | 157 TEST_F(ModelAssociationManagerTest, TypeFailModelAssociation) { |
149 controllers_[syncable::BOOKMARKS] = | 158 controllers_[syncable::BOOKMARKS] = |
150 new FakeDataTypeController(syncable::BOOKMARKS); | 159 new FakeDataTypeController(syncable::BOOKMARKS); |
151 ModelAssociationManager model_association_manager(&controllers_, | 160 ModelAssociationManager model_association_manager(&controllers_, |
152 &result_processor_); | 161 &result_processor_); |
153 syncable::ModelTypeSet types; | 162 syncable::ModelTypeSet types; |
154 types.Put(syncable::BOOKMARKS); | 163 types.Put(syncable::BOOKMARKS); |
155 std::list<SyncError> errors; | 164 std::list<SyncError> errors; |
156 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); | 165 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); |
157 errors.push_back(error); | 166 errors.push_back(error); |
158 DataTypeManager::ConfigureResult expected_result( | 167 DataTypeManager::ConfigureResult expected_result( |
159 DataTypeManager::PARTIAL_SUCCESS, | 168 DataTypeManager::PARTIAL_SUCCESS, |
160 types, | 169 types, |
161 errors); | 170 errors, |
171 syncable::ModelTypeSet()); | |
162 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 172 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
163 WillOnce(VerifyResult(expected_result)); | 173 WillOnce(VerifyResult(expected_result)); |
164 | 174 |
165 model_association_manager.Initialize(types); | 175 model_association_manager.Initialize(types); |
166 model_association_manager.StopDisabledTypes(); | 176 model_association_manager.StopDisabledTypes(); |
167 model_association_manager.StartAssociationAsync(); | 177 model_association_manager.StartAssociationAsync(); |
168 | 178 |
169 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 179 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
170 DataTypeController::MODEL_STARTING); | 180 DataTypeController::MODEL_LOADED); |
171 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 181 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
172 DataTypeController::ASSOCIATION_FAILED); | 182 DataTypeController::ASSOCIATION_FAILED); |
173 } | 183 } |
174 | 184 |
175 // Ensure configuring stops when a type returns a unrecoverable error. | 185 // Ensure configuring stops when a type returns a unrecoverable error. |
176 TEST_F(ModelAssociationManagerTest, TypeReturnUnrecoverableError) { | 186 TEST_F(ModelAssociationManagerTest, TypeReturnUnrecoverableError) { |
177 controllers_[syncable::BOOKMARKS] = | 187 controllers_[syncable::BOOKMARKS] = |
178 new FakeDataTypeController(syncable::BOOKMARKS); | 188 new FakeDataTypeController(syncable::BOOKMARKS); |
179 ModelAssociationManager model_association_manager(&controllers_, | 189 ModelAssociationManager model_association_manager(&controllers_, |
180 &result_processor_); | 190 &result_processor_); |
181 syncable::ModelTypeSet types; | 191 syncable::ModelTypeSet types; |
182 types.Put(syncable::BOOKMARKS); | 192 types.Put(syncable::BOOKMARKS); |
183 std::list<SyncError> errors; | 193 std::list<SyncError> errors; |
184 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); | 194 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); |
185 errors.push_back(error); | 195 errors.push_back(error); |
186 DataTypeManager::ConfigureResult expected_result( | 196 DataTypeManager::ConfigureResult expected_result( |
187 DataTypeManager::UNRECOVERABLE_ERROR, | 197 DataTypeManager::UNRECOVERABLE_ERROR, |
188 types, | 198 types, |
189 errors); | 199 errors, |
200 syncable::ModelTypeSet()); | |
190 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 201 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
191 WillOnce(VerifyResult(expected_result)); | 202 WillOnce(VerifyResult(expected_result)); |
192 | 203 |
193 model_association_manager.Initialize(types); | 204 model_association_manager.Initialize(types); |
194 model_association_manager.StopDisabledTypes(); | 205 model_association_manager.StopDisabledTypes(); |
195 model_association_manager.StartAssociationAsync(); | 206 model_association_manager.StartAssociationAsync(); |
196 | 207 |
197 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 208 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
198 DataTypeController::MODEL_STARTING); | 209 DataTypeController::MODEL_LOADED); |
199 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 210 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
200 DataTypeController::UNRECOVERABLE_ERROR); | 211 DataTypeController::UNRECOVERABLE_ERROR); |
201 } | 212 } |
202 | 213 |
214 // Start 2 types. One of which timeout loading. Ensure that type is | |
215 // fully configured eventually. | |
216 TEST_F(ModelAssociationManagerTest, ModelStartWithSlowLoadingType) { | |
217 controllers_[syncable::BOOKMARKS] = | |
218 new FakeDataTypeController(syncable::BOOKMARKS); | |
219 controllers_[syncable::APPS] = | |
220 new FakeDataTypeController(syncable::APPS); | |
221 GetController(controllers_, syncable::BOOKMARKS)->SetDelayModelLoad(); | |
222 ModelAssociationManager model_association_manager(&controllers_, | |
223 &result_processor_); | |
224 syncable::ModelTypeSet types; | |
225 types.Put(syncable::BOOKMARKS); | |
226 types.Put(syncable::APPS); | |
227 | |
228 syncable::ModelTypeSet expected_types_waiting_to_load; | |
229 expected_types_waiting_to_load.Put(syncable::BOOKMARKS); | |
230 DataTypeManager::ConfigureResult expected_result_partially_done( | |
231 DataTypeManager::PARTIAL_SUCCESS, | |
232 types, | |
233 std::list<SyncError>(), | |
234 expected_types_waiting_to_load); | |
235 | |
236 DataTypeManager::ConfigureResult expected_result_done( | |
237 DataTypeManager::OK, | |
238 types, | |
239 std::list<SyncError>(), | |
240 syncable::ModelTypeSet()); | |
241 | |
242 | |
tim (not reviewing)
2012/05/23 03:18:52
extra newline
lipalani1
2012/05/23 17:47:21
Done.
| |
243 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | |
244 WillOnce(VerifyResult(expected_result_partially_done)); | |
245 EXPECT_CALL(result_processor_, OnTypesLoaded()); | |
246 | |
247 model_association_manager.Initialize(types); | |
248 model_association_manager.StopDisabledTypes(); | |
249 model_association_manager.StartAssociationAsync(); | |
250 | |
251 base::OneShotTimer<ModelAssociationManager>* timer = | |
252 model_association_manager.GetTimerForTesting(); | |
tim (not reviewing)
2012/05/23 03:18:52
indent
lipalani1
2012/05/23 17:47:21
Done.
| |
253 | |
254 base::Closure task = timer->user_task(); | |
255 timer->Stop(); | |
256 task.Run(); | |
257 | |
258 GetController(controllers_, syncable::APPS)->FinishStart( | |
259 DataTypeController::OK); | |
260 | |
261 GetController(controllers_, syncable::BOOKMARKS)->FinishModelLoad(); | |
tim (not reviewing)
2012/05/23 03:18:52
Comment like "Simulate delayed loading of bookmark
lipalani1
2012/05/23 17:47:21
Done.
| |
262 | |
263 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | |
264 WillOnce(VerifyResult(expected_result_done)); | |
265 | |
266 // Do it once more to associate bookmarks. | |
267 model_association_manager.Initialize(types); | |
268 model_association_manager.StopDisabledTypes(); | |
269 model_association_manager.StartAssociationAsync(); | |
270 | |
271 GetController(controllers_, syncable::BOOKMARKS)->FinishModelLoad(); | |
272 | |
273 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | |
274 DataTypeController::OK); | |
275 } | |
276 | |
277 | |
203 } // namespace browser_sync | 278 } // namespace browser_sync |
OLD | NEW |