Commit fa626b9f authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

[s13n] Convert identity_apitest.cc to use IdentityTestEnvironment

This bug is a follow up of [1] and [2], where production code
(//c/b/extensions/api/identity/identity_api.cc) is migrated away from
using PO2TS in favor of IdentityManager, and the API is extended to
accommodate the needs of this migration, respectively.

[1] https://crrev.com/b/1340890
[2] https://crrev.com/b/1346609

BUG=906615

Change-Id: Ib41f99f08b7d181bbcff28be97c8b55993aa8c04
Reviewed-on: https://chromium-review.googlesource.com/c/1343019
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610844}
parent da87860c
......@@ -33,13 +33,8 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/account_fetcher_service_factory.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/signin/fake_gaia_cookie_manager_service_builder.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
#include "chrome/browser/signin/fake_signin_manager_builder.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_switches.h"
......@@ -50,8 +45,6 @@
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/account_fetcher_service.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/fake_gaia_cookie_manager_service.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_buildflags.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_pref_names.h"
......@@ -365,8 +358,8 @@ class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
account_state.has_refresh_token = true;
account_state.is_primary_account = true;
OnPrimaryAccountAvailable(
SigninManagerFactory::GetForProfile(GetProfile())
->GetAuthenticatedAccountInfo(),
IdentityManagerFactory::GetForProfile(GetProfile())
->GetPrimaryAccountInfo(),
account_state);
} else {
SigninFailed();
......@@ -437,15 +430,10 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
// Replace the signin manager and token service with fakes. Do this ahead of
// creating the browser so that a bunch of classes don't register as
// observers and end up needing to unregister when the fake is substituted.
SigninManagerFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&BuildFakeSigninManagerForTesting));
ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&BuildFakeProfileOAuth2TokenService));
GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory(
IdentityTestEnvironmentProfileAdaptor::
SetIdentityTestEnvironmentFactoriesOnBrowserContext(
context,
base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceWithOptions,
/*create_fake_url_loader_factory_for_cookie_requests=*/false));
/*create_fake_url_loader_factory_for_cookie_requests=*/false);
// Ensure that AccountFetcherService is (1) created at all and (2) created
// early enough for it to observe the Profile initialization process and
......@@ -453,9 +441,6 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
// naturally in production) is necessary for the flow of
// AccountTrackerService having accounts removed when tokens are revoked
// with PO2TS to work as expected in this testing context.
// TODO(blundell): Change these tests to interact with
// IdentityTestEnvironment once the production code is changed to interact
// with IdentityManager.
AccountFetcherServiceFactory::GetInstance()->GetForProfile(
Profile::FromBrowserContext(context));
}
......@@ -463,14 +448,8 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
void SetUpOnMainThread() override {
AsyncExtensionBrowserTest::SetUpOnMainThread();
// Grab references to the fake signin manager and token service.
signin_manager_ = static_cast<FakeSigninManagerForTesting*>(
SigninManagerFactory::GetInstance()->GetForProfile(profile()));
ASSERT_TRUE(signin_manager_);
token_service_ = static_cast<FakeProfileOAuth2TokenService*>(
ProfileOAuth2TokenServiceFactory::GetInstance()->GetForProfile(
profile()));
ASSERT_TRUE(token_service_);
identity_test_env_profile_adaptor_ =
std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile());
#if defined(OS_CHROMEOS)
// On ChromeOS, ProfileOAuth2TokenService does not fire
......@@ -486,18 +465,21 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
#endif
}
void TearDownOnMainThread() override {
// Must be destroyed before the Profile.
identity_test_env_profile_adaptor_.reset();
}
protected:
// Returns the account ID of the created account.
std::string SignIn(const std::string& email) {
identity::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile());
identity::MakePrimaryAccountAvailable(identity_manager, email);
return identity_manager->GetPrimaryAccountId();
auto account_info = identity_test_env()->MakePrimaryAccountAvailable(email);
return account_info.account_id;
}
std::string AddAccount(const std::string& email) {
std::string account_id = SeedAccountInfo(email);
token_service_->UpdateCredentials(account_id, "refresh_token");
identity_test_env()->SetRefreshTokenForAccount(account_id);
return account_id;
}
......@@ -513,8 +495,12 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
return IdentityAPI::GetFactoryInstance()->Get(browser()->profile());
}
FakeSigninManagerForTesting* signin_manager_;
FakeProfileOAuth2TokenService* token_service_;
identity::IdentityTestEnvironment* identity_test_env() {
return identity_test_env_profile_adaptor_->identity_test_env();
}
std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
identity_test_env_profile_adaptor_;
std::unique_ptr<
base::CallbackList<void(content::BrowserContext*)>::Subscription>
......@@ -620,7 +606,7 @@ IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, NoPrimaryAccount) {
IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest,
PrimaryAccountHasNoRefreshToken) {
std::string primary_account_id = SignIn("primary@example.com");
token_service_->RevokeCredentials(primary_account_id);
identity_test_env()->RemoveRefreshTokenForAccount(primary_account_id);
EXPECT_TRUE(ExpectGetAccounts({}));
}
......@@ -729,7 +715,7 @@ IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest,
class GetAuthTokenFunctionTest
: public IdentityTestWithSignin,
public OAuth2TokenService::DiagnosticsObserver {
public identity::IdentityManager::DiagnosticsObserver {
public:
GetAuthTokenFunctionTest() {
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
......@@ -743,7 +729,8 @@ class GetAuthTokenFunctionTest
std::string IssueLoginAccessTokenForAccount(const std::string& account_id) {
std::string access_token = "access_token-" + account_id;
token_service_->IssueAllTokensForAccount(
identity_test_env()
->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
account_id, access_token,
base::Time::Now() + base::TimeDelta::FromSeconds(3600));
return access_token;
......@@ -759,11 +746,11 @@ class GetAuthTokenFunctionTest
void SetUpOnMainThread() override {
IdentityTestWithSignin::SetUpOnMainThread();
token_service_->AddDiagnosticsObserver(this);
identity_test_env()->identity_manager()->AddDiagnosticsObserver(this);
}
void TearDownOnMainThread() override {
token_service_->RemoveDiagnosticsObserver(this);
identity_test_env()->identity_manager()->RemoveDiagnosticsObserver(this);
IdentityTestWithSignin::TearDownOnMainThread();
}
......@@ -797,9 +784,7 @@ class GetAuthTokenFunctionTest
}
const std::string& GetPrimaryAccountId() {
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(browser()->profile());
return signin_manager->GetAuthenticatedAccountId();
return identity_test_env()->identity_manager()->GetPrimaryAccountId();
}
void SetCachedToken(const IdentityTokenCacheValue& token_data) {
......@@ -829,11 +814,10 @@ class GetAuthTokenFunctionTest
base::OnceClosure on_access_token_requested_;
private:
// OAuth2TokenService::DiagnosticsObserver:
void OnAccessTokenRequested(
const std::string& account_id,
// identity::IdentityManager::DiagnosticsObserver:
void OnAccessTokenRequested(const std::string& account_id,
const std::string& consumer_id,
const OAuth2TokenService::ScopeSet& scopes) override {
const identity::ScopeSet& scopes) override {
if (on_access_token_requested_.is_null())
return;
std::move(on_access_token_requested_).Run();
......@@ -1734,7 +1718,7 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ManuallyIssueTokenFailure) {
RunFunctionAsync(func.get(), "[{}]");
run_loop.Run();
token_service_->IssueErrorForAllPendingRequestsForAccount(
identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
primary_account_id,
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
......@@ -2365,7 +2349,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, FireOnPrimaryAccountSignOut) {
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
// Sign out and verify that the callback fires.
signin_manager_->ForceSignOut();
identity_test_env()->ClearPrimaryAccount();
EXPECT_FALSE(HasExpectedEvent());
}
......@@ -2384,7 +2368,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest,
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
// Revoke the refresh token and verify that the callback fires.
token_service_->RevokeCredentials(primary_account_id);
identity_test_env()->RemoveRefreshTokenForAccount(primary_account_id);
EXPECT_FALSE(HasExpectedEvent());
}
......@@ -2400,7 +2384,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest,
std::string primary_account_id = SignIn("primary@example.com");
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
token_service_->RevokeCredentials(primary_account_id);
identity_test_env()->RemoveRefreshTokenForAccount(primary_account_id);
account_info.id = "gaia_id_for_primary_example.com";
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, true));
......@@ -2432,7 +2416,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, FireForSecondaryAccount) {
// fires.
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
token_service_->RevokeCredentials(secondary_account_id);
identity_test_env()->RemoveRefreshTokenForAccount(secondary_account_id);
EXPECT_FALSE(HasExpectedEvent());
}
......
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