Home | History | Annotate | Download | only in signin
      1 // Copyright (c) 2012 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/signin/signin_tracker.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/compiler_specific.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/signin/fake_auth_status_provider.h"
     13 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
     14 #include "chrome/browser/signin/fake_signin_manager.h"
     15 #include "chrome/browser/signin/profile_oauth2_token_service.h"
     16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
     17 #include "chrome/browser/signin/signin_manager.h"
     18 #include "chrome/browser/signin/signin_manager_factory.h"
     19 #include "chrome/browser/sync/profile_sync_service_factory.h"
     20 #include "chrome/browser/sync/profile_sync_service_mock.h"
     21 #include "chrome/test/base/testing_profile.h"
     22 #include "content/public/browser/notification_service.h"
     23 #include "content/public/test/test_browser_thread_bundle.h"
     24 #include "google_apis/gaia/gaia_constants.h"
     25 #include "google_apis/gaia/google_service_auth_error.h"
     26 
     27 #include "testing/gmock/include/gmock/gmock.h"
     28 #include "testing/gtest/include/gtest/gtest.h"
     29 
     30 using ::testing::_;
     31 using ::testing::AnyNumber;
     32 using ::testing::Mock;
     33 using ::testing::Return;
     34 using ::testing::ReturnRef;
     35 
     36 namespace {
     37 
     38 class MockObserver : public SigninTracker::Observer {
     39  public:
     40   MockObserver() {}
     41   ~MockObserver() {}
     42 
     43   MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&));
     44   MOCK_METHOD0(SigninSuccess, void(void));
     45 };
     46 
     47 }  // namespace
     48 
     49 class SigninTrackerTest : public testing::Test {
     50  public:
     51   SigninTrackerTest() {}
     52   virtual void SetUp() OVERRIDE {
     53     TestingProfile::Builder builder;
     54     builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
     55                               FakeProfileOAuth2TokenService::Build);
     56 
     57     profile_ = builder.Build();
     58 
     59     fake_oauth2_token_service_ =
     60         static_cast<FakeProfileOAuth2TokenService*>(
     61             ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()));
     62 
     63     mock_signin_manager_ = static_cast<FakeSigninManagerBase*>(
     64         SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
     65             profile_.get(), FakeSigninManagerBase::Build));
     66     mock_signin_manager_->Initialize(profile_.get(), NULL);
     67 
     68     tracker_.reset(new SigninTracker(profile_.get(), &observer_));
     69   }
     70   virtual void TearDown() OVERRIDE {
     71     tracker_.reset();
     72     profile_.reset();
     73   }
     74 
     75   content::TestBrowserThreadBundle thread_bundle_;
     76   scoped_ptr<SigninTracker> tracker_;
     77   scoped_ptr<TestingProfile> profile_;
     78   FakeSigninManagerBase* mock_signin_manager_;
     79   FakeProfileOAuth2TokenService* fake_oauth2_token_service_;
     80   MockObserver observer_;
     81 };
     82 
     83 TEST_F(SigninTrackerTest, SignInFails) {
     84   // SIGNIN_FAILED notification should result in a SigninFailed callback.
     85   const GoogleServiceAuthError error(
     86       GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
     87   EXPECT_CALL(observer_, SigninSuccess()).Times(0);
     88   EXPECT_CALL(observer_, SigninFailed(error));
     89 
     90   content::NotificationService::current()->Notify(
     91       chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED,
     92       content::Source<Profile>(profile_.get()),
     93       content::Details<const GoogleServiceAuthError>(&error));
     94 }
     95 
     96 TEST_F(SigninTrackerTest, SignInSucceeds) {
     97   EXPECT_CALL(observer_, SigninSuccess());
     98   EXPECT_CALL(observer_, SigninFailed(_)).Times(0);
     99 
    100   mock_signin_manager_->SetAuthenticatedUsername("user (at) gmail.com");
    101   fake_oauth2_token_service_->IssueRefreshToken("refresh_token");
    102 }
    103