Commit 3b37c4e4 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

Eliminate FakeIdentityProvider

As a convenience step in the porting of the IdentityProvider interface
and implementation away from OAuth2TokenService, this CL eliminates
FakeIdentityProvider. This change reduces the number of IdentityProvider
implementations to two and will avoid duplication of code between
FakeIdentityProvider and DeviceIdentityProvider when
IdentityProvider and ProfileIdentityProvider are later ported away from
knowledge of/dependence on OAuth2TokenService.

The change is straightforward: The remaining tests that use
FakeIdentityProvider don't use any of its custom hooks for testing. All
that was necessary was to let ProfileIdentityProvider have a test
constructor that allows for its SigninManager instance to be null. This
avoids the need for these tests to construct a FakeSigninManager(Base)
instance, which is a pain to do. Once ProfileIdentityProvider is
converted to take in IdentityManager, this test constructor can go
away, as it is a breeze to construct IdentityManager in a testing
context via IdentityTestEnvironment.

Bug: 809452
Change-Id: I2f699d6776542626800e59bda731579338076077
Reviewed-on: https://chromium-review.googlesource.com/1087054Reviewed-by: default avatarPavel Yatsuk <pavely@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564856}
parent f0bd5834
...@@ -177,8 +177,6 @@ source_set("unit_tests") { ...@@ -177,8 +177,6 @@ source_set("unit_tests") {
static_library("test_support") { static_library("test_support") {
testonly = true testonly = true
sources = [ sources = [
"fake_identity_provider.cc",
"fake_identity_provider.h",
"fake_invalidation_handler.cc", "fake_invalidation_handler.cc",
"fake_invalidation_handler.h", "fake_invalidation_handler.h",
"fake_invalidation_service.cc", "fake_invalidation_service.cc",
...@@ -208,8 +206,6 @@ static_library("test_support") { ...@@ -208,8 +206,6 @@ static_library("test_support") {
"//base", "//base",
"//components/gcm_driver:test_support", "//components/gcm_driver:test_support",
"//components/keyed_service/core", "//components/keyed_service/core",
"//google_apis",
"//google_apis:test_support",
"//jingle:notifier", "//jingle:notifier",
"//net", "//net",
"//testing/gmock", "//testing/gmock",
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/invalidation/impl/fake_identity_provider.h"
#include "google_apis/gaia/oauth2_token_service.h"
namespace invalidation {
FakeIdentityProvider::FakeIdentityProvider(OAuth2TokenService* token_service)
: token_service_(token_service) {}
FakeIdentityProvider::~FakeIdentityProvider() {}
void FakeIdentityProvider::SetActiveUsername(const std::string& account_id) {
account_id_ = account_id;
}
void FakeIdentityProvider::LogIn(const std::string& account_id) {
SetActiveUsername(account_id);
FireOnActiveAccountLogin();
}
void FakeIdentityProvider::LogOut() {
account_id_.clear();
FireOnActiveAccountLogout();
}
std::string FakeIdentityProvider::GetActiveAccountId() {
return account_id_;
}
bool FakeIdentityProvider::IsActiveAccountAvailable() {
if (account_id_.empty() || !token_service_ ||
!token_service_->RefreshTokenIsAvailable(account_id_))
return false;
return true;
}
OAuth2TokenService* FakeIdentityProvider::GetTokenService() {
return token_service_;
}
} // namespace invalidation
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_INVALIDATION_IMPL_FAKE_IDENTITY_PROVIDER_H_
#define COMPONENTS_INVALIDATION_IMPL_FAKE_IDENTITY_PROVIDER_H_
#include <string>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "components/invalidation/public/identity_provider.h"
class OAuth2TokenService;
namespace invalidation {
// Fake identity provider implementation.
class FakeIdentityProvider : public IdentityProvider {
public:
explicit FakeIdentityProvider(OAuth2TokenService* token_service);
~FakeIdentityProvider() override;
// Sets the active username.
void SetActiveUsername(const std::string& account_id);
// Sets the active username and fires the OnActiveAccountLogin() callback.
void LogIn(const std::string& account_id);
// Clears the active username and fires the OnActiveAccountLogout() callback.
void LogOut();
// IdentityProvider:
std::string GetActiveAccountId() override;
bool IsActiveAccountAvailable() override;
OAuth2TokenService* GetTokenService() override;
private:
std::string account_id_;
OAuth2TokenService* token_service_;
DISALLOW_COPY_AND_ASSIGN(FakeIdentityProvider);
};
} // namespace invalidation
#endif // COMPONENTS_INVALIDATION_IMPL_FAKE_IDENTITY_PROVIDER_H_
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/gcm_driver/fake_gcm_driver.h" #include "components/gcm_driver/fake_gcm_driver.h"
#include "components/gcm_driver/gcm_driver.h" #include "components/gcm_driver/gcm_driver.h"
#include "components/invalidation/impl/fake_identity_provider.h" #include "components/invalidation/impl/profile_identity_provider.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h" #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "google_apis/gaia/google_service_auth_error.h" #include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
...@@ -60,7 +60,7 @@ class GCMInvalidationBridgeTest : public ::testing::Test { ...@@ -60,7 +60,7 @@ class GCMInvalidationBridgeTest : public ::testing::Test {
token_service_->UpdateCredentials("", "fake_refresh_token"); token_service_->UpdateCredentials("", "fake_refresh_token");
gcm_driver_.reset(new CustomFakeGCMDriver()); gcm_driver_.reset(new CustomFakeGCMDriver());
identity_provider_.reset(new FakeIdentityProvider(token_service_.get())); identity_provider_.reset(new ProfileIdentityProvider(token_service_.get()));
bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(), bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(),
identity_provider_.get())); identity_provider_.get()));
...@@ -96,7 +96,7 @@ class GCMInvalidationBridgeTest : public ::testing::Test { ...@@ -96,7 +96,7 @@ class GCMInvalidationBridgeTest : public ::testing::Test {
base::MessageLoop message_loop_; base::MessageLoop message_loop_;
std::unique_ptr<FakeProfileOAuth2TokenService> token_service_; std::unique_ptr<FakeProfileOAuth2TokenService> token_service_;
std::unique_ptr<gcm::GCMDriver> gcm_driver_; std::unique_ptr<gcm::GCMDriver> gcm_driver_;
std::unique_ptr<FakeIdentityProvider> identity_provider_; std::unique_ptr<ProfileIdentityProvider> identity_provider_;
std::vector<std::string> issued_tokens_; std::vector<std::string> issued_tokens_;
std::vector<GoogleServiceAuthError> request_token_errors_; std::vector<GoogleServiceAuthError> request_token_errors_;
......
...@@ -16,10 +16,24 @@ ProfileIdentityProvider::ProfileIdentityProvider( ...@@ -16,10 +16,24 @@ ProfileIdentityProvider::ProfileIdentityProvider(
} }
ProfileIdentityProvider::~ProfileIdentityProvider() { ProfileIdentityProvider::~ProfileIdentityProvider() {
// In unittests |signin_manager_| is allowed to be null.
// TODO(809452): Eliminate this short-circuit when this class is converted to
// take in IdentityManager, at which point the tests can use
// IdentityTestEnvironment.
if (!signin_manager_)
return;
signin_manager_->RemoveObserver(this); signin_manager_->RemoveObserver(this);
} }
std::string ProfileIdentityProvider::GetActiveAccountId() { std::string ProfileIdentityProvider::GetActiveAccountId() {
// In unittests |signin_manager_| is allowed to be null.
// TODO(809452): Eliminate this short-circuit when this class is converted to
// take in IdentityManager, at which point the tests can use
// IdentityTestEnvironment.
if (!signin_manager_)
return std::string();
return signin_manager_->GetAuthenticatedAccountId(); return signin_manager_->GetAuthenticatedAccountId();
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define COMPONENTS_INVALIDATION_IMPL_PROFILE_IDENTITY_PROVIDER_H_ #define COMPONENTS_INVALIDATION_IMPL_PROFILE_IDENTITY_PROVIDER_H_
#include "base/macros.h" #include "base/macros.h"
#include "build/build_config.h"
#include "components/invalidation/public/identity_provider.h" #include "components/invalidation/public/identity_provider.h"
#include "components/signin/core/browser/signin_manager_base.h" #include "components/signin/core/browser/signin_manager_base.h"
...@@ -20,6 +21,16 @@ class ProfileIdentityProvider : public IdentityProvider, ...@@ -20,6 +21,16 @@ class ProfileIdentityProvider : public IdentityProvider,
public: public:
ProfileIdentityProvider(SigninManagerBase* signin_manager, ProfileIdentityProvider(SigninManagerBase* signin_manager,
ProfileOAuth2TokenService* token_service); ProfileOAuth2TokenService* token_service);
#if defined(UNIT_TEST)
// Provide a testing constructor that allows for a null SigninManager instance
// to be passed, as there are tests that don't interact with the login
// functionality and it is a pain to set up FakeSigninManager(Base).
// TODO(809452): Eliminate this testing constructor when this class is
// converted to take in IdentityManager, at which point the tests can use
// IdentityTestEnvironment.
ProfileIdentityProvider(ProfileOAuth2TokenService* token_service)
: signin_manager_(nullptr), token_service_(token_service) {}
#endif
~ProfileIdentityProvider() override; ~ProfileIdentityProvider() override;
// IdentityProvider: // IdentityProvider:
......
...@@ -13,14 +13,14 @@ ...@@ -13,14 +13,14 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "components/gcm_driver/fake_gcm_driver.h" #include "components/gcm_driver/fake_gcm_driver.h"
#include "components/gcm_driver/gcm_driver.h" #include "components/gcm_driver/gcm_driver.h"
#include "components/invalidation/impl/fake_identity_provider.h"
#include "components/invalidation/impl/fake_invalidation_state_tracker.h" #include "components/invalidation/impl/fake_invalidation_state_tracker.h"
#include "components/invalidation/impl/fake_invalidator.h" #include "components/invalidation/impl/fake_invalidator.h"
#include "components/invalidation/impl/gcm_invalidation_bridge.h" #include "components/invalidation/impl/gcm_invalidation_bridge.h"
#include "components/invalidation/impl/invalidation_service_test_template.h" #include "components/invalidation/impl/invalidation_service_test_template.h"
#include "components/invalidation/impl/invalidation_state_tracker.h" #include "components/invalidation/impl/invalidation_state_tracker.h"
#include "components/invalidation/impl/invalidator.h" #include "components/invalidation/impl/invalidator.h"
#include "google_apis/gaia/fake_oauth2_token_service.h" #include "components/invalidation/impl/profile_identity_provider.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -68,7 +68,7 @@ class TiclInvalidationServiceTestDelegate { ...@@ -68,7 +68,7 @@ class TiclInvalidationServiceTestDelegate {
gcm_driver_.reset(new gcm::FakeGCMDriver()); gcm_driver_.reset(new gcm::FakeGCMDriver());
invalidation_service_.reset(new TiclInvalidationService( invalidation_service_.reset(new TiclInvalidationService(
"TestUserAgent", "TestUserAgent",
std::make_unique<FakeIdentityProvider>(&token_service_), std::make_unique<ProfileIdentityProvider>(&token_service_),
std::unique_ptr<TiclSettingsProvider>(new FakeTiclSettingsProvider), std::unique_ptr<TiclSettingsProvider>(new FakeTiclSettingsProvider),
gcm_driver_.get(), nullptr)); gcm_driver_.get(), nullptr));
} }
...@@ -97,7 +97,7 @@ class TiclInvalidationServiceTestDelegate { ...@@ -97,7 +97,7 @@ class TiclInvalidationServiceTestDelegate {
fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map); fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map);
} }
FakeOAuth2TokenService token_service_; FakeProfileOAuth2TokenService token_service_;
std::unique_ptr<gcm::GCMDriver> gcm_driver_; std::unique_ptr<gcm::GCMDriver> gcm_driver_;
syncer::FakeInvalidator* fake_invalidator_; // Owned by the service. syncer::FakeInvalidator* fake_invalidator_; // Owned by the service.
......
...@@ -11,17 +11,16 @@ ...@@ -11,17 +11,16 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/gcm_driver/fake_gcm_driver.h" #include "components/gcm_driver/fake_gcm_driver.h"
#include "components/gcm_driver/gcm_channel_status_syncer.h" #include "components/gcm_driver/gcm_channel_status_syncer.h"
#include "components/invalidation/impl/fake_identity_provider.h"
#include "components/invalidation/impl/fake_invalidation_state_tracker.h" #include "components/invalidation/impl/fake_invalidation_state_tracker.h"
#include "components/invalidation/impl/invalidation_prefs.h" #include "components/invalidation/impl/invalidation_prefs.h"
#include "components/invalidation/impl/invalidation_state_tracker.h" #include "components/invalidation/impl/invalidation_state_tracker.h"
#include "components/invalidation/impl/profile_identity_provider.h"
#include "components/invalidation/impl/profile_invalidation_provider.h" #include "components/invalidation/impl/profile_invalidation_provider.h"
#include "components/invalidation/impl/ticl_invalidation_service.h" #include "components/invalidation/impl/ticl_invalidation_service.h"
#include "components/invalidation/impl/ticl_settings_provider.h" #include "components/invalidation/impl/ticl_settings_provider.h"
#include "components/invalidation/public/identity_provider.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h" #include "components/sync_preferences/testing_pref_service_syncable.h"
#include "google_apis/gaia/fake_oauth2_token_service.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -43,7 +42,7 @@ class TiclProfileSettingsProviderTest : public testing::Test { ...@@ -43,7 +42,7 @@ class TiclProfileSettingsProviderTest : public testing::Test {
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
gcm::FakeGCMDriver gcm_driver_; gcm::FakeGCMDriver gcm_driver_;
sync_preferences::TestingPrefServiceSyncable pref_service_; sync_preferences::TestingPrefServiceSyncable pref_service_;
FakeOAuth2TokenService token_service_; FakeProfileOAuth2TokenService token_service_;
std::unique_ptr<TiclInvalidationService> invalidation_service_; std::unique_ptr<TiclInvalidationService> invalidation_service_;
...@@ -63,8 +62,9 @@ void TiclProfileSettingsProviderTest::SetUp() { ...@@ -63,8 +62,9 @@ void TiclProfileSettingsProviderTest::SetUp() {
new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get()); new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get());
invalidation_service_.reset(new TiclInvalidationService( invalidation_service_.reset(new TiclInvalidationService(
"TestUserAgent", std::unique_ptr<IdentityProvider>( "TestUserAgent",
new FakeIdentityProvider(&token_service_)), std::unique_ptr<IdentityProvider>(
new ProfileIdentityProvider(&token_service_)),
std::unique_ptr<TiclSettingsProvider>( std::unique_ptr<TiclSettingsProvider>(
new TiclProfileSettingsProvider(&pref_service_)), new TiclProfileSettingsProvider(&pref_service_)),
&gcm_driver_, request_context_getter_)); &gcm_driver_, request_context_getter_));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment