Refactor views app list services to allow more code sharing
[chromium-blink-merge.git] / chrome / browser / ui / sync / one_click_signin_sync_starter_unittest.cc
blob446b363e4b3cc1d6bfe831117575346dc5471617
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/testing_profile.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace {
19 const char* kTestingUsername = "fake_username";
20 } // namespace
22 class OneClickSigninSyncStarterTest : public testing::Test {
23 public:
24 OneClickSigninSyncStarterTest()
25 : sync_starter_(NULL),
26 failed_count_(0),
27 succeeded_count_(0) {}
29 // testing::Test:
30 virtual void SetUp() OVERRIDE {
31 testing::Test::SetUp();
33 // Create the sign in manager required by OneClickSigninSyncStarter.
34 TestingProfile::Builder builder;
35 builder.AddTestingFactory(
36 SigninManagerFactory::GetInstance(),
37 &OneClickSigninSyncStarterTest::BuildSigninManager);
38 profile_ = builder.Build();
40 SigninManagerBase* signin_manager = static_cast<FakeSigninManager*>(
41 SigninManagerFactory::GetForProfile(profile_.get()));
43 // Disable sync to simplify the creation of a OneClickSigninSyncStarter.
44 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync);
46 signin_manager->Initialize(NULL);
47 signin_manager->SetAuthenticatedUsername(kTestingUsername);
50 void Callback(OneClickSigninSyncStarter::SyncSetupResult result) {
51 if (result == OneClickSigninSyncStarter::SYNC_SETUP_SUCCESS)
52 ++succeeded_count_;
53 else
54 ++failed_count_;
57 protected:
58 void CreateSyncStarter(OneClickSigninSyncStarter::Callback callback) {
59 sync_starter_ = new OneClickSigninSyncStarter(
60 profile_.get(),
61 NULL,
62 kTestingUsername,
63 std::string(),
64 "refresh_token",
65 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS,
66 NULL,
67 OneClickSigninSyncStarter::NO_CONFIRMATION,
68 callback);
71 content::TestBrowserThreadBundle thread_bundle_;
73 scoped_ptr<TestingProfile> profile_;
75 // Deletes itself when SigninFailed() or SigninSuccess() is called.
76 OneClickSigninSyncStarter* sync_starter_;
78 // Number of times that the callback is called with SYNC_SETUP_FAILURE.
79 int failed_count_;
81 // Number of times that the callback is called with SYNC_SETUP_SUCCESS.
82 int succeeded_count_;
84 private:
85 static KeyedService* BuildSigninManager(content::BrowserContext* profile) {
86 return new FakeSigninManager(static_cast<Profile*>(profile));
89 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarterTest);
92 // Verifies that the callback is invoked when sync setup fails.
93 TEST_F(OneClickSigninSyncStarterTest, CallbackSigninFailed) {
94 CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback,
95 base::Unretained(this)));
96 sync_starter_->SigninFailed(GoogleServiceAuthError(
97 GoogleServiceAuthError::REQUEST_CANCELED));
98 EXPECT_EQ(1, failed_count_);
99 EXPECT_EQ(0, succeeded_count_);
102 // Verifies that there is no crash when the callback is NULL.
103 TEST_F(OneClickSigninSyncStarterTest, CallbackNull) {
104 CreateSyncStarter(OneClickSigninSyncStarter::Callback());
105 sync_starter_->SigninFailed(GoogleServiceAuthError(
106 GoogleServiceAuthError::REQUEST_CANCELED));
107 EXPECT_EQ(0, failed_count_);
108 EXPECT_EQ(0, succeeded_count_);