OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/js/js_sync_manager_observer.h" | 5 #include "chrome/browser/sync/js/js_sync_manager_observer.h" |
6 | 6 |
7 #include <cstddef> | |
8 | |
9 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
10 #include "base/location.h" | 8 #include "base/location.h" |
11 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
12 #include "base/values.h" | 10 #include "base/values.h" |
13 #include "chrome/browser/sync/internal_api/read_node.h" | |
14 #include "chrome/browser/sync/internal_api/read_transaction.h" | |
15 #include "chrome/browser/sync/internal_api/write_node.h" | |
16 #include "chrome/browser/sync/internal_api/write_transaction.h" | |
17 #include "chrome/browser/sync/js/js_arg_list.h" | 11 #include "chrome/browser/sync/js/js_arg_list.h" |
18 #include "chrome/browser/sync/js/js_event_details.h" | 12 #include "chrome/browser/sync/js/js_event_details.h" |
19 #include "chrome/browser/sync/js/js_test_util.h" | 13 #include "chrome/browser/sync/js/js_test_util.h" |
20 #include "chrome/browser/sync/protocol/sync_protocol_error.h" | 14 #include "chrome/browser/sync/protocol/sync_protocol_error.h" |
21 #include "chrome/browser/sync/sessions/session_state.h" | 15 #include "chrome/browser/sync/sessions/session_state.h" |
22 #include "chrome/browser/sync/syncable/model_type.h" | 16 #include "chrome/browser/sync/syncable/model_type.h" |
23 #include "chrome/browser/sync/test/engine/test_user_share.h" | |
24 #include "chrome/browser/sync/util/weak_handle.h" | 17 #include "chrome/browser/sync/util/weak_handle.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
26 | 19 |
27 namespace browser_sync { | 20 namespace browser_sync { |
28 namespace { | 21 namespace { |
29 | 22 |
30 using ::testing::InSequence; | 23 using ::testing::InSequence; |
31 using ::testing::StrictMock; | 24 using ::testing::StrictMock; |
32 | 25 |
33 class JsSyncManagerObserverTest : public testing::Test { | 26 class JsSyncManagerObserverTest : public testing::Test { |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 } | 227 } |
235 | 228 |
236 EXPECT_CALL(mock_js_event_handler_, | 229 EXPECT_CALL(mock_js_event_handler_, |
237 HandleJsEvent("onEncryptionComplete", | 230 HandleJsEvent("onEncryptionComplete", |
238 HasDetailsAsDictionary(expected_details))); | 231 HasDetailsAsDictionary(expected_details))); |
239 | 232 |
240 js_sync_manager_observer_.OnEncryptionComplete(encrypted_types); | 233 js_sync_manager_observer_.OnEncryptionComplete(encrypted_types); |
241 PumpLoop(); | 234 PumpLoop(); |
242 } | 235 } |
243 | 236 |
244 namespace { | |
245 | |
246 // Makes a node of the given model type. Returns the id of the | |
247 // newly-created node. | |
248 int64 MakeNode(sync_api::UserShare* share, syncable::ModelType model_type) { | |
249 sync_api::WriteTransaction trans(FROM_HERE, share); | |
250 sync_api::ReadNode root_node(&trans); | |
251 root_node.InitByRootLookup(); | |
252 sync_api::WriteNode node(&trans); | |
253 EXPECT_TRUE(node.InitUniqueByCreation( | |
254 model_type, root_node, | |
255 syncable::ModelTypeToString(model_type))); | |
256 node.SetIsFolder(false); | |
257 return node.GetId(); | |
258 } | |
259 | |
260 } // namespace | |
261 | |
262 TEST_F(JsSyncManagerObserverTest, OnChangesApplied) { | 237 TEST_F(JsSyncManagerObserverTest, OnChangesApplied) { |
263 InSequence dummy; | 238 InSequence dummy; |
264 | 239 |
265 TestUserShare test_user_share; | |
266 test_user_share.SetUp(); | |
267 | |
268 // We don't test with passwords as that requires additional setup. | 240 // We don't test with passwords as that requires additional setup. |
269 | 241 |
270 // Build a list of example ChangeRecords. | 242 // Build a list of example ChangeRecords. |
271 sync_api::ChangeRecord changes[syncable::MODEL_TYPE_COUNT]; | 243 sync_api::ChangeRecord changes[syncable::MODEL_TYPE_COUNT]; |
272 for (int i = syncable::AUTOFILL_PROFILE; | 244 for (int i = syncable::AUTOFILL_PROFILE; |
273 i < syncable::MODEL_TYPE_COUNT; ++i) { | 245 i < syncable::MODEL_TYPE_COUNT; ++i) { |
274 changes[i].id = | 246 changes[i].id = i; |
275 MakeNode(test_user_share.user_share(), syncable::ModelTypeFromInt(i)); | |
276 switch (i % 3) { | 247 switch (i % 3) { |
277 case 0: | 248 case 0: |
278 changes[i].action = | 249 changes[i].action = |
279 sync_api::ChangeRecord::ACTION_ADD; | 250 sync_api::ChangeRecord::ACTION_ADD; |
280 break; | 251 break; |
281 case 1: | 252 case 1: |
282 changes[i].action = | 253 changes[i].action = |
283 sync_api::ChangeRecord::ACTION_UPDATE; | 254 sync_api::ChangeRecord::ACTION_UPDATE; |
284 break; | 255 break; |
285 default: | 256 default: |
286 changes[i].action = | 257 changes[i].action = |
287 sync_api::ChangeRecord::ACTION_DELETE; | 258 sync_api::ChangeRecord::ACTION_DELETE; |
288 break; | 259 break; |
289 } | 260 } |
290 { | |
291 sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); | |
292 sync_api::ReadNode node(&trans); | |
293 EXPECT_TRUE(node.InitByIdLookup(changes[i].id)); | |
294 changes[i].specifics = node.GetEntry()->Get(syncable::SPECIFICS); | |
295 } | |
296 } | 261 } |
297 | 262 |
298 // For each i, we call OnChangesApplied() with the first arg equal | 263 // For each i, we call OnChangesApplied() with the first arg equal |
299 // to i cast to ModelType and the second argument with the changes | 264 // to i cast to ModelType and the second argument with the changes |
300 // starting from changes[i]. | 265 // starting from changes[i]. |
301 | 266 |
302 // Set expectations for each data type. | 267 // Set expectations for each data type. |
303 for (int i = syncable::AUTOFILL_PROFILE; | 268 for (int i = syncable::AUTOFILL_PROFILE; |
304 i < syncable::MODEL_TYPE_COUNT; ++i) { | 269 i < syncable::MODEL_TYPE_COUNT; ++i) { |
305 const std::string& model_type_str = | 270 const std::string& model_type_str = |
306 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); | 271 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); |
307 DictionaryValue expected_details; | 272 DictionaryValue expected_details; |
308 expected_details.SetString("modelType", model_type_str); | 273 expected_details.SetString("modelType", model_type_str); |
| 274 expected_details.SetString("writeTransactionId", "0"); |
309 ListValue* expected_changes = new ListValue(); | 275 ListValue* expected_changes = new ListValue(); |
310 expected_details.Set("changes", expected_changes); | 276 expected_details.Set("changes", expected_changes); |
311 for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { | 277 for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { |
312 sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); | 278 expected_changes->Append(changes[j].ToValue()); |
313 expected_changes->Append(changes[j].ToValue(&trans)); | |
314 } | 279 } |
315 EXPECT_CALL(mock_js_event_handler_, | 280 EXPECT_CALL(mock_js_event_handler_, |
316 HandleJsEvent("onChangesApplied", | 281 HandleJsEvent("onChangesApplied", |
317 HasDetailsAsDictionary(expected_details))); | 282 HasDetailsAsDictionary(expected_details))); |
318 } | 283 } |
319 | 284 |
320 // Fire OnChangesApplied() for each data type. | 285 // Fire OnChangesApplied() for each data type. |
321 for (int i = syncable::AUTOFILL_PROFILE; | 286 for (int i = syncable::AUTOFILL_PROFILE; |
322 i < syncable::MODEL_TYPE_COUNT; ++i) { | 287 i < syncable::MODEL_TYPE_COUNT; ++i) { |
323 sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); | |
324 sync_api::ChangeRecordList | 288 sync_api::ChangeRecordList |
325 local_changes(changes + i, changes + arraysize(changes)); | 289 local_changes(changes + i, changes + arraysize(changes)); |
326 js_sync_manager_observer_.OnChangesApplied( | 290 js_sync_manager_observer_.OnChangesApplied( |
327 syncable::ModelTypeFromInt(i), | 291 syncable::ModelTypeFromInt(i), |
328 &trans, | 292 0, sync_api::ImmutableChangeRecordList(&local_changes)); |
329 sync_api::ImmutableChangeRecordList(&local_changes)); | |
330 } | 293 } |
331 | 294 |
332 test_user_share.TearDown(); | |
333 PumpLoop(); | 295 PumpLoop(); |
334 } | 296 } |
335 | 297 |
336 } // namespace | 298 } // namespace |
337 } // namespace browser_sync | 299 } // namespace browser_sync |
OLD | NEW |