Commit d0138da3 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

FakeProfileOAuth2TokenService: fix auto_post_fetch_response vs manual response

FakeProfileOAuth2TokenService has two modes of operation:
auto_post_fetch_response mode, which means it'll automatically post
successful responses to all access token requests to the message loop,
or regular (manual) mode, where the user calls IssueToken*/IssueError*
etc. This CL adds DCHECKs to make sure the two modes are not mixed.
It also fixes some ProfileSyncService tests which did just that.

Bug: 825190
Change-Id: Ibed800c7defa09fb8efbf8cb8a56608731aaf70e
Reviewed-on: https://chromium-review.googlesource.com/995434Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548067}
parent ef18e86a
......@@ -649,6 +649,10 @@ TEST_F(ProfileSyncServiceTest, ClearDataOnSignOut) {
// Verify that credential errors get returned from GetAuthError().
TEST_F(ProfileSyncServiceTest, CredentialErrorReturned) {
// This test needs to manually send access tokens (or errors), so disable
// automatic replies to access token requests.
auth_service()->set_auto_post_fetch_response_on_message_loop(false);
CreateService(ProfileSyncService::AUTO_START);
IssueTestTokens();
ExpectDataTypeManagerCreation(1, GetDefaultConfigureCalledCallback());
......@@ -663,6 +667,8 @@ TEST_F(ProfileSyncServiceTest, CredentialErrorReturned) {
signin_manager()->GetAuthenticatedAccountId();
auth_service()->LoadCredentials(primary_account_id);
base::RunLoop().RunUntilIdle();
auth_service()->IssueAllTokensForAccount(primary_account_id, "access token",
base::Time::Max());
ASSERT_FALSE(service()->GetAccessTokenForTest().empty());
ASSERT_EQ(GoogleServiceAuthError::NONE, service()->GetAuthError().state());
......@@ -685,6 +691,10 @@ TEST_F(ProfileSyncServiceTest, CredentialErrorReturned) {
// Verify that credential errors get cleared when a new token is fetched
// successfully.
TEST_F(ProfileSyncServiceTest, CredentialErrorClearsOnNewToken) {
// This test needs to manually send access tokens (or errors), so disable
// automatic replies to access token requests.
auth_service()->set_auto_post_fetch_response_on_message_loop(false);
CreateService(ProfileSyncService::AUTO_START);
IssueTestTokens();
ExpectDataTypeManagerCreation(1, GetDefaultConfigureCalledCallback());
......@@ -699,6 +709,8 @@ TEST_F(ProfileSyncServiceTest, CredentialErrorClearsOnNewToken) {
signin_manager()->GetAuthenticatedAccountId();
auth_service()->LoadCredentials(primary_account_id);
base::RunLoop().RunUntilIdle();
auth_service()->IssueAllTokensForAccount(primary_account_id, "access token",
base::Time::Max());
ASSERT_FALSE(service()->GetAccessTokenForTest().empty());
ASSERT_EQ(GoogleServiceAuthError::NONE, service()->GetAuthError().state());
......
......@@ -35,6 +35,7 @@ void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
const std::string& account_id,
const std::string& access_token,
const base::Time& expiration) {
DCHECK(!auto_post_fetch_response_on_message_loop_);
CompleteRequests(account_id, true, ScopeSet(),
GoogleServiceAuthError::AuthErrorNone(), access_token,
expiration);
......@@ -43,6 +44,7 @@ void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
const std::string& account_id,
const GoogleServiceAuthError& error) {
DCHECK(!auto_post_fetch_response_on_message_loop_);
CompleteRequests(account_id, true, ScopeSet(), error, std::string(),
base::Time());
}
......@@ -51,6 +53,7 @@ void FakeProfileOAuth2TokenService::IssueTokenForScope(
const ScopeSet& scope,
const std::string& access_token,
const base::Time& expiration) {
DCHECK(!auto_post_fetch_response_on_message_loop_);
CompleteRequests("", false, scope, GoogleServiceAuthError::AuthErrorNone(),
access_token, expiration);
}
......@@ -58,17 +61,20 @@ void FakeProfileOAuth2TokenService::IssueTokenForScope(
void FakeProfileOAuth2TokenService::IssueErrorForScope(
const ScopeSet& scope,
const GoogleServiceAuthError& error) {
DCHECK(!auto_post_fetch_response_on_message_loop_);
CompleteRequests("", false, scope, error, std::string(), base::Time());
}
void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
const GoogleServiceAuthError& error) {
DCHECK(!auto_post_fetch_response_on_message_loop_);
CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
}
void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
const std::string& access_token,
const base::Time& expiration) {
DCHECK(!auto_post_fetch_response_on_message_loop_);
CompleteRequests("", true, ScopeSet(),
GoogleServiceAuthError::AuthErrorNone(), access_token,
expiration);
......@@ -125,8 +131,10 @@ void FakeProfileOAuth2TokenService::FetchOAuth2Token(
if (auto_post_fetch_response_on_message_loop_) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(&FakeProfileOAuth2TokenService::IssueAllTokensForAccount,
weak_ptr_factory_.GetWeakPtr(), account_id, "access_token",
base::BindOnce(&FakeProfileOAuth2TokenService::CompleteRequests,
weak_ptr_factory_.GetWeakPtr(), account_id,
/*all_scoped=*/true, ScopeSet(),
GoogleServiceAuthError::AuthErrorNone(), "access_token",
base::Time::Max()));
}
}
......
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