| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/sync_driver/startup_controller.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "components/sync_driver/sync_driver_switches.h" | |
| 14 #include "components/sync_driver/sync_prefs.h" | |
| 15 #include "components/syncable_prefs/testing_pref_service_syncable.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace browser_sync { | |
| 19 | |
| 20 // These are coupled to the implementation of StartupController's | |
| 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 | |
| 23 // outputting the correct values for the debug string. | |
| 24 static const char kStateStringStarted[] = "Started"; | |
| 25 static const char kStateStringDeferred[] = "Deferred"; | |
| 26 static const char kStateStringNotStarted[] = "Not started"; | |
| 27 | |
| 28 class StartupControllerTest : public testing::Test { | |
| 29 public: | |
| 30 StartupControllerTest() : can_start_(false), started_(false) {} | |
| 31 | |
| 32 void SetUp() override { | |
| 33 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); | |
| 34 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_)); | |
| 35 controller_.reset(new StartupController( | |
| 36 sync_prefs_.get(), | |
| 37 base::Bind(&StartupControllerTest::CanStart, base::Unretained(this)), | |
| 38 base::Bind(&StartupControllerTest::FakeStartBackend, | |
| 39 base::Unretained(this)))); | |
| 40 controller_->Reset(syncer::UserTypes()); | |
| 41 controller_->OverrideFallbackTimeoutForTest( | |
| 42 base::TimeDelta::FromSeconds(0)); | |
| 43 } | |
| 44 | |
| 45 bool CanStart() { return can_start_; } | |
| 46 | |
| 47 void SetCanStart(bool can_start) { can_start_ = can_start; } | |
| 48 | |
| 49 void FakeStartBackend() { | |
| 50 started_ = true; | |
| 51 sync_prefs()->SetFirstSetupComplete(); | |
| 52 } | |
| 53 | |
| 54 void ExpectStarted() { | |
| 55 EXPECT_TRUE(started()); | |
| 56 EXPECT_EQ(kStateStringStarted, | |
| 57 controller()->GetBackendInitializationStateString()); | |
| 58 } | |
| 59 | |
| 60 void ExpectStartDeferred() { | |
| 61 const bool deferred_start = | |
| 62 !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 63 switches::kSyncDisableDeferredStartup); | |
| 64 EXPECT_EQ(!deferred_start, started()); | |
| 65 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted, | |
| 66 controller()->GetBackendInitializationStateString()); | |
| 67 } | |
| 68 | |
| 69 void ExpectNotStarted() { | |
| 70 EXPECT_FALSE(started()); | |
| 71 EXPECT_EQ(kStateStringNotStarted, | |
| 72 controller()->GetBackendInitializationStateString()); | |
| 73 } | |
| 74 | |
| 75 bool started() const { return started_; } | |
| 76 void clear_started() { started_ = false; } | |
| 77 StartupController* controller() { return controller_.get(); } | |
| 78 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | |
| 79 | |
| 80 private: | |
| 81 bool can_start_; | |
| 82 bool started_; | |
| 83 base::MessageLoop message_loop_; | |
| 84 syncable_prefs::TestingPrefServiceSyncable pref_service_; | |
| 85 std::unique_ptr<sync_driver::SyncPrefs> sync_prefs_; | |
| 86 std::unique_ptr<StartupController> controller_; | |
| 87 }; | |
| 88 | |
| 89 // Test that sync doesn't start if setup is not in progress or complete. | |
| 90 TEST_F(StartupControllerTest, NoSetupComplete) { | |
| 91 controller()->TryStart(); | |
| 92 ExpectNotStarted(); | |
| 93 | |
| 94 SetCanStart(true); | |
| 95 controller()->TryStart(); | |
| 96 ExpectNotStarted(); | |
| 97 } | |
| 98 | |
| 99 // Test that sync defers if first setup is complete. | |
| 100 TEST_F(StartupControllerTest, DefersAfterFirstSetupComplete) { | |
| 101 sync_prefs()->SetFirstSetupComplete(); | |
| 102 SetCanStart(true); | |
| 103 controller()->TryStart(); | |
| 104 ExpectStartDeferred(); | |
| 105 } | |
| 106 | |
| 107 // Test that a data type triggering startup starts sync immediately. | |
| 108 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) { | |
| 109 sync_prefs()->SetFirstSetupComplete(); | |
| 110 SetCanStart(true); | |
| 111 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | |
| 112 ExpectStarted(); | |
| 113 } | |
| 114 | |
| 115 // Test that a data type trigger interrupts the deferral timer and starts | |
| 116 // sync immediately. | |
| 117 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) { | |
| 118 sync_prefs()->SetFirstSetupComplete(); | |
| 119 SetCanStart(true); | |
| 120 controller()->TryStart(); | |
| 121 ExpectStartDeferred(); | |
| 122 | |
| 123 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | |
| 124 ExpectStarted(); | |
| 125 | |
| 126 // The fallback timer shouldn't result in another invocation of the closure | |
| 127 // we passed to the StartupController. | |
| 128 clear_started(); | |
| 129 base::RunLoop().RunUntilIdle(); | |
| 130 EXPECT_FALSE(started()); | |
| 131 } | |
| 132 | |
| 133 // Test that the fallback timer starts sync in the event all | |
| 134 // conditions are met and no data type requests sync. | |
| 135 TEST_F(StartupControllerTest, FallbackTimer) { | |
| 136 sync_prefs()->SetFirstSetupComplete(); | |
| 137 SetCanStart(true); | |
| 138 controller()->TryStart(); | |
| 139 ExpectStartDeferred(); | |
| 140 | |
| 141 base::RunLoop().RunUntilIdle(); | |
| 142 ExpectStarted(); | |
| 143 } | |
| 144 | |
| 145 // Test that we start immediately if sessions is disabled. | |
| 146 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { | |
| 147 syncer::ModelTypeSet types(syncer::UserTypes()); | |
| 148 // Disabling sessions means disabling 4 types due to groupings. | |
| 149 types.Remove(syncer::SESSIONS); | |
| 150 types.Remove(syncer::PROXY_TABS); | |
| 151 types.Remove(syncer::TYPED_URLS); | |
| 152 types.Remove(syncer::SUPERVISED_USER_SETTINGS); | |
| 153 sync_prefs()->SetKeepEverythingSynced(false); | |
| 154 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); | |
| 155 controller()->Reset(syncer::UserTypes()); | |
| 156 | |
| 157 sync_prefs()->SetFirstSetupComplete(); | |
| 158 SetCanStart(true); | |
| 159 controller()->TryStart(); | |
| 160 ExpectStarted(); | |
| 161 } | |
| 162 | |
| 163 // Sanity check that the fallback timer doesn't fire before startup | |
| 164 // conditions are met. | |
| 165 TEST_F(StartupControllerTest, FallbackTimerWaits) { | |
| 166 controller()->TryStart(); | |
| 167 ExpectNotStarted(); | |
| 168 base::RunLoop().RunUntilIdle(); | |
| 169 ExpectNotStarted(); | |
| 170 } | |
| 171 | |
| 172 // Test that sync starts immediately when setup in progress is true. | |
| 173 TEST_F(StartupControllerTest, NoDeferralSetupInProgressTrigger) { | |
| 174 sync_prefs()->SetFirstSetupComplete(); | |
| 175 SetCanStart(true); | |
| 176 controller()->SetSetupInProgress(true); | |
| 177 ExpectStarted(); | |
| 178 } | |
| 179 | |
| 180 // Test that setup in progress being set to true interrupts the deferral timer | |
| 181 // and starts sync immediately. | |
| 182 TEST_F(StartupControllerTest, SetupInProgressTriggerInterruptsDeferral) { | |
| 183 sync_prefs()->SetFirstSetupComplete(); | |
| 184 SetCanStart(true); | |
| 185 controller()->TryStart(); | |
| 186 ExpectStartDeferred(); | |
| 187 | |
| 188 controller()->SetSetupInProgress(true); | |
| 189 ExpectStarted(); | |
| 190 } | |
| 191 | |
| 192 // Test that immediate startup can be forced. | |
| 193 TEST_F(StartupControllerTest, ForceImmediateStartup) { | |
| 194 SetCanStart(true); | |
| 195 controller()->TryStartImmediately(); | |
| 196 ExpectStarted(); | |
| 197 } | |
| 198 | |
| 199 // Test that setup-in-progress tracking is persistent across a Reset. | |
| 200 TEST_F(StartupControllerTest, ResetDuringSetup) { | |
| 201 SetCanStart(true); | |
| 202 | |
| 203 // Simulate UI telling us setup is in progress. | |
| 204 controller()->SetSetupInProgress(true); | |
| 205 | |
| 206 // This could happen if the UI triggers a stop-syncing permanently call. | |
| 207 controller()->Reset(syncer::UserTypes()); | |
| 208 | |
| 209 // From the UI's point of view, setup is still in progress. | |
| 210 EXPECT_TRUE(controller()->IsSetupInProgress()); | |
| 211 } | |
| 212 | |
| 213 } // namespace browser_sync | |
| OLD | NEW |