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

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

Issue 10701085: Revert "Revert 142517 - [Sync] Refactor sync configuration logic." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix deps Created 8 years, 5 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
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/internal_api/public/base/model_type_test_util.h" 26 #include "sync/internal_api/public/base/model_type_test_util.h"
27 #include "sync/engine/sync_scheduler.h"
27 #include "sync/internal_api/public/change_record.h" 28 #include "sync/internal_api/public/change_record.h"
28 #include "sync/internal_api/public/engine/model_safe_worker.h" 29 #include "sync/internal_api/public/engine/model_safe_worker.h"
29 #include "sync/internal_api/public/engine/polling_constants.h" 30 #include "sync/internal_api/public/engine/polling_constants.h"
30 #include "sync/internal_api/public/http_post_provider_factory.h" 31 #include "sync/internal_api/public/http_post_provider_factory.h"
31 #include "sync/internal_api/public/http_post_provider_interface.h" 32 #include "sync/internal_api/public/http_post_provider_interface.h"
32 #include "sync/internal_api/public/read_node.h" 33 #include "sync/internal_api/public/read_node.h"
33 #include "sync/internal_api/public/read_transaction.h" 34 #include "sync/internal_api/public/read_transaction.h"
34 #include "sync/internal_api/public/sync_manager.h" 35 #include "sync/internal_api/public/sync_manager.h"
35 #include "sync/internal_api/public/test/test_user_share.h" 36 #include "sync/internal_api/public/test/test_user_share.h"
36 #include "sync/internal_api/public/write_node.h" 37 #include "sync/internal_api/public/write_node.h"
(...skipping 13 matching lines...) Expand all
50 #include "sync/protocol/preference_specifics.pb.h" 51 #include "sync/protocol/preference_specifics.pb.h"
51 #include "sync/protocol/proto_value_conversions.h" 52 #include "sync/protocol/proto_value_conversions.h"
52 #include "sync/protocol/sync.pb.h" 53 #include "sync/protocol/sync.pb.h"
53 #include "sync/sessions/sync_session.h" 54 #include "sync/sessions/sync_session.h"
54 #include "sync/syncable/directory.h" 55 #include "sync/syncable/directory.h"
55 #include "sync/syncable/entry.h" 56 #include "sync/syncable/entry.h"
56 #include "sync/syncable/mutable_entry.h" 57 #include "sync/syncable/mutable_entry.h"
57 #include "sync/syncable/nigori_util.h" 58 #include "sync/syncable/nigori_util.h"
58 #include "sync/syncable/syncable_id.h" 59 #include "sync/syncable/syncable_id.h"
59 #include "sync/syncable/write_transaction.h" 60 #include "sync/syncable/write_transaction.h"
61 #include "sync/test/callback_counter.h"
60 #include "sync/test/fake_encryptor.h" 62 #include "sync/test/fake_encryptor.h"
61 #include "sync/test/fake_extensions_activity_monitor.h" 63 #include "sync/test/fake_extensions_activity_monitor.h"
62 #include "sync/util/cryptographer.h" 64 #include "sync/util/cryptographer.h"
63 #include "sync/util/extensions_activity_monitor.h" 65 #include "sync/util/extensions_activity_monitor.h"
64 #include "sync/util/test_unrecoverable_error_handler.h" 66 #include "sync/util/test_unrecoverable_error_handler.h"
65 #include "sync/util/time.h" 67 #include "sync/util/time.h"
66 #include "testing/gmock/include/gmock/gmock.h" 68 #include "testing/gmock/include/gmock/gmock.h"
67 #include "testing/gtest/include/gtest/gtest.h" 69 #include "testing/gtest/include/gtest/gtest.h"
68 70
69 using base::ExpectDictStringValue; 71 using base::ExpectDictStringValue;
70 using testing::_; 72 using testing::_;
71 using testing::AnyNumber; 73 using testing::AnyNumber;
72 using testing::AtLeast; 74 using testing::AtLeast;
75 using testing::DoAll;
73 using testing::InSequence; 76 using testing::InSequence;
74 using testing::Invoke; 77 using testing::Invoke;
78 using testing::Return;
75 using testing::SaveArg; 79 using testing::SaveArg;
76 using testing::StrictMock; 80 using testing::StrictMock;
77 81
78 namespace syncer { 82 namespace syncer {
79 83
80 using sessions::SyncSessionSnapshot; 84 using sessions::SyncSessionSnapshot;
81 using syncable::IS_DEL; 85 using syncable::IS_DEL;
82 using syncable::IS_UNSYNCED; 86 using syncable::IS_UNSYNCED;
83 using syncable::kEncryptedString; 87 using syncable::kEncryptedString;
84 using syncable::NON_UNIQUE_NAME; 88 using syncable::NON_UNIQUE_NAME;
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 761
758 std::vector<ModelSafeWorker*> workers; 762 std::vector<ModelSafeWorker*> workers;
759 ModelSafeRoutingInfo routing_info; 763 ModelSafeRoutingInfo routing_info;
760 GetModelSafeRoutingInfo(&routing_info); 764 GetModelSafeRoutingInfo(&routing_info);
761 765
762 // Takes ownership of |sync_notifier_mock_|. 766 // Takes ownership of |sync_notifier_mock_|.
763 sync_manager_.Init(temp_dir_.path(), 767 sync_manager_.Init(temp_dir_.path(),
764 WeakHandle<JsEventHandler>(), 768 WeakHandle<JsEventHandler>(),
765 "bogus", 0, false, 769 "bogus", 0, false,
766 base::MessageLoopProxy::current(), 770 base::MessageLoopProxy::current(),
767 new TestHttpPostProviderFactory(), routing_info, workers, 771 scoped_ptr<HttpPostProviderFactory>(
772 new TestHttpPostProviderFactory()),
773 routing_info, workers,
768 &extensions_activity_monitor_, this, 774 &extensions_activity_monitor_, this,
769 credentials, 775 credentials,
770 sync_notifier_mock_, "", 776 scoped_ptr<SyncNotifier>(sync_notifier_mock_),
777 "",
771 syncer::SyncManager::TEST_IN_MEMORY, 778 syncer::SyncManager::TEST_IN_MEMORY,
772 &encryptor_, 779 &encryptor_,
773 &handler_, 780 &handler_,
774 NULL); 781 NULL);
775 782
776 EXPECT_TRUE(sync_notifier_observer_); 783 EXPECT_TRUE(sync_notifier_observer_);
777 EXPECT_TRUE(js_backend_.IsInitialized()); 784 EXPECT_TRUE(js_backend_.IsInitialized());
778 785
779 EXPECT_EQ(0, update_enabled_types_call_count_); 786 EXPECT_EQ(0, update_enabled_types_call_count_);
780 787
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag); 906 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag);
900 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG, 907 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG,
901 hash); 908 hash);
902 EXPECT_TRUE(entry.good()); 909 EXPECT_TRUE(entry.good());
903 if (!entry.Get(IS_UNSYNCED)) 910 if (!entry.Get(IS_UNSYNCED))
904 return false; 911 return false;
905 entry.Put(IS_UNSYNCED, false); 912 entry.Put(IS_UNSYNCED, false);
906 return true; 913 return true;
907 } 914 }
908 915
916 void SetScheduler(scoped_ptr<SyncScheduler> scheduler) {
917 sync_manager_.SetSyncSchedulerForTest(scheduler.Pass());
918 }
919
909 private: 920 private:
910 // Needed by |sync_manager_|. 921 // Needed by |sync_manager_|.
911 MessageLoop message_loop_; 922 MessageLoop message_loop_;
912 // Needed by |sync_manager_|. 923 // Needed by |sync_manager_|.
913 ScopedTempDir temp_dir_; 924 ScopedTempDir temp_dir_;
914 // Sync Id's for the roots of the enabled datatypes. 925 // Sync Id's for the roots of the enabled datatypes.
915 std::map<ModelType, int64> type_roots_; 926 std::map<ModelType, int64> type_roots_;
916 FakeExtensionsActivityMonitor extensions_activity_monitor_; 927 FakeExtensionsActivityMonitor extensions_activity_monitor_;
917 StrictMock<SyncNotifierMock>* sync_notifier_mock_; 928 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
918 929
(...skipping 1559 matching lines...) Expand 10 before | Expand all | Expand 10 after
2478 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2489 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag));
2479 EXPECT_EQ(title, node.GetTitle()); 2490 EXPECT_EQ(title, node.GetTitle());
2480 EXPECT_EQ(GURL(url2), node.GetURL()); 2491 EXPECT_EQ(GURL(url2), node.GetURL());
2481 const syncable::Entry* node_entry = node.GetEntry(); 2492 const syncable::Entry* node_entry = node.GetEntry();
2482 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2493 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2483 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2494 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2484 EXPECT_TRUE(specifics.has_encrypted()); 2495 EXPECT_TRUE(specifics.has_encrypted());
2485 } 2496 }
2486 } 2497 }
2487 2498
2488 } // namespace syncer 2499 class MockSyncScheduler : public SyncScheduler {
2500 public:
2501 MockSyncScheduler() : SyncScheduler("name", NULL, NULL) {}
2502 virtual ~MockSyncScheduler() {}
2503
2504 MOCK_METHOD1(Start, void(SyncScheduler::Mode));
2505 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&));
2506 };
2507
2508 // Test that the configuration params are properly created and sent to
2509 // ScheduleConfigure. No callback should be invoked.
2510 TEST_F(SyncManagerTest, BasicConfiguration) {
2511 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2512 syncer::ModelTypeSet types_to_download(syncer::BOOKMARKS,
2513 syncer::PREFERENCES);
2514 syncer::ModelSafeRoutingInfo new_routing_info;
2515 GetModelSafeRoutingInfo(&new_routing_info);
2516
2517 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler());
2518 ConfigurationParams params;
2519 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE));
2520 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)).
2521 WillOnce(DoAll(SaveArg<0>(&params), Return(true)));
2522 SetScheduler(scheduler.PassAs<SyncScheduler>());
2523
2524 CallbackCounter ready_task_counter, retry_task_counter;
2525 sync_manager_.ConfigureSyncer(
2526 reason,
2527 types_to_download,
2528 new_routing_info,
2529 base::Bind(&CallbackCounter::Callback,
2530 base::Unretained(&ready_task_counter)),
2531 base::Bind(&CallbackCounter::Callback,
2532 base::Unretained(&retry_task_counter)));
2533 EXPECT_EQ(0, ready_task_counter.times_called());
2534 EXPECT_EQ(0, retry_task_counter.times_called());
2535 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2536 params.source);
2537 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2538 EXPECT_EQ(new_routing_info, params.routing_info);
2539 }
2540
2541 // Test that the retry callback is invoked on configuration failure.
2542 TEST_F(SyncManagerTest, ConfigurationRetry) {
2543 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2544 syncer::ModelTypeSet types_to_download(syncer::BOOKMARKS,
2545 syncer::PREFERENCES);
2546 syncer::ModelSafeRoutingInfo new_routing_info;
2547 GetModelSafeRoutingInfo(&new_routing_info);
2548
2549 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler());
2550 ConfigurationParams params;
2551 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE));
2552 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)).
2553 WillOnce(DoAll(SaveArg<0>(&params), Return(false)));
2554 SetScheduler(scheduler.PassAs<SyncScheduler>());
2555
2556 CallbackCounter ready_task_counter, retry_task_counter;
2557 sync_manager_.ConfigureSyncer(
2558 reason,
2559 types_to_download,
2560 new_routing_info,
2561 base::Bind(&CallbackCounter::Callback,
2562 base::Unretained(&ready_task_counter)),
2563 base::Bind(&CallbackCounter::Callback,
2564 base::Unretained(&retry_task_counter)));
2565 EXPECT_EQ(0, ready_task_counter.times_called());
2566 EXPECT_EQ(1, retry_task_counter.times_called());
2567 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2568 params.source);
2569 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2570 EXPECT_EQ(new_routing_info, params.routing_info);
2571 }
2572
2573 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698