NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / sync / one_click_signin_helper_unittest.cc
blob245cece69485bebf59569ab095b686388b46eba9
1 // Copyright (c) 2012 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.
5 #include "base/prefs/pref_service.h"
6 #include "base/prefs/scoped_user_pref_update.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/content_settings/cookie_settings.h"
11 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_info_cache.h"
14 #include "chrome/browser/profiles/profile_io_data.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
17 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h"
18 #include "chrome/browser/signin/fake_signin_manager.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/signin/signin_manager.h"
22 #include "chrome/browser/signin/signin_manager_factory.h"
23 #include "chrome/browser/signin/signin_names_io_thread.h"
24 #include "chrome/browser/signin/signin_promo.h"
25 #include "chrome/browser/sync/profile_sync_service_factory.h"
26 #include "chrome/browser/sync/profile_sync_service_mock.h"
27 #include "chrome/browser/sync/test_profile_sync_service.h"
28 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
29 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
30 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
33 #include "chrome/test/base/testing_browser_process.h"
34 #include "chrome/test/base/testing_pref_service_syncable.h"
35 #include "chrome/test/base/testing_profile.h"
36 #include "chrome/test/base/testing_profile_manager.h"
37 #include "components/autofill/core/common/password_form.h"
38 #include "content/public/browser/browser_context.h"
39 #include "content/public/browser/navigation_details.h"
40 #include "content/public/browser/web_contents.h"
41 #include "content/public/common/frame_navigate_params.h"
42 #include "content/public/common/url_constants.h"
43 #include "content/public/test/mock_render_process_host.h"
44 #include "grit/chromium_strings.h"
45 #include "grit/generated_resources.h"
46 #include "testing/gtest/include/gtest/gtest.h"
47 #include "ui/base/l10n/l10n_util.h"
49 using ::testing::_;
50 using ::testing::AtLeast;
51 using ::testing::Return;
53 namespace {
55 class SigninManagerMock : public FakeSigninManager {
56 public:
57 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) {
58 Initialize(profile, NULL);
60 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username));
63 static BrowserContextKeyedService* BuildSigninManagerMock(
64 content::BrowserContext* profile) {
65 return new SigninManagerMock(static_cast<Profile*>(profile));
68 class TestProfileIOData : public ProfileIOData {
69 public:
70 TestProfileIOData(bool is_incognito, PrefService* pref_service,
71 PrefService* local_state, CookieSettings* cookie_settings)
72 : ProfileIOData(is_incognito) {
73 // Initialize the IO members required for these tests, but keep them on
74 // this thread since we don't use a background thread here.
75 google_services_username()->Init(prefs::kGoogleServicesUsername,
76 pref_service);
77 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled,
78 pref_service);
79 one_click_signin_rejected_email_list()->Init(
80 prefs::kReverseAutologinRejectedEmailList, pref_service);
82 google_services_username_pattern()->Init(
83 prefs::kGoogleServicesUsernamePattern, local_state);
85 sync_disabled()->Init(prefs::kSyncManaged, pref_service);
87 signin_allowed()->Init(prefs::kSigninAllowed, pref_service);
89 set_signin_names_for_testing(new SigninNamesOnIOThread());
90 SetCookieSettingsForTesting(cookie_settings);
93 virtual ~TestProfileIOData() {
94 signin_names()->ReleaseResourcesOnUIThread();
97 // ProfileIOData overrides:
98 virtual void InitializeInternal(
99 ProfileParams* profile_params,
100 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE {
101 NOTREACHED();
103 virtual void InitializeExtensionsRequestContext(
104 ProfileParams* profile_params) const OVERRIDE {
105 NOTREACHED();
107 virtual ChromeURLRequestContext* InitializeAppRequestContext(
108 ChromeURLRequestContext* main_context,
109 const StoragePartitionDescriptor& details,
110 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
111 protocol_handler_interceptor,
112 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE {
113 NOTREACHED();
114 return NULL;
116 virtual ChromeURLRequestContext* InitializeMediaRequestContext(
117 ChromeURLRequestContext* original_context,
118 const StoragePartitionDescriptor& details) const OVERRIDE {
119 NOTREACHED();
120 return NULL;
122 virtual ChromeURLRequestContext*
123 AcquireMediaRequestContext() const OVERRIDE {
124 NOTREACHED();
125 return NULL;
127 virtual ChromeURLRequestContext*
128 AcquireIsolatedAppRequestContext(
129 ChromeURLRequestContext* main_context,
130 const StoragePartitionDescriptor& partition_descriptor,
131 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
132 protocol_handler_interceptor,
133 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE {
134 NOTREACHED();
135 return NULL;
137 virtual ChromeURLRequestContext*
138 AcquireIsolatedMediaRequestContext(
139 ChromeURLRequestContext* app_context,
140 const StoragePartitionDescriptor& partition_descriptor)
141 const OVERRIDE {
142 NOTREACHED();
143 return NULL;
147 class TestURLRequest : public base::SupportsUserData {
148 public:
149 TestURLRequest() {}
150 virtual ~TestURLRequest() {}
153 class OneClickTestProfileSyncService : public TestProfileSyncService {
154 public:
155 virtual ~OneClickTestProfileSyncService() {}
157 // Helper routine to be used in conjunction with
158 // BrowserContextKeyedServiceFactory::SetTestingFactory().
159 static BrowserContextKeyedService* Build(content::BrowserContext* profile) {
160 return new OneClickTestProfileSyncService(static_cast<Profile*>(profile));
163 // Need to control this for certain tests.
164 virtual bool FirstSetupInProgress() const OVERRIDE {
165 return first_setup_in_progress_;
168 // Controls return value of FirstSetupInProgress. Because some bits
169 // of UI depend on that value, it's useful to control it separately
170 // from the internal work and components that are triggered (such as
171 // ReconfigureDataTypeManager) to facilitate unit tests.
172 void set_first_setup_in_progress(bool in_progress) {
173 first_setup_in_progress_ = in_progress;
176 private:
177 explicit OneClickTestProfileSyncService(Profile* profile)
178 : TestProfileSyncService(
179 NULL,
180 profile,
181 SigninManagerFactory::GetForProfile(profile),
182 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
183 ProfileSyncService::MANUAL_START),
184 first_setup_in_progress_(false) {}
186 bool first_setup_in_progress_;
189 } // namespace
191 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness {
192 public:
193 OneClickSigninHelperTest();
195 virtual void SetUp() OVERRIDE;
196 virtual void TearDown() OVERRIDE;
198 // Creates the sign-in manager for tests. If |username| is
199 // is not empty, the profile of the mock WebContents will be connected to
200 // the given account.
201 void CreateSigninManager(const std::string& username);
203 // Set the ID of the signin process that the test will assume to be the
204 // only process allowed to sign the user in to Chrome.
205 void SetTrustedSigninProcessID(int id);
207 void AddEmailToOneClickRejectedList(const std::string& email);
208 void EnableOneClick(bool enable);
209 void AllowSigninCookies(bool enable);
210 void SetAllowedUsernamePattern(const std::string& pattern);
211 ProfileSyncServiceMock* CreateProfileSyncServiceMock();
212 void SubmitGAIAPassword(OneClickSigninHelper* helper);
214 SigninManagerMock* signin_manager_;
215 FakeProfileOAuth2TokenService* fake_oauth2_token_service_;
217 protected:
218 GoogleServiceAuthError no_error_;
220 private:
221 // ChromeRenderViewHostTestHarness overrides:
222 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE;
224 // The ID of the signin process the test will assume to be trusted.
225 // By default, set to the test RenderProcessHost's process ID, but
226 // overridden by SetTrustedSigninProcessID.
227 int trusted_signin_process_id_;
229 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest);
232 OneClickSigninHelperTest::OneClickSigninHelperTest()
233 : signin_manager_(NULL),
234 fake_oauth2_token_service_(NULL),
235 no_error_(GoogleServiceAuthError::NONE),
236 trusted_signin_process_id_(-1) {
239 void OneClickSigninHelperTest::SetUp() {
240 signin::ForceWebBasedSigninFlowForTesting(true);
241 content::RenderViewHostTestHarness::SetUp();
242 SetTrustedSigninProcessID(process()->GetID());
245 void OneClickSigninHelperTest::TearDown() {
246 signin::ForceWebBasedSigninFlowForTesting(false);
247 content::RenderViewHostTestHarness::TearDown();
250 void OneClickSigninHelperTest::SetTrustedSigninProcessID(int id) {
251 trusted_signin_process_id_ = id;
254 void OneClickSigninHelperTest::CreateSigninManager(
255 const std::string& username) {
256 signin_manager_ = static_cast<SigninManagerMock*>(
257 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
258 profile(), BuildSigninManagerMock));
259 if (signin_manager_)
260 signin_manager_->SetSigninProcess(trusted_signin_process_id_);
262 if (!username.empty()) {
263 ASSERT_TRUE(signin_manager_);
264 signin_manager_->SetAuthenticatedUsername(username);
268 void OneClickSigninHelperTest::EnableOneClick(bool enable) {
269 PrefService* pref_service = profile()->GetPrefs();
270 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable);
273 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList(
274 const std::string& email) {
275 PrefService* pref_service = profile()->GetPrefs();
276 ListPrefUpdate updater(pref_service,
277 prefs::kReverseAutologinRejectedEmailList);
278 updater->AppendIfNotPresent(new base::StringValue(email));
281 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) {
282 CookieSettings* cookie_settings =
283 CookieSettings::Factory::GetForProfile(profile()).get();
284 cookie_settings->SetDefaultCookieSetting(enable ? CONTENT_SETTING_ALLOW
285 : CONTENT_SETTING_BLOCK);
288 void OneClickSigninHelperTest::SetAllowedUsernamePattern(
289 const std::string& pattern) {
290 PrefService* local_state = g_browser_process->local_state();
291 local_state->SetString(prefs::kGoogleServicesUsernamePattern, pattern);
294 ProfileSyncServiceMock*
295 OneClickSigninHelperTest::CreateProfileSyncServiceMock() {
296 ProfileSyncServiceMock* sync_service = static_cast<ProfileSyncServiceMock*>(
297 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
298 profile(),
299 ProfileSyncServiceMock::BuildMockProfileSyncService));
300 EXPECT_CALL(*sync_service, FirstSetupInProgress()).WillRepeatedly(
301 Return(false));
302 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true));
303 EXPECT_CALL(*sync_service, GetAuthError()).
304 WillRepeatedly(::testing::ReturnRef(no_error_));
305 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(false));
306 sync_service->Initialize();
307 return sync_service;
310 void OneClickSigninHelperTest::SubmitGAIAPassword(
311 OneClickSigninHelper* helper) {
312 autofill::PasswordForm password_form;
313 password_form.origin = GURL("https://accounts.google.com");
314 password_form.signon_realm = "https://accounts.google.com";
315 password_form.password_value = base::UTF8ToUTF16("password");
316 helper->PasswordSubmitted(password_form);
319 content::BrowserContext* OneClickSigninHelperTest::CreateBrowserContext() {
320 TestingProfile::Builder builder;
321 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
322 FakeProfileOAuth2TokenServiceWrapper::Build);
323 scoped_ptr<TestingProfile> profile = builder.Build();
325 fake_oauth2_token_service_ =
326 static_cast<FakeProfileOAuth2TokenService*>(
327 ProfileOAuth2TokenServiceFactory::GetForProfile(profile.get()));
329 return profile.release();
332 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest {
333 public:
334 OneClickSigninHelperIOTest();
336 virtual void SetUp() OVERRIDE;
338 TestProfileIOData* CreateTestProfileIOData(bool is_incognito);
340 protected:
341 TestingProfileManager testing_profile_manager_;
342 TestURLRequest request_;
343 const GURL valid_gaia_url_;
345 private:
346 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperIOTest);
349 OneClickSigninHelperIOTest::OneClickSigninHelperIOTest()
350 : testing_profile_manager_(
351 TestingBrowserProcess::GetGlobal()),
352 valid_gaia_url_("https://accounts.google.com/") {
355 void OneClickSigninHelperIOTest::SetUp() {
356 OneClickSigninHelperTest::SetUp();
357 ASSERT_TRUE(testing_profile_manager_.SetUp());
360 TestProfileIOData* OneClickSigninHelperIOTest::CreateTestProfileIOData(
361 bool is_incognito) {
362 PrefService* pref_service = profile()->GetPrefs();
363 PrefService* local_state = g_browser_process->local_state();
364 CookieSettings* cookie_settings =
365 CookieSettings::Factory::GetForProfile(profile()).get();
366 TestProfileIOData* io_data = new TestProfileIOData(
367 is_incognito, pref_service, local_state, cookie_settings);
368 io_data->set_reverse_autologin_pending_email("user@gmail.com");
369 return io_data;
372 class OneClickSigninHelperIncognitoTest : public OneClickSigninHelperTest {
373 protected:
374 // content::RenderViewHostTestHarness.
375 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE;
378 content::BrowserContext*
379 OneClickSigninHelperIncognitoTest::CreateBrowserContext() {
380 // Builds an incognito profile to run this test.
381 TestingProfile::Builder builder;
382 builder.SetIncognito();
383 return builder.Build().release();
386 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) {
387 std::string error_message;
388 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
389 NULL, OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
390 "user@gmail.com", &error_message));
391 EXPECT_EQ("", error_message);
392 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
393 NULL, OneClickSigninHelper::CAN_OFFER_FOR_ALL,
394 "user@gmail.com", &error_message));
395 EXPECT_EQ("", error_message);
396 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
397 NULL,
398 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
399 std::string(),
400 &error_message));
401 EXPECT_EQ("", error_message);
404 TEST_F(OneClickSigninHelperTest, CanOffer) {
405 CreateSigninManager(std::string());
407 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
408 WillRepeatedly(Return(true));
410 EnableOneClick(true);
411 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
412 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
413 "user@gmail.com", NULL));
414 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
415 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
416 "user@gmail.com", NULL));
417 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
418 web_contents(),
419 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
420 std::string(),
421 NULL));
423 EnableOneClick(false);
425 std::string error_message;
426 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
427 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
428 "user@gmail.com", &error_message));
429 EXPECT_EQ("", error_message);
431 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
432 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
433 "user@gmail.com", &error_message));
434 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
435 web_contents(),
436 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
437 std::string(),
438 &error_message));
439 EXPECT_EQ("", error_message);
442 TEST_F(OneClickSigninHelperTest, CanOfferFirstSetup) {
443 CreateSigninManager(std::string());
445 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
446 WillRepeatedly(Return(true));
448 // Invoke OneClickTestProfileSyncService factory function and grab result.
449 OneClickTestProfileSyncService* sync =
450 static_cast<OneClickTestProfileSyncService*>(
451 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
452 static_cast<Profile*>(browser_context()),
453 OneClickTestProfileSyncService::Build));
454 sync->Initialize();
455 sync->set_first_setup_in_progress(true);
457 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
458 web_contents(),
459 OneClickSigninHelper::CAN_OFFER_FOR_ALL,
460 "foo@gmail.com", NULL));
461 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
462 web_contents(),
463 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
464 "foo@gmail.com", NULL));
465 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
466 web_contents(),
467 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
468 std::string(),
469 NULL));
472 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) {
473 CreateSigninManager("foo@gmail.com");
475 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
476 WillRepeatedly(Return(true));
478 std::string error_message;
479 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
480 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
481 "foo@gmail.com", &error_message));
482 EXPECT_EQ("", error_message);
483 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
484 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
485 "foo", &error_message));
486 EXPECT_EQ("", error_message);
487 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
488 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
489 "user@gmail.com", &error_message));
490 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
491 base::UTF8ToUTF16("foo@gmail.com")),
492 error_message);
493 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
494 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
495 "foo@gmail.com", &error_message));
496 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
497 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
498 "foo", &error_message));
499 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
500 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
501 "user@gmail.com", &error_message));
502 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
503 base::UTF8ToUTF16("foo@gmail.com")),
504 error_message);
505 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
506 web_contents(),
507 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
508 std::string(),
509 &error_message));
512 TEST_F(OneClickSigninHelperTest, CanOfferUsernameNotAllowed) {
513 CreateSigninManager(std::string());
515 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
516 WillRepeatedly(Return(false));
518 std::string error_message;
519 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
520 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
521 "foo@gmail.com", &error_message));
522 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
523 error_message);
524 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
525 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
526 "foo@gmail.com", &error_message));
527 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
528 error_message);
529 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
530 web_contents(),
531 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
532 std::string(),
533 &error_message));
536 TEST_F(OneClickSigninHelperTest, CanOfferWithRejectedEmail) {
537 CreateSigninManager(std::string());
539 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
540 WillRepeatedly(Return(true));
542 AddEmailToOneClickRejectedList("foo@gmail.com");
543 AddEmailToOneClickRejectedList("user@gmail.com");
545 std::string error_message;
546 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
547 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
548 "foo@gmail.com", &error_message));
549 EXPECT_EQ("", error_message);
550 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
551 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
552 "user@gmail.com", &error_message));
553 EXPECT_EQ("", error_message);
554 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
555 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
556 "foo@gmail.com", &error_message));
557 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
558 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
559 "user@gmail.com", &error_message));
560 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
561 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
562 "john@gmail.com", &error_message));
565 TEST_F(OneClickSigninHelperIncognitoTest, CanOfferIncognito) {
566 CreateSigninManager(std::string());
568 std::string error_message;
569 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
570 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
571 "user@gmail.com", &error_message));
572 EXPECT_EQ("", error_message);
573 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
574 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
575 "user@gmail.com", &error_message));
576 EXPECT_EQ("", error_message);
577 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
578 web_contents(),
579 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
580 std::string(),
581 &error_message));
582 EXPECT_EQ("", error_message);
585 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) {
586 CreateSigninManager(std::string());
587 AllowSigninCookies(false);
589 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
590 WillRepeatedly(Return(true));
592 std::string error_message;
593 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
594 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
595 "user@gmail.com", &error_message));
596 EXPECT_EQ("", error_message);
597 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
598 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
599 "user@gmail.com", &error_message));
600 EXPECT_EQ("", error_message);
601 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
602 web_contents(),
603 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
604 std::string(),
605 &error_message));
606 EXPECT_EQ("", error_message);
609 TEST_F(OneClickSigninHelperTest, CanOfferDisabledByPolicy) {
610 CreateSigninManager(std::string());
612 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
613 WillRepeatedly(Return(true));
615 EnableOneClick(true);
616 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
617 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
618 "user@gmail.com", NULL));
620 // Simulate a policy disabling signin by writing kSigninAllowed directly.
621 profile()->GetTestingPrefService()->SetManagedPref(
622 prefs::kSigninAllowed, base::Value::CreateBooleanValue(false));
624 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
625 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
626 "user@gmail.com", NULL));
628 // Reset the preference value to true.
629 profile()->GetTestingPrefService()->SetManagedPref(
630 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true));
632 // Simulate a policy disabling sync by writing kSyncManaged directly.
633 profile()->GetTestingPrefService()->SetManagedPref(
634 prefs::kSyncManaged, base::Value::CreateBooleanValue(true));
636 // Should still offer even if sync is disabled by policy.
637 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
638 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
639 "user@gmail.com", NULL));
642 // Should not crash if a helper instance is not associated with an incognito
643 // web contents.
644 TEST_F(OneClickSigninHelperIncognitoTest, ShowInfoBarUIThreadIncognito) {
645 CreateSigninManager(std::string());
646 OneClickSigninHelper* helper =
647 OneClickSigninHelper::FromWebContents(web_contents());
648 EXPECT_EQ(NULL, helper);
650 OneClickSigninHelper::ShowInfoBarUIThread(
651 "session_index", "email", OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED,
652 signin::SOURCE_UNKNOWN, GURL(), process()->GetID(),
653 rvh()->GetRoutingID());
656 // If Chrome signin is triggered from a webstore install, and user chooses to
657 // config sync, then Chrome should redirect immediately to sync settings page,
658 // and upon successful setup, redirect back to webstore.
659 TEST_F(OneClickSigninHelperTest, SigninFromWebstoreWithConfigSyncfirst) {
660 CreateSigninManager(std::string());
661 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_))
662 .WillRepeatedly(Return(true));
664 ProfileSyncServiceMock* sync_service = CreateProfileSyncServiceMock();
665 EXPECT_CALL(*sync_service, AddObserver(_)).Times(AtLeast(1));
666 EXPECT_CALL(*sync_service, RemoveObserver(_)).Times(AtLeast(1));
667 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true));
669 content::WebContents* contents = web_contents();
671 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
672 OneClickSigninHelper* helper =
673 OneClickSigninHelper::FromWebContents(contents);
674 helper->SetDoNotClearPendingEmailForTesting();
675 helper->set_do_not_start_sync_for_testing();
677 GURL continueUrl("https://chrome.google.com/webstore?source=5");
678 OneClickSigninHelper::ShowInfoBarUIThread(
679 "session_index", "user@gmail.com",
680 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT,
681 signin::SOURCE_WEBSTORE_INSTALL,
682 continueUrl, process()->GetID(), rvh()->GetRoutingID());
684 SubmitGAIAPassword(helper);
686 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3"));
687 helper->DidStopLoading(rvh());
688 helper->OnStateChanged();
689 EXPECT_EQ(GURL(continueUrl), contents->GetURL());
692 // Checks that the state of OneClickSigninHelper is cleaned when there is a
693 // navigation away from the sign in flow that is not triggered by the
694 // web contents.
695 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) {
696 content::WebContents* contents = web_contents();
698 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
699 OneClickSigninHelper* helper =
700 OneClickSigninHelper::FromWebContents(contents);
701 helper->SetDoNotClearPendingEmailForTesting();
702 helper->auto_accept_ = OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT;
704 content::LoadCommittedDetails details;
705 content::FrameNavigateParams params;
706 params.url = GURL("http://crbug.com");
707 params.transition = content::PAGE_TRANSITION_TYPED;
708 helper->DidNavigateMainFrame(details, params);
710 EXPECT_EQ(OneClickSigninHelper::AUTO_ACCEPT_NONE, helper->auto_accept_);
713 // Checks that OneClickSigninHelper doesn't stay an observer of the profile
714 // sync service after it's deleted.
715 TEST_F(OneClickSigninHelperTest, RemoveObserverFromProfileSyncService) {
716 content::WebContents* contents = web_contents();
718 ProfileSyncServiceMock* sync_service = CreateProfileSyncServiceMock();
720 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
721 OneClickSigninHelper* helper =
722 OneClickSigninHelper::FromWebContents(contents);
723 helper->SetDoNotClearPendingEmailForTesting();
725 // Need to expect two calls, because sync service also tears down observers.
726 // TODO(signin): gmock probably isn't the best solution here.
727 EXPECT_CALL(*sync_service, RemoveObserver(_));
728 EXPECT_CALL(*sync_service, RemoveObserver(helper));
729 SetContents(NULL);
732 // I/O thread tests
734 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThread) {
735 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
736 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
737 OneClickSigninHelper::CanOfferOnIOThreadImpl(
738 valid_gaia_url_, &request_, io_data.get()));
741 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadIncognito) {
742 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(true));
743 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
744 OneClickSigninHelper::CanOfferOnIOThreadImpl(
745 valid_gaia_url_, &request_, io_data.get()));
748 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoIOData) {
749 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
750 OneClickSigninHelper::CanOfferOnIOThreadImpl(
751 valid_gaia_url_, &request_, NULL));
754 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadBadURL) {
755 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
756 EXPECT_EQ(
757 OneClickSigninHelper::IGNORE_REQUEST,
758 OneClickSigninHelper::CanOfferOnIOThreadImpl(
759 GURL("https://foo.com/"), &request_, io_data.get()));
760 EXPECT_EQ(OneClickSigninHelper::IGNORE_REQUEST,
761 OneClickSigninHelper::CanOfferOnIOThreadImpl(
762 GURL("http://accounts.google.com/"),
763 &request_,
764 io_data.get()));
767 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabled) {
768 EnableOneClick(false);
769 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
770 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
771 OneClickSigninHelper::CanOfferOnIOThreadImpl(
772 valid_gaia_url_, &request_, io_data.get()));
775 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadSignedIn) {
776 PrefService* pref_service = profile()->GetPrefs();
777 pref_service->SetString(prefs::kGoogleServicesUsername, "user@gmail.com");
779 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
780 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
781 OneClickSigninHelper::CanOfferOnIOThreadImpl(
782 valid_gaia_url_, &request_, io_data.get()));
785 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailNotAllowed) {
786 SetAllowedUsernamePattern("*@example.com");
787 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
788 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
789 OneClickSigninHelper::CanOfferOnIOThreadImpl(
790 valid_gaia_url_, &request_, io_data.get()));
793 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailAlreadyUsed) {
794 ProfileInfoCache* cache = testing_profile_manager_.profile_info_cache();
795 const base::FilePath& user_data_dir = cache->GetUserDataDir();
796 cache->AddProfileToCache(user_data_dir.Append(FILE_PATH_LITERAL("user")),
797 base::UTF8ToUTF16("user"),
798 base::UTF8ToUTF16("user@gmail.com"), 0,
799 std::string());
801 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
802 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
803 OneClickSigninHelper::CanOfferOnIOThreadImpl(
804 valid_gaia_url_, &request_, io_data.get()));
807 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadWithRejectedEmail) {
808 AddEmailToOneClickRejectedList("user@gmail.com");
809 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
810 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
811 OneClickSigninHelper::CanOfferOnIOThreadImpl(
812 valid_gaia_url_, &request_, io_data.get()));
815 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoSigninCookies) {
816 AllowSigninCookies(false);
817 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
818 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
819 OneClickSigninHelper::CanOfferOnIOThreadImpl(
820 valid_gaia_url_, &request_, io_data.get()));
823 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabledByPolicy) {
824 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
825 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
826 OneClickSigninHelper::CanOfferOnIOThreadImpl(
827 valid_gaia_url_, &request_, io_data.get()));
829 // Simulate a policy disabling signin by writing kSigninAllowed directly.
830 // We should not offer to sign in the browser.
831 profile()->GetTestingPrefService()->SetManagedPref(
832 prefs::kSigninAllowed, base::Value::CreateBooleanValue(false));
833 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
834 OneClickSigninHelper::CanOfferOnIOThreadImpl(
835 valid_gaia_url_, &request_, io_data.get()));
837 // Reset the preference.
838 profile()->GetTestingPrefService()->SetManagedPref(
839 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true));
841 // Simulate a policy disabling sync by writing kSyncManaged directly.
842 // We should still offer to sign in the browser.
843 profile()->GetTestingPrefService()->SetManagedPref(
844 prefs::kSyncManaged, base::Value::CreateBooleanValue(true));
845 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
846 OneClickSigninHelper::CanOfferOnIOThreadImpl(
847 valid_gaia_url_, &request_, io_data.get()));