summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/sync_setup_wizard_unittest.cc
blob: a40583ac397d66a3b11a8a93be7b6d349c4244d6 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2012 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/sync_setup_wizard.h"

#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/pref_service_mock_builder.h"
#include "chrome/browser/prefs/testing_pref_store.h"
#include "chrome/browser/signin/signin_manager_fake.h"
#include "chrome/browser/sync/profile_sync_components_factory_mock.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/sync/sync_setup_flow.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/webui/options/options_sync_setup_handler.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"

static const char kTestUser[] = "chrome.p13n.test@gmail.com";

typedef GoogleServiceAuthError AuthError;

class MockSyncSetupHandler : public OptionsSyncSetupHandler {
 public:
  explicit MockSyncSetupHandler(Profile* profile)
      : OptionsSyncSetupHandler(NULL),
        profile_(profile) {}

  // SyncSetupFlowHandler implementation.
  virtual void ShowFatalError() OVERRIDE {
    ShowSetupDone(string16());
  }
  virtual void ShowConfigure(const DictionaryValue& args) OVERRIDE {}
  virtual void ShowPassphraseEntry(const DictionaryValue& args) OVERRIDE {}
  virtual void ShowSettingUp() OVERRIDE {}
  virtual void ShowSetupDone(const string16& user) OVERRIDE {
    if (flow())
      flow()->OnDialogClosed("");
  }

  SyncSetupFlow* GetFlow() {
    return flow();
  }

  void CloseSetupUI() {
    ShowSetupDone(string16());
  }

  virtual Profile* GetProfile() const OVERRIDE {
    return profile_;
  }

 private:
  // Weak ptr to injected parent profile.
  Profile* profile_;
  DISALLOW_COPY_AND_ASSIGN(MockSyncSetupHandler);
};

class SigninManagerMock : public FakeSigninManager {
 public:
  SigninManagerMock() : auth_error_(AuthError::None()) {}

  virtual void StartSignIn(const std::string& username,
                           const std::string& password,
                           const std::string& token,
                           const std::string& captcha) OVERRIDE {
    FakeSigninManager::StartSignIn(username, password, token, captcha);
    username_ = username;
    password_ = password;
    captcha_ = captcha;
  }

  virtual const AuthError& GetLoginAuthError() const OVERRIDE {
    return auth_error_;
  }

  void ResetTestStats() {
    username_.clear();
    password_.clear();
    captcha_.clear();
  }

  AuthError auth_error_;
  std::string username_;
  std::string password_;
  std::string captcha_;
};

// A PSS subtype to inject.
class ProfileSyncServiceForWizardTest : public ProfileSyncService {
 public:
  virtual ~ProfileSyncServiceForWizardTest() {}

  static ProfileKeyedBase* BuildManual(Profile* profile) {
    return new ProfileSyncServiceForWizardTest(profile,
        ProfileSyncService::MANUAL_START);
  }

  static ProfileKeyedBase* BuildAuto(Profile* profile) {
    return new ProfileSyncServiceForWizardTest(profile,
        ProfileSyncService::AUTO_START);
  }

  virtual void OnUserChoseDatatypes(
      bool sync_everything, syncable::ModelTypeSet chosen_types) OVERRIDE {
    user_chose_data_types_ = true;
    chosen_data_types_ = chosen_types;
  }

  virtual void OnUserCancelledDialog() OVERRIDE {
    user_cancelled_dialog_ = true;
  }

  virtual void SetPassphrase(const std::string& passphrase,
                             PassphraseType type,
                             PassphraseSource source) OVERRIDE {
    passphrase_ = passphrase;
  }

  virtual bool IsUsingSecondaryPassphrase() const OVERRIDE {
    return is_using_secondary_passphrase_;
  }

  void set_last_auth_error(const AuthError& error) {
    // Set the cached auth error in the SigninManager and ProfileSyncService.
    last_auth_error_ = error;
    mock_signin_.auth_error_ = error;
  }

  void set_is_using_secondary_passphrase(bool secondary) {
    is_using_secondary_passphrase_ = secondary;
  }

  void set_passphrase_required_reason(
      sync_api::PassphraseRequiredReason reason) {
    passphrase_required_reason_ = reason;
  }

  virtual bool sync_initialized() const OVERRIDE {
    return true;
  }

  virtual bool EncryptEverythingEnabled() const OVERRIDE {
    return encrypt_everything_;
  }

  void set_encrypt_everything(bool encrypt_everything) {
    encrypt_everything_ = encrypt_everything;
  }

  void ResetTestStats() {
    mock_signin_.ResetTestStats();
    user_cancelled_dialog_ = false;
    user_chose_data_types_ = false;
    keep_everything_synced_ = false;
    chosen_data_types_.Clear();
  }

  virtual void ShowSyncSetup(const std::string& sub_page) OVERRIDE {
    // Do Nothing.
  }

  void ClearObservers() {
    observers_.Clear();
  }

  SyncSetupWizard* GetWizard() {
    return &wizard_;
  }

  SigninManagerMock mock_signin_;
  bool user_cancelled_dialog_;
  bool user_chose_data_types_;
  bool keep_everything_synced_;
  bool is_using_secondary_passphrase_;
  bool encrypt_everything_;
  syncable::ModelTypeSet chosen_data_types_;
  std::string passphrase_;

 private:
  ProfileSyncServiceForWizardTest(Profile* profile,
                                  ProfileSyncService::StartBehavior behavior)
      : ProfileSyncService(NULL, profile, NULL, behavior),
        user_cancelled_dialog_(false),
        is_using_secondary_passphrase_(false),
        encrypt_everything_(false) {
    signin_ = &mock_signin_;
    ResetTestStats();
  }

  DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceForWizardTest);
};

// TODO(jhawkins): Subclass Browser (specifically, ShowOptionsTab) and inject it
// here to test the visibility of the Sync UI.
class SyncSetupWizardTest : public BrowserWithTestWindowTest {
 public:
  SyncSetupWizardTest()
      : wizard_(NULL),
        service_(NULL) {}
  virtual ~SyncSetupWizardTest() {}
  virtual TestingProfile* BuildProfile() {
    TestingProfile* profile = new TestingProfile();
    ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile,
        ProfileSyncServiceForWizardTest::BuildManual);
    return profile;
  }
  virtual void SetUp() {
    set_profile(BuildProfile());
    profile()->CreateBookmarkModel(false);
    // Wait for the bookmarks model to load.
    profile()->BlockUntilBookmarkModelLoaded();
    PrefService* prefs = profile()->GetPrefs();
    prefs->SetString(prefs::kGoogleServicesUsername, kTestUser);

    handler_.reset(new MockSyncSetupHandler(profile()));
    set_browser(new Browser(Browser::TYPE_TABBED, profile()));
    browser()->SetWindowForTesting(window());
    BrowserList::SetLastActive(browser());
    service_ = static_cast<ProfileSyncServiceForWizardTest*>(
        ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()));
    wizard_ = service_->GetWizard();
  }

  virtual void TearDown() {
    wizard_ = NULL;
    service_ = NULL;
    handler_.reset();
  }

 protected:
  void AttachSyncSetupHandler() {
    wizard_->AttachSyncSetupHandler(handler_.get());
  }

  void CompleteSetup() {
    wizard_->Step(SyncSetupWizard::SYNC_EVERYTHING);
    AttachSyncSetupHandler();
    wizard_->Step(SyncSetupWizard::SETTING_UP);
    wizard_->Step(SyncSetupWizard::DONE);
  }

  void CloseSetupUI() {
    handler_->CloseSetupUI();
  }

  // This pointer is owned by the |Service_|.
  SyncSetupWizard* wizard_;
  ProfileSyncServiceForWizardTest* service_;
  scoped_ptr<MockSyncSetupHandler> handler_;
};

TEST_F(SyncSetupWizardTest, ChooseDataTypesSetsPrefs) {
  wizard_->Step(SyncSetupWizard::CONFIGURE);
  AttachSyncSetupHandler();

  ListValue data_type_choices_value;
  std::string data_type_choices =
      "{\"syncAllDataTypes\":false,\"syncBookmarks\":true,"
      "\"syncPreferences\":true,\"syncThemes\":false,\"syncPasswords\":false,"
      "\"syncAutofill\":false,\"syncExtensions\":false,\"syncTypedUrls\":true,"
      "\"syncApps\":true,\"syncSessions\":false,\"usePassphrase\":false,"
      "\"encryptAllData\":false}";
  data_type_choices_value.Append(new StringValue(data_type_choices));

  // Simulate the user choosing data types; bookmarks, prefs, typed URLS, and
  // apps are on, the rest are off.
  handler_->HandleConfigure(&data_type_choices_value);
  // Since we don't need a passphrase, wizard should have transitioned to
  // DONE state and closed the UI.
  EXPECT_FALSE(wizard_->IsVisible());
  EXPECT_FALSE(service_->keep_everything_synced_);
  EXPECT_TRUE(service_->chosen_data_types_.Has(syncable::BOOKMARKS));
  EXPECT_TRUE(service_->chosen_data_types_.Has(syncable::PREFERENCES));
  EXPECT_FALSE(service_->chosen_data_types_.Has(syncable::THEMES));
  EXPECT_FALSE(service_->chosen_data_types_.Has(syncable::PASSWORDS));
  EXPECT_FALSE(service_->chosen_data_types_.Has(syncable::AUTOFILL));
  EXPECT_FALSE(service_->chosen_data_types_.Has(syncable::EXTENSIONS));
  EXPECT_TRUE(service_->chosen_data_types_.Has(syncable::TYPED_URLS));
  EXPECT_TRUE(service_->chosen_data_types_.Has(syncable::APPS));
  EXPECT_FALSE(service_->chosen_data_types_.Has(
      syncable::APP_NOTIFICATIONS));
}

TEST_F(SyncSetupWizardTest, ShowErrorUIForPassphraseTest) {
  service_->ClearObservers();
  CompleteSetup();

  // Simulate a passphrase error and make sure the start and end state are set
  // right and wizard is shown.
  service_->set_passphrase_required_reason(sync_api::REASON_ENCRYPTION);
  service_->set_is_using_secondary_passphrase(true);
  service_->ShowErrorUI();
  AttachSyncSetupHandler();
  EXPECT_EQ(SyncSetupWizard::ENTER_PASSPHRASE,
            handler_->GetFlow()->current_state_);
  EXPECT_EQ(SyncSetupWizard::DONE, handler_->GetFlow()->end_state_);
  ASSERT_TRUE(wizard_->IsVisible());

  // Make sure the wizard is dismissed.
  wizard_->Step(SyncSetupWizard::DONE);
  ASSERT_FALSE(wizard_->IsVisible());
}

TEST_F(SyncSetupWizardTest, EnterPassphraseRequired) {
  wizard_->Step(SyncSetupWizard::CONFIGURE);
  AttachSyncSetupHandler();
  wizard_->Step(SyncSetupWizard::SETTING_UP);
  service_->set_passphrase_required_reason(sync_api::REASON_ENCRYPTION);
  wizard_->Step(SyncSetupWizard::ENTER_PASSPHRASE);
  EXPECT_EQ(SyncSetupWizard::ENTER_PASSPHRASE,
            handler_->GetFlow()->current_state_);

  ListValue value;
  value.Append(new StringValue("{\"passphrase\":\"myPassphrase\","
                                "\"mode\":\"gaia\"}"));
  handler_->HandlePassphraseEntry(&value);
  EXPECT_EQ("myPassphrase", service_->passphrase_);
  CloseSetupUI();
}

TEST_F(SyncSetupWizardTest, InvalidTransitions) {
  wizard_->Step(SyncSetupWizard::DONE);
  EXPECT_FALSE(wizard_->IsVisible());

  wizard_->Step(SyncSetupWizard::SYNC_EVERYTHING);
  AttachSyncSetupHandler();
  EXPECT_EQ(SyncSetupWizard::SYNC_EVERYTHING,
            handler_->GetFlow()->current_state_);
  EXPECT_TRUE(wizard_->IsVisible());

  wizard_->Step(SyncSetupWizard::FATAL_ERROR);
  // Stepping to FATAL_ERROR leaves us in a FATAL_ERROR state and blows away
  // the SyncSetupFlow.
  EXPECT_FALSE(wizard_->IsVisible());
  EXPECT_TRUE(handler_->GetFlow() == NULL);
}

TEST_F(SyncSetupWizardTest, FullSuccessfulRunSetsPref) {
  CompleteSetup();
  EXPECT_FALSE(wizard_->IsVisible());
  EXPECT_TRUE(service_->profile()->GetPrefs()->GetBoolean(
      prefs::kSyncHasSetupCompleted));
}

TEST_F(SyncSetupWizardTest, AbortedByPendingClear) {
  wizard_->Step(SyncSetupWizard::SYNC_EVERYTHING);
  AttachSyncSetupHandler();
  EXPECT_TRUE(wizard_->IsVisible());
  wizard_->Step(SyncSetupWizard::ABORT);
  // Stepping to ABORT should leave us in ABORT state, and should close the
  // wizard.
  EXPECT_FALSE(wizard_->IsVisible());
}

TEST_F(SyncSetupWizardTest, DiscreteRunChooseDataTypes) {
  CompleteSetup();

  wizard_->Step(SyncSetupWizard::CONFIGURE);
  AttachSyncSetupHandler();
  EXPECT_EQ(SyncSetupWizard::DONE, handler_->GetFlow()->end_state_);

  wizard_->Step(SyncSetupWizard::SETTING_UP);
  wizard_->Step(SyncSetupWizard::DONE);
  EXPECT_FALSE(wizard_->IsVisible());
}

TEST_F(SyncSetupWizardTest, DiscreteRunChooseDataTypesAbortedByPendingClear) {
  CompleteSetup();

  wizard_->Step(SyncSetupWizard::CONFIGURE);
  AttachSyncSetupHandler();
  EXPECT_TRUE(wizard_->IsVisible());
  EXPECT_EQ(SyncSetupWizard::DONE, handler_->GetFlow()->end_state_);
  wizard_->Step(SyncSetupWizard::ABORT);
  // Stepping to ABORT should leave us in the ABORT state and close the dialog.
  EXPECT_FALSE(wizard_->IsVisible());
}

TEST_F(SyncSetupWizardTest, EnterPassphrase) {
  CompleteSetup();

  // Set up the ENTER_PASSPHRASE case.
  service_->set_passphrase_required_reason(sync_api::REASON_ENCRYPTION);
  service_->set_is_using_secondary_passphrase(true);
  wizard_->Step(SyncSetupWizard::ENTER_PASSPHRASE);
  AttachSyncSetupHandler();
  EXPECT_EQ(SyncSetupWizard::ENTER_PASSPHRASE,
            handler_->GetFlow()->current_state_);
  EXPECT_EQ(SyncSetupWizard::DONE, handler_->GetFlow()->end_state_);
  CloseSetupUI();
  EXPECT_FALSE(wizard_->IsVisible());
}

TEST_F(SyncSetupWizardTest, FatalErrorDuringConfigure) {
  CompleteSetup();

  wizard_->Step(SyncSetupWizard::CONFIGURE);
  AttachSyncSetupHandler();
  EXPECT_EQ(SyncSetupWizard::DONE, handler_->GetFlow()->end_state_);
  wizard_->Step(SyncSetupWizard::FATAL_ERROR);
  EXPECT_TRUE(handler_->GetFlow() == NULL);
  EXPECT_FALSE(wizard_->IsVisible());
}

class SyncSetupWizardCrosTest : public SyncSetupWizardTest {
 public:
  virtual TestingProfile* BuildProfile() {
    TestingProfile* profile = new TestingProfile();
    ProfileSyncServiceFactory* f = ProfileSyncServiceFactory::GetInstance();
    f->SetTestingFactory(profile, ProfileSyncServiceForWizardTest::BuildAuto);
    f->GetForProfile(profile)->signin()->SetAuthenticatedUsername(kTestUser);
    return profile;
  }
};