Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / ui / sync / one_click_signin_helper_unittest.cc
bloba0b1d03207f9d9a5f867a4b6ae7caa94c69d4c0c
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/strings/utf_string_conversions.h"
7 #include "base/values.h"
8 #include "chrome/browser/content_settings/cookie_settings.h"
9 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
10 #include "chrome/browser/prefs/scoped_user_pref_update.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_io_data.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/signin/fake_signin_manager.h"
16 #include "chrome/browser/signin/signin_manager.h"
17 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/signin/signin_names_io_thread.h"
19 #include "chrome/browser/signin/signin_promo.h"
20 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/sync/profile_sync_service_mock.h"
22 #include "chrome/browser/sync/test_profile_sync_service.h"
23 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
26 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/testing_pref_service_syncable.h"
28 #include "chrome/test/base/testing_profile.h"
29 #include "chrome/test/base/testing_profile_manager.h"
30 #include "content/public/browser/browser_context.h"
31 #include "content/public/browser/navigation_details.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/frame_navigate_params.h"
34 #include "content/public/common/password_form.h"
35 #include "content/public/common/url_constants.h"
36 #include "content/public/test/mock_render_process_host.h"
37 #include "grit/chromium_strings.h"
38 #include "grit/generated_resources.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "ui/base/l10n/l10n_util.h"
42 using ::testing::_;
43 using ::testing::AtLeast;
44 using ::testing::Return;
46 namespace {
48 // Explicit URLs are sign in URLs created by chrome for specific sign in access
49 // points. Implicit URLs are those to sign for some Google service, like gmail
50 // or drive. In former case, with a valid URL, we don't want to offer the
51 // interstitial. In all other cases we do.
53 const char kImplicitURLString[] =
54 "https://accounts.google.com/ServiceLogin"
55 "?service=foo&continue=http://foo.google.com";
57 class SigninManagerMock : public FakeSigninManager {
58 public:
59 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) {
60 Initialize(profile, NULL);
62 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username));
65 class TestProfileIOData : public ProfileIOData {
66 public:
67 TestProfileIOData(bool is_incognito, PrefService* pref_service,
68 PrefService* local_state, CookieSettings* cookie_settings)
69 : ProfileIOData(is_incognito) {
70 // Initialize the IO members required for these tests, but keep them on
71 // this thread since we don't use a background thread here.
72 google_services_username()->Init(prefs::kGoogleServicesUsername,
73 pref_service);
74 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled,
75 pref_service);
76 one_click_signin_rejected_email_list()->Init(
77 prefs::kReverseAutologinRejectedEmailList, pref_service);
79 google_services_username_pattern()->Init(
80 prefs::kGoogleServicesUsernamePattern, local_state);
82 sync_disabled()->Init(prefs::kSyncManaged, pref_service);
84 signin_allowed()->Init(prefs::kSigninAllowed, pref_service);
86 set_signin_names_for_testing(new SigninNamesOnIOThread());
87 SetCookieSettingsForTesting(cookie_settings);
90 virtual ~TestProfileIOData() {
91 signin_names()->ReleaseResourcesOnUIThread();
94 // ProfileIOData overrides:
95 virtual void InitializeInternal(
96 ProfileParams* profile_params,
97 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE {
98 NOTREACHED();
100 virtual void InitializeExtensionsRequestContext(
101 ProfileParams* profile_params) const OVERRIDE {
102 NOTREACHED();
104 virtual ChromeURLRequestContext* InitializeAppRequestContext(
105 ChromeURLRequestContext* main_context,
106 const StoragePartitionDescriptor& details,
107 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
108 protocol_handler_interceptor,
109 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE {
110 NOTREACHED();
111 return NULL;
113 virtual ChromeURLRequestContext* InitializeMediaRequestContext(
114 ChromeURLRequestContext* original_context,
115 const StoragePartitionDescriptor& details) const OVERRIDE {
116 NOTREACHED();
117 return NULL;
119 virtual ChromeURLRequestContext*
120 AcquireMediaRequestContext() const OVERRIDE {
121 NOTREACHED();
122 return NULL;
124 virtual ChromeURLRequestContext*
125 AcquireIsolatedAppRequestContext(
126 ChromeURLRequestContext* main_context,
127 const StoragePartitionDescriptor& partition_descriptor,
128 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
129 protocol_handler_interceptor,
130 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE {
131 NOTREACHED();
132 return NULL;
134 virtual ChromeURLRequestContext*
135 AcquireIsolatedMediaRequestContext(
136 ChromeURLRequestContext* app_context,
137 const StoragePartitionDescriptor& partition_descriptor)
138 const OVERRIDE {
139 NOTREACHED();
140 return NULL;
142 virtual chrome_browser_net::LoadTimeStats* GetLoadTimeStats(
143 IOThread::Globals* io_thread_globals) const OVERRIDE {
144 NOTREACHED();
145 return NULL;
149 class TestURLRequest : public base::SupportsUserData {
150 public:
151 TestURLRequest() {}
152 virtual ~TestURLRequest() {}
155 class OneClickTestProfileSyncService : public TestProfileSyncService {
156 public:
157 virtual ~OneClickTestProfileSyncService() {}
159 // Helper routine to be used in conjunction with
160 // BrowserContextKeyedServiceFactory::SetTestingFactory().
161 static BrowserContextKeyedService* Build(content::BrowserContext* profile) {
162 return new OneClickTestProfileSyncService(static_cast<Profile*>(profile));
165 // Need to control this for certain tests.
166 virtual bool FirstSetupInProgress() const OVERRIDE {
167 return first_setup_in_progress_;
170 // Controls return value of FirstSetupInProgress. Because some bits
171 // of UI depend on that value, it's useful to control it separately
172 // from the internal work and components that are triggered (such as
173 // ReconfigureDataTypeManager) to facilitate unit tests.
174 void set_first_setup_in_progress(bool in_progress) {
175 first_setup_in_progress_ = in_progress;
178 // Override ProfileSyncService::Shutdown() to avoid CHECK on
179 // |invalidator_registrar_|.
180 virtual void Shutdown() OVERRIDE {};
182 private:
183 explicit OneClickTestProfileSyncService(Profile* profile)
184 : TestProfileSyncService(NULL,
185 profile,
186 NULL,
187 ProfileSyncService::MANUAL_START,
188 false), // synchronous_backend_init
189 first_setup_in_progress_(false) {}
191 bool first_setup_in_progress_;
194 static BrowserContextKeyedService* BuildSigninManagerMock(
195 content::BrowserContext* profile) {
196 return new SigninManagerMock(static_cast<Profile*>(profile));
199 } // namespace
201 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness {
202 public:
203 OneClickSigninHelperTest();
205 virtual void SetUp() OVERRIDE;
206 virtual void TearDown() OVERRIDE;
208 // Creates the sign-in manager for tests. If |username| is
209 // is not empty, the profile of the mock WebContents will be connected to
210 // the given account.
211 void CreateSigninManager(const std::string& username);
213 // Set the ID of the signin process that the test will assume to be the
214 // only process allowed to sign the user in to Chrome.
215 void SetTrustedSigninProcessID(int id);
217 void AddEmailToOneClickRejectedList(const std::string& email);
218 void EnableOneClick(bool enable);
219 void AllowSigninCookies(bool enable);
220 void SetAllowedUsernamePattern(const std::string& pattern);
221 ProfileSyncServiceMock* CreateProfileSyncServiceMock();
222 void SubmitGAIAPassword(OneClickSigninHelper* helper);
223 OneClickSigninHelper* SetupHelperForSignin();
225 SigninManagerMock* signin_manager_;
227 protected:
228 GoogleServiceAuthError no_error_;
230 private:
231 // The ID of the signin process the test will assume to be trusted.
232 // By default, set to the test RenderProcessHost's process ID, but
233 // overridden by SetTrustedSigninProcessID.
234 int trusted_signin_process_id_;
236 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest);
239 OneClickSigninHelperTest::OneClickSigninHelperTest()
240 : no_error_(GoogleServiceAuthError::NONE),
241 trusted_signin_process_id_(-1) {
244 void OneClickSigninHelperTest::SetUp() {
245 signin::ForceWebBasedSigninFlowForTesting(true);
246 content::RenderViewHostTestHarness::SetUp();
247 SetTrustedSigninProcessID(process()->GetID());
250 void OneClickSigninHelperTest::TearDown() {
251 signin::ForceWebBasedSigninFlowForTesting(false);
252 content::RenderViewHostTestHarness::TearDown();
255 void OneClickSigninHelperTest::SetTrustedSigninProcessID(int id) {
256 trusted_signin_process_id_ = id;
259 void OneClickSigninHelperTest::CreateSigninManager(
260 const std::string& username) {
261 signin_manager_ = static_cast<SigninManagerMock*>(
262 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
263 profile(), BuildSigninManagerMock));
264 if (signin_manager_)
265 signin_manager_->SetSigninProcess(trusted_signin_process_id_);
267 if (!username.empty()) {
268 ASSERT_TRUE(signin_manager_);
269 signin_manager_->SetAuthenticatedUsername(username);
273 void OneClickSigninHelperTest::EnableOneClick(bool enable) {
274 PrefService* pref_service = profile()->GetPrefs();
275 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable);
278 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList(
279 const std::string& email) {
280 PrefService* pref_service = profile()->GetPrefs();
281 ListPrefUpdate updater(pref_service,
282 prefs::kReverseAutologinRejectedEmailList);
283 updater->AppendIfNotPresent(new base::StringValue(email));
286 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) {
287 CookieSettings* cookie_settings =
288 CookieSettings::Factory::GetForProfile(profile()).get();
289 cookie_settings->SetDefaultCookieSetting(enable ? CONTENT_SETTING_ALLOW
290 : CONTENT_SETTING_BLOCK);
293 void OneClickSigninHelperTest::SetAllowedUsernamePattern(
294 const std::string& pattern) {
295 PrefService* local_state = g_browser_process->local_state();
296 local_state->SetString(prefs::kGoogleServicesUsernamePattern, pattern);
299 ProfileSyncServiceMock*
300 OneClickSigninHelperTest::CreateProfileSyncServiceMock() {
301 ProfileSyncServiceMock* sync_service = static_cast<ProfileSyncServiceMock*>(
302 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
303 profile(),
304 ProfileSyncServiceMock::BuildMockProfileSyncService));
305 EXPECT_CALL(*sync_service, FirstSetupInProgress()).WillRepeatedly(
306 Return(false));
307 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true));
308 EXPECT_CALL(*sync_service, GetAuthError()).
309 WillRepeatedly(::testing::ReturnRef(no_error_));
310 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(false));
311 sync_service->Initialize();
312 return sync_service;
315 void OneClickSigninHelperTest::SubmitGAIAPassword(
316 OneClickSigninHelper* helper) {
317 content::PasswordForm password_form;
318 password_form.origin = GURL("https://accounts.google.com");
319 password_form.signon_realm = "https://accounts.google.com";
320 password_form.password_value = UTF8ToUTF16("password");
321 helper->PasswordSubmitted(password_form);
324 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest {
325 public:
326 OneClickSigninHelperIOTest();
328 virtual void SetUp() OVERRIDE;
330 TestProfileIOData* CreateTestProfileIOData(bool is_incognito);
332 protected:
333 TestingProfileManager testing_profile_manager_;
334 TestURLRequest request_;
335 const GURL valid_gaia_url_;
337 private:
338 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperIOTest);
341 OneClickSigninHelperIOTest::OneClickSigninHelperIOTest()
342 : testing_profile_manager_(
343 TestingBrowserProcess::GetGlobal()),
344 valid_gaia_url_("https://accounts.google.com/") {
347 void OneClickSigninHelperIOTest::SetUp() {
348 OneClickSigninHelperTest::SetUp();
349 ASSERT_TRUE(testing_profile_manager_.SetUp());
352 TestProfileIOData* OneClickSigninHelperIOTest::CreateTestProfileIOData(
353 bool is_incognito) {
354 PrefService* pref_service = profile()->GetPrefs();
355 PrefService* local_state = g_browser_process->local_state();
356 CookieSettings* cookie_settings =
357 CookieSettings::Factory::GetForProfile(profile()).get();
358 TestProfileIOData* io_data = new TestProfileIOData(
359 is_incognito, pref_service, local_state, cookie_settings);
360 io_data->set_reverse_autologin_pending_email("user@gmail.com");
361 return io_data;
364 class OneClickSigninHelperIncognitoTest : public OneClickSigninHelperTest {
365 protected:
366 // content::RenderViewHostTestHarness.
367 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE;
370 content::BrowserContext*
371 OneClickSigninHelperIncognitoTest::CreateBrowserContext() {
372 // Builds an incognito profile to run this test.
373 TestingProfile::Builder builder;
374 builder.SetIncognito();
375 return builder.Build().release();
378 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) {
379 std::string error_message;
380 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
381 NULL, OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
382 "user@gmail.com", &error_message));
383 EXPECT_EQ("", error_message);
384 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
385 NULL, OneClickSigninHelper::CAN_OFFER_FOR_ALL,
386 "user@gmail.com", &error_message));
387 EXPECT_EQ("", error_message);
388 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
389 NULL,
390 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
391 std::string(),
392 &error_message));
393 EXPECT_EQ("", error_message);
396 TEST_F(OneClickSigninHelperTest, CanOffer) {
397 CreateSigninManager(std::string());
399 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
400 WillRepeatedly(Return(true));
402 EnableOneClick(true);
403 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
404 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
405 "user@gmail.com", NULL));
406 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
407 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
408 "user@gmail.com", NULL));
409 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
410 web_contents(),
411 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
412 std::string(),
413 NULL));
415 EnableOneClick(false);
417 std::string error_message;
418 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
419 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
420 "user@gmail.com", &error_message));
421 EXPECT_EQ("", error_message);
423 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
424 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
425 "user@gmail.com", &error_message));
426 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
427 web_contents(),
428 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
429 std::string(),
430 &error_message));
431 EXPECT_EQ("", error_message);
434 TEST_F(OneClickSigninHelperTest, CanOfferFirstSetup) {
435 CreateSigninManager(std::string());
437 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
438 WillRepeatedly(Return(true));
440 // Invoke OneClickTestProfileSyncService factory function and grab result.
441 OneClickTestProfileSyncService* sync =
442 static_cast<OneClickTestProfileSyncService*>(
443 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
444 static_cast<Profile*>(browser_context()),
445 OneClickTestProfileSyncService::Build));
447 sync->set_first_setup_in_progress(true);
449 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
450 web_contents(),
451 OneClickSigninHelper::CAN_OFFER_FOR_ALL,
452 "foo@gmail.com", NULL));
453 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
454 web_contents(),
455 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
456 "foo@gmail.com", NULL));
457 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
458 web_contents(),
459 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
460 std::string(),
461 NULL));
464 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) {
465 CreateSigninManager("foo@gmail.com");
467 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
468 WillRepeatedly(Return(true));
470 std::string error_message;
471 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
472 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
473 "foo@gmail.com", &error_message));
474 EXPECT_EQ("", error_message);
475 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
476 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
477 "foo", &error_message));
478 EXPECT_EQ("", error_message);
479 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
480 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
481 "user@gmail.com", &error_message));
482 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
483 UTF8ToUTF16("foo@gmail.com")),
484 error_message);
485 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
486 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
487 "foo@gmail.com", &error_message));
488 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
489 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
490 "foo", &error_message));
491 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
492 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
493 "user@gmail.com", &error_message));
494 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
495 UTF8ToUTF16("foo@gmail.com")),
496 error_message);
497 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
498 web_contents(),
499 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
500 std::string(),
501 &error_message));
504 TEST_F(OneClickSigninHelperTest, CanOfferUsernameNotAllowed) {
505 CreateSigninManager(std::string());
507 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
508 WillRepeatedly(Return(false));
510 std::string error_message;
511 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
512 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
513 "foo@gmail.com", &error_message));
514 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
515 error_message);
516 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
517 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
518 "foo@gmail.com", &error_message));
519 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
520 error_message);
521 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
522 web_contents(),
523 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
524 std::string(),
525 &error_message));
528 TEST_F(OneClickSigninHelperTest, CanOfferWithRejectedEmail) {
529 CreateSigninManager(std::string());
531 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
532 WillRepeatedly(Return(true));
534 AddEmailToOneClickRejectedList("foo@gmail.com");
535 AddEmailToOneClickRejectedList("user@gmail.com");
537 std::string error_message;
538 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
539 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
540 "foo@gmail.com", &error_message));
541 EXPECT_EQ("", error_message);
542 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
543 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
544 "user@gmail.com", &error_message));
545 EXPECT_EQ("", error_message);
546 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
547 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
548 "foo@gmail.com", &error_message));
549 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
550 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
551 "user@gmail.com", &error_message));
552 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
553 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
554 "john@gmail.com", &error_message));
557 TEST_F(OneClickSigninHelperIncognitoTest, CanOfferIncognito) {
558 CreateSigninManager(std::string());
560 std::string error_message;
561 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
562 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
563 "user@gmail.com", &error_message));
564 EXPECT_EQ("", error_message);
565 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
566 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
567 "user@gmail.com", &error_message));
568 EXPECT_EQ("", error_message);
569 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
570 web_contents(),
571 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
572 std::string(),
573 &error_message));
574 EXPECT_EQ("", error_message);
577 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) {
578 CreateSigninManager(std::string());
579 AllowSigninCookies(false);
581 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
582 WillRepeatedly(Return(true));
584 std::string error_message;
585 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
586 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
587 "user@gmail.com", &error_message));
588 EXPECT_EQ("", error_message);
589 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
590 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
591 "user@gmail.com", &error_message));
592 EXPECT_EQ("", error_message);
593 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
594 web_contents(),
595 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
596 std::string(),
597 &error_message));
598 EXPECT_EQ("", error_message);
601 TEST_F(OneClickSigninHelperTest, CanOfferDisabledByPolicy) {
602 CreateSigninManager(std::string());
604 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
605 WillRepeatedly(Return(true));
607 EnableOneClick(true);
608 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
609 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
610 "user@gmail.com", NULL));
612 // Simulate a policy disabling signin by writing kSigninAllowed directly.
613 profile()->GetTestingPrefService()->SetManagedPref(
614 prefs::kSigninAllowed, base::Value::CreateBooleanValue(false));
616 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
617 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
618 "user@gmail.com", NULL));
620 // Reset the preference value to true.
621 profile()->GetTestingPrefService()->SetManagedPref(
622 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true));
624 // Simulate a policy disabling sync by writing kSyncManaged directly.
625 profile()->GetTestingPrefService()->SetManagedPref(
626 prefs::kSyncManaged, base::Value::CreateBooleanValue(true));
628 // Should still offer even if sync is disabled by policy.
629 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
630 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
631 "user@gmail.com", NULL));
634 // Should not crash if a helper instance is not associated with an incognito
635 // web contents.
636 TEST_F(OneClickSigninHelperIncognitoTest, ShowInfoBarUIThreadIncognito) {
637 CreateSigninManager(std::string());
638 OneClickSigninHelper* helper =
639 OneClickSigninHelper::FromWebContents(web_contents());
640 EXPECT_EQ(NULL, helper);
642 OneClickSigninHelper::ShowInfoBarUIThread(
643 "session_index", "email", OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED,
644 signin::SOURCE_UNKNOWN, GURL(), process()->GetID(),
645 rvh()->GetRoutingID());
648 // If Chrome signin is triggered from a webstore install, and user chooses to
649 // config sync, then Chrome should redirect immediately to sync settings page,
650 // and upon successful setup, redirect back to webstore.
651 TEST_F(OneClickSigninHelperTest, SigninFromWebstoreWithConfigSyncfirst) {
652 CreateSigninManager(std::string());
653 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_))
654 .WillRepeatedly(Return(true));
656 ProfileSyncServiceMock* sync_service = CreateProfileSyncServiceMock();
657 EXPECT_CALL(*sync_service, SetSetupInProgress(true));
658 EXPECT_CALL(*sync_service, AddObserver(_)).Times(AtLeast(1));
659 EXPECT_CALL(*sync_service, RemoveObserver(_)).Times(AtLeast(1));
660 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true));
662 content::WebContents* contents = web_contents();
664 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
665 OneClickSigninHelper* helper =
666 OneClickSigninHelper::FromWebContents(contents);
667 helper->SetDoNotClearPendingEmailForTesting();
669 GURL continueUrl("https://chrome.google.com/webstore?source=5");
670 OneClickSigninHelper::ShowInfoBarUIThread(
671 "session_index", "user@gmail.com",
672 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT,
673 signin::SOURCE_WEBSTORE_INSTALL,
674 continueUrl, process()->GetID(), rvh()->GetRoutingID());
676 SubmitGAIAPassword(helper);
678 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3"));
679 helper->DidStopLoading(rvh());
681 helper->OnStateChanged();
682 EXPECT_EQ(GURL(continueUrl), contents->GetURL());
683 EXPECT_EQ("user@gmail.com", signin_manager_->GetAuthenticatedUsername());
686 // Checks that the state of OneClickSigninHelper is cleaned when there is a
687 // navigation away from the sign in flow that is not triggered by the
688 // web contents.
689 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) {
690 content::WebContents* contents = web_contents();
692 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
693 OneClickSigninHelper* helper =
694 OneClickSigninHelper::FromWebContents(contents);
695 helper->SetDoNotClearPendingEmailForTesting();
696 helper->auto_accept_ = OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT;
698 content::LoadCommittedDetails details;
699 content::FrameNavigateParams params;
700 params.url = GURL("http://crbug.com");
701 params.transition = content::PAGE_TRANSITION_TYPED;
702 helper->DidNavigateMainFrame(details, params);
704 EXPECT_EQ(OneClickSigninHelper::AUTO_ACCEPT_NONE, helper->auto_accept_);
707 // Checks that OneClickSigninHelper doesn't stay an observer of the profile
708 // sync service after it's deleted.
709 TEST_F(OneClickSigninHelperTest, RemoveObserverFromProfileSyncService) {
710 content::WebContents* contents = web_contents();
712 ProfileSyncServiceMock* sync_service = CreateProfileSyncServiceMock();
714 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
715 OneClickSigninHelper* helper =
716 OneClickSigninHelper::FromWebContents(contents);
717 helper->SetDoNotClearPendingEmailForTesting();
719 EXPECT_CALL(*sync_service, RemoveObserver(helper));
720 SetContents(NULL);
723 // I/O thread tests
725 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThread) {
726 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
727 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
728 OneClickSigninHelper::CanOfferOnIOThreadImpl(
729 valid_gaia_url_, std::string(), &request_, io_data.get()));
732 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadIncognito) {
733 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(true));
734 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
735 OneClickSigninHelper::CanOfferOnIOThreadImpl(
736 valid_gaia_url_, std::string(), &request_, io_data.get()));
739 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoIOData) {
740 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
741 OneClickSigninHelper::CanOfferOnIOThreadImpl(
742 valid_gaia_url_, std::string(), &request_, NULL));
745 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadBadURL) {
746 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
747 EXPECT_EQ(
748 OneClickSigninHelper::IGNORE_REQUEST,
749 OneClickSigninHelper::CanOfferOnIOThreadImpl(
750 GURL("https://foo.com/"), std::string(), &request_, io_data.get()));
751 EXPECT_EQ(OneClickSigninHelper::IGNORE_REQUEST,
752 OneClickSigninHelper::CanOfferOnIOThreadImpl(
753 GURL("http://accounts.google.com/"),
754 std::string(),
755 &request_,
756 io_data.get()));
759 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadReferrer) {
760 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
761 std::string continue_url(signin::GetPromoURL(
762 signin::SOURCE_START_PAGE, false).spec());
764 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
765 OneClickSigninHelper::CanOfferOnIOThreadImpl(
766 valid_gaia_url_, continue_url, &request_, io_data.get()));
768 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
769 OneClickSigninHelper::CanOfferOnIOThreadImpl(
770 valid_gaia_url_, kImplicitURLString, &request_, io_data.get()));
772 std::string bad_url_1 = continue_url;
773 const std::string service_name = "chromiumsync";
774 bad_url_1.replace(bad_url_1.find(service_name), service_name.length(),
775 "foo");
777 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
778 OneClickSigninHelper::CanOfferOnIOThreadImpl(
779 valid_gaia_url_, bad_url_1, &request_, io_data.get()));
781 std::string bad_url_2 = continue_url;
782 const std::string source_num = "%3D0";
783 bad_url_2.replace(bad_url_1.find(source_num), source_num.length(), "%3D10");
785 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
786 OneClickSigninHelper::CanOfferOnIOThreadImpl(
787 valid_gaia_url_, bad_url_2, &request_, io_data.get()));
789 std::string bad_url_3 = continue_url;
790 const std::string source = "source%3D0";
791 bad_url_3.erase(bad_url_1.find(source), source.length());
793 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
794 OneClickSigninHelper::CanOfferOnIOThreadImpl(
795 valid_gaia_url_, bad_url_3, &request_, io_data.get()));
798 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabled) {
799 EnableOneClick(false);
800 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
801 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
802 OneClickSigninHelper::CanOfferOnIOThreadImpl(
803 valid_gaia_url_, std::string(), &request_, io_data.get()));
806 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadSignedIn) {
807 PrefService* pref_service = profile()->GetPrefs();
808 pref_service->SetString(prefs::kGoogleServicesUsername, "user@gmail.com");
810 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
811 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
812 OneClickSigninHelper::CanOfferOnIOThreadImpl(
813 valid_gaia_url_, std::string(), &request_, io_data.get()));
816 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailNotAllowed) {
817 SetAllowedUsernamePattern("*@example.com");
818 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
819 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
820 OneClickSigninHelper::CanOfferOnIOThreadImpl(
821 valid_gaia_url_, std::string(), &request_, io_data.get()));
824 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailAlreadyUsed) {
825 ProfileInfoCache* cache = testing_profile_manager_.profile_info_cache();
826 const base::FilePath& user_data_dir = cache->GetUserDataDir();
827 cache->AddProfileToCache(user_data_dir.Append(FILE_PATH_LITERAL("user")),
828 UTF8ToUTF16("user"),
829 UTF8ToUTF16("user@gmail.com"), 0, std::string());
831 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
832 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
833 OneClickSigninHelper::CanOfferOnIOThreadImpl(
834 valid_gaia_url_, std::string(), &request_, io_data.get()));
837 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadWithRejectedEmail) {
838 AddEmailToOneClickRejectedList("user@gmail.com");
839 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
840 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
841 OneClickSigninHelper::CanOfferOnIOThreadImpl(
842 valid_gaia_url_, std::string(), &request_, io_data.get()));
845 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoSigninCookies) {
846 AllowSigninCookies(false);
847 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
848 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
849 OneClickSigninHelper::CanOfferOnIOThreadImpl(
850 valid_gaia_url_, std::string(), &request_, io_data.get()));
853 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabledByPolicy) {
854 scoped_ptr<TestProfileIOData> io_data(CreateTestProfileIOData(false));
855 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
856 OneClickSigninHelper::CanOfferOnIOThreadImpl(
857 valid_gaia_url_, std::string(), &request_, io_data.get()));
859 // Simulate a policy disabling signin by writing kSigninAllowed directly.
860 // We should not offer to sign in the browser.
861 profile()->GetTestingPrefService()->SetManagedPref(
862 prefs::kSigninAllowed, base::Value::CreateBooleanValue(false));
863 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
864 OneClickSigninHelper::CanOfferOnIOThreadImpl(
865 valid_gaia_url_, std::string(), &request_, io_data.get()));
867 // Reset the preference.
868 profile()->GetTestingPrefService()->SetManagedPref(
869 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true));
871 // Simulate a policy disabling sync by writing kSyncManaged directly.
872 // We should still offer to sign in the browser.
873 profile()->GetTestingPrefService()->SetManagedPref(
874 prefs::kSyncManaged, base::Value::CreateBooleanValue(true));
875 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
876 OneClickSigninHelper::CanOfferOnIOThreadImpl(
877 valid_gaia_url_, std::string(), &request_, io_data.get()));