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