1 // Copyright 2013 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 "chrome/browser/ui/sync/one_click_signin_sync_starter.h"
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/signin/account_tracker_service_factory.h"
12 #include "chrome/browser/signin/chrome_signin_client_factory.h"
13 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/signin/core/browser/account_tracker_service.h"
20 #include "components/signin/core/browser/fake_signin_manager.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 const char* kTestingGaiaId
= "gaia_id";
29 const char* kTestingUsername
= "fake_username";
33 class OneClickSigninSyncStarterTest
: public ChromeRenderViewHostTestHarness
{
35 OneClickSigninSyncStarterTest()
36 : sync_starter_(NULL
),
38 succeeded_count_(0) {}
40 // ChromeRenderViewHostTestHarness:
41 void SetUp() override
{
42 ChromeRenderViewHostTestHarness::SetUp();
44 // Disable sync to simplify the creation of a OneClickSigninSyncStarter.
45 base::CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kDisableSync
);
48 SigninManagerBase
* signin_manager
= static_cast<FakeSigninManager
*>(
49 SigninManagerFactory::GetForProfile(profile()));
51 signin_manager
->Initialize(NULL
);
52 signin_manager
->SetAuthenticatedAccountInfo(kTestingGaiaId
,
56 void Callback(OneClickSigninSyncStarter::SyncSetupResult result
) {
57 if (result
== OneClickSigninSyncStarter::SYNC_SETUP_SUCCESS
)
63 // ChromeRenderViewHostTestHarness:
64 content::BrowserContext
* CreateBrowserContext() override
{
65 // Create the sign in manager required by OneClickSigninSyncStarter.
66 TestingProfile::Builder builder
;
67 builder
.AddTestingFactory(
68 SigninManagerFactory::GetInstance(),
69 &OneClickSigninSyncStarterTest::BuildSigninManager
);
70 return builder
.Build().release();
74 void CreateSyncStarter(OneClickSigninSyncStarter::Callback callback
,
75 const GURL
& continue_url
) {
76 sync_starter_
= new OneClickSigninSyncStarter(
83 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS
,
85 OneClickSigninSyncStarter::NO_CONFIRMATION
,
90 // Deletes itself when SigninFailed() or SigninSuccess() is called.
91 OneClickSigninSyncStarter
* sync_starter_
;
93 // Number of times that the callback is called with SYNC_SETUP_FAILURE.
96 // Number of times that the callback is called with SYNC_SETUP_SUCCESS.
100 static scoped_ptr
<KeyedService
> BuildSigninManager(
101 content::BrowserContext
* context
) {
102 Profile
* profile
= static_cast<Profile
*>(context
);
103 return make_scoped_ptr(new FakeSigninManager(
104 ChromeSigninClientFactory::GetForProfile(profile
),
105 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
106 AccountTrackerServiceFactory::GetForProfile(profile
),
107 GaiaCookieManagerServiceFactory::GetForProfile(profile
)));
110 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarterTest
);
113 // Verifies that the callback is invoked when sync setup fails.
114 TEST_F(OneClickSigninSyncStarterTest
, CallbackSigninFailed
) {
115 CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback
,
116 base::Unretained(this)),
118 sync_starter_
->SigninFailed(GoogleServiceAuthError(
119 GoogleServiceAuthError::REQUEST_CANCELED
));
120 EXPECT_EQ(1, failed_count_
);
121 EXPECT_EQ(0, succeeded_count_
);
124 // Verifies that there is no crash when the callback is NULL.
125 TEST_F(OneClickSigninSyncStarterTest
, CallbackNull
) {
126 CreateSyncStarter(OneClickSigninSyncStarter::Callback(), GURL());
127 sync_starter_
->SigninFailed(GoogleServiceAuthError(
128 GoogleServiceAuthError::REQUEST_CANCELED
));
129 EXPECT_EQ(0, failed_count_
);
130 EXPECT_EQ(0, succeeded_count_
);
133 // Verifies that the continue URL is loaded once signin completes.
134 TEST_F(OneClickSigninSyncStarterTest
, LoadContinueUrl
) {
135 content::NavigationController
& controller
= web_contents()->GetController();
136 EXPECT_FALSE(controller
.GetPendingEntry());
138 const GURL kTestURL
= GURL("http://www.example.com");
139 CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback
,
140 base::Unretained(this)),
142 sync_starter_
->AccountAddedToCookie(
143 GoogleServiceAuthError(GoogleServiceAuthError::NONE
));
144 EXPECT_EQ(1, succeeded_count_
);
145 EXPECT_EQ(kTestURL
, controller
.GetPendingEntry()->GetURL());