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 901 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 |
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 | 946 |
931 protected: | 947 protected: |
(...skipping 1563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2495 public: | 2511 public: |
2496 MockSyncScheduler() : FakeSyncScheduler() {} | 2512 MockSyncScheduler() : FakeSyncScheduler() {} |
2497 virtual ~MockSyncScheduler() {} | 2513 virtual ~MockSyncScheduler() {} |
2498 | 2514 |
2499 MOCK_METHOD1(Start, void(SyncScheduler::Mode)); | 2515 MOCK_METHOD1(Start, void(SyncScheduler::Mode)); |
2500 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&)); | 2516 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&)); |
2501 }; | 2517 }; |
2502 | 2518 |
2503 class ComponentsFactory : public TestInternalComponentsFactory { | 2519 class ComponentsFactory : public TestInternalComponentsFactory { |
2504 public: | 2520 public: |
2505 ComponentsFactory(SyncScheduler* scheduler_to_use) | 2521 ComponentsFactory(SyncScheduler* scheduler_to_use, |
| 2522 sessions::SyncSessionContext** session_context) |
2506 : TestInternalComponentsFactory( | 2523 : TestInternalComponentsFactory( |
2507 TestInternalComponentsFactory::IN_MEMORY), | 2524 TestInternalComponentsFactory::IN_MEMORY), |
2508 scheduler_to_use_(scheduler_to_use) {} | 2525 scheduler_to_use_(scheduler_to_use), |
| 2526 session_context_(session_context) {} |
2509 virtual ~ComponentsFactory() {} | 2527 virtual ~ComponentsFactory() {} |
2510 | 2528 |
2511 virtual scoped_ptr<SyncScheduler> BuildScheduler( | 2529 virtual scoped_ptr<SyncScheduler> BuildScheduler( |
2512 const std::string& name, | 2530 const std::string& name, |
2513 sessions::SyncSessionContext* context) OVERRIDE { | 2531 sessions::SyncSessionContext* context) OVERRIDE { |
| 2532 *session_context_ = context; |
2514 return scheduler_to_use_.Pass(); | 2533 return scheduler_to_use_.Pass(); |
2515 } | 2534 } |
| 2535 |
| 2536 private: |
2516 scoped_ptr<SyncScheduler> scheduler_to_use_; | 2537 scoped_ptr<SyncScheduler> scheduler_to_use_; |
| 2538 sessions::SyncSessionContext** session_context_; |
2517 }; | 2539 }; |
2518 | 2540 |
2519 class SyncManagerTestWithMockScheduler : public SyncManagerTest { | 2541 class SyncManagerTestWithMockScheduler : public SyncManagerTest { |
2520 public: | 2542 public: |
2521 SyncManagerTestWithMockScheduler() : scheduler_(NULL) {} | 2543 SyncManagerTestWithMockScheduler() : scheduler_(NULL) {} |
2522 virtual InternalComponentsFactory* GetFactory() OVERRIDE { | 2544 virtual InternalComponentsFactory* GetFactory() OVERRIDE { |
2523 scheduler_ = new MockSyncScheduler(); | 2545 scheduler_ = new MockSyncScheduler(); |
2524 return new ComponentsFactory(scheduler_); | 2546 return new ComponentsFactory(scheduler_, &session_context_); |
2525 } | 2547 } |
| 2548 |
| 2549 MockSyncScheduler* scheduler() { return scheduler_; } |
| 2550 sessions::SyncSessionContext* session_context() { |
| 2551 return session_context_; |
| 2552 } |
| 2553 |
| 2554 private: |
2526 MockSyncScheduler* scheduler_; | 2555 MockSyncScheduler* scheduler_; |
| 2556 sessions::SyncSessionContext* session_context_; |
2527 }; | 2557 }; |
2528 | 2558 |
2529 // Test that the configuration params are properly created and sent to | 2559 // Test that the configuration params are properly created and sent to |
2530 // ScheduleConfigure. No callback should be invoked. | 2560 // ScheduleConfigure. No callback should be invoked. Any disabled datatypes |
| 2561 // should be purged. |
2531 TEST_F(SyncManagerTestWithMockScheduler, BasicConfiguration) { | 2562 TEST_F(SyncManagerTestWithMockScheduler, BasicConfiguration) { |
2532 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | 2563 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; |
2533 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | 2564 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); |
2534 ModelSafeRoutingInfo new_routing_info; | 2565 ModelSafeRoutingInfo new_routing_info; |
2535 GetModelSafeRoutingInfo(&new_routing_info); | 2566 GetModelSafeRoutingInfo(&new_routing_info); |
| 2567 ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info); |
| 2568 ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types); |
2536 | 2569 |
2537 ConfigurationParams params; | 2570 ConfigurationParams params; |
2538 EXPECT_CALL(*scheduler_, Start(SyncScheduler::CONFIGURATION_MODE)); | 2571 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE)); |
2539 EXPECT_CALL(*scheduler_, ScheduleConfiguration(_)). | 2572 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)). |
2540 WillOnce(DoAll(SaveArg<0>(¶ms), Return(true))); | 2573 WillOnce(DoAll(SaveArg<0>(¶ms), Return(true))); |
2541 | 2574 |
| 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 |
2542 CallbackCounter ready_task_counter, retry_task_counter; | 2582 CallbackCounter ready_task_counter, retry_task_counter; |
2543 sync_manager_.ConfigureSyncer( | 2583 sync_manager_.ConfigureSyncer( |
2544 reason, | 2584 reason, |
2545 types_to_download, | 2585 types_to_download, |
2546 new_routing_info, | 2586 new_routing_info, |
2547 base::Bind(&CallbackCounter::Callback, | 2587 base::Bind(&CallbackCounter::Callback, |
2548 base::Unretained(&ready_task_counter)), | 2588 base::Unretained(&ready_task_counter)), |
2549 base::Bind(&CallbackCounter::Callback, | 2589 base::Bind(&CallbackCounter::Callback, |
2550 base::Unretained(&retry_task_counter))); | 2590 base::Unretained(&retry_task_counter))); |
2551 EXPECT_EQ(0, ready_task_counter.times_called()); | 2591 EXPECT_EQ(0, ready_task_counter.times_called()); |
2552 EXPECT_EQ(0, retry_task_counter.times_called()); | 2592 EXPECT_EQ(0, retry_task_counter.times_called()); |
2553 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, | 2593 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, |
2554 params.source); | 2594 params.source); |
2555 EXPECT_TRUE(types_to_download.Equals(params.types_to_download)); | 2595 EXPECT_TRUE(types_to_download.Equals(params.types_to_download)); |
2556 EXPECT_EQ(new_routing_info, params.routing_info); | 2596 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)); |
2557 } | 2661 } |
2558 | 2662 |
2559 // Test that the retry callback is invoked on configuration failure. | 2663 // Test that the retry callback is invoked on configuration failure. |
2560 TEST_F(SyncManagerTestWithMockScheduler, ConfigurationRetry) { | 2664 TEST_F(SyncManagerTestWithMockScheduler, ConfigurationRetry) { |
2561 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; | 2665 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; |
2562 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); | 2666 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES); |
2563 ModelSafeRoutingInfo new_routing_info; | 2667 ModelSafeRoutingInfo new_routing_info; |
2564 GetModelSafeRoutingInfo(&new_routing_info); | 2668 GetModelSafeRoutingInfo(&new_routing_info); |
2565 | 2669 |
2566 ConfigurationParams params; | 2670 ConfigurationParams params; |
2567 EXPECT_CALL(*scheduler_, Start(SyncScheduler::CONFIGURATION_MODE)); | 2671 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE)); |
2568 EXPECT_CALL(*scheduler_, ScheduleConfiguration(_)). | 2672 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)). |
2569 WillOnce(DoAll(SaveArg<0>(¶ms), Return(false))); | 2673 WillOnce(DoAll(SaveArg<0>(¶ms), Return(false))); |
2570 | 2674 |
2571 CallbackCounter ready_task_counter, retry_task_counter; | 2675 CallbackCounter ready_task_counter, retry_task_counter; |
2572 sync_manager_.ConfigureSyncer( | 2676 sync_manager_.ConfigureSyncer( |
2573 reason, | 2677 reason, |
2574 types_to_download, | 2678 types_to_download, |
2575 new_routing_info, | 2679 new_routing_info, |
2576 base::Bind(&CallbackCounter::Callback, | 2680 base::Bind(&CallbackCounter::Callback, |
2577 base::Unretained(&ready_task_counter)), | 2681 base::Unretained(&ready_task_counter)), |
2578 base::Bind(&CallbackCounter::Callback, | 2682 base::Bind(&CallbackCounter::Callback, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2620 | 2724 |
2621 // Ensure only bookmarks and nigori lost their progress marker. Preferences | 2725 // Ensure only bookmarks and nigori lost their progress marker. Preferences |
2622 // should still have it. | 2726 // should still have it. |
2623 partial_types = | 2727 partial_types = |
2624 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); | 2728 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); |
2625 EXPECT_TRUE(partial_types.Has(NIGORI)); | 2729 EXPECT_TRUE(partial_types.Has(NIGORI)); |
2626 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); | 2730 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); |
2627 EXPECT_FALSE(partial_types.Has(PREFERENCES)); | 2731 EXPECT_FALSE(partial_types.Has(PREFERENCES)); |
2628 } | 2732 } |
2629 | 2733 |
| 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 |
2630 } // namespace | 2774 } // namespace |
OLD | NEW |