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/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "chrome/browser/sync/glue/fake_data_type_controller.h" | 6 #include "chrome/browser/sync/glue/fake_data_type_controller.h" |
7 #include "chrome/browser/sync/glue/model_association_manager.h" | 7 #include "chrome/browser/sync/glue/model_association_manager.h" |
8 #include "content/test/test_browser_thread.h" | 8 #include "content/test/test_browser_thread.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using ::testing::_; | 12 using ::testing::_; |
13 namespace browser_sync { | 13 namespace browser_sync { |
14 class MockModelAssociationResultProcessor : | 14 class MockModelAssociationResultProcessor : |
15 public ModelAssociationResultProcessor { | 15 public ModelAssociationResultProcessor { |
16 public: | 16 public: |
17 MockModelAssociationResultProcessor() {} | 17 MockModelAssociationResultProcessor() {} |
18 ~MockModelAssociationResultProcessor() {} | 18 ~MockModelAssociationResultProcessor() {} |
19 MOCK_METHOD1(OnModelAssociationDone, void( | 19 MOCK_METHOD1(OnModelAssociationDone, void( |
20 const DataTypeManager::ConfigureResult& result)); | 20 const DataTypeManager::ConfigureResult& result)); |
| 21 MOCK_METHOD0(OnTypesLoaded, void()); |
21 }; | 22 }; |
22 | 23 |
23 scoped_refptr<FakeDataTypeController> GetController( | 24 scoped_refptr<FakeDataTypeController> GetController( |
24 const DataTypeController::TypeMap& controllers, | 25 const DataTypeController::TypeMap& controllers, |
25 syncable::ModelType model_type) { | 26 syncable::ModelType model_type) { |
26 DataTypeController::TypeMap::const_iterator it = | 27 DataTypeController::TypeMap::const_iterator it = |
27 controllers.find(model_type); | 28 controllers.find(model_type); |
28 if (it == controllers.end()) { | 29 if (it == controllers.end()) { |
29 return NULL; | 30 return NULL; |
30 } | 31 } |
31 return make_scoped_refptr( | 32 return make_scoped_refptr( |
32 static_cast<FakeDataTypeController*>(it->second.get())); | 33 static_cast<FakeDataTypeController*>(it->second.get())); |
33 } | 34 } |
34 | 35 |
35 ACTION_P(VerifyResult, expected_result) { | 36 ACTION_P(VerifyResult, expected_result) { |
36 EXPECT_EQ(arg0.status, expected_result.status); | 37 EXPECT_EQ(arg0.status, expected_result.status); |
37 EXPECT_TRUE(arg0.requested_types.Equals(expected_result.requested_types)); | 38 EXPECT_TRUE(arg0.requested_types.Equals(expected_result.requested_types)); |
38 EXPECT_EQ(arg0.errors.size(), expected_result.errors.size()); | 39 EXPECT_EQ(arg0.failed_data_types.size(), |
| 40 expected_result.failed_data_types.size()); |
39 | 41 |
40 if (arg0.errors.size() == expected_result.errors.size()) { | 42 if (arg0.failed_data_types.size() == |
| 43 expected_result.failed_data_types.size()) { |
41 std::list<SyncError>::const_iterator it1, it2; | 44 std::list<SyncError>::const_iterator it1, it2; |
42 for (it1 = arg0.errors.begin(), | 45 for (it1 = arg0.failed_data_types.begin(), |
43 it2 = expected_result.errors.begin(); | 46 it2 = expected_result.failed_data_types.begin(); |
44 it1 != arg0.errors.end(); | 47 it1 != arg0.failed_data_types.end(); |
45 ++it1, ++it2) { | 48 ++it1, ++it2) { |
46 EXPECT_EQ((*it1).type(), (*it2).type()); | 49 EXPECT_EQ((*it1).type(), (*it2).type()); |
47 } | 50 } |
48 } | 51 } |
| 52 |
| 53 EXPECT_TRUE(arg0.waiting_to_start.Equals(expected_result.waiting_to_start)); |
49 } | 54 } |
50 | 55 |
51 class ModelAssociationManagerTest : public testing::Test { | 56 class ModelAssociationManagerTest : public testing::Test { |
52 public: | 57 public: |
53 ModelAssociationManagerTest() : | 58 ModelAssociationManagerTest() : |
54 ui_thread_(content::BrowserThread::UI, &ui_loop_) { | 59 ui_thread_(content::BrowserThread::UI, &ui_loop_) { |
55 } | 60 } |
56 | 61 |
57 protected: | 62 protected: |
58 MessageLoopForUI ui_loop_; | 63 MessageLoopForUI ui_loop_; |
59 content::TestBrowserThread ui_thread_; | 64 content::TestBrowserThread ui_thread_; |
60 MockModelAssociationResultProcessor result_processor_; | 65 MockModelAssociationResultProcessor result_processor_; |
61 DataTypeController::TypeMap controllers_; | 66 DataTypeController::TypeMap controllers_; |
62 }; | 67 }; |
63 | 68 |
64 // Start a type and make sure ModelAssociationManager callst the |Start| | 69 // Start a type and make sure ModelAssociationManager callst the |Start| |
65 // method and calls the callback when it is done. | 70 // method and calls the callback when it is done. |
66 TEST_F(ModelAssociationManagerTest, SimpleModelStart) { | 71 TEST_F(ModelAssociationManagerTest, SimpleModelStart) { |
67 controllers_[syncable::BOOKMARKS] = | 72 controllers_[syncable::BOOKMARKS] = |
68 new FakeDataTypeController(syncable::BOOKMARKS); | 73 new FakeDataTypeController(syncable::BOOKMARKS); |
69 ModelAssociationManager model_association_manager(&controllers_, | 74 ModelAssociationManager model_association_manager(&controllers_, |
70 &result_processor_); | 75 &result_processor_); |
71 syncable::ModelTypeSet types; | 76 syncable::ModelTypeSet types; |
72 types.Put(syncable::BOOKMARKS); | 77 types.Put(syncable::BOOKMARKS); |
73 DataTypeManager::ConfigureResult expected_result( | 78 DataTypeManager::ConfigureResult expected_result( |
74 DataTypeManager::OK, | 79 DataTypeManager::OK, |
75 types, | 80 types, |
76 std::list<SyncError>()); | 81 std::list<SyncError>(), |
| 82 syncable::ModelTypeSet()); |
77 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 83 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
78 WillOnce(VerifyResult(expected_result)); | 84 WillOnce(VerifyResult(expected_result)); |
79 | 85 |
80 model_association_manager.Initialize(types); | 86 model_association_manager.Initialize(types); |
81 model_association_manager.StopDisabledTypes(); | 87 model_association_manager.StopDisabledTypes(); |
82 model_association_manager.StartAssociationAsync(); | 88 model_association_manager.StartAssociationAsync(); |
83 | 89 |
84 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 90 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
85 DataTypeController::MODEL_STARTING); | 91 DataTypeController::MODEL_STARTING); |
86 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 92 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
87 DataTypeController::OK); | 93 DataTypeController::OK); |
88 } | 94 } |
89 | 95 |
90 // Start a type and call stop before it finishes associating. | 96 // Start a type and call stop before it finishes associating. |
91 TEST_F(ModelAssociationManagerTest, StopModelBeforeFinish) { | 97 TEST_F(ModelAssociationManagerTest, StopModelBeforeFinish) { |
92 controllers_[syncable::BOOKMARKS] = | 98 controllers_[syncable::BOOKMARKS] = |
93 new FakeDataTypeController(syncable::BOOKMARKS); | 99 new FakeDataTypeController(syncable::BOOKMARKS); |
94 ModelAssociationManager model_association_manager(&controllers_, | 100 ModelAssociationManager model_association_manager(&controllers_, |
95 &result_processor_); | 101 &result_processor_); |
96 | 102 |
97 syncable::ModelTypeSet types; | 103 syncable::ModelTypeSet types; |
98 types.Put(syncable::BOOKMARKS); | 104 types.Put(syncable::BOOKMARKS); |
99 | 105 |
100 DataTypeManager::ConfigureResult expected_result( | 106 DataTypeManager::ConfigureResult expected_result( |
101 DataTypeManager::ABORTED, | 107 DataTypeManager::ABORTED, |
102 types, | 108 types, |
103 std::list<SyncError>()); | 109 std::list<SyncError>(), |
| 110 syncable::ModelTypeSet()); |
104 | 111 |
105 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 112 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
106 WillOnce(VerifyResult(expected_result)); | 113 WillOnce(VerifyResult(expected_result)); |
107 | 114 |
108 model_association_manager.Initialize(types); | 115 model_association_manager.Initialize(types); |
109 model_association_manager.StopDisabledTypes(); | 116 model_association_manager.StopDisabledTypes(); |
110 model_association_manager.StartAssociationAsync(); | 117 model_association_manager.StartAssociationAsync(); |
111 | 118 |
112 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 119 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
113 DataTypeController::MODEL_STARTING); | 120 DataTypeController::MODEL_STARTING); |
114 model_association_manager.Stop(); | 121 model_association_manager.Stop(); |
115 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 122 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
116 DataTypeController::NOT_RUNNING); | 123 DataTypeController::NOT_RUNNING); |
117 } | 124 } |
118 | 125 |
119 // Start a type, let it finish and then call stop. | 126 // Start a type, let it finish and then call stop. |
120 TEST_F(ModelAssociationManagerTest, StopAfterFinish) { | 127 TEST_F(ModelAssociationManagerTest, StopAfterFinish) { |
121 controllers_[syncable::BOOKMARKS] = | 128 controllers_[syncable::BOOKMARKS] = |
122 new FakeDataTypeController(syncable::BOOKMARKS); | 129 new FakeDataTypeController(syncable::BOOKMARKS); |
123 ModelAssociationManager model_association_manager(&controllers_, | 130 ModelAssociationManager model_association_manager(&controllers_, |
124 &result_processor_); | 131 &result_processor_); |
125 syncable::ModelTypeSet types; | 132 syncable::ModelTypeSet types; |
126 types.Put(syncable::BOOKMARKS); | 133 types.Put(syncable::BOOKMARKS); |
127 DataTypeManager::ConfigureResult expected_result( | 134 DataTypeManager::ConfigureResult expected_result( |
128 DataTypeManager::OK, | 135 DataTypeManager::OK, |
129 types, | 136 types, |
130 std::list<SyncError>()); | 137 std::list<SyncError>(), |
| 138 syncable::ModelTypeSet()); |
131 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 139 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
132 WillOnce(VerifyResult(expected_result)); | 140 WillOnce(VerifyResult(expected_result)); |
133 | 141 |
134 model_association_manager.Initialize(types); | 142 model_association_manager.Initialize(types); |
135 model_association_manager.StopDisabledTypes(); | 143 model_association_manager.StopDisabledTypes(); |
136 model_association_manager.StartAssociationAsync(); | 144 model_association_manager.StartAssociationAsync(); |
137 | 145 |
138 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 146 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
139 DataTypeController::MODEL_STARTING); | 147 DataTypeController::MODEL_STARTING); |
140 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 148 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
(...skipping 11 matching lines...) Expand all Loading... |
152 ModelAssociationManager model_association_manager(&controllers_, | 160 ModelAssociationManager model_association_manager(&controllers_, |
153 &result_processor_); | 161 &result_processor_); |
154 syncable::ModelTypeSet types; | 162 syncable::ModelTypeSet types; |
155 types.Put(syncable::BOOKMARKS); | 163 types.Put(syncable::BOOKMARKS); |
156 std::list<SyncError> errors; | 164 std::list<SyncError> errors; |
157 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); | 165 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); |
158 errors.push_back(error); | 166 errors.push_back(error); |
159 DataTypeManager::ConfigureResult expected_result( | 167 DataTypeManager::ConfigureResult expected_result( |
160 DataTypeManager::PARTIAL_SUCCESS, | 168 DataTypeManager::PARTIAL_SUCCESS, |
161 types, | 169 types, |
162 errors); | 170 errors, |
| 171 syncable::ModelTypeSet()); |
163 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 172 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
164 WillOnce(VerifyResult(expected_result)); | 173 WillOnce(VerifyResult(expected_result)); |
165 | 174 |
166 model_association_manager.Initialize(types); | 175 model_association_manager.Initialize(types); |
167 model_association_manager.StopDisabledTypes(); | 176 model_association_manager.StopDisabledTypes(); |
168 model_association_manager.StartAssociationAsync(); | 177 model_association_manager.StartAssociationAsync(); |
169 | 178 |
170 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 179 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
171 DataTypeController::MODEL_STARTING); | 180 DataTypeController::MODEL_STARTING); |
172 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 181 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
173 DataTypeController::ASSOCIATION_FAILED); | 182 DataTypeController::ASSOCIATION_FAILED); |
174 } | 183 } |
175 | 184 |
176 // Ensure configuring stops when a type returns a unrecoverable error. | 185 // Ensure configuring stops when a type returns a unrecoverable error. |
177 TEST_F(ModelAssociationManagerTest, TypeReturnUnrecoverableError) { | 186 TEST_F(ModelAssociationManagerTest, TypeReturnUnrecoverableError) { |
178 controllers_[syncable::BOOKMARKS] = | 187 controllers_[syncable::BOOKMARKS] = |
179 new FakeDataTypeController(syncable::BOOKMARKS); | 188 new FakeDataTypeController(syncable::BOOKMARKS); |
180 ModelAssociationManager model_association_manager(&controllers_, | 189 ModelAssociationManager model_association_manager(&controllers_, |
181 &result_processor_); | 190 &result_processor_); |
182 syncable::ModelTypeSet types; | 191 syncable::ModelTypeSet types; |
183 types.Put(syncable::BOOKMARKS); | 192 types.Put(syncable::BOOKMARKS); |
184 std::list<SyncError> errors; | 193 std::list<SyncError> errors; |
185 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); | 194 SyncError error(FROM_HERE, "Failed", syncable::BOOKMARKS); |
186 errors.push_back(error); | 195 errors.push_back(error); |
187 DataTypeManager::ConfigureResult expected_result( | 196 DataTypeManager::ConfigureResult expected_result( |
188 DataTypeManager::UNRECOVERABLE_ERROR, | 197 DataTypeManager::UNRECOVERABLE_ERROR, |
189 types, | 198 types, |
190 errors); | 199 errors, |
| 200 syncable::ModelTypeSet()); |
191 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). | 201 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
192 WillOnce(VerifyResult(expected_result)); | 202 WillOnce(VerifyResult(expected_result)); |
193 | 203 |
194 model_association_manager.Initialize(types); | 204 model_association_manager.Initialize(types); |
195 model_association_manager.StopDisabledTypes(); | 205 model_association_manager.StopDisabledTypes(); |
196 model_association_manager.StartAssociationAsync(); | 206 model_association_manager.StartAssociationAsync(); |
197 | 207 |
198 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), | 208 EXPECT_EQ(GetController(controllers_, syncable::BOOKMARKS)->state(), |
199 DataTypeController::MODEL_STARTING); | 209 DataTypeController::MODEL_STARTING); |
200 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( | 210 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
201 DataTypeController::UNRECOVERABLE_ERROR); | 211 DataTypeController::UNRECOVERABLE_ERROR); |
202 } | 212 } |
203 | 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 |
| 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 model_association_manager.TestSimulateDataTypeLoadTimeout(); |
| 252 |
| 253 GetController(controllers_, syncable::APPS)->FinishStart( |
| 254 DataTypeController::OK); |
| 255 |
| 256 GetController(controllers_, syncable::BOOKMARKS)->FinishModelLoad(); |
| 257 |
| 258 EXPECT_CALL(result_processor_, OnModelAssociationDone(_)). |
| 259 WillOnce(VerifyResult(expected_result_done)); |
| 260 |
| 261 // Do it once more to associate bookmarks. |
| 262 model_association_manager.Initialize(types); |
| 263 model_association_manager.StopDisabledTypes(); |
| 264 model_association_manager.StartAssociationAsync(); |
| 265 |
| 266 GetController(controllers_, syncable::BOOKMARKS)->FinishModelLoad(); |
| 267 |
| 268 GetController(controllers_, syncable::BOOKMARKS)->FinishStart( |
| 269 DataTypeController::OK); |
| 270 } |
| 271 |
| 272 |
204 } // namespace browser_sync | 273 } // namespace browser_sync |
OLD | NEW |