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_signin_manager.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
20 #include "chrome/browser/signin/signin_manager.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/browser/signin/signin_names_io_thread.h"
23 #include "chrome/browser/signin/signin_promo.h"
24 #include "chrome/browser/sync/profile_sync_service_factory.h"
25 #include "chrome/browser/sync/profile_sync_service_mock.h"
26 #include "chrome/browser/sync/test_profile_sync_service.h"
27 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
28 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
29 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
32 #include "chrome/test/base/testing_browser_process.h"
33 #include "chrome/test/base/testing_pref_service_syncable.h"
34 #include "chrome/test/base/testing_profile.h"
35 #include "chrome/test/base/testing_profile_manager.h"
36 #include "components/autofill/core/common/password_form.h"
37 #include "content/public/browser/browser_context.h"
38 #include "content/public/browser/navigation_details.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/common/frame_navigate_params.h"
41 #include "content/public/common/url_constants.h"
42 #include "content/public/test/mock_render_process_host.h"
43 #include "grit/chromium_strings.h"
44 #include "grit/generated_resources.h"
45 #include "testing/gtest/include/gtest/gtest.h"
46 #include "ui/base/l10n/l10n_util.h"
49 using ::testing::AtLeast
;
50 using ::testing::Return
;
54 class SigninManagerMock
: public FakeSigninManager
{
56 explicit SigninManagerMock(Profile
* profile
) : FakeSigninManager(profile
) {
57 Initialize(profile
, NULL
);
59 MOCK_CONST_METHOD1(IsAllowedUsername
, bool(const std::string
& username
));
62 static BrowserContextKeyedService
* BuildSigninManagerMock(
63 content::BrowserContext
* profile
) {
64 return new SigninManagerMock(static_cast<Profile
*>(profile
));
67 class TestProfileIOData
: public ProfileIOData
{
69 TestProfileIOData(bool is_incognito
, PrefService
* pref_service
,
70 PrefService
* local_state
, CookieSettings
* cookie_settings
)
71 : ProfileIOData(is_incognito
) {
72 // Initialize the IO members required for these tests, but keep them on
73 // this thread since we don't use a background thread here.
74 google_services_username()->Init(prefs::kGoogleServicesUsername
,
76 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled
,
78 one_click_signin_rejected_email_list()->Init(
79 prefs::kReverseAutologinRejectedEmailList
, pref_service
);
81 google_services_username_pattern()->Init(
82 prefs::kGoogleServicesUsernamePattern
, local_state
);
84 sync_disabled()->Init(prefs::kSyncManaged
, pref_service
);
86 signin_allowed()->Init(prefs::kSigninAllowed
, pref_service
);
88 set_signin_names_for_testing(new SigninNamesOnIOThread());
89 SetCookieSettingsForTesting(cookie_settings
);
92 virtual ~TestProfileIOData() {
93 signin_names()->ReleaseResourcesOnUIThread();
96 // ProfileIOData overrides:
97 virtual void InitializeInternal(
98 ProfileParams
* profile_params
,
99 content::ProtocolHandlerMap
* protocol_handlers
) const OVERRIDE
{
102 virtual void InitializeExtensionsRequestContext(
103 ProfileParams
* profile_params
) const OVERRIDE
{
106 virtual ChromeURLRequestContext
* InitializeAppRequestContext(
107 ChromeURLRequestContext
* main_context
,
108 const StoragePartitionDescriptor
& details
,
109 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
110 protocol_handler_interceptor
,
111 content::ProtocolHandlerMap
* protocol_handlers
) const OVERRIDE
{
115 virtual ChromeURLRequestContext
* InitializeMediaRequestContext(
116 ChromeURLRequestContext
* original_context
,
117 const StoragePartitionDescriptor
& details
) const OVERRIDE
{
121 virtual ChromeURLRequestContext
*
122 AcquireMediaRequestContext() const OVERRIDE
{
126 virtual ChromeURLRequestContext
*
127 AcquireIsolatedAppRequestContext(
128 ChromeURLRequestContext
* main_context
,
129 const StoragePartitionDescriptor
& partition_descriptor
,
130 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
131 protocol_handler_interceptor
,
132 content::ProtocolHandlerMap
* protocol_handlers
) const OVERRIDE
{
136 virtual ChromeURLRequestContext
*
137 AcquireIsolatedMediaRequestContext(
138 ChromeURLRequestContext
* app_context
,
139 const StoragePartitionDescriptor
& partition_descriptor
)
146 class TestURLRequest
: public base::SupportsUserData
{
149 virtual ~TestURLRequest() {}
152 class OneClickTestProfileSyncService
: public TestProfileSyncService
{
154 virtual ~OneClickTestProfileSyncService() {}
156 // Helper routine to be used in conjunction with
157 // BrowserContextKeyedServiceFactory::SetTestingFactory().
158 static BrowserContextKeyedService
* Build(content::BrowserContext
* profile
) {
159 return new OneClickTestProfileSyncService(static_cast<Profile
*>(profile
));
162 // Need to control this for certain tests.
163 virtual bool FirstSetupInProgress() const OVERRIDE
{
164 return first_setup_in_progress_
;
167 // Controls return value of FirstSetupInProgress. Because some bits
168 // of UI depend on that value, it's useful to control it separately
169 // from the internal work and components that are triggered (such as
170 // ReconfigureDataTypeManager) to facilitate unit tests.
171 void set_first_setup_in_progress(bool in_progress
) {
172 first_setup_in_progress_
= in_progress
;
175 // Override ProfileSyncService::Shutdown() to avoid CHECK on
176 // |invalidator_registrar_|.
177 virtual void Shutdown() OVERRIDE
{};
180 explicit OneClickTestProfileSyncService(Profile
* profile
)
181 : TestProfileSyncService(NULL
,
185 ProfileSyncService::MANUAL_START
),
186 first_setup_in_progress_(false) {}
188 bool first_setup_in_progress_
;
193 class OneClickSigninHelperTest
: public ChromeRenderViewHostTestHarness
{
195 OneClickSigninHelperTest();
197 virtual void SetUp() OVERRIDE
;
198 virtual void TearDown() OVERRIDE
;
200 // Creates the sign-in manager for tests. If |username| is
201 // is not empty, the profile of the mock WebContents will be connected to
202 // the given account.
203 void CreateSigninManager(const std::string
& username
);
205 // Set the ID of the signin process that the test will assume to be the
206 // only process allowed to sign the user in to Chrome.
207 void SetTrustedSigninProcessID(int id
);
209 void AddEmailToOneClickRejectedList(const std::string
& email
);
210 void EnableOneClick(bool enable
);
211 void AllowSigninCookies(bool enable
);
212 void SetAllowedUsernamePattern(const std::string
& pattern
);
213 ProfileSyncServiceMock
* CreateProfileSyncServiceMock();
214 void SubmitGAIAPassword(OneClickSigninHelper
* helper
);
216 SigninManagerMock
* signin_manager_
;
217 FakeProfileOAuth2TokenService
* fake_oauth2_token_service_
;
220 GoogleServiceAuthError no_error_
;
223 // ChromeRenderViewHostTestHarness overrides:
224 virtual content::BrowserContext
* CreateBrowserContext() OVERRIDE
;
226 // The ID of the signin process the test will assume to be trusted.
227 // By default, set to the test RenderProcessHost's process ID, but
228 // overridden by SetTrustedSigninProcessID.
229 int trusted_signin_process_id_
;
231 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest
);
234 OneClickSigninHelperTest::OneClickSigninHelperTest()
235 : signin_manager_(NULL
),
236 fake_oauth2_token_service_(NULL
),
237 no_error_(GoogleServiceAuthError::NONE
),
238 trusted_signin_process_id_(-1) {
241 void OneClickSigninHelperTest::SetUp() {
242 signin::ForceWebBasedSigninFlowForTesting(true);
243 content::RenderViewHostTestHarness::SetUp();
244 SetTrustedSigninProcessID(process()->GetID());
247 void OneClickSigninHelperTest::TearDown() {
248 signin::ForceWebBasedSigninFlowForTesting(false);
249 content::RenderViewHostTestHarness::TearDown();
252 void OneClickSigninHelperTest::SetTrustedSigninProcessID(int id
) {
253 trusted_signin_process_id_
= id
;
256 void OneClickSigninHelperTest::CreateSigninManager(
257 const std::string
& username
) {
258 signin_manager_
= static_cast<SigninManagerMock
*>(
259 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
260 profile(), BuildSigninManagerMock
));
262 signin_manager_
->SetSigninProcess(trusted_signin_process_id_
);
264 if (!username
.empty()) {
265 ASSERT_TRUE(signin_manager_
);
266 signin_manager_
->SetAuthenticatedUsername(username
);
270 void OneClickSigninHelperTest::EnableOneClick(bool enable
) {
271 PrefService
* pref_service
= profile()->GetPrefs();
272 pref_service
->SetBoolean(prefs::kReverseAutologinEnabled
, enable
);
275 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList(
276 const std::string
& email
) {
277 PrefService
* pref_service
= profile()->GetPrefs();
278 ListPrefUpdate
updater(pref_service
,
279 prefs::kReverseAutologinRejectedEmailList
);
280 updater
->AppendIfNotPresent(new base::StringValue(email
));
283 void OneClickSigninHelperTest::AllowSigninCookies(bool enable
) {
284 CookieSettings
* cookie_settings
=
285 CookieSettings::Factory::GetForProfile(profile()).get();
286 cookie_settings
->SetDefaultCookieSetting(enable
? CONTENT_SETTING_ALLOW
287 : CONTENT_SETTING_BLOCK
);
290 void OneClickSigninHelperTest::SetAllowedUsernamePattern(
291 const std::string
& pattern
) {
292 PrefService
* local_state
= g_browser_process
->local_state();
293 local_state
->SetString(prefs::kGoogleServicesUsernamePattern
, pattern
);
296 ProfileSyncServiceMock
*
297 OneClickSigninHelperTest::CreateProfileSyncServiceMock() {
298 ProfileSyncServiceMock
* sync_service
= static_cast<ProfileSyncServiceMock
*>(
299 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
301 ProfileSyncServiceMock::BuildMockProfileSyncService
));
302 EXPECT_CALL(*sync_service
, FirstSetupInProgress()).WillRepeatedly(
304 EXPECT_CALL(*sync_service
, sync_initialized()).WillRepeatedly(Return(true));
305 EXPECT_CALL(*sync_service
, GetAuthError()).
306 WillRepeatedly(::testing::ReturnRef(no_error_
));
307 EXPECT_CALL(*sync_service
, sync_initialized()).WillRepeatedly(Return(false));
308 sync_service
->Initialize();
312 void OneClickSigninHelperTest::SubmitGAIAPassword(
313 OneClickSigninHelper
* helper
) {
314 autofill::PasswordForm password_form
;
315 password_form
.origin
= GURL("https://accounts.google.com");
316 password_form
.signon_realm
= "https://accounts.google.com";
317 password_form
.password_value
= base::UTF8ToUTF16("password");
318 helper
->PasswordSubmitted(password_form
);
321 content::BrowserContext
* OneClickSigninHelperTest::CreateBrowserContext() {
322 TestingProfile::Builder builder
;
323 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
324 FakeProfileOAuth2TokenService::Build
);
325 scoped_ptr
<TestingProfile
> profile
= builder
.Build();
327 fake_oauth2_token_service_
=
328 static_cast<FakeProfileOAuth2TokenService
*>(
329 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
.get()));
331 return profile
.release();
334 class OneClickSigninHelperIOTest
: public OneClickSigninHelperTest
{
336 OneClickSigninHelperIOTest();
338 virtual void SetUp() OVERRIDE
;
340 TestProfileIOData
* CreateTestProfileIOData(bool is_incognito
);
343 TestingProfileManager testing_profile_manager_
;
344 TestURLRequest request_
;
345 const GURL valid_gaia_url_
;
348 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperIOTest
);
351 OneClickSigninHelperIOTest::OneClickSigninHelperIOTest()
352 : testing_profile_manager_(
353 TestingBrowserProcess::GetGlobal()),
354 valid_gaia_url_("https://accounts.google.com/") {
357 void OneClickSigninHelperIOTest::SetUp() {
358 OneClickSigninHelperTest::SetUp();
359 ASSERT_TRUE(testing_profile_manager_
.SetUp());
362 TestProfileIOData
* OneClickSigninHelperIOTest::CreateTestProfileIOData(
364 PrefService
* pref_service
= profile()->GetPrefs();
365 PrefService
* local_state
= g_browser_process
->local_state();
366 CookieSettings
* cookie_settings
=
367 CookieSettings::Factory::GetForProfile(profile()).get();
368 TestProfileIOData
* io_data
= new TestProfileIOData(
369 is_incognito
, pref_service
, local_state
, cookie_settings
);
370 io_data
->set_reverse_autologin_pending_email("user@gmail.com");
374 class OneClickSigninHelperIncognitoTest
: public OneClickSigninHelperTest
{
376 // content::RenderViewHostTestHarness.
377 virtual content::BrowserContext
* CreateBrowserContext() OVERRIDE
;
380 content::BrowserContext
*
381 OneClickSigninHelperIncognitoTest::CreateBrowserContext() {
382 // Builds an incognito profile to run this test.
383 TestingProfile::Builder builder
;
384 builder
.SetIncognito();
385 return builder
.Build().release();
388 TEST_F(OneClickSigninHelperTest
, CanOfferNoContents
) {
389 std::string error_message
;
390 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
391 NULL
, OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
392 "user@gmail.com", &error_message
));
393 EXPECT_EQ("", error_message
);
394 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
395 NULL
, OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
396 "user@gmail.com", &error_message
));
397 EXPECT_EQ("", error_message
);
398 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
400 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
403 EXPECT_EQ("", error_message
);
406 TEST_F(OneClickSigninHelperTest
, CanOffer
) {
407 CreateSigninManager(std::string());
409 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
410 WillRepeatedly(Return(true));
412 EnableOneClick(true);
413 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
414 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
415 "user@gmail.com", NULL
));
416 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
417 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
418 "user@gmail.com", NULL
));
419 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
421 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
425 EnableOneClick(false);
427 std::string error_message
;
428 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
429 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
430 "user@gmail.com", &error_message
));
431 EXPECT_EQ("", error_message
);
433 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
434 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
435 "user@gmail.com", &error_message
));
436 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
438 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
441 EXPECT_EQ("", error_message
);
444 TEST_F(OneClickSigninHelperTest
, CanOfferFirstSetup
) {
445 CreateSigninManager(std::string());
447 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
448 WillRepeatedly(Return(true));
450 // Invoke OneClickTestProfileSyncService factory function and grab result.
451 OneClickTestProfileSyncService
* sync
=
452 static_cast<OneClickTestProfileSyncService
*>(
453 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
454 static_cast<Profile
*>(browser_context()),
455 OneClickTestProfileSyncService::Build
));
457 sync
->set_first_setup_in_progress(true);
459 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
461 OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
462 "foo@gmail.com", NULL
));
463 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
465 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
466 "foo@gmail.com", NULL
));
467 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
469 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
474 TEST_F(OneClickSigninHelperTest
, CanOfferProfileConnected
) {
475 CreateSigninManager("foo@gmail.com");
477 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
478 WillRepeatedly(Return(true));
480 std::string error_message
;
481 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
482 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
483 "foo@gmail.com", &error_message
));
484 EXPECT_EQ("", error_message
);
485 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
486 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
487 "foo", &error_message
));
488 EXPECT_EQ("", error_message
);
489 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
490 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
491 "user@gmail.com", &error_message
));
492 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL
,
493 base::UTF8ToUTF16("foo@gmail.com")),
495 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
496 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
497 "foo@gmail.com", &error_message
));
498 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
499 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
500 "foo", &error_message
));
501 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
502 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
503 "user@gmail.com", &error_message
));
504 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL
,
505 base::UTF8ToUTF16("foo@gmail.com")),
507 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
509 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
514 TEST_F(OneClickSigninHelperTest
, CanOfferUsernameNotAllowed
) {
515 CreateSigninManager(std::string());
517 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
518 WillRepeatedly(Return(false));
520 std::string error_message
;
521 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
522 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
523 "foo@gmail.com", &error_message
));
524 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED
),
526 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
527 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
528 "foo@gmail.com", &error_message
));
529 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED
),
531 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
533 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
538 TEST_F(OneClickSigninHelperTest
, CanOfferWithRejectedEmail
) {
539 CreateSigninManager(std::string());
541 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
542 WillRepeatedly(Return(true));
544 AddEmailToOneClickRejectedList("foo@gmail.com");
545 AddEmailToOneClickRejectedList("user@gmail.com");
547 std::string error_message
;
548 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
549 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
550 "foo@gmail.com", &error_message
));
551 EXPECT_EQ("", error_message
);
552 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
553 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
554 "user@gmail.com", &error_message
));
555 EXPECT_EQ("", error_message
);
556 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
557 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
558 "foo@gmail.com", &error_message
));
559 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
560 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
561 "user@gmail.com", &error_message
));
562 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
563 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
564 "john@gmail.com", &error_message
));
567 TEST_F(OneClickSigninHelperIncognitoTest
, CanOfferIncognito
) {
568 CreateSigninManager(std::string());
570 std::string error_message
;
571 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
572 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
573 "user@gmail.com", &error_message
));
574 EXPECT_EQ("", error_message
);
575 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
576 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
577 "user@gmail.com", &error_message
));
578 EXPECT_EQ("", error_message
);
579 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
581 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
584 EXPECT_EQ("", error_message
);
587 TEST_F(OneClickSigninHelperTest
, CanOfferNoSigninCookies
) {
588 CreateSigninManager(std::string());
589 AllowSigninCookies(false);
591 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
592 WillRepeatedly(Return(true));
594 std::string error_message
;
595 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
596 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
597 "user@gmail.com", &error_message
));
598 EXPECT_EQ("", error_message
);
599 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
600 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
601 "user@gmail.com", &error_message
));
602 EXPECT_EQ("", error_message
);
603 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
605 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
608 EXPECT_EQ("", error_message
);
611 TEST_F(OneClickSigninHelperTest
, CanOfferDisabledByPolicy
) {
612 CreateSigninManager(std::string());
614 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
)).
615 WillRepeatedly(Return(true));
617 EnableOneClick(true);
618 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
619 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
620 "user@gmail.com", NULL
));
622 // Simulate a policy disabling signin by writing kSigninAllowed directly.
623 profile()->GetTestingPrefService()->SetManagedPref(
624 prefs::kSigninAllowed
, base::Value::CreateBooleanValue(false));
626 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
627 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
628 "user@gmail.com", NULL
));
630 // Reset the preference value to true.
631 profile()->GetTestingPrefService()->SetManagedPref(
632 prefs::kSigninAllowed
, base::Value::CreateBooleanValue(true));
634 // Simulate a policy disabling sync by writing kSyncManaged directly.
635 profile()->GetTestingPrefService()->SetManagedPref(
636 prefs::kSyncManaged
, base::Value::CreateBooleanValue(true));
638 // Should still offer even if sync is disabled by policy.
639 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
640 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL
,
641 "user@gmail.com", NULL
));
644 // Should not crash if a helper instance is not associated with an incognito
646 TEST_F(OneClickSigninHelperIncognitoTest
, ShowInfoBarUIThreadIncognito
) {
647 CreateSigninManager(std::string());
648 OneClickSigninHelper
* helper
=
649 OneClickSigninHelper::FromWebContents(web_contents());
650 EXPECT_EQ(NULL
, helper
);
652 OneClickSigninHelper::ShowInfoBarUIThread(
653 "session_index", "email", OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED
,
654 signin::SOURCE_UNKNOWN
, GURL(), process()->GetID(),
655 rvh()->GetRoutingID());
658 // If Chrome signin is triggered from a webstore install, and user chooses to
659 // config sync, then Chrome should redirect immediately to sync settings page,
660 // and upon successful setup, redirect back to webstore.
661 TEST_F(OneClickSigninHelperTest
, SigninFromWebstoreWithConfigSyncfirst
) {
662 CreateSigninManager(std::string());
663 EXPECT_CALL(*signin_manager_
, IsAllowedUsername(_
))
664 .WillRepeatedly(Return(true));
666 ProfileSyncServiceMock
* sync_service
= CreateProfileSyncServiceMock();
667 EXPECT_CALL(*sync_service
, AddObserver(_
)).Times(AtLeast(1));
668 EXPECT_CALL(*sync_service
, RemoveObserver(_
)).Times(AtLeast(1));
669 EXPECT_CALL(*sync_service
, sync_initialized()).WillRepeatedly(Return(true));
671 content::WebContents
* contents
= web_contents();
673 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents
, NULL
);
674 OneClickSigninHelper
* helper
=
675 OneClickSigninHelper::FromWebContents(contents
);
676 helper
->SetDoNotClearPendingEmailForTesting();
677 helper
->set_do_not_start_sync_for_testing();
679 GURL
continueUrl("https://chrome.google.com/webstore?source=5");
680 OneClickSigninHelper::ShowInfoBarUIThread(
681 "session_index", "user@gmail.com",
682 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT
,
683 signin::SOURCE_WEBSTORE_INSTALL
,
684 continueUrl
, process()->GetID(), rvh()->GetRoutingID());
686 SubmitGAIAPassword(helper
);
688 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3"));
689 helper
->DidStopLoading(rvh());
690 helper
->OnStateChanged();
691 EXPECT_EQ(GURL(continueUrl
), contents
->GetURL());
694 // Checks that the state of OneClickSigninHelper is cleaned when there is a
695 // navigation away from the sign in flow that is not triggered by the
697 TEST_F(OneClickSigninHelperTest
, CleanTransientStateOnNavigate
) {
698 content::WebContents
* contents
= web_contents();
700 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents
, NULL
);
701 OneClickSigninHelper
* helper
=
702 OneClickSigninHelper::FromWebContents(contents
);
703 helper
->SetDoNotClearPendingEmailForTesting();
704 helper
->auto_accept_
= OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT
;
706 content::LoadCommittedDetails details
;
707 content::FrameNavigateParams params
;
708 params
.url
= GURL("http://crbug.com");
709 params
.transition
= content::PAGE_TRANSITION_TYPED
;
710 helper
->DidNavigateMainFrame(details
, params
);
712 EXPECT_EQ(OneClickSigninHelper::AUTO_ACCEPT_NONE
, helper
->auto_accept_
);
715 // Checks that OneClickSigninHelper doesn't stay an observer of the profile
716 // sync service after it's deleted.
717 TEST_F(OneClickSigninHelperTest
, RemoveObserverFromProfileSyncService
) {
718 content::WebContents
* contents
= web_contents();
720 ProfileSyncServiceMock
* sync_service
= CreateProfileSyncServiceMock();
722 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents
, NULL
);
723 OneClickSigninHelper
* helper
=
724 OneClickSigninHelper::FromWebContents(contents
);
725 helper
->SetDoNotClearPendingEmailForTesting();
727 EXPECT_CALL(*sync_service
, RemoveObserver(helper
));
733 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThread
) {
734 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
735 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER
,
736 OneClickSigninHelper::CanOfferOnIOThreadImpl(
737 valid_gaia_url_
, &request_
, io_data
.get()));
740 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadIncognito
) {
741 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(true));
742 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
743 OneClickSigninHelper::CanOfferOnIOThreadImpl(
744 valid_gaia_url_
, &request_
, io_data
.get()));
747 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadNoIOData
) {
748 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
749 OneClickSigninHelper::CanOfferOnIOThreadImpl(
750 valid_gaia_url_
, &request_
, NULL
));
753 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadBadURL
) {
754 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
756 OneClickSigninHelper::IGNORE_REQUEST
,
757 OneClickSigninHelper::CanOfferOnIOThreadImpl(
758 GURL("https://foo.com/"), &request_
, io_data
.get()));
759 EXPECT_EQ(OneClickSigninHelper::IGNORE_REQUEST
,
760 OneClickSigninHelper::CanOfferOnIOThreadImpl(
761 GURL("http://accounts.google.com/"),
766 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadDisabled
) {
767 EnableOneClick(false);
768 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
769 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
770 OneClickSigninHelper::CanOfferOnIOThreadImpl(
771 valid_gaia_url_
, &request_
, io_data
.get()));
774 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadSignedIn
) {
775 PrefService
* pref_service
= profile()->GetPrefs();
776 pref_service
->SetString(prefs::kGoogleServicesUsername
, "user@gmail.com");
778 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
779 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
780 OneClickSigninHelper::CanOfferOnIOThreadImpl(
781 valid_gaia_url_
, &request_
, io_data
.get()));
784 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadEmailNotAllowed
) {
785 SetAllowedUsernamePattern("*@example.com");
786 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
787 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
788 OneClickSigninHelper::CanOfferOnIOThreadImpl(
789 valid_gaia_url_
, &request_
, io_data
.get()));
792 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadEmailAlreadyUsed
) {
793 ProfileInfoCache
* cache
= testing_profile_manager_
.profile_info_cache();
794 const base::FilePath
& user_data_dir
= cache
->GetUserDataDir();
795 cache
->AddProfileToCache(user_data_dir
.Append(FILE_PATH_LITERAL("user")),
796 base::UTF8ToUTF16("user"),
797 base::UTF8ToUTF16("user@gmail.com"), 0,
800 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
801 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
802 OneClickSigninHelper::CanOfferOnIOThreadImpl(
803 valid_gaia_url_
, &request_
, io_data
.get()));
806 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadWithRejectedEmail
) {
807 AddEmailToOneClickRejectedList("user@gmail.com");
808 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
809 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
810 OneClickSigninHelper::CanOfferOnIOThreadImpl(
811 valid_gaia_url_
, &request_
, io_data
.get()));
814 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadNoSigninCookies
) {
815 AllowSigninCookies(false);
816 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
817 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
818 OneClickSigninHelper::CanOfferOnIOThreadImpl(
819 valid_gaia_url_
, &request_
, io_data
.get()));
822 TEST_F(OneClickSigninHelperIOTest
, CanOfferOnIOThreadDisabledByPolicy
) {
823 scoped_ptr
<TestProfileIOData
> io_data(CreateTestProfileIOData(false));
824 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER
,
825 OneClickSigninHelper::CanOfferOnIOThreadImpl(
826 valid_gaia_url_
, &request_
, io_data
.get()));
828 // Simulate a policy disabling signin by writing kSigninAllowed directly.
829 // We should not offer to sign in the browser.
830 profile()->GetTestingPrefService()->SetManagedPref(
831 prefs::kSigninAllowed
, base::Value::CreateBooleanValue(false));
832 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER
,
833 OneClickSigninHelper::CanOfferOnIOThreadImpl(
834 valid_gaia_url_
, &request_
, io_data
.get()));
836 // Reset the preference.
837 profile()->GetTestingPrefService()->SetManagedPref(
838 prefs::kSigninAllowed
, base::Value::CreateBooleanValue(true));
840 // Simulate a policy disabling sync by writing kSyncManaged directly.
841 // We should still offer to sign in the browser.
842 profile()->GetTestingPrefService()->SetManagedPref(
843 prefs::kSyncManaged
, base::Value::CreateBooleanValue(true));
844 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER
,
845 OneClickSigninHelper::CanOfferOnIOThreadImpl(
846 valid_gaia_url_
, &request_
, io_data
.get()));