OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // Unit tests for the SyncApi. Note that a lot of the underlying | 5 // Unit tests for the SyncApi. Note that a lot of the underlying |
6 // functionality is provided by the Syncable layer, which has its own | 6 // functionality is provided by the Syncable layer, which has its own |
7 // unit tests. We'll test SyncApi specific things in this harness. | 7 // unit tests. We'll test SyncApi specific things in this harness. |
8 | 8 |
| 9 #include <map> |
| 10 |
9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/format_macros.h" |
10 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
11 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
12 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
13 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 17 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 19 #include "base/values.h" |
16 #include "chrome/browser/browser_thread.h" | 20 #include "chrome/browser/browser_thread.h" |
| 21 #include "chrome/browser/sync/engine/model_safe_worker.h" |
17 #include "chrome/browser/sync/engine/syncapi.h" | 22 #include "chrome/browser/sync/engine/syncapi.h" |
18 #include "chrome/browser/sync/js_arg_list.h" | 23 #include "chrome/browser/sync/js_arg_list.h" |
19 #include "chrome/browser/sync/js_backend.h" | 24 #include "chrome/browser/sync/js_backend.h" |
20 #include "chrome/browser/sync/js_event_handler.h" | 25 #include "chrome/browser/sync/js_event_handler.h" |
21 #include "chrome/browser/sync/js_event_router.h" | 26 #include "chrome/browser/sync/js_event_router.h" |
22 #include "chrome/browser/sync/js_test_util.h" | 27 #include "chrome/browser/sync/js_test_util.h" |
23 #include "chrome/browser/sync/protocol/password_specifics.pb.h" | 28 #include "chrome/browser/sync/protocol/password_specifics.pb.h" |
24 #include "chrome/browser/sync/protocol/proto_value_conversions.h" | 29 #include "chrome/browser/sync/protocol/proto_value_conversions.h" |
| 30 #include "chrome/browser/sync/sessions/sync_session.h" |
25 #include "chrome/browser/sync/syncable/directory_manager.h" | 31 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 32 #include "chrome/browser/sync/syncable/nigori_util.h" |
26 #include "chrome/browser/sync/syncable/syncable.h" | 33 #include "chrome/browser/sync/syncable/syncable.h" |
| 34 #include "chrome/browser/sync/syncable/syncable_id.h" |
| 35 #include "chrome/browser/sync/util/cryptographer.h" |
27 #include "chrome/test/sync/engine/test_directory_setter_upper.h" | 36 #include "chrome/test/sync/engine/test_directory_setter_upper.h" |
28 #include "chrome/test/values_test_util.h" | 37 #include "chrome/test/values_test_util.h" |
29 #include "jingle/notifier/base/notifier_options.h" | 38 #include "jingle/notifier/base/notifier_options.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 39 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
32 | 41 |
| 42 using browser_sync::Cryptographer; |
33 using browser_sync::HasArgsAsList; | 43 using browser_sync::HasArgsAsList; |
34 using browser_sync::KeyParams; | 44 using browser_sync::KeyParams; |
35 using browser_sync::JsArgList; | 45 using browser_sync::JsArgList; |
36 using browser_sync::MockJsEventHandler; | 46 using browser_sync::MockJsEventHandler; |
37 using browser_sync::MockJsEventRouter; | 47 using browser_sync::MockJsEventRouter; |
| 48 using browser_sync::ModelSafeRoutingInfo; |
| 49 using browser_sync::ModelSafeWorker; |
| 50 using browser_sync::ModelSafeWorkerRegistrar; |
| 51 using browser_sync::sessions::SyncSessionSnapshot; |
| 52 using syncable::ModelType; |
| 53 using syncable::ModelTypeSet; |
38 using test::ExpectDictionaryValue; | 54 using test::ExpectDictionaryValue; |
39 using test::ExpectStringValue; | 55 using test::ExpectStringValue; |
40 using testing::_; | 56 using testing::_; |
41 using testing::Invoke; | 57 using testing::Invoke; |
42 using testing::SaveArg; | 58 using testing::SaveArg; |
43 using testing::StrictMock; | 59 using testing::StrictMock; |
44 | 60 |
45 namespace sync_api { | 61 namespace sync_api { |
46 | 62 |
47 namespace { | 63 namespace { |
48 | 64 |
49 void ExpectInt64Value(int64 expected_value, | 65 void ExpectInt64Value(int64 expected_value, |
50 const DictionaryValue& value, const std::string& key) { | 66 const DictionaryValue& value, const std::string& key) { |
51 std::string int64_str; | 67 std::string int64_str; |
52 EXPECT_TRUE(value.GetString(key, &int64_str)); | 68 EXPECT_TRUE(value.GetString(key, &int64_str)); |
53 int64 val = 0; | 69 int64 val = 0; |
54 EXPECT_TRUE(base::StringToInt64(int64_str, &val)); | 70 EXPECT_TRUE(base::StringToInt64(int64_str, &val)); |
55 EXPECT_EQ(expected_value, val); | 71 EXPECT_EQ(expected_value, val); |
56 } | 72 } |
57 | 73 |
58 // Makes a non-folder child of the root node. Returns the id of the | 74 // Makes a non-folder child of the root node. Returns the id of the |
59 // newly-created node. | 75 // newly-created node. |
60 int64 MakeNode(UserShare* share, | 76 int64 MakeNode(UserShare* share, |
61 syncable::ModelType model_type, | 77 ModelType model_type, |
62 const std::string& client_tag) { | 78 const std::string& client_tag) { |
63 WriteTransaction trans(share); | 79 WriteTransaction trans(share); |
64 ReadNode root_node(&trans); | 80 ReadNode root_node(&trans); |
65 root_node.InitByRootLookup(); | 81 root_node.InitByRootLookup(); |
66 WriteNode node(&trans); | 82 WriteNode node(&trans); |
67 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); | 83 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); |
68 node.SetIsFolder(false); | 84 node.SetIsFolder(false); |
69 return node.GetId(); | 85 return node.GetId(); |
70 } | 86 } |
71 | 87 |
| 88 // Make a folder as a child of the root node. Returns the id of the |
| 89 // newly-created node. |
| 90 int64 MakeFolder(UserShare* share, |
| 91 syncable::ModelType model_type, |
| 92 const std::string& client_tag) { |
| 93 WriteTransaction trans(share); |
| 94 ReadNode root_node(&trans); |
| 95 root_node.InitByRootLookup(); |
| 96 WriteNode node(&trans); |
| 97 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); |
| 98 node.SetIsFolder(true); |
| 99 return node.GetId(); |
| 100 } |
| 101 |
| 102 // Makes a non-folder child of a non-root node. Returns the id of the |
| 103 // newly-created node. |
| 104 int64 MakeNodeWithParent(UserShare* share, |
| 105 ModelType model_type, |
| 106 const std::string& client_tag, |
| 107 int64 parent_id) { |
| 108 WriteTransaction trans(share); |
| 109 ReadNode parent_node(&trans); |
| 110 parent_node.InitByIdLookup(parent_id); |
| 111 WriteNode node(&trans); |
| 112 EXPECT_TRUE(node.InitUniqueByCreation(model_type, parent_node, client_tag)); |
| 113 node.SetIsFolder(false); |
| 114 return node.GetId(); |
| 115 } |
| 116 |
| 117 // Makes a folder child of a non-root node. Returns the id of the |
| 118 // newly-created node. |
| 119 int64 MakeFolderWithParent(UserShare* share, |
| 120 ModelType model_type, |
| 121 int64 parent_id, |
| 122 BaseNode* predecessor) { |
| 123 WriteTransaction trans(share); |
| 124 ReadNode parent_node(&trans); |
| 125 parent_node.InitByIdLookup(parent_id); |
| 126 WriteNode node(&trans); |
| 127 EXPECT_TRUE(node.InitByCreation(model_type, parent_node, predecessor)); |
| 128 node.SetIsFolder(true); |
| 129 return node.GetId(); |
| 130 } |
| 131 |
| 132 // Creates the "synced" root node for a particular datatype. We use the syncable |
| 133 // methods here so that the syncer treats these nodes as if they were already |
| 134 // received from the server. |
| 135 int64 MakeServerNodeForType(UserShare* share, |
| 136 ModelType model_type) { |
| 137 sync_pb::EntitySpecifics specifics; |
| 138 syncable::AddDefaultExtensionValue(model_type, &specifics); |
| 139 syncable::ScopedDirLookup dir(share->dir_manager.get(), share->name); |
| 140 EXPECT_TRUE(dir.good()); |
| 141 syncable::WriteTransaction trans(dir, syncable::UNITTEST, __FILE__, __LINE__); |
| 142 // Attempt to lookup by nigori tag. |
| 143 std::string type_tag = syncable::ModelTypeToRootTag(model_type); |
| 144 syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag); |
| 145 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 146 node_id); |
| 147 EXPECT_TRUE(entry.good()); |
| 148 entry.Put(syncable::BASE_VERSION, 1); |
| 149 entry.Put(syncable::SERVER_VERSION, 1); |
| 150 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
| 151 entry.Put(syncable::SERVER_PARENT_ID, syncable::kNullId); |
| 152 entry.Put(syncable::SERVER_IS_DIR, false); |
| 153 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 154 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag); |
| 155 entry.Put(syncable::NON_UNIQUE_NAME, type_tag); |
| 156 entry.Put(syncable::IS_DEL, false); |
| 157 entry.Put(syncable::SPECIFICS, specifics); |
| 158 return entry.Get(syncable::META_HANDLE); |
| 159 } |
| 160 |
72 } // namespace | 161 } // namespace |
73 | 162 |
74 class SyncApiTest : public testing::Test { | 163 class SyncApiTest : public testing::Test { |
75 public: | 164 public: |
76 virtual void SetUp() { | 165 virtual void SetUp() { |
77 setter_upper_.SetUp(); | 166 setter_upper_.SetUp(); |
78 share_.dir_manager.reset(setter_upper_.manager()); | 167 share_.dir_manager.reset(setter_upper_.manager()); |
79 share_.name = setter_upper_.name(); | 168 share_.name = setter_upper_.name(); |
80 } | 169 } |
81 | 170 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 WriteTransaction trans(&share_); | 280 WriteTransaction trans(&share_); |
192 WriteNode node(&trans); | 281 WriteNode node(&trans); |
193 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, | 282 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, |
194 "testtag")); | 283 "testtag")); |
195 } | 284 } |
196 } | 285 } |
197 | 286 |
198 // TODO(chron): Hook this all up to the server and write full integration tests | 287 // TODO(chron): Hook this all up to the server and write full integration tests |
199 // for update->undelete behavior. | 288 // for update->undelete behavior. |
200 TEST_F(SyncApiTest, TestDeleteBehavior) { | 289 TEST_F(SyncApiTest, TestDeleteBehavior) { |
201 | |
202 int64 node_id; | 290 int64 node_id; |
203 int64 folder_id; | 291 int64 folder_id; |
204 std::wstring test_title(L"test1"); | 292 std::wstring test_title(L"test1"); |
205 | 293 |
206 { | 294 { |
207 WriteTransaction trans(&share_); | 295 WriteTransaction trans(&share_); |
208 ReadNode root_node(&trans); | 296 ReadNode root_node(&trans); |
209 root_node.InitByRootLookup(); | 297 root_node.InitByRootLookup(); |
210 | 298 |
211 // we'll use this spare folder later | 299 // we'll use this spare folder later |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 ExpectInt64Value(node.GetId(), value, "id"); | 397 ExpectInt64Value(node.GetId(), value, "id"); |
310 ExpectInt64Value(node.GetModificationTime(), value, "modificationTime"); | 398 ExpectInt64Value(node.GetModificationTime(), value, "modificationTime"); |
311 ExpectInt64Value(node.GetParentId(), value, "parentId"); | 399 ExpectInt64Value(node.GetParentId(), value, "parentId"); |
312 { | 400 { |
313 bool is_folder = false; | 401 bool is_folder = false; |
314 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder)); | 402 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder)); |
315 EXPECT_EQ(node.GetIsFolder(), is_folder); | 403 EXPECT_EQ(node.GetIsFolder(), is_folder); |
316 } | 404 } |
317 ExpectStringValue(WideToUTF8(node.GetTitle()), value, "title"); | 405 ExpectStringValue(WideToUTF8(node.GetTitle()), value, "title"); |
318 { | 406 { |
319 syncable::ModelType expected_model_type = node.GetModelType(); | 407 ModelType expected_model_type = node.GetModelType(); |
320 std::string type_str; | 408 std::string type_str; |
321 EXPECT_TRUE(value.GetString("type", &type_str)); | 409 EXPECT_TRUE(value.GetString("type", &type_str)); |
322 if (expected_model_type >= syncable::FIRST_REAL_MODEL_TYPE) { | 410 if (expected_model_type >= syncable::FIRST_REAL_MODEL_TYPE) { |
323 syncable::ModelType model_type = | 411 ModelType model_type = |
324 syncable::ModelTypeFromString(type_str); | 412 syncable::ModelTypeFromString(type_str); |
325 EXPECT_EQ(expected_model_type, model_type); | 413 EXPECT_EQ(expected_model_type, model_type); |
326 } else if (expected_model_type == syncable::TOP_LEVEL_FOLDER) { | 414 } else if (expected_model_type == syncable::TOP_LEVEL_FOLDER) { |
327 EXPECT_EQ("Top-level folder", type_str); | 415 EXPECT_EQ("Top-level folder", type_str); |
328 } else if (expected_model_type == syncable::UNSPECIFIED) { | 416 } else if (expected_model_type == syncable::UNSPECIFIED) { |
329 EXPECT_EQ("Unspecified", type_str); | 417 EXPECT_EQ("Unspecified", type_str); |
330 } else { | 418 } else { |
331 ADD_FAILURE(); | 419 ADD_FAILURE(); |
332 } | 420 } |
333 } | 421 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 if (record.extra.get()) { | 501 if (record.extra.get()) { |
414 expected_extra_value.reset(record.extra->ToValue()); | 502 expected_extra_value.reset(record.extra->ToValue()); |
415 } | 503 } |
416 Value* extra_value = NULL; | 504 Value* extra_value = NULL; |
417 EXPECT_EQ(record.extra.get() != NULL, | 505 EXPECT_EQ(record.extra.get() != NULL, |
418 node_value->Get("extra", &extra_value)); | 506 node_value->Get("extra", &extra_value)); |
419 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); | 507 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); |
420 } | 508 } |
421 } | 509 } |
422 | 510 |
423 class MockExtraChangeRecordData : public SyncManager::ExtraChangeRecordData { | 511 class MockExtraChangeRecordData |
| 512 : public SyncManager::ExtraPasswordChangeRecordData { |
424 public: | 513 public: |
425 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); | 514 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); |
426 }; | 515 }; |
427 | 516 |
428 } // namespace | 517 } // namespace |
429 | 518 |
430 TEST_F(SyncApiTest, ChangeRecordToValue) { | 519 TEST_F(SyncApiTest, ChangeRecordToValue) { |
431 int64 child_id = MakeNode(&share_, syncable::BOOKMARKS, "testtag"); | 520 int64 child_id = MakeNode(&share_, syncable::BOOKMARKS, "testtag"); |
432 sync_pb::EntitySpecifics child_specifics; | 521 sync_pb::EntitySpecifics child_specifics; |
433 { | 522 { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 virtual ~TestHttpPostProviderFactory() {} | 589 virtual ~TestHttpPostProviderFactory() {} |
501 virtual HttpPostProviderInterface* Create() { | 590 virtual HttpPostProviderInterface* Create() { |
502 NOTREACHED(); | 591 NOTREACHED(); |
503 return NULL; | 592 return NULL; |
504 } | 593 } |
505 virtual void Destroy(HttpPostProviderInterface* http) { | 594 virtual void Destroy(HttpPostProviderInterface* http) { |
506 NOTREACHED(); | 595 NOTREACHED(); |
507 } | 596 } |
508 }; | 597 }; |
509 | 598 |
510 class SyncManagerTest : public testing::Test { | 599 class SyncManagerObserverMock : public SyncManager::Observer { |
| 600 public: |
| 601 MOCK_METHOD4(OnChangesApplied, |
| 602 void(ModelType, |
| 603 const BaseTransaction*, |
| 604 const SyncManager::ChangeRecord*, |
| 605 int)); // NOLINT |
| 606 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT |
| 607 MOCK_METHOD1(OnSyncCycleCompleted, |
| 608 void(const SyncSessionSnapshot*)); // NOLINT |
| 609 MOCK_METHOD0(OnInitializationComplete, void()); // NOLINT |
| 610 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT |
| 611 MOCK_METHOD1(OnPassphraseRequired, void(bool)); // NOLINT |
| 612 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT |
| 613 MOCK_METHOD0(OnPaused, void()); // NOLINT |
| 614 MOCK_METHOD0(OnResumed, void()); // NOLINT |
| 615 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT |
| 616 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT |
| 617 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT |
| 618 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT |
| 619 MOCK_METHOD1(OnEncryptionComplete, void(const ModelTypeSet&)); // NOLINT |
| 620 }; |
| 621 |
| 622 class SyncManagerTest : public testing::Test, |
| 623 public ModelSafeWorkerRegistrar { |
511 protected: | 624 protected: |
512 SyncManagerTest() : ui_thread_(BrowserThread::UI, &ui_loop_) {} | 625 SyncManagerTest() : ui_thread_(BrowserThread::UI, &ui_loop_) {} |
513 | 626 |
| 627 // Test implementation. |
514 void SetUp() { | 628 void SetUp() { |
515 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 629 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
516 sync_manager_.Init(temp_dir_.path(), "bogus", 0, false, | 630 sync_manager_.Init(temp_dir_.path(), "bogus", 0, false, |
517 new TestHttpPostProviderFactory(), NULL, "bogus", | 631 new TestHttpPostProviderFactory(), this, "bogus", |
518 SyncCredentials(), notifier::NotifierOptions(), | 632 SyncCredentials(), notifier::NotifierOptions(), |
519 "", true /* setup_for_test_mode */); | 633 "", true /* setup_for_test_mode */); |
| 634 sync_manager_.AddObserver(&observer_); |
| 635 ModelSafeRoutingInfo routes; |
| 636 GetModelSafeRoutingInfo(&routes); |
| 637 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); |
| 638 ++i) { |
| 639 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _, 1)) |
| 640 .RetiresOnSaturation(); |
| 641 EXPECT_CALL(observer_, OnChangesComplete(i->first)) |
| 642 .RetiresOnSaturation(); |
| 643 type_roots_[i->first] = MakeServerNodeForType( |
| 644 sync_manager_.GetUserShare(), i->first); |
| 645 } |
520 } | 646 } |
521 | 647 |
522 void TearDown() { | 648 void TearDown() { |
| 649 sync_manager_.RemoveObserver(&observer_); |
523 sync_manager_.Shutdown(); | 650 sync_manager_.Shutdown(); |
524 } | 651 } |
525 | 652 |
| 653 // ModelSafeWorkerRegistrar implementation. |
| 654 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) { |
| 655 NOTIMPLEMENTED(); |
| 656 out->clear(); |
| 657 } |
| 658 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { |
| 659 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE; |
| 660 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE; |
| 661 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE; |
| 662 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE; |
| 663 } |
| 664 |
| 665 // Helper methods. |
| 666 bool SetUpEncryption() { |
| 667 // We need to create the nigori node as if it were an applied server update. |
| 668 UserShare* share = sync_manager_.GetUserShare(); |
| 669 int64 nigori_id = GetIdForDataType(syncable::NIGORI); |
| 670 if (nigori_id == kInvalidId) |
| 671 return false; |
| 672 |
| 673 // Set the nigori cryptographer information. |
| 674 Cryptographer* cryptographer = share->dir_manager->cryptographer(); |
| 675 if (!cryptographer) |
| 676 return false; |
| 677 KeyParams params = {"localhost", "dummy", "foobar"}; |
| 678 cryptographer->AddKey(params); |
| 679 sync_pb::NigoriSpecifics nigori; |
| 680 cryptographer->GetKeys(nigori.mutable_encrypted()); |
| 681 WriteTransaction trans(share); |
| 682 WriteNode node(&trans); |
| 683 node.InitByIdLookup(nigori_id); |
| 684 node.SetNigoriSpecifics(nigori); |
| 685 return cryptographer->is_ready(); |
| 686 } |
| 687 |
| 688 int64 GetIdForDataType(ModelType type) { |
| 689 if (type_roots_.count(type) == 0) |
| 690 return 0; |
| 691 return type_roots_[type]; |
| 692 } |
| 693 |
526 private: | 694 private: |
527 // Needed by |ui_thread_|. | 695 // Needed by |ui_thread_|. |
528 MessageLoopForUI ui_loop_; | 696 MessageLoopForUI ui_loop_; |
529 // Needed by |sync_manager_|. | 697 // Needed by |sync_manager_|. |
530 BrowserThread ui_thread_; | 698 BrowserThread ui_thread_; |
531 // Needed by |sync_manager_|. | 699 // Needed by |sync_manager_|. |
532 ScopedTempDir temp_dir_; | 700 ScopedTempDir temp_dir_; |
| 701 // Sync Id's for the roots of the enabled datatypes. |
| 702 std::map<ModelType, int64> type_roots_; |
533 | 703 |
534 protected: | 704 protected: |
535 SyncManager sync_manager_; | 705 SyncManager sync_manager_; |
| 706 StrictMock<SyncManagerObserverMock> observer_; |
536 }; | 707 }; |
537 | 708 |
538 TEST_F(SyncManagerTest, ParentJsEventRouter) { | 709 TEST_F(SyncManagerTest, ParentJsEventRouter) { |
539 StrictMock<MockJsEventRouter> event_router; | 710 StrictMock<MockJsEventRouter> event_router; |
540 browser_sync::JsBackend* js_backend = sync_manager_.GetJsBackend(); | 711 browser_sync::JsBackend* js_backend = sync_manager_.GetJsBackend(); |
541 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); | 712 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); |
542 js_backend->SetParentJsEventRouter(&event_router); | 713 js_backend->SetParentJsEventRouter(&event_router); |
543 EXPECT_EQ(&event_router, js_backend->GetParentJsEventRouter()); | 714 EXPECT_EQ(&event_router, js_backend->GetParentJsEventRouter()); |
544 js_backend->RemoveParentJsEventRouter(); | 715 js_backend->RemoveParentJsEventRouter(); |
545 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); | 716 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 967 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
797 | 968 |
798 js_backend->SetParentJsEventRouter(&event_router); | 969 js_backend->SetParentJsEventRouter(&event_router); |
799 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 970 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
800 js_backend->RemoveParentJsEventRouter(); | 971 js_backend->RemoveParentJsEventRouter(); |
801 | 972 |
802 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); | 973 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); |
803 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 974 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
804 } | 975 } |
805 | 976 |
| 977 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { |
| 978 EXPECT_TRUE(SetUpEncryption()); |
| 979 ModelTypeSet encrypted_types; |
| 980 encrypted_types.insert(syncable::BOOKMARKS); |
| 981 EXPECT_CALL(observer_, OnEncryptionComplete(encrypted_types)); |
| 982 sync_manager_.EncryptDataTypes(encrypted_types); |
| 983 { |
| 984 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 985 EXPECT_EQ(encrypted_types, |
| 986 GetEncryptedDataTypes(trans.GetWrappedTrans())); |
| 987 } |
| 988 } |
| 989 |
| 990 TEST_F(SyncManagerTest, EncryptDataTypesWithData) { |
| 991 size_t batch_size = 5; |
| 992 EXPECT_TRUE(SetUpEncryption()); |
| 993 |
| 994 // Create some unencrypted unsynced data. |
| 995 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), |
| 996 syncable::BOOKMARKS, |
| 997 GetIdForDataType(syncable::BOOKMARKS), |
| 998 NULL); |
| 999 // First batch_size nodes are children of folder. |
| 1000 size_t i; |
| 1001 for (i = 0; i < batch_size; ++i) { |
| 1002 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::BOOKMARKS, |
| 1003 StringPrintf("%"PRIuS"", i), folder); |
| 1004 } |
| 1005 // Next batch_size nodes are a different type and on their own. |
| 1006 for (; i < 2*batch_size; ++i) { |
| 1007 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::SESSIONS, |
| 1008 StringPrintf("%"PRIuS"", i), |
| 1009 GetIdForDataType(syncable::SESSIONS)); |
| 1010 } |
| 1011 // Last batch_size nodes are a third type that will not need encryption. |
| 1012 for (; i < 3*batch_size; ++i) { |
| 1013 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::THEMES, |
| 1014 StringPrintf("%"PRIuS"", i), |
| 1015 GetIdForDataType(syncable::THEMES)); |
| 1016 } |
| 1017 |
| 1018 { |
| 1019 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1020 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1021 syncable::BOOKMARKS, |
| 1022 false /* not encrypted */)); |
| 1023 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1024 syncable::SESSIONS, |
| 1025 false /* not encrypted */)); |
| 1026 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1027 syncable::THEMES, |
| 1028 false /* not encrypted */)); |
| 1029 } |
| 1030 |
| 1031 ModelTypeSet encrypted_types; |
| 1032 encrypted_types.insert(syncable::BOOKMARKS); |
| 1033 encrypted_types.insert(syncable::SESSIONS); |
| 1034 EXPECT_CALL(observer_, OnEncryptionComplete(encrypted_types)); |
| 1035 sync_manager_.EncryptDataTypes(encrypted_types); |
| 1036 |
| 1037 { |
| 1038 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1039 EXPECT_EQ(encrypted_types, |
| 1040 GetEncryptedDataTypes(trans.GetWrappedTrans())); |
| 1041 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1042 syncable::BOOKMARKS, |
| 1043 true /* is encrypted */)); |
| 1044 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1045 syncable::SESSIONS, |
| 1046 true /* is encrypted */)); |
| 1047 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1048 syncable::THEMES, |
| 1049 false /* not encrypted */)); |
| 1050 } |
| 1051 } |
| 1052 |
806 } // namespace | 1053 } // namespace |
807 | 1054 |
808 } // namespace browser_sync | 1055 } // namespace browser_sync |
OLD | NEW |