Commit 4c026001 authored by Sergio Villar Senin's avatar Sergio Villar Senin Committed by Commit Bot

Convert authentication_service_unittest.mm to IdentityManager

Partially migrates authentication_service_unitttest.mm to
IdentityManager. In particular it does no longer observ
OAuth2TokenService::Observer but IdentityManager::Observer. Apart from
that a IdentityTestEnvironment was created to wrap all the testing
factories used by the test. The IdentityManager returned by that
object is then used to completely replace the calls to the
SigninManager object.

Bug: 890821
Change-Id: I0a04a4e8e605546a96606ecb9a3a2338e9060257
Reviewed-on: https://chromium-review.googlesource.com/c/1323552
Commit-Queue: Sergio Villar <svillar@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607718}
parent 992abbe4
...@@ -162,6 +162,7 @@ source_set("unit_tests") { ...@@ -162,6 +162,7 @@ source_set("unit_tests") {
"//ios/web/public/test", "//ios/web/public/test",
"//ios/web/public/test/fakes", "//ios/web/public/test/fakes",
"//net:test_support", "//net:test_support",
"//services/identity/public/cpp:test_support",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
"//third_party/ocmock", "//third_party/ocmock",
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "components/keyed_service/core/service_access_type.h" #include "components/keyed_service/core/service_access_type.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h" #include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_pref_names.h" #include "components/signin/core/browser/signin_pref_names.h"
...@@ -28,8 +27,7 @@ ...@@ -28,8 +27,7 @@
#import "ios/chrome/browser/signin/authentication_service.h" #import "ios/chrome/browser/signin/authentication_service.h"
#import "ios/chrome/browser/signin/authentication_service_delegate_fake.h" #import "ios/chrome/browser/signin/authentication_service_delegate_fake.h"
#import "ios/chrome/browser/signin/authentication_service_factory.h" #import "ios/chrome/browser/signin/authentication_service_factory.h"
#include "ios/chrome/browser/signin/fake_oauth2_token_service_builder.h" #import "ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.h"
#include "ios/chrome/browser/signin/fake_signin_manager_builder.h"
#include "ios/chrome/browser/signin/ios_chrome_signin_client.h" #include "ios/chrome/browser/signin/ios_chrome_signin_client.h"
#include "ios/chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "ios/chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "ios/chrome/browser/signin/signin_client_factory.h" #include "ios/chrome/browser/signin/signin_client_factory.h"
...@@ -44,6 +42,8 @@ ...@@ -44,6 +42,8 @@
#import "ios/public/provider/chrome/browser/signin/chrome_identity.h" #import "ios/public/provider/chrome/browser/signin/chrome_identity.h"
#import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.h" #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.h"
#include "ios/web/public/test/test_web_thread_bundle.h" #include "ios/web/public/test/test_web_thread_bundle.h"
#import "services/identity/public/cpp/identity_test_environment.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -99,7 +99,7 @@ std::unique_ptr<KeyedService> BuildMockSyncSetupService( ...@@ -99,7 +99,7 @@ std::unique_ptr<KeyedService> BuildMockSyncSetupService(
} // namespace } // namespace
class AuthenticationServiceTest : public PlatformTest, class AuthenticationServiceTest : public PlatformTest,
public OAuth2TokenService::Observer { public identity::IdentityManager::Observer {
protected: protected:
AuthenticationServiceTest() AuthenticationServiceTest()
: scoped_browser_state_manager_( : scoped_browser_state_manager_(
...@@ -121,21 +121,20 @@ class AuthenticationServiceTest : public PlatformTest, ...@@ -121,21 +121,20 @@ class AuthenticationServiceTest : public PlatformTest,
builder.SetPrefService(CreatePrefService()); builder.SetPrefService(CreatePrefService());
builder.AddTestingFactory(SigninClientFactory::GetInstance(), builder.AddTestingFactory(SigninClientFactory::GetInstance(),
base::BindRepeating(&BuildFakeTestSigninClient)); base::BindRepeating(&BuildFakeTestSigninClient));
builder.AddTestingFactory(
ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeOAuth2TokenService));
builder.AddTestingFactory(
ios::SigninManagerFactory::GetInstance(),
base::BindRepeating(&ios::BuildFakeSigninManager));
builder.AddTestingFactory( builder.AddTestingFactory(
ProfileSyncServiceFactory::GetInstance(), ProfileSyncServiceFactory::GetInstance(),
base::BindRepeating(&BuildMockProfileSyncService)); base::BindRepeating(&BuildMockProfileSyncService));
builder.AddTestingFactory(SyncSetupServiceFactory::GetInstance(), builder.AddTestingFactory(SyncSetupServiceFactory::GetInstance(),
base::BindRepeating(&BuildMockSyncSetupService)); base::BindRepeating(&BuildMockSyncSetupService));
browser_state_ = builder.Build(); builder.SetPrefService(CreatePrefService());
browser_state_ = IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment(builder);
identity_test_environment_adaptor_ =
std::make_unique<IdentityTestEnvironmentChromeBrowserStateAdaptor>(
browser_state_.get());
signin_manager_ =
ios::SigninManagerFactory::GetForBrowserState(browser_state_.get());
profile_sync_service_mock_ = profile_sync_service_mock_ =
static_cast<browser_sync::ProfileSyncServiceMock*>( static_cast<browser_sync::ProfileSyncServiceMock*>(
ProfileSyncServiceFactory::GetForBrowserState( ProfileSyncServiceFactory::GetForBrowserState(
...@@ -143,8 +142,7 @@ class AuthenticationServiceTest : public PlatformTest, ...@@ -143,8 +142,7 @@ class AuthenticationServiceTest : public PlatformTest,
sync_setup_service_mock_ = static_cast<SyncSetupServiceMock*>( sync_setup_service_mock_ = static_cast<SyncSetupServiceMock*>(
SyncSetupServiceFactory::GetForBrowserState(browser_state_.get())); SyncSetupServiceFactory::GetForBrowserState(browser_state_.get()));
CreateAuthenticationService(); CreateAuthenticationService();
ProfileOAuth2TokenServiceFactory::GetForBrowserState(browser_state_.get()) identity_manager()->AddObserver(this);
->AddObserver(this);
} }
std::unique_ptr<sync_preferences::PrefServiceSyncable> CreatePrefService() { std::unique_ptr<sync_preferences::PrefServiceSyncable> CreatePrefService() {
...@@ -158,8 +156,8 @@ class AuthenticationServiceTest : public PlatformTest, ...@@ -158,8 +156,8 @@ class AuthenticationServiceTest : public PlatformTest,
} }
void TearDown() override { void TearDown() override {
ProfileOAuth2TokenServiceFactory::GetForBrowserState(browser_state_.get()) identity_manager()->RemoveObserver(this);
->RemoveObserver(this); identity_test_environment_adaptor_.reset();
authentication_service_->Shutdown(); authentication_service_->Shutdown();
authentication_service_.reset(); authentication_service_.reset();
browser_state_.reset(); browser_state_.reset();
...@@ -232,18 +230,26 @@ class AuthenticationServiceTest : public PlatformTest, ...@@ -232,18 +230,26 @@ class AuthenticationServiceTest : public PlatformTest,
base::SysNSStringToUTF8([identity gaiaID])) > 0; base::SysNSStringToUTF8([identity gaiaID])) > 0;
} }
void OnRefreshTokenAvailable(const std::string& account_id) override { // IdentityManager::Observer
void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info,
bool is_valid) override {
refresh_token_available_count_++; refresh_token_available_count_++;
} }
identity::IdentityManager* identity_manager() {
return identity_test_environment_adaptor_->identity_test_env()
->identity_manager();
}
web::TestWebThreadBundle thread_bundle_; web::TestWebThreadBundle thread_bundle_;
IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_; IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_;
std::unique_ptr<TestChromeBrowserState> browser_state_; std::unique_ptr<TestChromeBrowserState> browser_state_;
ios::FakeChromeIdentityService* identity_service_; ios::FakeChromeIdentityService* identity_service_;
browser_sync::ProfileSyncServiceMock* profile_sync_service_mock_; browser_sync::ProfileSyncServiceMock* profile_sync_service_mock_;
SyncSetupServiceMock* sync_setup_service_mock_; SyncSetupServiceMock* sync_setup_service_mock_;
SigninManager* signin_manager_;
std::unique_ptr<AuthenticationService> authentication_service_; std::unique_ptr<AuthenticationService> authentication_service_;
std::unique_ptr<IdentityTestEnvironmentChromeBrowserStateAdaptor>
identity_test_environment_adaptor_;
ChromeIdentity* identity_; ChromeIdentity* identity_;
ChromeIdentity* identity2_; ChromeIdentity* identity2_;
int refresh_token_available_count_; int refresh_token_available_count_;
...@@ -296,7 +302,7 @@ TEST_F(AuthenticationServiceTest, OnAppEnterForegroundWithSyncSetupCompleted) { ...@@ -296,7 +302,7 @@ TEST_F(AuthenticationServiceTest, OnAppEnterForegroundWithSyncSetupCompleted) {
CreateAuthenticationService(); CreateAuthenticationService();
EXPECT_EQ(base::SysNSStringToUTF8([identity_ userEmail]), EXPECT_EQ(base::SysNSStringToUTF8([identity_ userEmail]),
signin_manager_->GetAuthenticatedAccountInfo().email); identity_manager()->GetPrimaryAccountInfo().email);
EXPECT_NSEQ([identity_ userEmail], EXPECT_NSEQ([identity_ userEmail],
authentication_service_->GetAuthenticatedUserEmail()); authentication_service_->GetAuthenticatedUserEmail());
EXPECT_EQ(identity_, authentication_service_->GetAuthenticatedIdentity()); EXPECT_EQ(identity_, authentication_service_->GetAuthenticatedIdentity());
...@@ -317,7 +323,7 @@ TEST_F(AuthenticationServiceTest, OnAppEnterForegroundWithSyncDisabled) { ...@@ -317,7 +323,7 @@ TEST_F(AuthenticationServiceTest, OnAppEnterForegroundWithSyncDisabled) {
CreateAuthenticationService(); CreateAuthenticationService();
EXPECT_EQ(base::SysNSStringToUTF8([identity_ userEmail]), EXPECT_EQ(base::SysNSStringToUTF8([identity_ userEmail]),
signin_manager_->GetAuthenticatedAccountInfo().email); identity_manager()->GetPrimaryAccountInfo().email);
EXPECT_NSEQ([identity_ userEmail], EXPECT_NSEQ([identity_ userEmail],
authentication_service_->GetAuthenticatedUserEmail()); authentication_service_->GetAuthenticatedUserEmail());
EXPECT_EQ(identity_, authentication_service_->GetAuthenticatedIdentity()); EXPECT_EQ(identity_, authentication_service_->GetAuthenticatedIdentity());
...@@ -337,7 +343,7 @@ TEST_F(AuthenticationServiceTest, OnAppEnterForegroundWithSyncNotConfigured) { ...@@ -337,7 +343,7 @@ TEST_F(AuthenticationServiceTest, OnAppEnterForegroundWithSyncNotConfigured) {
CreateAuthenticationService(); CreateAuthenticationService();
EXPECT_EQ("", signin_manager_->GetAuthenticatedAccountInfo().email); EXPECT_EQ("", identity_manager()->GetPrimaryAccountInfo().email);
EXPECT_NSEQ(nil, authentication_service_->GetAuthenticatedUserEmail()); EXPECT_NSEQ(nil, authentication_service_->GetAuthenticatedUserEmail());
EXPECT_FALSE(authentication_service_->GetAuthenticatedIdentity()); EXPECT_FALSE(authentication_service_->GetAuthenticatedIdentity());
} }
...@@ -356,7 +362,7 @@ TEST_F(AuthenticationServiceTest, TestHandleForgottenIdentityNoPromptSignIn) { ...@@ -356,7 +362,7 @@ TEST_F(AuthenticationServiceTest, TestHandleForgottenIdentityNoPromptSignIn) {
// User is signed out (no corresponding identity), but not prompted for sign // User is signed out (no corresponding identity), but not prompted for sign
// in (as the action was user initiated). // in (as the action was user initiated).
EXPECT_EQ("", signin_manager_->GetAuthenticatedAccountInfo().email); EXPECT_EQ("", identity_manager()->GetPrimaryAccountInfo().email);
EXPECT_FALSE(authentication_service_->GetAuthenticatedIdentity()); EXPECT_FALSE(authentication_service_->GetAuthenticatedIdentity());
EXPECT_FALSE(authentication_service_->ShouldPromptForSignIn()); EXPECT_FALSE(authentication_service_->ShouldPromptForSignIn());
} }
...@@ -374,7 +380,7 @@ TEST_F(AuthenticationServiceTest, TestHandleForgottenIdentityPromptSignIn) { ...@@ -374,7 +380,7 @@ TEST_F(AuthenticationServiceTest, TestHandleForgottenIdentityPromptSignIn) {
// User is signed out (no corresponding identity), but not prompted for sign // User is signed out (no corresponding identity), but not prompted for sign
// in (as the action was user initiated). // in (as the action was user initiated).
EXPECT_EQ("", signin_manager_->GetAuthenticatedAccountInfo().email); EXPECT_EQ("", identity_manager()->GetPrimaryAccountInfo().email);
EXPECT_FALSE(authentication_service_->GetAuthenticatedIdentity()); EXPECT_FALSE(authentication_service_->GetAuthenticatedIdentity());
EXPECT_TRUE(authentication_service_->ShouldPromptForSignIn()); EXPECT_TRUE(authentication_service_->ShouldPromptForSignIn());
} }
......
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