| 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" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 class StartupControllerTest : public testing::Test { | 48 class StartupControllerTest : public testing::Test { |
| 49 public: | 49 public: |
| 50 StartupControllerTest() : started_(false) {} | 50 StartupControllerTest() : started_(false) {} |
| 51 | 51 |
| 52 void SetUp() override { | 52 void SetUp() override { |
| 53 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); | 53 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); |
| 54 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_)); | 54 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_)); |
| 55 token_service_.reset(new FakeProfileOAuth2TokenService()); | 55 token_service_.reset(new FakeProfileOAuth2TokenService()); |
| 56 signin_.reset(new FakeSigninManagerWrapper()); | 56 signin_.reset(new FakeSigninManagerWrapper()); |
| 57 | 57 |
| 58 SetUpController(AUTO_START); | 58 SetUpController(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void TearDown() override { | 61 void TearDown() override { |
| 62 controller_.reset(); | 62 controller_.reset(); |
| 63 signin_.reset(); | 63 signin_.reset(); |
| 64 token_service_->Shutdown(); | 64 token_service_->Shutdown(); |
| 65 token_service_.reset(); | 65 token_service_.reset(); |
| 66 sync_prefs_.reset(); | 66 sync_prefs_.reset(); |
| 67 started_ = false; | 67 started_ = false; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void SetUpController(ProfileSyncServiceStartBehavior start_behavior) { | 70 void SetUpController() { |
| 71 started_ = false; | 71 started_ = false; |
| 72 base::Closure fake_start_backend = base::Bind( | 72 base::Closure fake_start_backend = base::Bind( |
| 73 &StartupControllerTest::FakeStartBackend, base::Unretained(this)); | 73 &StartupControllerTest::FakeStartBackend, base::Unretained(this)); |
| 74 controller_.reset(new StartupController(start_behavior, token_service(), | 74 controller_.reset(new StartupController(token_service(), sync_prefs_.get(), |
| 75 sync_prefs_.get(), signin_.get(), | 75 signin_.get(), fake_start_backend)); |
| 76 fake_start_backend)); | |
| 77 controller_->Reset(syncer::UserTypes()); | 76 controller_->Reset(syncer::UserTypes()); |
| 78 controller_->OverrideFallbackTimeoutForTest( | 77 controller_->OverrideFallbackTimeoutForTest( |
| 79 base::TimeDelta::FromSeconds(0)); | 78 base::TimeDelta::FromSeconds(0)); |
| 80 } | 79 } |
| 81 | 80 |
| 82 void FakeStartBackend() { | 81 void FakeStartBackend() { |
| 83 started_ = true; | 82 started_ = true; |
| 83 sync_prefs()->SetFirstSetupComplete(); |
| 84 } |
| 85 |
| 86 void ExpectStarted() { |
| 87 EXPECT_TRUE(started()); |
| 88 EXPECT_EQ(kStateStringStarted, |
| 89 controller()->GetBackendInitializationStateString()); |
| 90 } |
| 91 |
| 92 void ExpectStartDeferred() { |
| 93 const bool deferred_start = |
| 94 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 95 switches::kSyncDisableDeferredStartup); |
| 96 EXPECT_EQ(!deferred_start, started()); |
| 97 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted, |
| 98 controller()->GetBackendInitializationStateString()); |
| 99 } |
| 100 |
| 101 void ExpectNotStarted() { |
| 102 EXPECT_FALSE(started()); |
| 103 EXPECT_EQ(kStateStringNotStarted, |
| 104 controller()->GetBackendInitializationStateString()); |
| 84 } | 105 } |
| 85 | 106 |
| 86 bool started() const { return started_; } | 107 bool started() const { return started_; } |
| 87 void clear_started() { started_ = false; } | 108 void clear_started() { started_ = false; } |
| 88 StartupController* controller() { return controller_.get(); } | 109 StartupController* controller() { return controller_.get(); } |
| 89 FakeSigninManagerWrapper* signin() { return signin_.get(); } | 110 FakeSigninManagerWrapper* signin() { return signin_.get(); } |
| 90 FakeProfileOAuth2TokenService* token_service() { | 111 FakeProfileOAuth2TokenService* token_service() { |
| 91 return token_service_.get(); | 112 return token_service_.get(); |
| 92 } | 113 } |
| 93 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | 114 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } |
| 94 | 115 |
| 95 private: | 116 private: |
| 96 bool started_; | 117 bool started_; |
| 97 base::MessageLoop message_loop_; | 118 base::MessageLoop message_loop_; |
| 98 syncable_prefs::TestingPrefServiceSyncable pref_service_; | 119 syncable_prefs::TestingPrefServiceSyncable pref_service_; |
| 99 scoped_ptr<StartupController> controller_; | 120 scoped_ptr<StartupController> controller_; |
| 100 scoped_ptr<FakeSigninManagerWrapper> signin_; | 121 scoped_ptr<FakeSigninManagerWrapper> signin_; |
| 101 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; | 122 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; |
| 102 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_; | 123 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_; |
| 103 }; | 124 }; |
| 104 | 125 |
| 105 // Test that sync doesn't start until all conditions are met. | 126 // Test that sync doesn't start until all conditions are met. |
| 106 TEST_F(StartupControllerTest, Basic) { | 127 TEST_F(StartupControllerTest, Basic) { |
| 107 controller()->TryStart(); | 128 controller()->TryStart(); |
| 108 EXPECT_FALSE(started()); | 129 ExpectNotStarted(); |
| 130 |
| 109 sync_prefs()->SetFirstSetupComplete(); | 131 sync_prefs()->SetFirstSetupComplete(); |
| 110 controller()->TryStart(); | 132 controller()->TryStart(); |
| 111 EXPECT_FALSE(started()); | 133 ExpectNotStarted(); |
| 134 |
| 112 signin()->set_account_id(kTestUser); | 135 signin()->set_account_id(kTestUser); |
| 113 controller()->TryStart(); | 136 controller()->TryStart(); |
| 114 EXPECT_FALSE(started()); | 137 ExpectNotStarted(); |
| 138 |
| 115 token_service()->UpdateCredentials(kTestUser, kTestToken); | 139 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 116 const bool deferred_start = | |
| 117 !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 118 switches::kSyncDisableDeferredStartup); | |
| 119 controller()->TryStart(); | 140 controller()->TryStart(); |
| 120 EXPECT_EQ(!deferred_start, started()); | 141 ExpectStartDeferred(); |
| 121 std::string state(controller()->GetBackendInitializationStateString()); | |
| 122 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred : | |
| 123 state == kStateStringStarted); | |
| 124 } | 142 } |
| 125 | 143 |
| 126 // Test that sync doesn't start when not requested even if all other | 144 // Test that sync doesn't start when not requested even if all other |
| 127 // conditons are met. | 145 // conditons are met. |
| 128 TEST_F(StartupControllerTest, NotRequested) { | 146 TEST_F(StartupControllerTest, NotRequested) { |
| 129 sync_prefs()->SetFirstSetupComplete(); | 147 sync_prefs()->SetFirstSetupComplete(); |
| 130 sync_prefs()->SetSyncRequested(false); | 148 sync_prefs()->SetSyncRequested(false); |
| 131 signin()->set_account_id(kTestUser); | 149 signin()->set_account_id(kTestUser); |
| 132 token_service()->UpdateCredentials(kTestUser, kTestToken); | 150 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 133 controller()->TryStart(); | 151 controller()->TryStart(); |
| 134 EXPECT_FALSE(started()); | 152 ExpectNotStarted(); |
| 135 EXPECT_EQ(kStateStringNotStarted, | |
| 136 controller()->GetBackendInitializationStateString()); | |
| 137 } | 153 } |
| 138 | 154 |
| 139 // Test that sync doesn't when managed even if all other conditons are met. | 155 // Test that sync doesn't when managed even if all other conditons are met. |
| 140 TEST_F(StartupControllerTest, Managed) { | 156 TEST_F(StartupControllerTest, Managed) { |
| 141 sync_prefs()->SetFirstSetupComplete(); | 157 sync_prefs()->SetFirstSetupComplete(); |
| 142 sync_prefs()->SetManagedForTest(true); | 158 sync_prefs()->SetManagedForTest(true); |
| 143 signin()->set_account_id(kTestUser); | 159 signin()->set_account_id(kTestUser); |
| 144 token_service()->UpdateCredentials(kTestUser, kTestToken); | 160 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 145 controller()->TryStart(); | 161 controller()->TryStart(); |
| 146 EXPECT_FALSE(started()); | 162 ExpectNotStarted(); |
| 147 EXPECT_EQ(kStateStringNotStarted, | |
| 148 controller()->GetBackendInitializationStateString()); | |
| 149 } | 163 } |
| 150 | 164 |
| 151 // Test that sync doesn't start until all conditions are met and a | 165 // Test that sync doesn't start until all conditions are met and a |
| 152 // data type triggers sync startup. | 166 // data type triggers sync startup. |
| 153 TEST_F(StartupControllerTest, DataTypeTriggered) { | 167 TEST_F(StartupControllerTest, DataTypeTriggered) { |
| 154 sync_prefs()->SetFirstSetupComplete(); | 168 sync_prefs()->SetFirstSetupComplete(); |
| 155 signin()->set_account_id(kTestUser); | 169 signin()->set_account_id(kTestUser); |
| 156 token_service()->UpdateCredentials(kTestUser, kTestToken); | 170 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 157 controller()->TryStart(); | 171 controller()->TryStart(); |
| 158 EXPECT_FALSE(started()); | 172 ExpectStartDeferred(); |
| 159 EXPECT_EQ(kStateStringDeferred, | 173 |
| 160 controller()->GetBackendInitializationStateString()); | |
| 161 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 174 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
| 162 EXPECT_TRUE(started()); | 175 ExpectStarted(); |
| 163 EXPECT_EQ(kStateStringStarted, | |
| 164 controller()->GetBackendInitializationStateString()); | |
| 165 | 176 |
| 166 // The fallback timer shouldn't result in another invocation of the closure | 177 // The fallback timer shouldn't result in another invocation of the closure |
| 167 // we passed to the StartupController. | 178 // we passed to the StartupController. |
| 168 clear_started(); | 179 clear_started(); |
| 169 base::RunLoop().RunUntilIdle(); | 180 base::RunLoop().RunUntilIdle(); |
| 170 EXPECT_FALSE(started()); | 181 EXPECT_FALSE(started()); |
| 171 } | 182 } |
| 172 | 183 |
| 173 // Test that the fallback timer starts sync in the event all | 184 // Test that the fallback timer starts sync in the event all |
| 174 // conditions are met and no data type requests sync. | 185 // conditions are met and no data type requests sync. |
| 175 TEST_F(StartupControllerTest, FallbackTimer) { | 186 TEST_F(StartupControllerTest, FallbackTimer) { |
| 176 sync_prefs()->SetFirstSetupComplete(); | 187 sync_prefs()->SetFirstSetupComplete(); |
| 177 signin()->set_account_id(kTestUser); | 188 signin()->set_account_id(kTestUser); |
| 178 token_service()->UpdateCredentials(kTestUser, kTestToken); | 189 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 179 controller()->TryStart(); | 190 controller()->TryStart(); |
| 180 EXPECT_FALSE(started()); | 191 ExpectStartDeferred(); |
| 192 |
| 181 base::RunLoop().RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
| 182 EXPECT_TRUE(started()); | 194 ExpectStarted(); |
| 183 } | 195 } |
| 184 | 196 |
| 185 // Test that we start immediately if sessions is disabled. | 197 // Test that we start immediately if sessions is disabled. |
| 186 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { | 198 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { |
| 187 syncer::ModelTypeSet types(syncer::UserTypes()); | 199 syncer::ModelTypeSet types(syncer::UserTypes()); |
| 188 // Disabling sessions means disabling 4 types due to groupings. | 200 // Disabling sessions means disabling 4 types due to groupings. |
| 189 types.Remove(syncer::SESSIONS); | 201 types.Remove(syncer::SESSIONS); |
| 190 types.Remove(syncer::PROXY_TABS); | 202 types.Remove(syncer::PROXY_TABS); |
| 191 types.Remove(syncer::TYPED_URLS); | 203 types.Remove(syncer::TYPED_URLS); |
| 192 types.Remove(syncer::SUPERVISED_USER_SETTINGS); | 204 types.Remove(syncer::SUPERVISED_USER_SETTINGS); |
| 193 sync_prefs()->SetKeepEverythingSynced(false); | 205 sync_prefs()->SetKeepEverythingSynced(false); |
| 194 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); | 206 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); |
| 195 controller()->Reset(syncer::UserTypes()); | 207 controller()->Reset(syncer::UserTypes()); |
| 196 sync_prefs()->SetFirstSetupComplete(); | 208 sync_prefs()->SetFirstSetupComplete(); |
| 209 |
| 197 signin()->set_account_id(kTestUser); | 210 signin()->set_account_id(kTestUser); |
| 198 token_service()->UpdateCredentials(kTestUser, kTestToken); | 211 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 199 controller()->TryStart(); | 212 controller()->TryStart(); |
| 200 EXPECT_TRUE(started()); | 213 ExpectStarted(); |
| 201 } | 214 } |
| 202 | 215 |
| 203 // Sanity check that the fallback timer doesn't fire before startup | 216 // Sanity check that the fallback timer doesn't fire before startup |
| 204 // conditions are met. | 217 // conditions are met. |
| 205 TEST_F(StartupControllerTest, FallbackTimerWaits) { | 218 TEST_F(StartupControllerTest, FallbackTimerWaits) { |
| 206 controller()->TryStart(); | 219 controller()->TryStart(); |
| 207 EXPECT_FALSE(started()); | 220 ExpectNotStarted(); |
| 208 base::RunLoop().RunUntilIdle(); | 221 base::RunLoop().RunUntilIdle(); |
| 209 EXPECT_FALSE(started()); | 222 ExpectNotStarted(); |
| 210 } | 223 } |
| 211 | 224 |
| 212 // Test that sync starts without the user having to explicitly ask for | 225 // Test that start isn't deferred when setup is in progress. |
| 213 // setup when AUTO_START is the startup behavior requested. | 226 TEST_F(StartupControllerTest, NoDeferralWithSetupInProgress) { |
| 214 TEST_F(StartupControllerTest, FirstSetupWithAutoStart) { | 227 sync_prefs()->SetFirstSetupComplete(); |
| 228 signin()->set_account_id(kTestUser); |
| 229 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 230 controller()->set_setup_in_progress(true); |
| 231 controller()->TryStart(); |
| 232 ExpectStarted(); |
| 233 } |
| 234 |
| 235 // Test that start isn't deferred on the first start but is on restarts. |
| 236 TEST_F(StartupControllerTest, DeferralOnRestart) { |
| 215 signin()->set_account_id(kTestUser); | 237 signin()->set_account_id(kTestUser); |
| 216 token_service()->UpdateCredentials(kTestUser, kTestToken); | 238 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 217 controller()->TryStart(); | 239 controller()->TryStart(); |
| 218 EXPECT_TRUE(started()); | 240 ExpectStarted(); |
| 219 } | |
| 220 | 241 |
| 221 // Test that sync starts only after user explicitly asks for setup when | |
| 222 // MANUAL_START is the startup behavior requested. | |
| 223 TEST_F(StartupControllerTest, FirstSetupWithManualStart) { | |
| 224 signin()->set_account_id(kTestUser); | |
| 225 token_service()->UpdateCredentials(kTestUser, kTestToken); | |
| 226 SetUpController(MANUAL_START); | |
| 227 controller()->TryStart(); | |
| 228 EXPECT_FALSE(started()); | |
| 229 controller()->set_setup_in_progress(true); | |
| 230 controller()->TryStart(); | |
| 231 EXPECT_TRUE(started()); | |
| 232 } | |
| 233 | |
| 234 TEST_F(StartupControllerTest, Reset) { | |
| 235 sync_prefs()->SetFirstSetupComplete(); | |
| 236 signin()->set_account_id(kTestUser); | |
| 237 token_service()->UpdateCredentials(kTestUser, kTestToken); | |
| 238 controller()->TryStart(); | |
| 239 const bool deferred_start = | |
| 240 !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 241 switches::kSyncDisableDeferredStartup); | |
| 242 EXPECT_EQ(!deferred_start, started()); | |
| 243 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | |
| 244 EXPECT_TRUE(started()); | |
| 245 clear_started(); | 242 clear_started(); |
| 246 controller()->Reset(syncer::UserTypes()); | 243 controller()->Reset(syncer::UserTypes()); |
| 247 EXPECT_FALSE(started()); | 244 ExpectNotStarted(); |
| 248 controller()->TryStart(); | 245 controller()->TryStart(); |
| 249 // Restart is not deferred. | 246 ExpectStartDeferred(); |
| 250 EXPECT_TRUE(started()); | |
| 251 } | 247 } |
| 252 | 248 |
| 253 // Test that setup-in-progress tracking is persistent across a Reset. | 249 // Test that setup-in-progress tracking is persistent across a Reset. |
| 254 TEST_F(StartupControllerTest, ResetDuringSetup) { | 250 TEST_F(StartupControllerTest, ResetDuringSetup) { |
| 255 signin()->set_account_id(kTestUser); | 251 signin()->set_account_id(kTestUser); |
| 256 token_service()->UpdateCredentials(kTestUser, kTestToken); | 252 token_service()->UpdateCredentials(kTestUser, kTestToken); |
| 257 | 253 |
| 258 // Simulate UI telling us setup is in progress. | 254 // Simulate UI telling us setup is in progress. |
| 259 controller()->set_setup_in_progress(true); | 255 controller()->set_setup_in_progress(true); |
| 260 | 256 |
| 261 // This could happen if the UI triggers a stop-syncing permanently call. | 257 // This could happen if the UI triggers a stop-syncing permanently call. |
| 262 controller()->Reset(syncer::UserTypes()); | 258 controller()->Reset(syncer::UserTypes()); |
| 263 | 259 |
| 264 // From the UI's point of view, setup is still in progress. | 260 // From the UI's point of view, setup is still in progress. |
| 265 EXPECT_TRUE(controller()->IsSetupInProgress()); | 261 EXPECT_TRUE(controller()->IsSetupInProgress()); |
| 266 } | 262 } |
| 267 | 263 |
| 268 } // namespace browser_sync | 264 } // namespace browser_sync |
| OLD | NEW |