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 net::URLRequestContextGetter* TestSigninClient::GetURLRequestContext() {
     45   return request_context_;
     46 }
     47 
     48 std::string TestSigninClient::GetProductVersion() { return ""; }
     49 
     50 void TestSigninClient::LoadDatabase() {
     51   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     52   base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
     53   scoped_refptr<WebDatabaseService> web_database =
     54       new WebDatabaseService(path,
     55                              base::MessageLoopProxy::current(),
     56                              base::MessageLoopProxy::current());
     57   web_database->AddTable(scoped_ptr<WebDatabaseTable>(new TokenServiceTable()));
     58   web_database->LoadDatabase();
     59   database_ = new TokenWebData(web_database,
     60                                base::MessageLoopProxy::current(),
     61                                base::MessageLoopProxy::current(),
     62                                WebDataServiceBase::ProfileErrorCallback());
     63   database_->Init();
     64 }
     65 
     66 bool TestSigninClient::ShouldMergeSigninCredentialsIntoCookieJar() {
     67   return true;
     68 }
     69 
     70 void TestSigninClient::SetCookieChangedCallback(
     71     const CookieChangedCallback& callback) {}
     72 
     73 #if defined(OS_IOS)
     74 ios::ProfileOAuth2TokenServiceIOSProvider* TestSigninClient::GetIOSProvider() {
     75   return GetIOSProviderAsFake();
     76 }
     77 
     78 ios::FakeProfileOAuth2TokenServiceIOSProvider*
     79 TestSigninClient::GetIOSProviderAsFake() {
     80   if (!iosProvider_) {
     81     iosProvider_.reset(new ios::FakeProfileOAuth2TokenServiceIOSProvider());
     82   }
     83   return iosProvider_.get();
     84 }
     85 #endif
     86 
     87 void TestSigninClient::SetSigninProcess(int process_id) {
     88   if (process_id == signin_host_id_)
     89     return;
     90   DLOG_IF(WARNING, signin_host_id_ != kInvalidProcessId)
     91       << "Replacing in-use signin process.";
     92   signin_host_id_ = process_id;
     93 }
     94 
     95 void TestSigninClient::ClearSigninProcess() {
     96   signin_host_id_ = kInvalidProcessId;
     97 }
     98 
     99 bool TestSigninClient::IsSigninProcess(int process_id) const {
    100   return process_id == signin_host_id_;
    101 }
    102 
    103 bool TestSigninClient::HasSigninProcess() const {
    104   return signin_host_id_ != kInvalidProcessId;
    105 }
    106