6d05b0a46cd49e80351371fb2af2725d51291b25
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / sync / one_click_signin_sync_starter_unittest.cc
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.
4
5 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h"
6
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"
20
21 namespace {
22 const char* kTestingUsername = "fake_username";
23 }  // namespace
24
25 class OneClickSigninSyncStarterTest : public ChromeRenderViewHostTestHarness {
26  public:
27   OneClickSigninSyncStarterTest()
28       : sync_starter_(NULL),
29         failed_count_(0),
30         succeeded_count_(0) {}
31
32   // ChromeRenderViewHostTestHarness:
33   void SetUp() override {
34     ChromeRenderViewHostTestHarness::SetUp();
35
36     // Disable sync to simplify the creation of a OneClickSigninSyncStarter.
37     CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync);
38
39     SigninManagerBase* signin_manager = static_cast<FakeSigninManager*>(
40         SigninManagerFactory::GetForProfile(profile()));
41
42     signin_manager->Initialize(NULL);
43     signin_manager->SetAuthenticatedUsername(kTestingUsername);
44   }
45
46   void Callback(OneClickSigninSyncStarter::SyncSetupResult result) {
47     if (result == OneClickSigninSyncStarter::SYNC_SETUP_SUCCESS)
48       ++succeeded_count_;
49     else
50       ++failed_count_;
51   }
52
53   // ChromeRenderViewHostTestHarness:
54   content::BrowserContext* CreateBrowserContext() override {
55     // Create the sign in manager required by OneClickSigninSyncStarter.
56     TestingProfile::Builder builder;
57     builder.AddTestingFactory(
58         SigninManagerFactory::GetInstance(),
59         &OneClickSigninSyncStarterTest::BuildSigninManager);
60     return builder.Build().release();
61   }
62
63  protected:
64   void CreateSyncStarter(OneClickSigninSyncStarter::Callback callback,
65                          const GURL& continue_url) {
66     sync_starter_ = new OneClickSigninSyncStarter(
67         profile(),
68         NULL,
69         kTestingUsername,
70         std::string(),
71         "refresh_token",
72         OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS,
73         web_contents(),
74         OneClickSigninSyncStarter::NO_CONFIRMATION,
75         continue_url,
76         callback);
77   }
78
79   // Deletes itself when SigninFailed() or SigninSuccess() is called.
80   OneClickSigninSyncStarter* sync_starter_;
81
82   // Number of times that the callback is called with SYNC_SETUP_FAILURE.
83   int failed_count_;
84
85   // Number of times that the callback is called with SYNC_SETUP_SUCCESS.
86   int succeeded_count_;
87
88  private:
89   static KeyedService* BuildSigninManager(content::BrowserContext* profile) {
90     return new FakeSigninManager(static_cast<Profile*>(profile));
91   }
92
93   DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarterTest);
94 };
95
96 // Verifies that the callback is invoked when sync setup fails.
97 TEST_F(OneClickSigninSyncStarterTest, CallbackSigninFailed) {
98   CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback,
99                                base::Unretained(this)),
100                     GURL());
101   sync_starter_->SigninFailed(GoogleServiceAuthError(
102       GoogleServiceAuthError::REQUEST_CANCELED));
103   EXPECT_EQ(1, failed_count_);
104   EXPECT_EQ(0, succeeded_count_);
105 }
106
107 // Verifies that there is no crash when the callback is NULL.
108 TEST_F(OneClickSigninSyncStarterTest, CallbackNull) {
109   CreateSyncStarter(OneClickSigninSyncStarter::Callback(), GURL());
110   sync_starter_->SigninFailed(GoogleServiceAuthError(
111       GoogleServiceAuthError::REQUEST_CANCELED));
112   EXPECT_EQ(0, failed_count_);
113   EXPECT_EQ(0, succeeded_count_);
114 }
115
116 // Verifies that the continue URL is loaded once signin completes.
117 TEST_F(OneClickSigninSyncStarterTest, LoadContinueUrl) {
118   content::NavigationController& controller = web_contents()->GetController();
119   EXPECT_FALSE(controller.GetPendingEntry());
120
121   const GURL kTestURL = GURL("http://www.example.com");
122   CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback,
123                                base::Unretained(this)),
124                     kTestURL);
125   sync_starter_->MergeSessionComplete(
126       GoogleServiceAuthError(GoogleServiceAuthError::NONE));
127   EXPECT_EQ(1, succeeded_count_);
128   EXPECT_EQ(kTestURL, controller.GetPendingEntry()->GetURL());
129 }