Commit 190fdbf1 authored by Mihai Sardarescu's avatar Mihai Sardarescu Committed by Commit Bot

Release all ChromeIdentity objects when forgetting them.

This CL ensures that all ChromeIdentities objects are not retained by
the global autorelease pool when they were forgotten.

This is needed as relying on the global autorelease pool has as a side
effect the fact that the underlying GTMSessionFetcher objects was also
retained and this confused the EarlGrey infrastructure that believed
that a network request was still on-going during the tear down phase.

Bug: 1011798
Change-Id: Ica44d3a26d4f37d30928e9a53c104aed7bd59e13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1948436
Commit-Queue: Mihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721031}
parent a98cea66
...@@ -27,6 +27,45 @@ ...@@ -27,6 +27,45 @@
namespace chrome_test_util { namespace chrome_test_util {
namespace {
// Forgets all identities from the ChromeIdentity services. Returns true if all
// identities were successfully forgotten.
bool ForgetAllIdentities() {
ios::ChromeIdentityService* identity_service =
ios::GetChromeBrowserProvider()->GetChromeIdentityService();
if (!identity_service->HasIdentities())
return YES;
NSArray* identities_to_remove =
[NSArray arrayWithArray:identity_service->GetAllIdentities()];
NSMutableArray* identities_left =
[NSMutableArray arrayWithArray:identities_to_remove];
for (ChromeIdentity* identity in identities_to_remove) {
identity_service->ForgetIdentity(identity, ^(NSError* error) {
if (error) {
NSLog(@"ForgetIdentity failed: [identity = %@, error = %@]",
identity.userEmail, [error localizedDescription]);
}
[identities_left removeObject:identity];
});
}
// Wait maximum 10s for all forget identitites to be forgotten.
NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:10.0];
while ([identities_left count] &&
[[NSDate date] compare:deadline] != NSOrderedDescending) {
base::test::ios::SpinRunLoopWithMaxDelay(
base::TimeDelta::FromSecondsD(0.01));
}
return !identity_service->HasIdentities();
}
} // namespace
void SetUpMockAuthentication() { void SetUpMockAuthentication() {
ios::ChromeBrowserProvider* provider = ios::GetChromeBrowserProvider(); ios::ChromeBrowserProvider* provider = ios::GetChromeBrowserProvider();
std::unique_ptr<ios::FakeChromeIdentityService> service( std::unique_ptr<ios::FakeChromeIdentityService> service(
...@@ -53,40 +92,32 @@ void TearDownMockAccountReconcilor() { ...@@ -53,40 +92,32 @@ void TearDownMockAccountReconcilor() {
} }
bool SignOutAndClearAccounts() { bool SignOutAndClearAccounts() {
ios::ChromeBrowserState* browser_state = GetOriginalBrowserState(); // EarlGrey monitors network requests by swizzling internal iOS network
DCHECK(browser_state); // objects and expects them to be dealloced before the tear down. It is
// important to autorelease all objects that make network requests to avoid
// Sign out current user. // EarlGrey being confused about on-going network traffic..
AuthenticationService* authentication_service = @autoreleasepool {
AuthenticationServiceFactory::GetForBrowserState(browser_state); ios::ChromeBrowserState* browser_state = GetOriginalBrowserState();
if (authentication_service->IsAuthenticated()) { DCHECK(browser_state);
authentication_service->SignOut(signin_metrics::SIGNOUT_TEST, nil);
} // Sign out current user.
AuthenticationService* authentication_service =
// Clear last signed in user preference. AuthenticationServiceFactory::GetForBrowserState(browser_state);
browser_state->GetPrefs()->ClearPref(prefs::kGoogleServicesLastAccountId); if (authentication_service->IsAuthenticated()) {
browser_state->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername); authentication_service->SignOut(signin_metrics::SIGNOUT_TEST, nil);
}
// |SignOutAndClearAccounts()| is called during shutdown. Commit all pref
// changes to ensure that clearing the last signed in account is saved on disk // Clear last signed in user preference.
// in case Chrome crashes during shutdown. browser_state->GetPrefs()->ClearPref(prefs::kGoogleServicesLastAccountId);
browser_state->GetPrefs()->CommitPendingWrite(); browser_state->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername);
// Clear known identities. // |SignOutAndClearAccounts()| is called during shutdown. Commit all pref
ios::ChromeIdentityService* identity_service = // changes to ensure that clearing the last signed in account is saved on
ios::GetChromeBrowserProvider()->GetChromeIdentityService(); // disk in case Chrome crashes during shutdown.
NSArray* identities([identity_service->GetAllIdentities() copy]); browser_state->GetPrefs()->CommitPendingWrite();
for (ChromeIdentity* identity in identities) {
identity_service->ForgetIdentity(identity, nil); return ForgetAllIdentities();
} }
NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:10.0];
while (identity_service->HasIdentities() &&
[[NSDate date] compare:deadline] != NSOrderedDescending) {
base::test::ios::SpinRunLoopWithMaxDelay(
base::TimeDelta::FromSecondsD(0.01));
}
return !identity_service->HasIdentities();
} }
void ResetMockAuthentication() { void ResetMockAuthentication() {
......
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