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 // 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 <cstddef> | 9 #include <cstddef> |
10 #include <map> | 10 #include <map> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
19 #include "base/message_loop_proxy.h" | 19 #include "base/message_loop_proxy.h" |
20 #include "base/scoped_temp_dir.h" | 20 #include "base/scoped_temp_dir.h" |
21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
22 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
23 #include "base/test/values_test_util.h" | 23 #include "base/test/values_test_util.h" |
24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
25 #include "base/values.h" | 25 #include "base/values.h" |
26 #include "sync/engine/nigori_util.h" | 26 #include "sync/engine/nigori_util.h" |
| 27 #include "sync/engine/sync_scheduler.h" |
27 #include "sync/internal_api/change_record.h" | 28 #include "sync/internal_api/change_record.h" |
28 #include "sync/internal_api/http_post_provider_factory.h" | 29 #include "sync/internal_api/http_post_provider_factory.h" |
29 #include "sync/internal_api/http_post_provider_interface.h" | 30 #include "sync/internal_api/http_post_provider_interface.h" |
30 #include "sync/internal_api/public/engine/model_safe_worker.h" | 31 #include "sync/internal_api/public/engine/model_safe_worker.h" |
31 #include "sync/internal_api/public/engine/polling_constants.h" | 32 #include "sync/internal_api/public/engine/polling_constants.h" |
32 #include "sync/internal_api/public/syncable/model_type_test_util.h" | 33 #include "sync/internal_api/public/syncable/model_type_test_util.h" |
33 #include "sync/internal_api/read_node.h" | 34 #include "sync/internal_api/read_node.h" |
34 #include "sync/internal_api/read_transaction.h" | 35 #include "sync/internal_api/read_transaction.h" |
35 #include "sync/internal_api/sync_manager.h" | 36 #include "sync/internal_api/sync_manager.h" |
36 #include "sync/internal_api/syncapi_internal.h" | 37 #include "sync/internal_api/syncapi_internal.h" |
(...skipping 10 matching lines...) Expand all Loading... |
47 #include "sync/protocol/bookmark_specifics.pb.h" | 48 #include "sync/protocol/bookmark_specifics.pb.h" |
48 #include "sync/protocol/encryption.pb.h" | 49 #include "sync/protocol/encryption.pb.h" |
49 #include "sync/protocol/extension_specifics.pb.h" | 50 #include "sync/protocol/extension_specifics.pb.h" |
50 #include "sync/protocol/password_specifics.pb.h" | 51 #include "sync/protocol/password_specifics.pb.h" |
51 #include "sync/protocol/preference_specifics.pb.h" | 52 #include "sync/protocol/preference_specifics.pb.h" |
52 #include "sync/protocol/proto_value_conversions.h" | 53 #include "sync/protocol/proto_value_conversions.h" |
53 #include "sync/protocol/sync.pb.h" | 54 #include "sync/protocol/sync.pb.h" |
54 #include "sync/sessions/sync_session.h" | 55 #include "sync/sessions/sync_session.h" |
55 #include "sync/syncable/syncable.h" | 56 #include "sync/syncable/syncable.h" |
56 #include "sync/syncable/syncable_id.h" | 57 #include "sync/syncable/syncable_id.h" |
| 58 #include "sync/test/callback_counter.h" |
57 #include "sync/test/fake_encryptor.h" | 59 #include "sync/test/fake_encryptor.h" |
58 #include "sync/test/fake_extensions_activity_monitor.h" | 60 #include "sync/test/fake_extensions_activity_monitor.h" |
59 #include "sync/util/cryptographer.h" | 61 #include "sync/util/cryptographer.h" |
60 #include "sync/util/extensions_activity_monitor.h" | 62 #include "sync/util/extensions_activity_monitor.h" |
61 #include "sync/util/test_unrecoverable_error_handler.h" | 63 #include "sync/util/test_unrecoverable_error_handler.h" |
62 #include "sync/util/time.h" | 64 #include "sync/util/time.h" |
63 #include "testing/gmock/include/gmock/gmock.h" | 65 #include "testing/gmock/include/gmock/gmock.h" |
64 #include "testing/gtest/include/gtest/gtest.h" | 66 #include "testing/gtest/include/gtest/gtest.h" |
65 | 67 |
66 using base::ExpectDictStringValue; | 68 using base::ExpectDictStringValue; |
| 69 using browser_sync::CallbackCounter; |
67 using browser_sync::Cryptographer; | 70 using browser_sync::Cryptographer; |
68 using browser_sync::FakeEncryptor; | 71 using browser_sync::FakeEncryptor; |
69 using browser_sync::FakeExtensionsActivityMonitor; | 72 using browser_sync::FakeExtensionsActivityMonitor; |
70 using browser_sync::HasArgsAsList; | 73 using browser_sync::HasArgsAsList; |
71 using browser_sync::HasDetailsAsDictionary; | 74 using browser_sync::HasDetailsAsDictionary; |
72 using browser_sync::KeyParams; | 75 using browser_sync::KeyParams; |
73 using browser_sync::kNigoriTag; | 76 using browser_sync::kNigoriTag; |
74 using browser_sync::JsArgList; | 77 using browser_sync::JsArgList; |
75 using browser_sync::JsBackend; | 78 using browser_sync::JsBackend; |
76 using browser_sync::JsEventHandler; | 79 using browser_sync::JsEventHandler; |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*)); | 716 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*)); |
714 MOCK_METHOD1(SetUniqueId, void(const std::string&)); | 717 MOCK_METHOD1(SetUniqueId, void(const std::string&)); |
715 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); | 718 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); |
716 MOCK_METHOD2(UpdateCredentials, | 719 MOCK_METHOD2(UpdateCredentials, |
717 void(const std::string&, const std::string&)); | 720 void(const std::string&, const std::string&)); |
718 MOCK_METHOD1(UpdateEnabledTypes, | 721 MOCK_METHOD1(UpdateEnabledTypes, |
719 void(syncable::ModelTypeSet)); | 722 void(syncable::ModelTypeSet)); |
720 MOCK_METHOD1(SendNotification, void(syncable::ModelTypeSet)); | 723 MOCK_METHOD1(SendNotification, void(syncable::ModelTypeSet)); |
721 }; | 724 }; |
722 | 725 |
| 726 class TestSyncManager : public SyncManager { |
| 727 public: |
| 728 TestSyncManager() |
| 729 : SyncManager("Test sync manager"), |
| 730 result_(true) {} |
| 731 |
| 732 browser_sync::ConfigureParams get_config_params() const { return params_; } |
| 733 void set_config_result(bool result) { result_ = result; } |
| 734 |
| 735 protected: |
| 736 // SyncManager implementation. |
| 737 virtual bool DoConfigureSyncer( |
| 738 const browser_sync::ConfigureParams& params) OVERRIDE { |
| 739 params_ = params; |
| 740 if (params.ready_task.is_null()) { |
| 741 ADD_FAILURE(); |
| 742 return result_; |
| 743 } |
| 744 |
| 745 if (result_) |
| 746 params.ready_task.Run(); |
| 747 return result_; |
| 748 } |
| 749 |
| 750 private: |
| 751 browser_sync::ConfigureParams params_; |
| 752 bool result_; |
| 753 }; |
| 754 |
723 } // namespace | 755 } // namespace |
724 | 756 |
725 class SyncManagerTest : public testing::Test, | 757 class SyncManagerTest : public testing::Test, |
726 public SyncManager::ChangeDelegate { | 758 public SyncManager::ChangeDelegate { |
727 protected: | 759 protected: |
728 enum NigoriStatus { | 760 enum NigoriStatus { |
729 DONT_WRITE_NIGORI, | 761 DONT_WRITE_NIGORI, |
730 WRITE_TO_NIGORI | 762 WRITE_TO_NIGORI |
731 }; | 763 }; |
732 | 764 |
733 enum EncryptionStatus { | 765 enum EncryptionStatus { |
734 UNINITIALIZED, | 766 UNINITIALIZED, |
735 DEFAULT_ENCRYPTION, | 767 DEFAULT_ENCRYPTION, |
736 FULL_ENCRYPTION | 768 FULL_ENCRYPTION |
737 }; | 769 }; |
738 | 770 |
739 SyncManagerTest() | 771 SyncManagerTest() |
740 : sync_notifier_mock_(NULL), | 772 : sync_notifier_mock_(NULL), |
741 sync_manager_("Test sync manager"), | |
742 sync_notifier_observer_(NULL), | 773 sync_notifier_observer_(NULL), |
743 update_enabled_types_call_count_(0) {} | 774 update_enabled_types_call_count_(0) {} |
744 | 775 |
745 virtual ~SyncManagerTest() { | 776 virtual ~SyncManagerTest() { |
746 EXPECT_FALSE(sync_notifier_mock_); | 777 EXPECT_FALSE(sync_notifier_mock_); |
747 } | 778 } |
748 | 779 |
749 // Test implementation. | 780 // Test implementation. |
750 void SetUp() { | 781 void SetUp() { |
751 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 782 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 // Needed by |sync_manager_|. | 963 // Needed by |sync_manager_|. |
933 ScopedTempDir temp_dir_; | 964 ScopedTempDir temp_dir_; |
934 // Sync Id's for the roots of the enabled datatypes. | 965 // Sync Id's for the roots of the enabled datatypes. |
935 std::map<ModelType, int64> type_roots_; | 966 std::map<ModelType, int64> type_roots_; |
936 FakeExtensionsActivityMonitor extensions_activity_monitor_; | 967 FakeExtensionsActivityMonitor extensions_activity_monitor_; |
937 StrictMock<SyncNotifierMock>* sync_notifier_mock_; | 968 StrictMock<SyncNotifierMock>* sync_notifier_mock_; |
938 | 969 |
939 protected: | 970 protected: |
940 FakeEncryptor encryptor_; | 971 FakeEncryptor encryptor_; |
941 TestUnrecoverableErrorHandler handler_; | 972 TestUnrecoverableErrorHandler handler_; |
942 SyncManager sync_manager_; | 973 TestSyncManager sync_manager_; |
943 WeakHandle<JsBackend> js_backend_; | 974 WeakHandle<JsBackend> js_backend_; |
944 StrictMock<SyncManagerObserverMock> observer_; | 975 StrictMock<SyncManagerObserverMock> observer_; |
945 sync_notifier::SyncNotifierObserver* sync_notifier_observer_; | 976 sync_notifier::SyncNotifierObserver* sync_notifier_observer_; |
946 int update_enabled_types_call_count_; | 977 int update_enabled_types_call_count_; |
947 }; | 978 }; |
948 | 979 |
949 TEST_F(SyncManagerTest, UpdateEnabledTypes) { | 980 TEST_F(SyncManagerTest, UpdateEnabledTypes) { |
950 EXPECT_EQ(0, update_enabled_types_call_count_); | 981 EXPECT_EQ(0, update_enabled_types_call_count_); |
951 | 982 |
952 ModelSafeRoutingInfo routes; | 983 ModelSafeRoutingInfo routes; |
(...skipping 1547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2500 node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | 2531 node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); |
2501 EXPECT_EQ(title, node.GetTitle()); | 2532 EXPECT_EQ(title, node.GetTitle()); |
2502 EXPECT_EQ(GURL(url2), node.GetURL()); | 2533 EXPECT_EQ(GURL(url2), node.GetURL()); |
2503 const syncable::Entry* node_entry = node.GetEntry(); | 2534 const syncable::Entry* node_entry = node.GetEntry(); |
2504 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | 2535 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); |
2505 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | 2536 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); |
2506 EXPECT_TRUE(specifics.has_encrypted()); | 2537 EXPECT_TRUE(specifics.has_encrypted()); |
2507 } | 2538 } |
2508 } | 2539 } |
2509 | 2540 |
| 2541 // Test that the configuration params are properly sent to DoConfigureSyncer. |
| 2542 TEST_F(SyncManagerTest, BasicConfiguration) { |
| 2543 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; |
| 2544 syncable::ModelTypeSet types_to_config(syncable::BOOKMARKS, |
| 2545 syncable::PREFERENCES); |
| 2546 browser_sync::ModelSafeRoutingInfo new_routing_info; |
| 2547 GetModelSafeRoutingInfo(&new_routing_info); |
| 2548 sync_manager_.set_config_result(true); |
| 2549 |
| 2550 CallbackCounter ready_task_counter, retry_task_counter; |
| 2551 sync_manager_.ConfigureSyncer( |
| 2552 reason, |
| 2553 types_to_config, |
| 2554 new_routing_info, |
| 2555 base::Bind(&CallbackCounter::Callback, |
| 2556 base::Unretained(&ready_task_counter)), |
| 2557 base::Bind(&CallbackCounter::Callback, |
| 2558 base::Unretained(&retry_task_counter))); |
| 2559 EXPECT_EQ(1, ready_task_counter.times_called()); |
| 2560 EXPECT_EQ(0, retry_task_counter.times_called()); |
| 2561 browser_sync::ConfigureParams params = sync_manager_.get_config_params(); |
| 2562 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, |
| 2563 params.source); |
| 2564 EXPECT_TRUE(types_to_config.Equals(params.types_to_config)); |
| 2565 EXPECT_EQ(new_routing_info, params.routing_info); |
| 2566 } |
| 2567 |
| 2568 // Test that the retry callback is invoked on configuration failure. |
| 2569 TEST_F(SyncManagerTest, ConfigurationRetry) { |
| 2570 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; |
| 2571 syncable::ModelTypeSet types_to_config(syncable::BOOKMARKS, |
| 2572 syncable::PREFERENCES); |
| 2573 browser_sync::ModelSafeRoutingInfo new_routing_info; |
| 2574 GetModelSafeRoutingInfo(&new_routing_info); |
| 2575 sync_manager_.set_config_result(false); |
| 2576 |
| 2577 CallbackCounter ready_task_counter, retry_task_counter; |
| 2578 sync_manager_.ConfigureSyncer( |
| 2579 reason, |
| 2580 types_to_config, |
| 2581 new_routing_info, |
| 2582 base::Bind(&CallbackCounter::Callback, |
| 2583 base::Unretained(&ready_task_counter)), |
| 2584 base::Bind(&CallbackCounter::Callback, |
| 2585 base::Unretained(&retry_task_counter))); |
| 2586 EXPECT_EQ(0, ready_task_counter.times_called()); |
| 2587 EXPECT_EQ(1, retry_task_counter.times_called()); |
| 2588 browser_sync::ConfigureParams params = sync_manager_.get_config_params(); |
| 2589 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, |
| 2590 params.source); |
| 2591 EXPECT_TRUE(types_to_config.Equals(params.types_to_config)); |
| 2592 EXPECT_EQ(new_routing_info, params.routing_info); |
| 2593 } |
| 2594 |
2510 } // namespace browser_sync | 2595 } // namespace browser_sync |
OLD | NEW |