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/browser/signin/signin_promo.h" 14 #include "chrome/common/chrome_switches.h" 15 #include "chrome/test/base/testing_profile.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace { 19 const char* kTestingUsername = "fake_username"; 20 } // namespace 21 22 class OneClickSigninSyncStarterTest : public testing::Test { 23 public: 24 OneClickSigninSyncStarterTest() 25 : sync_starter_(NULL), 26 failed_count_(0), 27 succeeded_count_(0) {} 28 29 // testing::Test: 30 virtual void SetUp() OVERRIDE { 31 testing::Test::SetUp(); 32 profile_.reset(new TestingProfile()); 33 34 // Disable sync to simplify the creation of a OneClickSigninSyncStarter. 35 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync); 36 37 // Create the sign in manager required by OneClickSigninSyncStarter. 38 SigninManagerBase* signin_manager = 39 static_cast<FakeSigninManager*>( 40 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( 41 profile_.get(), 42 &OneClickSigninSyncStarterTest::BuildSigninManager)); 43 signin_manager->Initialize(profile_.get(), NULL); 44 signin_manager->SetAuthenticatedUsername(kTestingUsername); 45 } 46 47 void Callback(OneClickSigninSyncStarter::SyncSetupResult result) { 48 if (result == OneClickSigninSyncStarter::SYNC_SETUP_SUCCESS) 49 ++succeeded_count_; 50 else 51 ++failed_count_; 52 } 53 54 protected: 55 void CreateSyncStarter(OneClickSigninSyncStarter::Callback callback) { 56 sync_starter_ = new OneClickSigninSyncStarter( 57 profile_.get(), 58 NULL, 59 std::string(), 60 kTestingUsername, 61 std::string(), 62 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS, 63 NULL, 64 OneClickSigninSyncStarter::NO_CONFIRMATION, 65 signin::SOURCE_UNKNOWN, 66 callback 67 ); 68 } 69 70 scoped_ptr<TestingProfile> profile_; 71 72 // Deletes itself when SigninFailed() or SigninSuccess() is called. 73 OneClickSigninSyncStarter* sync_starter_; 74 75 // Number of times that the callback is called with SYNC_SETUP_FAILURE. 76 int failed_count_; 77 78 // Number of times that the callback is called with SYNC_SETUP_SUCCESS. 79 int succeeded_count_; 80 81 private: 82 static BrowserContextKeyedService* BuildSigninManager( 83 content::BrowserContext* profile) { 84 return new FakeSigninManager(static_cast<Profile*>(profile)); 85 } 86 87 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarterTest); 88 }; 89 90 // Verifies that the callback is invoked when syn setup fails. 91 TEST_F(OneClickSigninSyncStarterTest, CallbackSigninFailed) { 92 CreateSyncStarter(base::Bind(&OneClickSigninSyncStarterTest::Callback, 93 base::Unretained(this))); 94 sync_starter_->SigninFailed(GoogleServiceAuthError( 95 GoogleServiceAuthError::REQUEST_CANCELED)); 96 EXPECT_EQ(1, failed_count_); 97 EXPECT_EQ(0, succeeded_count_); 98 } 99 100 // Verifies that there is no crash when the callback is NULL. 101 TEST_F(OneClickSigninSyncStarterTest, CallbackNull) { 102 CreateSyncStarter(OneClickSigninSyncStarter::Callback()); 103 sync_starter_->SigninFailed(GoogleServiceAuthError( 104 GoogleServiceAuthError::REQUEST_CANCELED)); 105 EXPECT_EQ(0, failed_count_); 106 EXPECT_EQ(0, succeeded_count_); 107 } 108