| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/sync/driver/startup_controller.h" | 5 #include "components/sync/driver/startup_controller.h" | 
| 6 | 6 | 
| 7 #include <string> | 7 #include <string> | 
| 8 | 8 | 
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" | 
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" | 
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" | 
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" | 
| 13 #include "components/sync/driver/sync_driver_switches.h" | 13 #include "components/sync/driver/sync_driver_switches.h" | 
| 14 #include "components/sync/driver/sync_prefs.h" | 14 #include "components/sync/driver/sync_prefs.h" | 
| 15 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 15 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" | 
| 17 | 17 | 
| 18 namespace browser_sync { | 18 namespace syncer { | 
| 19 | 19 | 
| 20 // These are coupled to the implementation of StartupController's | 20 // These are coupled to the implementation of StartupController's | 
| 21 // GetBackendInitializationStateString which is used by about:sync. We use it | 21 // GetBackendInitializationStateString which is used by about:sync. We use it | 
| 22 // as a convenient way to verify internal state and that the class is | 22 // as a convenient way to verify internal state and that the class is | 
| 23 // outputting the correct values for the debug string. | 23 // outputting the correct values for the debug string. | 
| 24 static const char kStateStringStarted[] = "Started"; | 24 static const char kStateStringStarted[] = "Started"; | 
| 25 static const char kStateStringDeferred[] = "Deferred"; | 25 static const char kStateStringDeferred[] = "Deferred"; | 
| 26 static const char kStateStringNotStarted[] = "Not started"; | 26 static const char kStateStringNotStarted[] = "Not started"; | 
| 27 | 27 | 
| 28 class StartupControllerTest : public testing::Test { | 28 class StartupControllerTest : public testing::Test { | 
| 29  public: | 29  public: | 
| 30   StartupControllerTest() : can_start_(false), started_(false) {} | 30   StartupControllerTest() : can_start_(false), started_(false) {} | 
| 31 | 31 | 
| 32   void SetUp() override { | 32   void SetUp() override { | 
| 33     sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); | 33     SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); | 
| 34     sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_)); | 34     sync_prefs_.reset(new SyncPrefs(&pref_service_)); | 
| 35     controller_.reset(new StartupController( | 35     controller_.reset(new StartupController( | 
| 36         sync_prefs_.get(), | 36         sync_prefs_.get(), | 
| 37         base::Bind(&StartupControllerTest::CanStart, base::Unretained(this)), | 37         base::Bind(&StartupControllerTest::CanStart, base::Unretained(this)), | 
| 38         base::Bind(&StartupControllerTest::FakeStartBackend, | 38         base::Bind(&StartupControllerTest::FakeStartBackend, | 
| 39                    base::Unretained(this)))); | 39                    base::Unretained(this)))); | 
| 40     controller_->Reset(syncer::UserTypes()); | 40     controller_->Reset(UserTypes()); | 
| 41     controller_->OverrideFallbackTimeoutForTest( | 41     controller_->OverrideFallbackTimeoutForTest( | 
| 42         base::TimeDelta::FromSeconds(0)); | 42         base::TimeDelta::FromSeconds(0)); | 
| 43   } | 43   } | 
| 44 | 44 | 
| 45   bool CanStart() { return can_start_; } | 45   bool CanStart() { return can_start_; } | 
| 46 | 46 | 
| 47   void SetCanStart(bool can_start) { can_start_ = can_start; } | 47   void SetCanStart(bool can_start) { can_start_ = can_start; } | 
| 48 | 48 | 
| 49   void FakeStartBackend() { | 49   void FakeStartBackend() { | 
| 50     started_ = true; | 50     started_ = true; | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 68 | 68 | 
| 69   void ExpectNotStarted() { | 69   void ExpectNotStarted() { | 
| 70     EXPECT_FALSE(started()); | 70     EXPECT_FALSE(started()); | 
| 71     EXPECT_EQ(kStateStringNotStarted, | 71     EXPECT_EQ(kStateStringNotStarted, | 
| 72               controller()->GetBackendInitializationStateString()); | 72               controller()->GetBackendInitializationStateString()); | 
| 73   } | 73   } | 
| 74 | 74 | 
| 75   bool started() const { return started_; } | 75   bool started() const { return started_; } | 
| 76   void clear_started() { started_ = false; } | 76   void clear_started() { started_ = false; } | 
| 77   StartupController* controller() { return controller_.get(); } | 77   StartupController* controller() { return controller_.get(); } | 
| 78   sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | 78   SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | 
| 79 | 79 | 
| 80  private: | 80  private: | 
| 81   bool can_start_; | 81   bool can_start_; | 
| 82   bool started_; | 82   bool started_; | 
| 83   base::MessageLoop message_loop_; | 83   base::MessageLoop message_loop_; | 
| 84   syncable_prefs::TestingPrefServiceSyncable pref_service_; | 84   syncable_prefs::TestingPrefServiceSyncable pref_service_; | 
| 85   std::unique_ptr<sync_driver::SyncPrefs> sync_prefs_; | 85   std::unique_ptr<SyncPrefs> sync_prefs_; | 
| 86   std::unique_ptr<StartupController> controller_; | 86   std::unique_ptr<StartupController> controller_; | 
| 87 }; | 87 }; | 
| 88 | 88 | 
| 89 // Test that sync doesn't start if setup is not in progress or complete. | 89 // Test that sync doesn't start if setup is not in progress or complete. | 
| 90 TEST_F(StartupControllerTest, NoSetupComplete) { | 90 TEST_F(StartupControllerTest, NoSetupComplete) { | 
| 91   controller()->TryStart(); | 91   controller()->TryStart(); | 
| 92   ExpectNotStarted(); | 92   ExpectNotStarted(); | 
| 93 | 93 | 
| 94   SetCanStart(true); | 94   SetCanStart(true); | 
| 95   controller()->TryStart(); | 95   controller()->TryStart(); | 
| 96   ExpectNotStarted(); | 96   ExpectNotStarted(); | 
| 97 } | 97 } | 
| 98 | 98 | 
| 99 // Test that sync defers if first setup is complete. | 99 // Test that sync defers if first setup is complete. | 
| 100 TEST_F(StartupControllerTest, DefersAfterFirstSetupComplete) { | 100 TEST_F(StartupControllerTest, DefersAfterFirstSetupComplete) { | 
| 101   sync_prefs()->SetFirstSetupComplete(); | 101   sync_prefs()->SetFirstSetupComplete(); | 
| 102   SetCanStart(true); | 102   SetCanStart(true); | 
| 103   controller()->TryStart(); | 103   controller()->TryStart(); | 
| 104   ExpectStartDeferred(); | 104   ExpectStartDeferred(); | 
| 105 } | 105 } | 
| 106 | 106 | 
| 107 // Test that a data type triggering startup starts sync immediately. | 107 // Test that a data type triggering startup starts sync immediately. | 
| 108 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) { | 108 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) { | 
| 109   sync_prefs()->SetFirstSetupComplete(); | 109   sync_prefs()->SetFirstSetupComplete(); | 
| 110   SetCanStart(true); | 110   SetCanStart(true); | 
| 111   controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 111   controller()->OnDataTypeRequestsSyncStartup(SESSIONS); | 
| 112   ExpectStarted(); | 112   ExpectStarted(); | 
| 113 } | 113 } | 
| 114 | 114 | 
| 115 // Test that a data type trigger interrupts the deferral timer and starts | 115 // Test that a data type trigger interrupts the deferral timer and starts | 
| 116 // sync immediately. | 116 // sync immediately. | 
| 117 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) { | 117 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) { | 
| 118   sync_prefs()->SetFirstSetupComplete(); | 118   sync_prefs()->SetFirstSetupComplete(); | 
| 119   SetCanStart(true); | 119   SetCanStart(true); | 
| 120   controller()->TryStart(); | 120   controller()->TryStart(); | 
| 121   ExpectStartDeferred(); | 121   ExpectStartDeferred(); | 
| 122 | 122 | 
| 123   controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 123   controller()->OnDataTypeRequestsSyncStartup(SESSIONS); | 
| 124   ExpectStarted(); | 124   ExpectStarted(); | 
| 125 | 125 | 
| 126   // The fallback timer shouldn't result in another invocation of the closure | 126   // The fallback timer shouldn't result in another invocation of the closure | 
| 127   // we passed to the StartupController. | 127   // we passed to the StartupController. | 
| 128   clear_started(); | 128   clear_started(); | 
| 129   base::RunLoop().RunUntilIdle(); | 129   base::RunLoop().RunUntilIdle(); | 
| 130   EXPECT_FALSE(started()); | 130   EXPECT_FALSE(started()); | 
| 131 } | 131 } | 
| 132 | 132 | 
| 133 // Test that the fallback timer starts sync in the event all | 133 // Test that the fallback timer starts sync in the event all | 
| 134 // conditions are met and no data type requests sync. | 134 // conditions are met and no data type requests sync. | 
| 135 TEST_F(StartupControllerTest, FallbackTimer) { | 135 TEST_F(StartupControllerTest, FallbackTimer) { | 
| 136   sync_prefs()->SetFirstSetupComplete(); | 136   sync_prefs()->SetFirstSetupComplete(); | 
| 137   SetCanStart(true); | 137   SetCanStart(true); | 
| 138   controller()->TryStart(); | 138   controller()->TryStart(); | 
| 139   ExpectStartDeferred(); | 139   ExpectStartDeferred(); | 
| 140 | 140 | 
| 141   base::RunLoop().RunUntilIdle(); | 141   base::RunLoop().RunUntilIdle(); | 
| 142   ExpectStarted(); | 142   ExpectStarted(); | 
| 143 } | 143 } | 
| 144 | 144 | 
| 145 // Test that we start immediately if sessions is disabled. | 145 // Test that we start immediately if sessions is disabled. | 
| 146 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { | 146 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { | 
| 147   syncer::ModelTypeSet types(syncer::UserTypes()); | 147   ModelTypeSet types(UserTypes()); | 
| 148   // Disabling sessions means disabling 4 types due to groupings. | 148   // Disabling sessions means disabling 4 types due to groupings. | 
| 149   types.Remove(syncer::SESSIONS); | 149   types.Remove(SESSIONS); | 
| 150   types.Remove(syncer::PROXY_TABS); | 150   types.Remove(PROXY_TABS); | 
| 151   types.Remove(syncer::TYPED_URLS); | 151   types.Remove(TYPED_URLS); | 
| 152   types.Remove(syncer::SUPERVISED_USER_SETTINGS); | 152   types.Remove(SUPERVISED_USER_SETTINGS); | 
| 153   sync_prefs()->SetKeepEverythingSynced(false); | 153   sync_prefs()->SetKeepEverythingSynced(false); | 
| 154   sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); | 154   sync_prefs()->SetPreferredDataTypes(UserTypes(), types); | 
| 155   controller()->Reset(syncer::UserTypes()); | 155   controller()->Reset(UserTypes()); | 
| 156 | 156 | 
| 157   sync_prefs()->SetFirstSetupComplete(); | 157   sync_prefs()->SetFirstSetupComplete(); | 
| 158   SetCanStart(true); | 158   SetCanStart(true); | 
| 159   controller()->TryStart(); | 159   controller()->TryStart(); | 
| 160   ExpectStarted(); | 160   ExpectStarted(); | 
| 161 } | 161 } | 
| 162 | 162 | 
| 163 // Sanity check that the fallback timer doesn't fire before startup | 163 // Sanity check that the fallback timer doesn't fire before startup | 
| 164 // conditions are met. | 164 // conditions are met. | 
| 165 TEST_F(StartupControllerTest, FallbackTimerWaits) { | 165 TEST_F(StartupControllerTest, FallbackTimerWaits) { | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 197 } | 197 } | 
| 198 | 198 | 
| 199 // Test that setup-in-progress tracking is persistent across a Reset. | 199 // Test that setup-in-progress tracking is persistent across a Reset. | 
| 200 TEST_F(StartupControllerTest, ResetDuringSetup) { | 200 TEST_F(StartupControllerTest, ResetDuringSetup) { | 
| 201   SetCanStart(true); | 201   SetCanStart(true); | 
| 202 | 202 | 
| 203   // Simulate UI telling us setup is in progress. | 203   // Simulate UI telling us setup is in progress. | 
| 204   controller()->SetSetupInProgress(true); | 204   controller()->SetSetupInProgress(true); | 
| 205 | 205 | 
| 206   // This could happen if the UI triggers a stop-syncing permanently call. | 206   // This could happen if the UI triggers a stop-syncing permanently call. | 
| 207   controller()->Reset(syncer::UserTypes()); | 207   controller()->Reset(UserTypes()); | 
| 208 | 208 | 
| 209   // From the UI's point of view, setup is still in progress. | 209   // From the UI's point of view, setup is still in progress. | 
| 210   EXPECT_TRUE(controller()->IsSetupInProgress()); | 210   EXPECT_TRUE(controller()->IsSetupInProgress()); | 
| 211 } | 211 } | 
| 212 | 212 | 
| 213 }  // namespace browser_sync | 213 }  // namespace syncer | 
| OLD | NEW | 
|---|