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> |
(...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 ModelSafeRoutingInfo routing_info; | 752 ModelSafeRoutingInfo routing_info; |
753 GetModelSafeRoutingInfo(&routing_info); | 753 GetModelSafeRoutingInfo(&routing_info); |
754 | 754 |
755 // Takes ownership of |sync_notifier_mock_|. | 755 // Takes ownership of |sync_notifier_mock_|. |
756 sync_manager_.Init(temp_dir_.path(), | 756 sync_manager_.Init(temp_dir_.path(), |
757 WeakHandle<JsEventHandler>(), | 757 WeakHandle<JsEventHandler>(), |
758 "bogus", 0, false, | 758 "bogus", 0, false, |
759 base::MessageLoopProxy::current(), | 759 base::MessageLoopProxy::current(), |
760 scoped_ptr<HttpPostProviderFactory>( | 760 scoped_ptr<HttpPostProviderFactory>( |
761 new TestHttpPostProviderFactory()), | 761 new TestHttpPostProviderFactory()), |
762 ModelSafeRoutingInfo(), workers, | 762 routing_info, workers, |
763 &extensions_activity_monitor_, this, | 763 &extensions_activity_monitor_, this, |
764 credentials, | 764 credentials, |
765 scoped_ptr<SyncNotifier>(sync_notifier_mock_), | 765 scoped_ptr<SyncNotifier>(sync_notifier_mock_), |
766 "", | 766 "", |
767 scoped_ptr<InternalComponentsFactory>(GetFactory()), | 767 scoped_ptr<InternalComponentsFactory>(GetFactory()), |
768 &encryptor_, | 768 &encryptor_, |
769 &handler_, | 769 &handler_, |
770 NULL); | 770 NULL); |
771 | 771 |
772 EXPECT_TRUE(js_backend_.IsInitialized()); | 772 EXPECT_TRUE(js_backend_.IsInitialized()); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 | 912 |
913 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { | 913 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { |
914 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 914 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
915 ModelTypePayloadMap model_types_with_payloads = | 915 ModelTypePayloadMap model_types_with_payloads = |
916 ModelTypePayloadMapFromEnumSet(model_types, std::string()); | 916 ModelTypePayloadMapFromEnumSet(model_types, std::string()); |
917 sync_manager_.OnIncomingNotification( | 917 sync_manager_.OnIncomingNotification( |
918 ModelTypePayloadMapToObjectIdPayloadMap(model_types_with_payloads), | 918 ModelTypePayloadMapToObjectIdPayloadMap(model_types_with_payloads), |
919 REMOTE_NOTIFICATION); | 919 REMOTE_NOTIFICATION); |
920 } | 920 } |
921 | 921 |
922 void SetProgressMarkerForType(ModelType type, bool set) { | |
923 if (set) { | |
924 sync_pb::DataTypeProgressMarker marker; | |
925 marker.set_token("token"); | |
926 marker.set_data_type_id(GetSpecificsFieldNumberFromModelType(type)); | |
927 sync_manager_.directory()->SetDownloadProgress(type, marker); | |
928 } else { | |
929 sync_pb::DataTypeProgressMarker marker; | |
930 sync_manager_.directory()->SetDownloadProgress(type, marker); | |
931 } | |
932 } | |
933 | |
934 void SetInitialSyncEndedForType(ModelType type, bool value) { | |
935 sync_manager_.directory()->set_initial_sync_ended_for_type(type, value); | |
936 } | |
937 | |
938 private: | 922 private: |
939 // Needed by |sync_manager_|. | 923 // Needed by |sync_manager_|. |
940 MessageLoop message_loop_; | 924 MessageLoop message_loop_; |
941 // Needed by |sync_manager_|. | 925 // Needed by |sync_manager_|. |
942 ScopedTempDir temp_dir_; | 926 ScopedTempDir temp_dir_; |
943 // Sync Id's for the roots of the enabled datatypes. | 927 // Sync Id's for the roots of the enabled datatypes. |
944 std::map<ModelType, int64> type_roots_; | 928 std::map<ModelType, int64> type_roots_; |
945 FakeExtensionsActivityMonitor extensions_activity_monitor_; | 929 FakeExtensionsActivityMonitor extensions_activity_monitor_; |
946 | 930 |
947 protected: | 931 protected: |
(...skipping 1563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2511 public: | 2495 public: |
2512 MockSyncScheduler() : FakeSyncScheduler() {} | 2496 MockSyncScheduler() : FakeSyncScheduler() {} |
2513 virtual ~MockSyncScheduler() {} | 2497 virtual ~MockSyncScheduler() {} |
2514 | 2498 |
2515 MOCK_METHOD1(Start, void(SyncScheduler::Mode)); | 2499 MOCK_METHOD1(Start, void(SyncScheduler::Mode)); |
2516 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&)); | 2500 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&)); |
2517 }; | 2501 }; |
2518 | 2502 |
2519 class ComponentsFactory : public TestInternalComponentsFactory { | 2503 class ComponentsFactory : public TestInternalComponentsFactory { |
2520 public: | 2504 public: |
2521 ComponentsFactory(SyncScheduler* scheduler_to_use, | 2505 ComponentsFactory(SyncScheduler* scheduler_to_use) |
2522 sessions::SyncSessionContext** session_context) | |
2523 : TestInternalComponentsFactory( | 2506 : TestInternalComponentsFactory( |
2524 TestInternalComponentsFactory::IN_MEMORY), | 2507 TestInternalComponentsFactory::IN_MEMORY), |
2525 scheduler_to_use_(scheduler_to_use), | 2508 scheduler_to_use_(scheduler_to_use) {} |
2526 session_context_(session_context) {} | |
2527 virtual ~ComponentsFactory() {} | 2509 virtual ~ComponentsFactory() {} |
2528 | 2510 |
2529 virtual scoped_ptr<SyncScheduler> BuildScheduler( | 2511 virtual scoped_ptr<SyncScheduler> BuildScheduler( |
2530 const std::string& name, | 2512 const std::string& name, |
2531 sessions::SyncSessionContext* context) OVERRIDE { | 2513 sessions::SyncSessionContext* context) OVERRIDE { |
2532 *session_context_ = context; | |
2533 return scheduler_to_use_.Pass(); | 2514 return scheduler_to_use_.Pass(); |
2534 } | 2515 } |
2535 | |
2536 private: | |
2537 scoped_ptr<SyncScheduler> scheduler_to_use_; | 2516 scoped_ptr<SyncScheduler> scheduler_to_use_; |
2538 sessions::SyncSessionContext** session_context_; | |
2539 }; | 2517 }; |
2540 | 2518 |
2541 class SyncManagerTestWithMockScheduler : public SyncManagerTest { | 2519 class SyncManagerTestWithMockScheduler : public SyncManagerTest { |
2542 public: | 2520 public: |
2543 SyncManagerTestWithMockScheduler() : scheduler_(NULL) {} | 2521 SyncManagerTestWithMockScheduler() : scheduler_(NULL) {} |
2544 virtual InternalComponentsFactory* GetFactory() OVERRIDE { | 2522 virtual InternalComponentsFactory* GetFactory() OVERRIDE { |
2545 scheduler_ = new MockSyncScheduler(); | 2523 scheduler_ = new MockSyncScheduler(); |
2546 return new ComponentsFactory(scheduler_, &session_context_); | 2524 return new ComponentsFactory(scheduler_); |
2547 } | 2525 } |
2548 | |
2549 MockSyncScheduler* scheduler() { return scheduler_; } | |
2550 sessions::SyncSessionContext* session_context() { | |
2551 return session_context_; | |
2552 } | |
2553 | |
2554 private: | |
2555 MockSyncScheduler* scheduler_; | 2526 MockSyncScheduler* scheduler_; |
2556 sessions::SyncSessionContext* session_context_; | |
2557 }; | 2527 }; |
2558 | 2528 |
2559 // Test that the configuration params are properly created and sent to | 2529 // Test that the configuration params are properly created and sent to |
2560 // ScheduleConfigure. No callback should be invoked. Any disabled datatypes | 2530 // ScheduleConfigure. No callback should be invoked. |
2561 // should be purged. | |
2562 TEST_F(SyncManagerTestWithMockScheduler, BasicConfiguration) { | 2531 TEST_F(SyncManagerTestWithMockScheduler, BasicConfiguration) { |
2563 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | 2532 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; |
2564 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | 2533 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); |
2565 ModelSafeRoutingInfo new_routing_info; | 2534 ModelSafeRoutingInfo new_routing_info; |
2566 GetModelSafeRoutingInfo(&new_routing_info); | 2535 GetModelSafeRoutingInfo(&new_routing_info); |
2567 ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info); | |
2568 ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types); | |
2569 | 2536 |
2570 ConfigurationParams params; | 2537 ConfigurationParams params; |
2571 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE)); | 2538 EXPECT_CALL(*scheduler_, Start(SyncScheduler::CONFIGURATION_MODE)); |
2572 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)). | 2539 EXPECT_CALL(*scheduler_, ScheduleConfiguration(_)). |
2573 WillOnce(DoAll(SaveArg<0>(¶ms), Return(true))); | 2540 WillOnce(DoAll(SaveArg<0>(¶ms), Return(true))); |
2574 | 2541 |
2575 // Set data for all types. | |
2576 for (ModelTypeSet::Iterator iter = ModelTypeSet::All().First(); iter.Good(); | |
2577 iter.Inc()) { | |
2578 SetProgressMarkerForType(iter.Get(), true); | |
2579 SetInitialSyncEndedForType(iter.Get(), true); | |
2580 } | |
2581 | |
2582 CallbackCounter ready_task_counter, retry_task_counter; | 2542 CallbackCounter ready_task_counter, retry_task_counter; |
2583 sync_manager_.ConfigureSyncer( | 2543 sync_manager_.ConfigureSyncer( |
2584 reason, | 2544 reason, |
2585 types_to_download, | 2545 types_to_download, |
2586 new_routing_info, | 2546 new_routing_info, |
2587 base::Bind(&CallbackCounter::Callback, | 2547 base::Bind(&CallbackCounter::Callback, |
2588 base::Unretained(&ready_task_counter)), | 2548 base::Unretained(&ready_task_counter)), |
2589 base::Bind(&CallbackCounter::Callback, | 2549 base::Bind(&CallbackCounter::Callback, |
2590 base::Unretained(&retry_task_counter))); | 2550 base::Unretained(&retry_task_counter))); |
2591 EXPECT_EQ(0, ready_task_counter.times_called()); | 2551 EXPECT_EQ(0, ready_task_counter.times_called()); |
2592 EXPECT_EQ(0, retry_task_counter.times_called()); | 2552 EXPECT_EQ(0, retry_task_counter.times_called()); |
2593 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, | 2553 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, |
2594 params.source); | 2554 params.source); |
2595 EXPECT_TRUE(types_to_download.Equals(params.types_to_download)); | 2555 EXPECT_TRUE(types_to_download.Equals(params.types_to_download)); |
2596 EXPECT_EQ(new_routing_info, params.routing_info); | 2556 EXPECT_EQ(new_routing_info, params.routing_info); |
2597 | |
2598 // Verify all the disabled types were purged. | |
2599 EXPECT_TRUE(sync_manager_.InitialSyncEndedTypes().Equals( | |
2600 enabled_types)); | |
2601 EXPECT_TRUE(sync_manager_.GetTypesWithEmptyProgressMarkerToken( | |
2602 ModelTypeSet::All()).Equals(disabled_types)); | |
2603 } | |
2604 | |
2605 // Test that on a reconfiguration (configuration where the session context | |
2606 // already has routing info), only those recently disabled types are purged. | |
2607 TEST_F(SyncManagerTestWithMockScheduler, ReConfiguration) { | |
2608 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | |
2609 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | |
2610 ModelTypeSet disabled_types = ModelTypeSet(THEMES, SESSIONS); | |
2611 ModelSafeRoutingInfo old_routing_info; | |
2612 ModelSafeRoutingInfo new_routing_info; | |
2613 GetModelSafeRoutingInfo(&old_routing_info); | |
2614 new_routing_info = old_routing_info; | |
2615 new_routing_info.erase(THEMES); | |
2616 new_routing_info.erase(SESSIONS); | |
2617 ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info); | |
2618 | |
2619 ConfigurationParams params; | |
2620 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE)); | |
2621 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)). | |
2622 WillOnce(DoAll(SaveArg<0>(¶ms), Return(true))); | |
2623 | |
2624 // Set data for all types except those recently disabled (so we can verify | |
2625 // only those recently disabled are purged) . | |
2626 for (ModelTypeSet::Iterator iter = ModelTypeSet::All().First(); iter.Good(); | |
2627 iter.Inc()) { | |
2628 if (!disabled_types.Has(iter.Get())) { | |
2629 SetProgressMarkerForType(iter.Get(), true); | |
2630 SetInitialSyncEndedForType(iter.Get(), true); | |
2631 } else { | |
2632 SetProgressMarkerForType(iter.Get(), false); | |
2633 SetInitialSyncEndedForType(iter.Get(), false); | |
2634 } | |
2635 } | |
2636 | |
2637 // Set the context to have the old routing info. | |
2638 session_context()->set_routing_info(old_routing_info); | |
2639 | |
2640 CallbackCounter ready_task_counter, retry_task_counter; | |
2641 sync_manager_.ConfigureSyncer( | |
2642 reason, | |
2643 types_to_download, | |
2644 new_routing_info, | |
2645 base::Bind(&CallbackCounter::Callback, | |
2646 base::Unretained(&ready_task_counter)), | |
2647 base::Bind(&CallbackCounter::Callback, | |
2648 base::Unretained(&retry_task_counter))); | |
2649 EXPECT_EQ(0, ready_task_counter.times_called()); | |
2650 EXPECT_EQ(0, retry_task_counter.times_called()); | |
2651 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, | |
2652 params.source); | |
2653 EXPECT_TRUE(types_to_download.Equals(params.types_to_download)); | |
2654 EXPECT_EQ(new_routing_info, params.routing_info); | |
2655 | |
2656 // Verify only the recently disabled types were purged. | |
2657 EXPECT_TRUE(sync_manager_.InitialSyncEndedTypes().Equals( | |
2658 Difference(ModelTypeSet::All(), disabled_types))); | |
2659 EXPECT_TRUE(sync_manager_.GetTypesWithEmptyProgressMarkerToken( | |
2660 ModelTypeSet::All()).Equals(disabled_types)); | |
2661 } | 2557 } |
2662 | 2558 |
2663 // Test that the retry callback is invoked on configuration failure. | 2559 // Test that the retry callback is invoked on configuration failure. |
2664 TEST_F(SyncManagerTestWithMockScheduler, ConfigurationRetry) { | 2560 TEST_F(SyncManagerTestWithMockScheduler, ConfigurationRetry) { |
2665 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | 2561 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; |
2666 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | 2562 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); |
2667 ModelSafeRoutingInfo new_routing_info; | 2563 ModelSafeRoutingInfo new_routing_info; |
2668 GetModelSafeRoutingInfo(&new_routing_info); | 2564 GetModelSafeRoutingInfo(&new_routing_info); |
2669 | 2565 |
2670 ConfigurationParams params; | 2566 ConfigurationParams params; |
2671 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE)); | 2567 EXPECT_CALL(*scheduler_, Start(SyncScheduler::CONFIGURATION_MODE)); |
2672 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)). | 2568 EXPECT_CALL(*scheduler_, ScheduleConfiguration(_)). |
2673 WillOnce(DoAll(SaveArg<0>(¶ms), Return(false))); | 2569 WillOnce(DoAll(SaveArg<0>(¶ms), Return(false))); |
2674 | 2570 |
2675 CallbackCounter ready_task_counter, retry_task_counter; | 2571 CallbackCounter ready_task_counter, retry_task_counter; |
2676 sync_manager_.ConfigureSyncer( | 2572 sync_manager_.ConfigureSyncer( |
2677 reason, | 2573 reason, |
2678 types_to_download, | 2574 types_to_download, |
2679 new_routing_info, | 2575 new_routing_info, |
2680 base::Bind(&CallbackCounter::Callback, | 2576 base::Bind(&CallbackCounter::Callback, |
2681 base::Unretained(&ready_task_counter)), | 2577 base::Unretained(&ready_task_counter)), |
2682 base::Bind(&CallbackCounter::Callback, | 2578 base::Bind(&CallbackCounter::Callback, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2724 | 2620 |
2725 // Ensure only bookmarks and nigori lost their progress marker. Preferences | 2621 // Ensure only bookmarks and nigori lost their progress marker. Preferences |
2726 // should still have it. | 2622 // should still have it. |
2727 partial_types = | 2623 partial_types = |
2728 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); | 2624 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); |
2729 EXPECT_TRUE(partial_types.Has(NIGORI)); | 2625 EXPECT_TRUE(partial_types.Has(NIGORI)); |
2730 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); | 2626 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); |
2731 EXPECT_FALSE(partial_types.Has(PREFERENCES)); | 2627 EXPECT_FALSE(partial_types.Has(PREFERENCES)); |
2732 } | 2628 } |
2733 | 2629 |
2734 // Test CleanipDisabledTypes properly purges all disabled types as specified | |
2735 // by the previous and current enabled params. Enabled partial types should not | |
2736 // be purged. | |
2737 TEST_F(SyncManagerTest, PurgeDisabledTypes) { | |
2738 ModelSafeRoutingInfo routing_info; | |
2739 GetModelSafeRoutingInfo(&routing_info); | |
2740 ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info); | |
2741 ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types); | |
2742 ModelTypeSet partial_enabled_types(PASSWORDS); | |
2743 | |
2744 // Set data for all non-partial types. | |
2745 for (ModelTypeSet::Iterator iter = ModelTypeSet::All().First(); iter.Good(); | |
2746 iter.Inc()) { | |
2747 SetProgressMarkerForType(iter.Get(), true); | |
2748 if (!partial_enabled_types.Has(iter.Get())) | |
2749 SetInitialSyncEndedForType(iter.Get(), true); | |
2750 } | |
2751 | |
2752 // Verify all the enabled types remain after cleanup, and all the disabled | |
2753 // types were purged. | |
2754 sync_manager_.PurgeDisabledTypes(ModelTypeSet::All(), enabled_types); | |
2755 EXPECT_TRUE(enabled_types.Equals( | |
2756 Union(sync_manager_.InitialSyncEndedTypes(), partial_enabled_types))); | |
2757 EXPECT_TRUE(disabled_types.Equals( | |
2758 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()))); | |
2759 | |
2760 // Disable some more types. | |
2761 disabled_types.Put(BOOKMARKS); | |
2762 disabled_types.Put(PREFERENCES); | |
2763 ModelTypeSet new_enabled_types = | |
2764 Difference(ModelTypeSet::All(), disabled_types); | |
2765 | |
2766 // Verify only the non-disabled types remain after cleanup. | |
2767 sync_manager_.PurgeDisabledTypes(enabled_types, new_enabled_types); | |
2768 EXPECT_TRUE(new_enabled_types.Equals( | |
2769 Union(sync_manager_.InitialSyncEndedTypes(), partial_enabled_types))); | |
2770 EXPECT_TRUE(disabled_types.Equals( | |
2771 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()))); | |
2772 } | |
2773 | |
2774 } // namespace | 2630 } // namespace |
OLD | NEW |