1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/startup_controller.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/sync/managed_user_signin_manager_wrapper.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_driver/sync_prefs.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace browser_sync {
static const char kTestUser[] = "test@gmail.com";
static const char kTestToken[] = "testToken";
// These are coupled to the implementation of StartupController's
// GetBackendInitializationStateString which is used by about:sync. We use it
// as a convenient way to verify internal state and that the class is
// outputting the correct values for the debug string.
static const char kStateStringStarted[] = "Started";
static const char kStateStringDeferred[] = "Deferred";
static const char kStateStringNotStarted[] = "Not started";
class FakeManagedUserSigninManagerWrapper
: public ManagedUserSigninManagerWrapper {
public:
FakeManagedUserSigninManagerWrapper()
: ManagedUserSigninManagerWrapper(NULL, NULL) {}
virtual std::string GetEffectiveUsername() const OVERRIDE {
return account_;
}
virtual std::string GetAccountIdToUse() const OVERRIDE {
return account_;
}
void set_account(const std::string& account) { account_ = account; }
private:
std::string account_;
};
class StartupControllerTest : public testing::Test {
public:
StartupControllerTest() : started_(false) {}
virtual void SetUp() OVERRIDE {
profile_.reset(new TestingProfile());
sync_prefs_.reset(new sync_driver::SyncPrefs(profile_->GetPrefs()));
token_service_.reset(static_cast<FakeProfileOAuth2TokenService*>(
BuildFakeProfileOAuth2TokenService(profile_.get())));
signin_.reset(new FakeManagedUserSigninManagerWrapper());
ProfileSyncServiceStartBehavior behavior =
browser_defaults::kSyncAutoStarts ? AUTO_START : MANUAL_START;
base::Closure fake_start_backend = base::Bind(
&StartupControllerTest::FakeStartBackend, base::Unretained(this));
controller_.reset(new StartupController(behavior, token_service(),
sync_prefs_.get(), signin_.get(),
fake_start_backend));
controller_->Reset(syncer::UserTypes());
controller_->OverrideFallbackTimeoutForTest(
base::TimeDelta::FromSeconds(0));
}
virtual void TearDown() OVERRIDE {
controller_.reset();
signin_.reset();
token_service_->Shutdown();
token_service_.reset();
sync_prefs_.reset();
started_ = false;
}
void FakeStartBackend() {
started_ = true;
}
bool started() const { return started_; }
void clear_started() { started_ = false; }
StartupController* controller() { return controller_.get(); }
FakeManagedUserSigninManagerWrapper* signin() { return signin_.get(); }
FakeProfileOAuth2TokenService* token_service() {
return token_service_.get();
}
sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
Profile* profile() { return profile_.get(); }
private:
bool started_;
base::MessageLoop message_loop_;
scoped_ptr<StartupController> controller_;
scoped_ptr<FakeManagedUserSigninManagerWrapper> signin_;
scoped_ptr<FakeProfileOAuth2TokenService> token_service_;
scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
scoped_ptr<TestingProfile> profile_;
};
// Test that sync doesn't start until all conditions are met.
TEST_F(StartupControllerTest, Basic) {
controller()->TryStart();
EXPECT_FALSE(started());
sync_prefs()->SetSyncSetupCompleted();
controller()->TryStart();
EXPECT_FALSE(started());
signin()->set_account(kTestUser);
controller()->TryStart();
EXPECT_FALSE(started());
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
const bool deferred_start = !CommandLine::ForCurrentProcess()->
HasSwitch(switches::kSyncDisableDeferredStartup);
controller()->TryStart();
EXPECT_EQ(!deferred_start, started());
std::string state(controller()->GetBackendInitializationStateString());
EXPECT_TRUE(deferred_start ? state == kStateStringDeferred :
state == kStateStringStarted);
}
// Test that sync doesn't when suppressed even if all other conditons are met.
TEST_F(StartupControllerTest, Suppressed) {
sync_prefs()->SetSyncSetupCompleted();
sync_prefs()->SetStartSuppressed(true);
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
EXPECT_FALSE(started());
EXPECT_EQ(kStateStringNotStarted,
controller()->GetBackendInitializationStateString());
}
// Test that sync doesn't when managed even if all other conditons are met.
TEST_F(StartupControllerTest, Managed) {
sync_prefs()->SetSyncSetupCompleted();
sync_prefs()->SetManagedForTest(true);
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
EXPECT_FALSE(started());
EXPECT_EQ(kStateStringNotStarted,
controller()->GetBackendInitializationStateString());
}
// Test that sync doesn't start until all conditions are met and a
// data type triggers sync startup.
TEST_F(StartupControllerTest, DataTypeTriggered) {
sync_prefs()->SetSyncSetupCompleted();
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
EXPECT_FALSE(started());
EXPECT_EQ(kStateStringDeferred,
controller()->GetBackendInitializationStateString());
controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
EXPECT_TRUE(started());
EXPECT_EQ(kStateStringStarted,
controller()->GetBackendInitializationStateString());
// The fallback timer shouldn't result in another invocation of the closure
// we passed to the StartupController.
clear_started();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(started());
}
// Test that the fallback timer starts sync in the event all
// conditions are met and no data type requests sync.
TEST_F(StartupControllerTest, FallbackTimer) {
sync_prefs()->SetSyncSetupCompleted();
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
EXPECT_FALSE(started());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(started());
}
// Test that we start immediately if sessions is disabled.
TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) {
syncer::ModelTypeSet types(syncer::UserTypes());
// Disabling sessions means disabling 4 types due to groupings.
types.Remove(syncer::SESSIONS);
types.Remove(syncer::PROXY_TABS);
types.Remove(syncer::TYPED_URLS);
types.Remove(syncer::SUPERVISED_USER_SETTINGS);
sync_prefs()->SetKeepEverythingSynced(false);
sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types);
controller()->Reset(syncer::UserTypes());
sync_prefs()->SetSyncSetupCompleted();
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
EXPECT_TRUE(started());
}
// Sanity check that the fallback timer doesn't fire before startup
// conditions are met.
TEST_F(StartupControllerTest, FallbackTimerWaits) {
controller()->TryStart();
EXPECT_FALSE(started());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(started());
}
// Test that sync starts when the user first asks to setup sync (which
// may be implicit due to the platform).
TEST_F(StartupControllerTest, FirstSetup) {
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
if (browser_defaults::kSyncAutoStarts) {
EXPECT_TRUE(started());
} else {
controller()->set_setup_in_progress(true);
controller()->TryStart();
EXPECT_TRUE(started());
}
}
// Test that the controller "forgets" that preconditions were met on reset.
TEST_F(StartupControllerTest, Reset) {
sync_prefs()->SetSyncSetupCompleted();
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
controller()->TryStart();
controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
EXPECT_TRUE(started());
clear_started();
controller()->Reset(syncer::UserTypes());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(started());
const bool deferred_start = !CommandLine::ForCurrentProcess()->
HasSwitch(switches::kSyncDisableDeferredStartup);
controller()->TryStart();
EXPECT_EQ(!deferred_start, started());
controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
EXPECT_TRUE(started());
}
// Test that setup-in-progress tracking is persistent across a Reset.
TEST_F(StartupControllerTest, ResetDuringSetup) {
signin()->set_account(kTestUser);
token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
// Simulate UI telling us setup is in progress.
controller()->set_setup_in_progress(true);
// This could happen if the UI triggers a stop-syncing permanently call.
controller()->Reset(syncer::UserTypes());
// From the UI's point of view, setup is still in progress.
EXPECT_TRUE(controller()->setup_in_progress());
}
} // namespace browser_sync
|