Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: sync/internal_api/syncapi_unittest.cc

Issue 10483015: [Sync] Refactor sync configuration logic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use mock sync scheduler instead of DoConfigureSyncer Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sync/internal_api/sync_manager.cc ('k') | sync/sync.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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;
70 using browser_sync::ConfigurationParams;
67 using browser_sync::Cryptographer; 71 using browser_sync::Cryptographer;
68 using browser_sync::FakeEncryptor; 72 using browser_sync::FakeEncryptor;
69 using browser_sync::FakeExtensionsActivityMonitor; 73 using browser_sync::FakeExtensionsActivityMonitor;
70 using browser_sync::HasArgsAsList; 74 using browser_sync::HasArgsAsList;
71 using browser_sync::HasDetailsAsDictionary; 75 using browser_sync::HasDetailsAsDictionary;
72 using browser_sync::KeyParams; 76 using browser_sync::KeyParams;
73 using browser_sync::kNigoriTag; 77 using browser_sync::kNigoriTag;
74 using browser_sync::JsArgList; 78 using browser_sync::JsArgList;
75 using browser_sync::JsBackend; 79 using browser_sync::JsBackend;
76 using browser_sync::JsEventHandler; 80 using browser_sync::JsEventHandler;
77 using browser_sync::JsReplyHandler; 81 using browser_sync::JsReplyHandler;
78 using browser_sync::MockJsEventHandler; 82 using browser_sync::MockJsEventHandler;
79 using browser_sync::MockJsReplyHandler; 83 using browser_sync::MockJsReplyHandler;
80 using browser_sync::ModelSafeRoutingInfo; 84 using browser_sync::ModelSafeRoutingInfo;
81 using browser_sync::ModelSafeWorker; 85 using browser_sync::ModelSafeWorker;
82 using browser_sync::sessions::SyncSessionSnapshot; 86 using browser_sync::sessions::SyncSessionSnapshot;
87 using browser_sync::SyncScheduler;
83 using browser_sync::TestUnrecoverableErrorHandler; 88 using browser_sync::TestUnrecoverableErrorHandler;
84 using browser_sync::WeakHandle; 89 using browser_sync::WeakHandle;
85 using syncable::IS_DEL; 90 using syncable::IS_DEL;
86 using syncable::IS_UNSYNCED; 91 using syncable::IS_UNSYNCED;
87 using syncable::kEncryptedString; 92 using syncable::kEncryptedString;
88 using syncable::ModelTypeSet; 93 using syncable::ModelTypeSet;
89 using syncable::ModelType; 94 using syncable::ModelType;
90 using syncable::NON_UNIQUE_NAME; 95 using syncable::NON_UNIQUE_NAME;
91 using syncable::SPECIFICS; 96 using syncable::SPECIFICS;
92 using testing::_; 97 using testing::_;
93 using testing::AnyNumber; 98 using testing::AnyNumber;
94 using testing::AtLeast; 99 using testing::AtLeast;
100 using testing::DoAll;
95 using testing::InSequence; 101 using testing::InSequence;
96 using testing::Invoke; 102 using testing::Invoke;
103 using testing::Return;
97 using testing::SaveArg; 104 using testing::SaveArg;
98 using testing::StrictMock; 105 using testing::StrictMock;
99 106
100 namespace sync_api { 107 namespace sync_api {
101 108
102 namespace { 109 namespace {
103 110
104 const char kTestChromeVersion[] = "test chrome version"; 111 const char kTestChromeVersion[] = "test chrome version";
105 112
106 void DoNothing() {} 113 void DoNothing() {}
(...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag); 926 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag);
920 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG, 927 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG,
921 hash); 928 hash);
922 EXPECT_TRUE(entry.good()); 929 EXPECT_TRUE(entry.good());
923 if (!entry.Get(IS_UNSYNCED)) 930 if (!entry.Get(IS_UNSYNCED))
924 return false; 931 return false;
925 entry.Put(IS_UNSYNCED, false); 932 entry.Put(IS_UNSYNCED, false);
926 return true; 933 return true;
927 } 934 }
928 935
936 void SetScheduler(scoped_ptr<SyncScheduler> scheduler) {
937 sync_manager_.SetSyncSchedulerForTest(scheduler.Pass());
938 }
939
929 private: 940 private:
930 // Needed by |sync_manager_|. 941 // Needed by |sync_manager_|.
931 MessageLoop message_loop_; 942 MessageLoop message_loop_;
932 // Needed by |sync_manager_|. 943 // Needed by |sync_manager_|.
933 ScopedTempDir temp_dir_; 944 ScopedTempDir temp_dir_;
934 // Sync Id's for the roots of the enabled datatypes. 945 // Sync Id's for the roots of the enabled datatypes.
935 std::map<ModelType, int64> type_roots_; 946 std::map<ModelType, int64> type_roots_;
936 FakeExtensionsActivityMonitor extensions_activity_monitor_; 947 FakeExtensionsActivityMonitor extensions_activity_monitor_;
937 StrictMock<SyncNotifierMock>* sync_notifier_mock_; 948 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
938 949
(...skipping 1561 matching lines...) Expand 10 before | Expand all | Expand 10 after
2500 node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); 2511 node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
2501 EXPECT_EQ(title, node.GetTitle()); 2512 EXPECT_EQ(title, node.GetTitle());
2502 EXPECT_EQ(GURL(url2), node.GetURL()); 2513 EXPECT_EQ(GURL(url2), node.GetURL());
2503 const syncable::Entry* node_entry = node.GetEntry(); 2514 const syncable::Entry* node_entry = node.GetEntry();
2504 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2515 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2505 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2516 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2506 EXPECT_TRUE(specifics.has_encrypted()); 2517 EXPECT_TRUE(specifics.has_encrypted());
2507 } 2518 }
2508 } 2519 }
2509 2520
2521 namespace {
2522
2523 class MockSyncScheduler : public SyncScheduler {
2524 public:
2525 MockSyncScheduler() : SyncScheduler("name", NULL, NULL) {}
2526 virtual ~MockSyncScheduler() {}
2527
2528 MOCK_METHOD1(Start, void(SyncScheduler::Mode));
2529 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&));
2530 };
2531
2532 } // namespace
2533
2534 // Test that the configuration params are properly created and sent to
2535 // ScheduleConfigure. No callback should be invoked.
2536 TEST_F(SyncManagerTest, BasicConfiguration) {
2537 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2538 syncable::ModelTypeSet types_to_download(syncable::BOOKMARKS,
2539 syncable::PREFERENCES);
2540 browser_sync::ModelSafeRoutingInfo new_routing_info;
2541 GetModelSafeRoutingInfo(&new_routing_info);
2542
2543 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler());
2544 ConfigurationParams params;
2545 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE));
2546 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)).
2547 WillOnce(DoAll(SaveArg<0>(&params), Return(true)));
2548 SetScheduler(scheduler.PassAs<SyncScheduler>());
2549
2550 CallbackCounter ready_task_counter, retry_task_counter;
2551 sync_manager_.ConfigureSyncer(
2552 reason,
2553 types_to_download,
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(0, ready_task_counter.times_called());
2560 EXPECT_EQ(0, retry_task_counter.times_called());
2561 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2562 params.source);
2563 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2564 EXPECT_EQ(new_routing_info, params.routing_info);
2565 }
2566
2567 // Test that the retry callback is invoked on configuration failure.
2568 TEST_F(SyncManagerTest, ConfigurationRetry) {
2569 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2570 syncable::ModelTypeSet types_to_download(syncable::BOOKMARKS,
2571 syncable::PREFERENCES);
2572 browser_sync::ModelSafeRoutingInfo new_routing_info;
2573 GetModelSafeRoutingInfo(&new_routing_info);
2574
2575 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler());
2576 ConfigurationParams params;
2577 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE));
2578 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)).
2579 WillOnce(DoAll(SaveArg<0>(&params), Return(false)));
2580 SetScheduler(scheduler.PassAs<SyncScheduler>());
2581
2582 CallbackCounter ready_task_counter, retry_task_counter;
2583 sync_manager_.ConfigureSyncer(
2584 reason,
2585 types_to_download,
2586 new_routing_info,
2587 base::Bind(&CallbackCounter::Callback,
2588 base::Unretained(&ready_task_counter)),
2589 base::Bind(&CallbackCounter::Callback,
2590 base::Unretained(&retry_task_counter)));
2591 EXPECT_EQ(0, ready_task_counter.times_called());
2592 EXPECT_EQ(1, retry_task_counter.times_called());
2593 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2594 params.source);
2595 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2596 EXPECT_EQ(new_routing_info, params.routing_info);
2597 }
2598
2510 } // namespace browser_sync 2599 } // namespace browser_sync
OLDNEW
« no previous file with comments | « sync/internal_api/sync_manager.cc ('k') | sync/sync.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698