Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 "base/logging.h"
      6 #include "components/signin/core/browser/test_signin_client.h"
      7 #include "components/signin/core/browser/webdata/token_service_table.h"
      8 #include "components/webdata/common/web_data_service_base.h"
      9 #include "components/webdata/common/web_database_service.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 #if defined(OS_IOS)
     13 #include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h"
     14 #endif
     15 
     16 namespace {
     17 
     18 // Helper for testing.
     19 const int kInvalidProcessId = -1;
     20 }
     21 
     22 TestSigninClient::TestSigninClient()
     23     : request_context_(new net::TestURLRequestContextGetter(
     24           base::MessageLoopProxy::current())),
     25       pref_service_(NULL) {
     26   LoadDatabase();
     27 }
     28 
     29 TestSigninClient::TestSigninClient(PrefService* pref_service)
     30     : pref_service_(pref_service) {}
     31 
     32 TestSigninClient::~TestSigninClient() {}
     33 
     34 PrefService* TestSigninClient::GetPrefs() {
     35   return pref_service_;
     36 }
     37 
     38 scoped_refptr<TokenWebData> TestSigninClient::GetDatabase() {
     39   return database_;
     40 }
     41 
     42 bool TestSigninClient::CanRevokeCredentials() { return true; }
     43 
     44 std::string TestSigninClient::GetSigninScopedDeviceId() {
     45   return std::string();
     46 }
     47 
     48 void TestSigninClient::ClearSigninScopedDeviceId() {
     49 }
     50 
     51 net::URLRequestContextGetter* TestSigninClient::GetURLRequestContext() {
     52   return request_context_.get();
     53 }
     54 
     55 void TestSigninClient::SetURLRequestContext(
     56     net::URLRequestContextGetter* request_context) {
     57   request_context_ = request_context;
     58 }
     59 
     60 std::string TestSigninClient::GetProductVersion() { return ""; }
     61 
     62 void TestSigninClient::LoadDatabase() {
     63   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     64   base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
     65   scoped_refptr<WebDatabaseService> web_database =
     66       new WebDatabaseService(path,
     67                              base::MessageLoopProxy::current(),
     68                              base::MessageLoopProxy::current());
     69   web_database->AddTable(scoped_ptr<WebDatabaseTable>(new TokenServiceTable()));
     70   web_database->LoadDatabase();
     71   database_ = new TokenWebData(web_database,
     72                                base::MessageLoopProxy::current(),
     73                                base::MessageLoopProxy::current(),
     74                                WebDataServiceBase::ProfileErrorCallback());
     75   database_->Init();
     76 }
     77 
     78 bool TestSigninClient::ShouldMergeSigninCredentialsIntoCookieJar() {
     79   return true;
     80 }
     81 
     82 scoped_ptr<SigninClient::CookieChangedCallbackList::Subscription>
     83 TestSigninClient::AddCookieChangedCallback(
     84     const SigninClient::CookieChangedCallback& callback) {
     85   return cookie_callbacks_.Add(callback);
     86 }
     87 
     88 #if defined(OS_IOS)
     89 ios::ProfileOAuth2TokenServiceIOSProvider* TestSigninClient::GetIOSProvider() {
     90   return GetIOSProviderAsFake();
     91 }
     92 
     93 ios::FakeProfileOAuth2TokenServiceIOSProvider*
     94 TestSigninClient::GetIOSProviderAsFake() {
     95   if (!iosProvider_) {
     96     iosProvider_.reset(new ios::FakeProfileOAuth2TokenServiceIOSProvider());
     97   }
     98   return iosProvider_.get();
     99 }
    100 #endif
    101 
    102 void TestSigninClient::SetSigninProcess(int process_id) {
    103   if (process_id == signin_host_id_)
    104     return;
    105   DLOG_IF(WARNING, signin_host_id_ != kInvalidProcessId)
    106       << "Replacing in-use signin process.";
    107   signin_host_id_ = process_id;
    108 }
    109 
    110 void TestSigninClient::ClearSigninProcess() {
    111   signin_host_id_ = kInvalidProcessId;
    112 }
    113 
    114 bool TestSigninClient::IsSigninProcess(int process_id) const {
    115   return process_id == signin_host_id_;
    116 }
    117 
    118 bool TestSigninClient::HasSigninProcess() const {
    119   return signin_host_id_ != kInvalidProcessId;
    120 }
    121 
    122 bool TestSigninClient::IsFirstRun() const {
    123   return false;
    124 }
    125 
    126 base::Time TestSigninClient::GetInstallDate() {
    127   return base::Time::Now();
    128 }
    129