ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / ui / sync / one_click_signin_helper_unittest.cc
blobebae62fa0a68c8ab6718e420d3523d7acc1ecdeb
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/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/content_settings/cookie_settings.h"
12 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_io_data.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/signin/chrome_signin_client.h"
18 #include "chrome/browser/signin/chrome_signin_client_factory.h"
19 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
20 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
21 #include "chrome/browser/signin/fake_signin_manager.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_manager_factory.h"
24 #include "chrome/browser/signin/signin_names_io_thread.h"
25 #include "chrome/browser/signin/signin_promo.h"
26 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
27 #include "chrome/browser/sync/profile_sync_service_factory.h"
28 #include "chrome/browser/sync/test_profile_sync_service.h"
29 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
30 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
31 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/grit/chromium_strings.h"
34 #include "chrome/grit/generated_resources.h"
35 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
36 #include "chrome/test/base/testing_browser_process.h"
37 #include "chrome/test/base/testing_pref_service_syncable.h"
38 #include "chrome/test/base/testing_profile.h"
39 #include "chrome/test/base/testing_profile_manager.h"
40 #include "components/autofill/core/common/password_form.h"
41 #include "components/signin/core/browser/profile_oauth2_token_service.h"
42 #include "components/signin/core/browser/signin_manager.h"
43 #include "components/sync_driver/pref_names.h"
44 #include "content/public/browser/browser_context.h"
45 #include "content/public/browser/navigation_details.h"
46 #include "content/public/browser/navigation_entry.h"
47 #include "content/public/browser/web_contents.h"
48 #include "content/public/browser/web_contents_delegate.h"
49 #include "content/public/common/frame_navigate_params.h"
50 #include "content/public/common/url_constants.h"
51 #include "content/public/test/mock_render_process_host.h"
52 #include "testing/gmock/include/gmock/gmock.h"
53 #include "testing/gtest/include/gtest/gtest.h"
54 #include "ui/base/l10n/l10n_util.h"
56 using ::testing::_;
57 using ::testing::AtLeast;
58 using ::testing::Return;
60 namespace {
62 // Used to confirm OneClickSigninHelper does not trigger redirect when there is
63 // a pending navigation.
64 class MockWebContentsDelegate : public content::WebContentsDelegate {
65 public:
66 MOCK_METHOD2(OpenURLFromTab,
67 content::WebContents*(content::WebContents* source,
68 const content::OpenURLParams& params));
71 class SigninManagerMock : public FakeSigninManager {
72 public:
73 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) {
74 Initialize(NULL);
76 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username));
79 static KeyedService* BuildSigninManagerMock(content::BrowserContext* profile) {
80 return new SigninManagerMock(static_cast<Profile*>(profile));
83 class TestProfileIOData : public ProfileIOData {
84 public:
85 TestProfileIOData(Profile::ProfileType profile_type,
86 PrefService* pref_service, PrefService* local_state,
87 CookieSettings* cookie_settings)
88 : ProfileIOData(profile_type) {
89 // Initialize the IO members required for these tests, but keep them on
90 // this thread since we don't use a background thread here.
91 google_services_username()->Init(prefs::kGoogleServicesUsername,
92 pref_service);
93 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled,
94 pref_service);
95 one_click_signin_rejected_email_list()->Init(
96 prefs::kReverseAutologinRejectedEmailList, pref_service);
98 google_services_username_pattern()->Init(
99 prefs::kGoogleServicesUsernamePattern, local_state);
101 sync_disabled()->Init(sync_driver::prefs::kSyncManaged, pref_service);
103 signin_allowed()->Init(prefs::kSigninAllowed, pref_service);
105 set_signin_names_for_testing(new SigninNamesOnIOThread());
106 SetCookieSettingsForTesting(cookie_settings);
109 ~TestProfileIOData() override {
110 signin_names()->ReleaseResourcesOnUIThread();
113 // ProfileIOData overrides:
114 void InitializeInternal(
115 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate,
116 ProfileParams* profile_params,
117 content::ProtocolHandlerMap* protocol_handlers,
118 content::URLRequestInterceptorScopedVector
119 request_interceptors) const override {
120 NOTREACHED();
122 void InitializeExtensionsRequestContext(
123 ProfileParams* profile_params) const override {
124 NOTREACHED();
126 net::URLRequestContext* InitializeAppRequestContext(
127 net::URLRequestContext* main_context,
128 const StoragePartitionDescriptor& details,
129 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
130 protocol_handler_interceptor,
131 content::ProtocolHandlerMap* protocol_handlers,
132 content::URLRequestInterceptorScopedVector request_interceptors)
133 const override {
134 NOTREACHED();
135 return NULL;
137 net::URLRequestContext* InitializeMediaRequestContext(
138 net::URLRequestContext* original_context,
139 const StoragePartitionDescriptor& details) const override {
140 NOTREACHED();
141 return NULL;
143 net::URLRequestContext* AcquireMediaRequestContext() const override {
144 NOTREACHED();
145 return NULL;
147 net::URLRequestContext* AcquireIsolatedAppRequestContext(
148 net::URLRequestContext* main_context,
149 const StoragePartitionDescriptor& partition_descriptor,
150 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
151 protocol_handler_interceptor,
152 content::ProtocolHandlerMap* protocol_handlers,
153 content::URLRequestInterceptorScopedVector request_interceptors)
154 const override {
155 NOTREACHED();
156 return NULL;
158 net::URLRequestContext* AcquireIsolatedMediaRequestContext(
159 net::URLRequestContext* app_context,
160 const StoragePartitionDescriptor& partition_descriptor) const override {
161 NOTREACHED();
162 return NULL;
166 class TestURLRequest : public base::SupportsUserData {
167 public:
168 TestURLRequest() {}
169 ~TestURLRequest() override {}
172 class OneClickTestProfileSyncService : public TestProfileSyncService {
173 public:
174 ~OneClickTestProfileSyncService() override {}
176 // Helper routine to be used in conjunction with
177 // BrowserContextKeyedServiceFactory::SetTestingFactory().
178 static KeyedService* Build(content::BrowserContext* profile) {
179 return new OneClickTestProfileSyncService(static_cast<Profile*>(profile));
182 // Need to control this for certain tests.
183 bool FirstSetupInProgress() const override {
184 return first_setup_in_progress_;
187 bool SyncActive() const override { return sync_active_; }
189 // Controls return value of FirstSetupInProgress. Because some bits
190 // of UI depend on that value, it's useful to control it separately
191 // from the internal work and components that are triggered (such as
192 // ReconfigureDataTypeManager) to facilitate unit tests.
193 void set_first_setup_in_progress(bool in_progress) {
194 first_setup_in_progress_ = in_progress;
197 void set_sync_active(bool active) {
198 sync_active_ = active;
201 private:
202 explicit OneClickTestProfileSyncService(Profile* profile)
203 : TestProfileSyncService(
204 scoped_ptr<ProfileSyncComponentsFactory>(
205 new ProfileSyncComponentsFactoryMock()),
206 profile,
207 SigninManagerFactory::GetForProfile(profile),
208 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
209 browser_sync::MANUAL_START),
210 first_setup_in_progress_(false),
211 sync_active_(false) {}
213 bool first_setup_in_progress_;
214 bool sync_active_;
217 } // namespace
219 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness {
220 public:
221 OneClickSigninHelperTest();
223 void SetUp() override;
224 void TearDown() override;
226 // Sets up the sign-in manager for tests. If |username| is
227 // is not empty, the profile of the mock WebContents will be connected to
228 // the given account.
229 void SetUpSigninManager(const std::string& username);
231 // Set the ID of the signin process that the test will assume to be the
232 // only process allowed to sign the user in to Chrome.
233 void SetTrustedSigninProcessID(int id);
235 void AddEmailToOneClickRejectedList(const std::string& email);
236 void EnableOneClick(bool enable);
237 void AllowSigninCookies(bool enable);
238 void SetAllowedUsernamePattern(const std::string& pattern);
239 void SubmitGAIAPassword(OneClickSigninHelper* helper);
241 SigninManagerMock* signin_manager_;
242 FakeProfileOAuth2TokenService* fake_oauth2_token_service_;
244 protected:
245 GoogleServiceAuthError no_error_;
247 private:
248 // ChromeRenderViewHostTestHarness overrides:
249 content::BrowserContext* CreateBrowserContext() override;
251 // The ID of the signin process the test will assume to be trusted.
252 // By default, set to the test RenderProcessHost's process ID, but
253 // overridden by SetTrustedSigninProcessID.
254 int trusted_signin_process_id_;
256 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest);
259 OneClickSigninHelperTest::OneClickSigninHelperTest()
260 : signin_manager_(NULL),
261 fake_oauth2_token_service_(NULL),
262 no_error_(GoogleServiceAuthError::NONE),
263 trusted_signin_process_id_(-1) {
266 void OneClickSigninHelperTest::SetUp() {
267 signin::ForceWebBasedSigninFlowForTesting(true);
268 content::RenderViewHostTestHarness::SetUp();
269 SetTrustedSigninProcessID(process()->GetID());
272 void OneClickSigninHelperTest::TearDown() {
273 signin::ForceWebBasedSigninFlowForTesting(false);
274 content::RenderViewHostTestHarness::TearDown();
277 void OneClickSigninHelperTest::SetTrustedSigninProcessID(int id) {
278 trusted_signin_process_id_ = id;
281 void OneClickSigninHelperTest::SetUpSigninManager(const std::string& username) {
282 SigninClient* signin_client =
283 ChromeSigninClientFactory::GetForProfile(profile());
284 if (signin_client)
285 signin_client->SetSigninProcess(trusted_signin_process_id_);
287 signin_manager_ = static_cast<SigninManagerMock*>(
288 SigninManagerFactory::GetForProfile(profile()));
289 if (!username.empty()) {
290 ASSERT_TRUE(signin_manager_);
291 signin_manager_->SetAuthenticatedUsername(username);
295 void OneClickSigninHelperTest::EnableOneClick(bool enable) {
296 PrefService* pref_service = profile()->GetPrefs();
297 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable);
300 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList(
301 const std::string& email) {
302 PrefService* pref_service = profile()->GetPrefs();
303 ListPrefUpdate updater(pref_service,
304 prefs::kReverseAutologinRejectedEmailList);
305 updater->AppendIfNotPresent(new base::StringValue(email));
308 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) {
309 CookieSettings* cookie_settings =
310 CookieSettings::Factory::GetForProfile(profile()).get();
311 cookie_settings->SetDefaultCookieSetting(enable ? CONTENT_SETTING_ALLOW
312 : CONTENT_SETTING_BLOCK);
315 void OneClickSigninHelperTest::SetAllowedUsernamePattern(
316 const std::string& pattern) {
317 PrefService* local_state = g_browser_process->local_state();
318 local_state->SetString(prefs::kGoogleServicesUsernamePattern, pattern);
321 void OneClickSigninHelperTest::SubmitGAIAPassword(
322 OneClickSigninHelper* helper) {
323 autofill::PasswordForm password_form;
324 password_form.origin = GURL("https://accounts.google.com");
325 password_form.signon_realm = "https://accounts.google.com";
326 password_form.password_value = base::UTF8ToUTF16("password");
327 helper->PasswordSubmitted(password_form);
330 content::BrowserContext* OneClickSigninHelperTest::CreateBrowserContext() {
331 TestingProfile::Builder builder;
332 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
333 BuildFakeProfileOAuth2TokenService);
334 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
335 BuildSigninManagerMock);
336 scoped_ptr<TestingProfile> profile = builder.Build();
338 fake_oauth2_token_service_ =
339 static_cast<FakeProfileOAuth2TokenService*>(
340 ProfileOAuth2TokenServiceFactory::GetForProfile(profile.get()));
342 return profile.release();
345 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest {
346 public:
347 OneClickSigninHelperIOTest();
349 void SetUp() override;
351 TestProfileIOData* CreateTestProfileIOData(Profile::ProfileType profile_type);
353 protected:
354 TestingProfileManager testing_profile_manager_;
355 TestURLRequest request_;
356 const GURL valid_gaia_url_;
358 private:
359 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperIOTest);
362 OneClickSigninHelperIOTest::OneClickSigninHelperIOTest()
363 : testing_profile_manager_(
364 TestingBrowserProcess::GetGlobal()),
365 valid_gaia_url_("https://accounts.google.com/") {
368 void OneClickSigninHelperIOTest::SetUp() {
369 OneClickSigninHelperTest::SetUp();
370 ASSERT_TRUE(testing_profile_manager_.SetUp());
373 TestProfileIOData* OneClickSigninHelperIOTest::CreateTestProfileIOData(
374 Profile::ProfileType profile_type) {
375 PrefService* pref_service = profile()->GetPrefs();
376 PrefService* local_state = g_browser_process->local_state();
377 CookieSettings* cookie_settings =
378 CookieSettings::Factory::GetForProfile(profile()).get();
379 TestProfileIOData* io_data = new TestProfileIOData(
380 profile_type, pref_service, local_state, cookie_settings);
381 io_data->set_reverse_autologin_pending_email("user@gmail.com");
382 return io_data;
385 class OneClickSigninHelperIncognitoTest : public OneClickSigninHelperTest {
386 protected:
387 // content::RenderViewHostTestHarness.
388 content::BrowserContext* CreateBrowserContext() override;
391 content::BrowserContext*
392 OneClickSigninHelperIncognitoTest::CreateBrowserContext() {
393 // Simulate an incognito profile to run this test. RenderViewHostTestHarness
394 // takes ownership of the return value, so it can't be a "proper" incognito
395 // profile, since they are owned by their parent, non-incognito profile.
396 scoped_ptr<TestingProfile> profile = TestingProfile::Builder().Build();
397 profile->ForceIncognito(true);
398 return profile.release();
401 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) {
402 std::string error_message;
403 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
404 NULL, OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
405 "user@gmail.com", &error_message));
406 EXPECT_EQ("", error_message);
407 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
408 NULL, OneClickSigninHelper::CAN_OFFER_FOR_ALL,
409 "user@gmail.com", &error_message));
410 EXPECT_EQ("", error_message);
411 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
412 NULL,
413 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
414 std::string(),
415 &error_message));
416 EXPECT_EQ("", error_message);
419 TEST_F(OneClickSigninHelperTest, CanOffer) {
420 SetUpSigninManager(std::string());
422 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
423 WillRepeatedly(Return(true));
425 EnableOneClick(true);
426 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
427 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
428 "user@gmail.com", NULL));
429 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
430 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
431 "user@gmail.com", NULL));
432 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
433 web_contents(),
434 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
435 std::string(),
436 NULL));
438 EnableOneClick(false);
440 std::string error_message;
441 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
442 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
443 "user@gmail.com", &error_message));
444 EXPECT_EQ("", error_message);
446 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
447 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
448 "user@gmail.com", &error_message));
449 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
450 web_contents(),
451 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
452 std::string(),
453 &error_message));
454 EXPECT_EQ("", error_message);
457 TEST_F(OneClickSigninHelperTest, CanOfferFirstSetup) {
458 SetUpSigninManager(std::string());
460 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
461 WillRepeatedly(Return(true));
463 // Invoke OneClickTestProfileSyncService factory function and grab result.
464 OneClickTestProfileSyncService* sync =
465 static_cast<OneClickTestProfileSyncService*>(
466 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
467 profile(), OneClickTestProfileSyncService::Build));
468 sync->set_sync_active(false);
469 sync->Initialize();
470 sync->set_sync_active(true);
471 sync->set_first_setup_in_progress(true);
473 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
474 web_contents(),
475 OneClickSigninHelper::CAN_OFFER_FOR_ALL,
476 "foo@gmail.com", NULL));
477 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
478 web_contents(),
479 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
480 "foo@gmail.com", NULL));
481 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
482 web_contents(),
483 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
484 std::string(),
485 NULL));
488 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) {
489 SetUpSigninManager("foo@gmail.com");
491 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
492 WillRepeatedly(Return(true));
494 std::string error_message;
495 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
496 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
497 "foo@gmail.com", &error_message));
498 EXPECT_EQ("", error_message);
499 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
500 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
501 "foo", &error_message));
502 EXPECT_EQ("", error_message);
503 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
504 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
505 "user@gmail.com", &error_message));
506 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
507 base::UTF8ToUTF16("foo@gmail.com")),
508 error_message);
509 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
510 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
511 "foo@gmail.com", &error_message));
512 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
513 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
514 "foo", &error_message));
515 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
516 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
517 "user@gmail.com", &error_message));
518 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
519 base::UTF8ToUTF16("foo@gmail.com")),
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, CanOfferUsernameNotAllowed) {
529 SetUpSigninManager(std::string());
531 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
532 WillRepeatedly(Return(false));
534 std::string error_message;
535 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
536 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
537 "foo@gmail.com", &error_message));
538 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
539 error_message);
540 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
541 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
542 "foo@gmail.com", &error_message));
543 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
544 error_message);
545 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
546 web_contents(),
547 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
548 std::string(),
549 &error_message));
552 TEST_F(OneClickSigninHelperTest, CanOfferWithRejectedEmail) {
553 SetUpSigninManager(std::string());
555 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
556 WillRepeatedly(Return(true));
558 AddEmailToOneClickRejectedList("foo@gmail.com");
559 AddEmailToOneClickRejectedList("user@gmail.com");
561 std::string error_message;
562 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
563 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
564 "foo@gmail.com", &error_message));
565 EXPECT_EQ("", error_message);
566 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
567 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
568 "user@gmail.com", &error_message));
569 EXPECT_EQ("", error_message);
570 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
571 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
572 "foo@gmail.com", &error_message));
573 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
574 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
575 "user@gmail.com", &error_message));
576 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
577 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
578 "john@gmail.com", &error_message));
581 TEST_F(OneClickSigninHelperIncognitoTest, CanOfferIncognito) {
582 SetUpSigninManager(std::string());
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, CanOfferNoSigninCookies) {
602 SetUpSigninManager(std::string());
603 AllowSigninCookies(false);
605 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
606 WillRepeatedly(Return(true));
608 std::string error_message;
609 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
610 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
611 "user@gmail.com", &error_message));
612 EXPECT_EQ("", error_message);
613 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
614 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
615 "user@gmail.com", &error_message));
616 EXPECT_EQ("", error_message);
617 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
618 web_contents(),
619 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
620 std::string(),
621 &error_message));
622 EXPECT_EQ("", error_message);
625 TEST_F(OneClickSigninHelperTest, CanOfferDisabledByPolicy) {
626 SetUpSigninManager(std::string());
628 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)).
629 WillRepeatedly(Return(true));
631 EnableOneClick(true);
632 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
633 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
634 "user@gmail.com", NULL));
636 // Simulate a policy disabling signin by writing kSigninAllowed directly.
637 profile()->GetTestingPrefService()->SetManagedPref(
638 prefs::kSigninAllowed, new base::FundamentalValue(false));
640 EXPECT_FALSE(OneClickSigninHelper::CanOffer(
641 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
642 "user@gmail.com", NULL));
644 // Reset the preference value to true.
645 profile()->GetTestingPrefService()->SetManagedPref(
646 prefs::kSigninAllowed, new base::FundamentalValue(true));
648 // Simulate a policy disabling sync by writing kSyncManaged directly.
649 profile()->GetTestingPrefService()->SetManagedPref(
650 sync_driver::prefs::kSyncManaged, new base::FundamentalValue(true));
652 // Should still offer even if sync is disabled by policy.
653 EXPECT_TRUE(OneClickSigninHelper::CanOffer(
654 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL,
655 "user@gmail.com", NULL));
658 // Should not crash if a helper instance is not associated with an incognito
659 // web contents.
660 TEST_F(OneClickSigninHelperIncognitoTest, ShowInfoBarUIThreadIncognito) {
661 SetUpSigninManager(std::string());
662 OneClickSigninHelper* helper =
663 OneClickSigninHelper::FromWebContents(web_contents());
664 EXPECT_EQ(NULL, helper);
666 OneClickSigninHelper::ShowInfoBarUIThread(
667 "session_index", "email", OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED,
668 signin_metrics::SOURCE_UNKNOWN, GURL(), process()->GetID(),
669 rvh()->GetRoutingID());
672 // Checks that the state of OneClickSigninHelper is cleaned when there is a
673 // navigation away from the sign in flow that is not triggered by the
674 // web contents.
675 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) {
676 content::WebContents* contents = web_contents();
678 OneClickSigninHelper::CreateForWebContents(contents);
679 OneClickSigninHelper* helper =
680 OneClickSigninHelper::FromWebContents(contents);
681 helper->SetDoNotClearPendingEmailForTesting();
682 helper->auto_accept_ = OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT;
684 content::LoadCommittedDetails details;
685 content::FrameNavigateParams params;
686 params.url = GURL("http://crbug.com");
687 params.transition = ui::PAGE_TRANSITION_TYPED;
688 helper->DidNavigateMainFrame(details, params);
690 EXPECT_EQ(OneClickSigninHelper::AUTO_ACCEPT_NONE, helper->auto_accept_);
693 TEST_F(OneClickSigninHelperTest, NoRedirectToNTPWithPendingEntry) {
694 content::NavigationController& controller = web_contents()->GetController();
695 EXPECT_FALSE(controller.GetPendingEntry());
697 const GURL fooWebUIURL("chrome://foo");
698 controller.LoadURL(fooWebUIURL, content::Referrer(),
699 ui::PAGE_TRANSITION_TYPED, std::string());
700 EXPECT_EQ(fooWebUIURL, controller.GetPendingEntry()->GetURL());
702 MockWebContentsDelegate delegate;
703 EXPECT_CALL(delegate, OpenURLFromTab(_, _)).Times(0);
704 web_contents()->SetDelegate(&delegate);
705 OneClickSigninHelper::RedirectToNtpOrAppsPage(
706 web_contents(), signin_metrics::SOURCE_UNKNOWN);
708 EXPECT_EQ(fooWebUIURL, controller.GetPendingEntry()->GetURL());
711 // I/O thread tests
713 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThread) {
714 scoped_ptr<TestProfileIOData> io_data(
715 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
716 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
717 OneClickSigninHelper::CanOfferOnIOThreadImpl(
718 valid_gaia_url_, &request_, io_data.get()));
721 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadIncognito) {
722 scoped_ptr<TestProfileIOData> io_data(
723 CreateTestProfileIOData(Profile::INCOGNITO_PROFILE));
724 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
725 OneClickSigninHelper::CanOfferOnIOThreadImpl(
726 valid_gaia_url_, &request_, io_data.get()));
729 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoIOData) {
730 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
731 OneClickSigninHelper::CanOfferOnIOThreadImpl(
732 valid_gaia_url_, &request_, NULL));
735 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadBadURL) {
736 scoped_ptr<TestProfileIOData> io_data(
737 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
738 EXPECT_EQ(
739 OneClickSigninHelper::IGNORE_REQUEST,
740 OneClickSigninHelper::CanOfferOnIOThreadImpl(
741 GURL("https://foo.com/"), &request_, io_data.get()));
742 EXPECT_EQ(OneClickSigninHelper::IGNORE_REQUEST,
743 OneClickSigninHelper::CanOfferOnIOThreadImpl(
744 GURL("http://accounts.google.com/"),
745 &request_,
746 io_data.get()));
749 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabled) {
750 EnableOneClick(false);
751 scoped_ptr<TestProfileIOData> io_data(
752 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
753 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
754 OneClickSigninHelper::CanOfferOnIOThreadImpl(
755 valid_gaia_url_, &request_, io_data.get()));
758 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadSignedIn) {
759 PrefService* pref_service = profile()->GetPrefs();
760 pref_service->SetString(prefs::kGoogleServicesUsername, "user@gmail.com");
762 scoped_ptr<TestProfileIOData> io_data(
763 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
764 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
765 OneClickSigninHelper::CanOfferOnIOThreadImpl(
766 valid_gaia_url_, &request_, io_data.get()));
769 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailNotAllowed) {
770 SetAllowedUsernamePattern("*@example.com");
771 scoped_ptr<TestProfileIOData> io_data(
772 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
773 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
774 OneClickSigninHelper::CanOfferOnIOThreadImpl(
775 valid_gaia_url_, &request_, io_data.get()));
778 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadEmailAlreadyUsed) {
779 ProfileInfoCache* cache = testing_profile_manager_.profile_info_cache();
780 const base::FilePath& user_data_dir = cache->GetUserDataDir();
781 cache->AddProfileToCache(user_data_dir.Append(FILE_PATH_LITERAL("user")),
782 base::UTF8ToUTF16("user"),
783 base::UTF8ToUTF16("user@gmail.com"), 0,
784 std::string());
786 scoped_ptr<TestProfileIOData> io_data(
787 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
788 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
789 OneClickSigninHelper::CanOfferOnIOThreadImpl(
790 valid_gaia_url_, &request_, io_data.get()));
793 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadWithRejectedEmail) {
794 AddEmailToOneClickRejectedList("user@gmail.com");
795 scoped_ptr<TestProfileIOData> io_data(
796 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
797 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
798 OneClickSigninHelper::CanOfferOnIOThreadImpl(
799 valid_gaia_url_, &request_, io_data.get()));
802 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadNoSigninCookies) {
803 AllowSigninCookies(false);
804 scoped_ptr<TestProfileIOData> io_data(
805 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
806 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
807 OneClickSigninHelper::CanOfferOnIOThreadImpl(
808 valid_gaia_url_, &request_, io_data.get()));
811 TEST_F(OneClickSigninHelperIOTest, CanOfferOnIOThreadDisabledByPolicy) {
812 scoped_ptr<TestProfileIOData> io_data(
813 CreateTestProfileIOData(Profile::REGULAR_PROFILE));
814 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
815 OneClickSigninHelper::CanOfferOnIOThreadImpl(
816 valid_gaia_url_, &request_, io_data.get()));
818 // Simulate a policy disabling signin by writing kSigninAllowed directly.
819 // We should not offer to sign in the browser.
820 profile()->GetTestingPrefService()->SetManagedPref(
821 prefs::kSigninAllowed, new base::FundamentalValue(false));
822 EXPECT_EQ(OneClickSigninHelper::DONT_OFFER,
823 OneClickSigninHelper::CanOfferOnIOThreadImpl(
824 valid_gaia_url_, &request_, io_data.get()));
826 // Reset the preference.
827 profile()->GetTestingPrefService()->SetManagedPref(
828 prefs::kSigninAllowed, new base::FundamentalValue(true));
830 // Simulate a policy disabling sync by writing kSyncManaged directly.
831 // We should still offer to sign in the browser.
832 profile()->GetTestingPrefService()->SetManagedPref(
833 sync_driver::prefs::kSyncManaged, new base::FundamentalValue(true));
834 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
835 OneClickSigninHelper::CanOfferOnIOThreadImpl(
836 valid_gaia_url_, &request_, io_data.get()));
840 class MockStarterWrapper
841 : public testing::StrictMock<OneClickSigninHelper::SyncStarterWrapper> {
842 public:
843 MockStarterWrapper(
844 const OneClickSigninHelper::StartSyncArgs& args,
845 OneClickSigninSyncStarter::StartSyncMode start_mode);
847 MOCK_METHOD1(DisplayErrorBubble, void(const std::string& error_message));
848 MOCK_METHOD0(StartSigninOAuthHelper, void());
849 MOCK_METHOD2(StartOneClickSigninSyncStarter,
850 void(const std::string& email,
851 const std::string& refresh_token));
854 MockStarterWrapper::MockStarterWrapper(
855 const OneClickSigninHelper::StartSyncArgs& args,
856 OneClickSigninSyncStarter::StartSyncMode start_mode)
857 : testing::StrictMock<OneClickSigninHelper::SyncStarterWrapper>(
858 args, start_mode) {
861 class OneClickSyncStarterWrapperTest : public testing::Test {
862 public:
863 void SetUp() override {
864 TestingProfile::Builder builder;
865 profile_ = builder.Build();
868 void TearDown() override {
869 // Let the SyncStarterWrapper delete itself.
870 base::RunLoop().RunUntilIdle();
873 void SetCookie(const std::string& value) {
874 // Set a valid LSID cookie in the test cookie store.
875 scoped_refptr<net::CookieMonster> cookie_monster =
876 profile()->GetCookieMonster();
877 net::CookieOptions options;
878 options.set_include_httponly();
879 cookie_monster->SetCookieWithOptionsAsync(
880 GURL("https://accounts.google.com"),
881 value, options,
882 net::CookieMonster::SetCookiesCallback());
885 void SimulateRefreshTokenFetched(
886 SigninOAuthHelper::Consumer* consumer,
887 const std::string& email,
888 const std::string& display_email,
889 const std::string& refresh_token) {
890 consumer->OnSigninOAuthInformationAvailable(
891 email, display_email, refresh_token);
894 TestingProfile* profile() { return profile_.get(); }
896 private:
897 content::TestBrowserThreadBundle thread_bundle_;
898 scoped_ptr<TestingProfile> profile_;
901 TEST_F(OneClickSyncStarterWrapperTest, SignInWithRefreshToken) {
902 OneClickSigninHelper::StartSyncArgs args;
903 args.email = "foo@gmail.com";
904 args.password = "password";
905 args.refresh_token = "refresh_token";
906 MockStarterWrapper* wrapper = new MockStarterWrapper(
907 args, OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
909 EXPECT_CALL(*wrapper,
910 StartOneClickSigninSyncStarter("foo@gmail.com",
911 "refresh_token"));
912 wrapper->Start();
915 TEST_F(OneClickSyncStarterWrapperTest, SignInWithPasswordNoRefreshToken) {
916 OneClickSigninHelper::StartSyncArgs args;
917 args.email = "foo@gmail.com";
918 args.password = "password";
919 MockStarterWrapper* wrapper = new MockStarterWrapper(
920 args, OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
922 EXPECT_CALL(*wrapper, StartSigninOAuthHelper());
923 EXPECT_CALL(*wrapper,
924 StartOneClickSigninSyncStarter("foo@gmail.com",
925 "refresh_token"));
926 wrapper->Start();
927 SimulateRefreshTokenFetched(wrapper, "foo@gmail.com", "foo@gmail.com",
928 "refresh_token");
931 TEST_F(OneClickSyncStarterWrapperTest, SignInWithWrongEmail) {
932 OneClickSigninHelper::StartSyncArgs args;
933 args.email = "foo@gmail.com";
934 args.password = "password";
935 MockStarterWrapper* wrapper = new MockStarterWrapper(
936 args, OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
938 EXPECT_CALL(*wrapper, StartSigninOAuthHelper());
939 EXPECT_CALL(*wrapper, DisplayErrorBubble(_));
940 wrapper->Start();
941 SimulateRefreshTokenFetched(wrapper, "bar@gmail.com", "bar@gmail.com",
942 "refresh_token");
945 TEST_F(OneClickSyncStarterWrapperTest, SignInWithEmptyPasswordValidCookie) {
946 OneClickSigninHelper::StartSyncArgs args;
947 args.email = "foo@gmail.com";
948 args.profile = profile();
949 MockStarterWrapper* wrapper = new MockStarterWrapper(
950 args, OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
951 SetCookie("LSID=1234; secure; httponly");
953 EXPECT_CALL(*wrapper, StartSigninOAuthHelper());
954 EXPECT_CALL(*wrapper,
955 StartOneClickSigninSyncStarter("foo@gmail.com",
956 "refresh_token"));
957 wrapper->Start();
958 base::RunLoop().RunUntilIdle();
959 SimulateRefreshTokenFetched(wrapper, "foo@gmail.com", "foo@gmail.com",
960 "refresh_token");
963 TEST_F(OneClickSyncStarterWrapperTest, SignInWithEmptyPasswordNoCookie) {
964 OneClickSigninHelper::StartSyncArgs args;
965 args.email = "foo@gmail.com";
966 args.profile = profile();
967 MockStarterWrapper* wrapper = new MockStarterWrapper(
968 args, OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
970 EXPECT_CALL(*wrapper, DisplayErrorBubble(_));
971 wrapper->Start();
972 base::RunLoop().RunUntilIdle();
975 TEST_F(OneClickSyncStarterWrapperTest, SignInWithEmptyPasswordInvalidCookie) {
976 OneClickSigninHelper::StartSyncArgs args;
977 args.email = "foo@gmail.com";
978 args.profile = profile();
979 MockStarterWrapper* wrapper = new MockStarterWrapper(
980 args, OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
981 SetCookie("LSID=1234; domain=google.com; secure; httponly");
983 EXPECT_CALL(*wrapper, DisplayErrorBubble(_));
984 wrapper->Start();
985 base::RunLoop().RunUntilIdle();