| 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 "chrome/browser/sync/startup_controller.h" | 5 #include "chrome/browser/sync/startup_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 signin_.reset(); | 79 signin_.reset(); |
| 80 token_service_.reset(); | 80 token_service_.reset(); |
| 81 sync_prefs_.reset(); | 81 sync_prefs_.reset(); |
| 82 started_ = false; | 82 started_ = false; |
| 83 } | 83 } |
| 84 | 84 |
| 85 void FakeStartBackend() { | 85 void FakeStartBackend() { |
| 86 started_ = true; | 86 started_ = true; |
| 87 } | 87 } |
| 88 | 88 |
| 89 void ForceDeferredStartup() { |
| 90 if (!CommandLine::ForCurrentProcess()-> |
| 91 HasSwitch(switches::kSyncEnableDeferredStartup)) { |
| 92 CommandLine::ForCurrentProcess()-> |
| 93 AppendSwitch(switches::kSyncEnableDeferredStartup); |
| 94 controller_->Reset(syncer::UserTypes()); |
| 95 } |
| 96 } |
| 97 |
| 89 bool started() const { return started_; } | 98 bool started() const { return started_; } |
| 90 void clear_started() { started_ = false; } | 99 void clear_started() { started_ = false; } |
| 91 StartupController* controller() { return controller_.get(); } | 100 StartupController* controller() { return controller_.get(); } |
| 92 FakeManagedUserSigninManagerWrapper* signin() { return signin_.get(); } | 101 FakeManagedUserSigninManagerWrapper* signin() { return signin_.get(); } |
| 93 FakeProfileOAuth2TokenService* token_service() { | 102 FakeProfileOAuth2TokenService* token_service() { |
| 94 return static_cast<FakeProfileOAuth2TokenService*>( | 103 return static_cast<FakeProfileOAuth2TokenService*>( |
| 95 token_service_->GetProfileOAuth2TokenService()); | 104 token_service_->GetProfileOAuth2TokenService()); |
| 96 } | 105 } |
| 97 SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | 106 SyncPrefs* sync_prefs() { return sync_prefs_.get(); } |
| 98 Profile* profile() { return profile_.get(); } | 107 Profile* profile() { return profile_.get(); } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 111 TEST_F(StartupControllerTest, Basic) { | 120 TEST_F(StartupControllerTest, Basic) { |
| 112 controller()->TryStart(); | 121 controller()->TryStart(); |
| 113 EXPECT_FALSE(started()); | 122 EXPECT_FALSE(started()); |
| 114 sync_prefs()->SetSyncSetupCompleted(); | 123 sync_prefs()->SetSyncSetupCompleted(); |
| 115 controller()->TryStart(); | 124 controller()->TryStart(); |
| 116 EXPECT_FALSE(started()); | 125 EXPECT_FALSE(started()); |
| 117 signin()->set_account(kTestUser); | 126 signin()->set_account(kTestUser); |
| 118 controller()->TryStart(); | 127 controller()->TryStart(); |
| 119 EXPECT_FALSE(started()); | 128 EXPECT_FALSE(started()); |
| 120 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 129 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 121 const bool deferred_start = !CommandLine::ForCurrentProcess()-> | 130 const bool deferred_start = CommandLine::ForCurrentProcess()-> |
| 122 HasSwitch(switches::kSyncDisableDeferredStartup); | 131 HasSwitch(switches::kSyncEnableDeferredStartup); |
| 123 controller()->TryStart(); | 132 controller()->TryStart(); |
| 124 EXPECT_EQ(!deferred_start, started()); | 133 EXPECT_EQ(!deferred_start, started()); |
| 125 std::string state(controller()->GetBackendInitializationStateString()); | 134 std::string state(controller()->GetBackendInitializationStateString()); |
| 126 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred : | 135 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred : |
| 127 state == kStateStringStarted); | 136 state == kStateStringStarted); |
| 128 } | 137 } |
| 129 | 138 |
| 130 // Test that sync doesn't when suppressed even if all other conditons are met. | 139 // Test that sync doesn't when suppressed even if all other conditons are met. |
| 131 TEST_F(StartupControllerTest, Suppressed) { | 140 TEST_F(StartupControllerTest, Suppressed) { |
| 132 sync_prefs()->SetSyncSetupCompleted(); | 141 sync_prefs()->SetSyncSetupCompleted(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 156 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 148 controller()->TryStart(); | 157 controller()->TryStart(); |
| 149 EXPECT_FALSE(started()); | 158 EXPECT_FALSE(started()); |
| 150 EXPECT_EQ(kStateStringNotStarted, | 159 EXPECT_EQ(kStateStringNotStarted, |
| 151 controller()->GetBackendInitializationStateString()); | 160 controller()->GetBackendInitializationStateString()); |
| 152 } | 161 } |
| 153 | 162 |
| 154 // Test that sync doesn't start until all conditions are met and a | 163 // Test that sync doesn't start until all conditions are met and a |
| 155 // data type triggers sync startup. | 164 // data type triggers sync startup. |
| 156 TEST_F(StartupControllerTest, DataTypeTriggered) { | 165 TEST_F(StartupControllerTest, DataTypeTriggered) { |
| 166 ForceDeferredStartup(); |
| 157 sync_prefs()->SetSyncSetupCompleted(); | 167 sync_prefs()->SetSyncSetupCompleted(); |
| 158 signin()->set_account(kTestUser); | 168 signin()->set_account(kTestUser); |
| 159 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 169 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 160 controller()->TryStart(); | 170 controller()->TryStart(); |
| 161 EXPECT_FALSE(started()); | 171 EXPECT_FALSE(started()); |
| 162 EXPECT_EQ(kStateStringDeferred, | 172 EXPECT_EQ(kStateStringDeferred, |
| 163 controller()->GetBackendInitializationStateString()); | 173 controller()->GetBackendInitializationStateString()); |
| 164 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 174 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
| 165 EXPECT_TRUE(started()); | 175 EXPECT_TRUE(started()); |
| 166 EXPECT_EQ(kStateStringStarted, | 176 EXPECT_EQ(kStateStringStarted, |
| 167 controller()->GetBackendInitializationStateString()); | 177 controller()->GetBackendInitializationStateString()); |
| 168 | 178 |
| 169 // The fallback timer shouldn't result in another invocation of the closure | 179 // The fallback timer shouldn't result in another invocation of the closure |
| 170 // we passed to the StartupController. | 180 // we passed to the StartupController. |
| 171 clear_started(); | 181 clear_started(); |
| 172 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
| 173 EXPECT_FALSE(started()); | 183 EXPECT_FALSE(started()); |
| 174 } | 184 } |
| 175 | 185 |
| 176 // Test that the fallback timer starts sync in the event all | 186 // Test that the fallback timer starts sync in the event all |
| 177 // conditions are met and no data type requests sync. | 187 // conditions are met and no data type requests sync. |
| 178 TEST_F(StartupControllerTest, FallbackTimer) { | 188 TEST_F(StartupControllerTest, FallbackTimer) { |
| 189 ForceDeferredStartup(); |
| 179 sync_prefs()->SetSyncSetupCompleted(); | 190 sync_prefs()->SetSyncSetupCompleted(); |
| 180 signin()->set_account(kTestUser); | 191 signin()->set_account(kTestUser); |
| 181 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 192 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 182 controller()->TryStart(); | 193 controller()->TryStart(); |
| 183 EXPECT_FALSE(started()); | 194 EXPECT_FALSE(started()); |
| 184 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 185 EXPECT_TRUE(started()); | 196 EXPECT_TRUE(started()); |
| 186 } | 197 } |
| 187 | 198 |
| 188 // Test that we start immediately if sessions is disabled. | 199 // Test that we start immediately if sessions is disabled. |
| 189 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { | 200 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { |
| 190 syncer::ModelTypeSet types(syncer::UserTypes()); | 201 syncer::ModelTypeSet types(syncer::UserTypes()); |
| 191 // Disabling sessions means disabling 4 types due to groupings. | 202 // Disabling sessions means disabling 4 types due to groupings. |
| 192 types.Remove(syncer::SESSIONS); | 203 types.Remove(syncer::SESSIONS); |
| 193 types.Remove(syncer::PROXY_TABS); | 204 types.Remove(syncer::PROXY_TABS); |
| 194 types.Remove(syncer::TYPED_URLS); | 205 types.Remove(syncer::TYPED_URLS); |
| 195 types.Remove(syncer::MANAGED_USER_SETTINGS); | 206 types.Remove(syncer::MANAGED_USER_SETTINGS); |
| 196 sync_prefs()->SetKeepEverythingSynced(false); | 207 sync_prefs()->SetKeepEverythingSynced(false); |
| 197 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); | 208 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); |
| 209 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 210 switches::kSyncEnableDeferredStartup); |
| 198 controller()->Reset(syncer::UserTypes()); | 211 controller()->Reset(syncer::UserTypes()); |
| 199 sync_prefs()->SetSyncSetupCompleted(); | 212 sync_prefs()->SetSyncSetupCompleted(); |
| 200 signin()->set_account(kTestUser); | 213 signin()->set_account(kTestUser); |
| 201 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 214 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 202 controller()->TryStart(); | 215 controller()->TryStart(); |
| 203 EXPECT_TRUE(started()); | 216 EXPECT_TRUE(started()); |
| 204 } | 217 } |
| 205 | 218 |
| 206 // Sanity check that the fallback timer doesn't fire before startup | 219 // Sanity check that the fallback timer doesn't fire before startup |
| 207 // conditions are met. | 220 // conditions are met. |
| 208 TEST_F(StartupControllerTest, FallbackTimerWaits) { | 221 TEST_F(StartupControllerTest, FallbackTimerWaits) { |
| 222 ForceDeferredStartup(); |
| 209 controller()->TryStart(); | 223 controller()->TryStart(); |
| 210 EXPECT_FALSE(started()); | 224 EXPECT_FALSE(started()); |
| 211 base::RunLoop().RunUntilIdle(); | 225 base::RunLoop().RunUntilIdle(); |
| 212 EXPECT_FALSE(started()); | 226 EXPECT_FALSE(started()); |
| 213 } | 227 } |
| 214 | 228 |
| 215 // Test that sync starts when the user first asks to setup sync (which | 229 // Test that sync starts when the user first asks to setup sync (which |
| 216 // may be implicit due to the platform). | 230 // may be implicit due to the platform). |
| 217 TEST_F(StartupControllerTest, FirstSetup) { | 231 TEST_F(StartupControllerTest, FirstSetup) { |
| 218 signin()->set_account(kTestUser); | 232 signin()->set_account(kTestUser); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 233 sync_prefs()->SetSyncSetupCompleted(); | 247 sync_prefs()->SetSyncSetupCompleted(); |
| 234 signin()->set_account(kTestUser); | 248 signin()->set_account(kTestUser); |
| 235 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 249 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 236 controller()->TryStart(); | 250 controller()->TryStart(); |
| 237 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 251 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
| 238 EXPECT_TRUE(started()); | 252 EXPECT_TRUE(started()); |
| 239 clear_started(); | 253 clear_started(); |
| 240 controller()->Reset(syncer::UserTypes()); | 254 controller()->Reset(syncer::UserTypes()); |
| 241 base::RunLoop().RunUntilIdle(); | 255 base::RunLoop().RunUntilIdle(); |
| 242 EXPECT_FALSE(started()); | 256 EXPECT_FALSE(started()); |
| 243 const bool deferred_start = !CommandLine::ForCurrentProcess()-> | 257 const bool deferred_start = CommandLine::ForCurrentProcess()-> |
| 244 HasSwitch(switches::kSyncDisableDeferredStartup); | 258 HasSwitch(switches::kSyncEnableDeferredStartup); |
| 245 controller()->TryStart(); | 259 controller()->TryStart(); |
| 246 EXPECT_EQ(!deferred_start, started()); | 260 EXPECT_EQ(!deferred_start, started()); |
| 247 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 261 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
| 248 EXPECT_TRUE(started()); | 262 EXPECT_TRUE(started()); |
| 249 } | 263 } |
| 250 | 264 |
| 251 // Test that setup-in-progress tracking is reset properly on Reset. | 265 // Test that setup-in-progress tracking is reset properly on Reset. |
| 252 // This scenario doesn't affect auto-start platforms. | 266 // This scenario doesn't affect auto-start platforms. |
| 253 TEST_F(StartupControllerTest, ResetDuringSetup) { | 267 TEST_F(StartupControllerTest, ResetDuringSetup) { |
| 254 signin()->set_account(kTestUser); | 268 signin()->set_account(kTestUser); |
| 255 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 269 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
| 256 controller()->set_setup_in_progress(true); | 270 controller()->set_setup_in_progress(true); |
| 257 controller()->Reset(syncer::UserTypes()); | 271 controller()->Reset(syncer::UserTypes()); |
| 258 controller()->TryStart(); | 272 controller()->TryStart(); |
| 259 | 273 |
| 260 if (!browser_defaults::kSyncAutoStarts) { | 274 if (!browser_defaults::kSyncAutoStarts) { |
| 261 EXPECT_FALSE(started()); | 275 EXPECT_FALSE(started()); |
| 262 EXPECT_EQ(kStateStringNotStarted, | 276 EXPECT_EQ(kStateStringNotStarted, |
| 263 controller()->GetBackendInitializationStateString()); | 277 controller()->GetBackendInitializationStateString()); |
| 264 } | 278 } |
| 265 } | 279 } |
| 266 | 280 |
| 267 } // namespace browser_sync | 281 } // namespace browser_sync |
| OLD | NEW |