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/fake_signin_manager.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
22 const char* kTestingUsername
= "fake_username";
25 class OneClickSigninSyncStarterTest
: public ChromeRenderViewHostTestHarness
{
27 OneClickSigninSyncStarterTest()
28 : sync_starter_(NULL
),
30 succeeded_count_(0) {}
32 // ChromeRenderViewHostTestHarness:
33 void SetUp() override
{
34 ChromeRenderViewHostTestHarness::SetUp();
36 // Disable sync to simplify the creation of a OneClickSigninSyncStarter.
37 base::CommandLine::ForCurrentProcess()->AppendSwitch(
38 switches::kDisableSync
);
40 SigninManagerBase
* signin_manager
= static_cast<FakeSigninManager
*>(
41 SigninManagerFactory::GetForProfile(profile()));
43 signin_manager
->Initialize(NULL
);
44 signin_manager
->SetAuthenticatedUsername(kTestingUsername
);
47 void Callback(OneClickSigninSyncStarter::SyncSetupResult result
) {
48 if (result
== OneClickSigninSyncStarter::SYNC_SETUP_SUCCESS
)
54 // ChromeRenderViewHostTestHarness:
55 content::BrowserContext
* CreateBrowserContext() override
{
56 // Create the sign in manager required by OneClickSigninSyncStarter.
57 TestingProfile::Builder builder
;
58 builder
.AddTestingFactory(
59 SigninManagerFactory::GetInstance(),
60 &OneClickSigninSyncStarterTest::BuildSigninManager
);
61 return builder
.Build().release();
65 void CreateSyncStarter(OneClickSigninSyncStarter::Callback callback
,
66 const GURL
& continue_url
) {
67 sync_starter_
= new OneClickSigninSyncStarter(
73 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS
,
75 OneClickSigninSyncStarter::NO_CONFIRMATION
,
80 // Deletes itself when SigninFailed() or SigninSuccess() is called.
81 OneClickSigninSyncStarter
* sync_starter_
;
83 // Number of times that the callback is called with SYNC_SETUP_FAILURE.
86 // Number of times that the callback is called with SYNC_SETUP_SUCCESS.
90 static KeyedService
* BuildSigninManager(content::BrowserContext
* profile
) {
91 return new FakeSigninManager(static_cast<Profile
*>(profile
));
94 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarterTest
);
97 // Verifies that the callback is invoked when sync setup fails.
98 TEST_F(OneClickSigninSyncStarterTest
, CallbackSigninFailed
) {
99 CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback
,
100 base::Unretained(this)),
102 sync_starter_
->SigninFailed(GoogleServiceAuthError(
103 GoogleServiceAuthError::REQUEST_CANCELED
));
104 EXPECT_EQ(1, failed_count_
);
105 EXPECT_EQ(0, succeeded_count_
);
108 // Verifies that there is no crash when the callback is NULL.
109 TEST_F(OneClickSigninSyncStarterTest
, CallbackNull
) {
110 CreateSyncStarter(OneClickSigninSyncStarter::Callback(), GURL());
111 sync_starter_
->SigninFailed(GoogleServiceAuthError(
112 GoogleServiceAuthError::REQUEST_CANCELED
));
113 EXPECT_EQ(0, failed_count_
);
114 EXPECT_EQ(0, succeeded_count_
);
117 // Verifies that the continue URL is loaded once signin completes.
118 TEST_F(OneClickSigninSyncStarterTest
, LoadContinueUrl
) {
119 content::NavigationController
& controller
= web_contents()->GetController();
120 EXPECT_FALSE(controller
.GetPendingEntry());
122 const GURL kTestURL
= GURL("http://www.example.com");
123 CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback
,
124 base::Unretained(this)),
126 sync_starter_
->MergeSessionComplete(
127 GoogleServiceAuthError(GoogleServiceAuthError::NONE
));
128 EXPECT_EQ(1, succeeded_count_
);
129 EXPECT_EQ(kTestURL
, controller
.GetPendingEntry()->GetURL());