[iOS] Fix FakeSystemIdentityManager for async calls

In FakeSystemIdentityManager, it is ok to have no system identity when
async methods are called. It is possible for those methods to run after
the identity was removed. EGtests should not wait for those calls to
shutdown.

Change-Id: I9751f5b3f5e45cfb5ae61b32557c5cfcd417c564
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5707989
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Auto-Submit: Jérôme Lebel <jlebel@chromium.org>
Reviewed-by: Jood Hajeer <jood@google.com>
Commit-Queue: Jérôme Lebel <jlebel@chromium.org>
Commit-Queue: Jood Hajeer <jood@google.com>
Cr-Commit-Position: refs/heads/main@{#1327550}
diff --git a/ios/chrome/browser/signin/model/fake_system_identity_manager.mm b/ios/chrome/browser/signin/model/fake_system_identity_manager.mm
index d94a6687..3d403c63 100644
--- a/ios/chrome/browser/signin/model/fake_system_identity_manager.mm
+++ b/ios/chrome/browser/signin/model/fake_system_identity_manager.mm
@@ -251,6 +251,7 @@
     id<SystemIdentity> identity,
     ForgetIdentityCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  DCHECK([storage_ containsIdentity:identity]);
   // Forgetting an identity is an asynchronous operation (as it requires some
   // network calls).
   PostClosure(FROM_HERE,
@@ -273,6 +274,7 @@
     const std::set<std::string>& scopes,
     AccessTokenCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  DCHECK([storage_ containsIdentity:identity]);
   // Fetching the access token is an asynchronous operation (as it requires
   // some network calls).
   PostClosure(FROM_HERE,
@@ -285,6 +287,7 @@
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
   // Fetching the avatar is an asynchronous operation (as it requires some
   // network calls).
+  DCHECK([storage_ containsIdentity:identity]);
   PostClosure(
       FROM_HERE,
       base::BindOnce(&FakeSystemIdentityManager::FetchAvatarForIdentityAsync,
@@ -302,6 +305,7 @@
 void FakeSystemIdentityManager::GetHostedDomain(id<SystemIdentity> identity,
                                                 HostedDomainCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  DCHECK([storage_ containsIdentity:identity]);
   // Fetching the hosted domain is an asynchronous operation (as it requires
   // some network calls).
   PostClosure(FROM_HERE,
@@ -321,6 +325,7 @@
     const std::set<std::string>& names,
     FetchCapabilitiesCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  DCHECK([storage_ containsIdentity:identity]);
   // Fetching the hosted domain is an asynchronous operation (as it requires
   // some network calls).
   PostClosure(
@@ -365,7 +370,11 @@
     ForgetIdentityCallback callback,
     bool notify_user) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
-  DCHECK([storage_ containsIdentity:identity]);
+  if (![storage_ containsIdentity:identity]) {
+    // The identity was removed before async method was called. There is
+    // nothing to do.
+    return;
+  }
   [storage_ removeIdentity:identity];
 
   FireIdentityListChanged(notify_user);
@@ -377,7 +386,11 @@
     id<SystemIdentity> identity,
     AccessTokenCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
-  DCHECK([storage_ containsIdentity:identity]);
+  if (![storage_ containsIdentity:identity]) {
+    // The identity was removed before async method was called. There is
+    // nothing to do.
+    return;
+  }
   FakeSystemIdentityDetails* details = [storage_ detailsForIdentity:identity];
   if (details.error) {
     NSError* error = [NSError errorWithDomain:@"com.google.HTTPStatus"
@@ -396,7 +409,11 @@
 void FakeSystemIdentityManager::FetchAvatarForIdentityAsync(
     id<SystemIdentity> identity) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
-  DCHECK([storage_ containsIdentity:identity]);
+  if (![storage_ containsIdentity:identity]) {
+    // The identity was removed before async method was called. There is
+    // nothing to do.
+    return;
+  }
   FakeSystemIdentityDetails* details = [storage_ detailsForIdentity:identity];
   if (!details.cachedAvatar) {
     details.cachedAvatar = ios::provider::GetSigninDefaultAvatar();
@@ -409,6 +426,11 @@
     id<SystemIdentity> identity,
     HostedDomainCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  if (![storage_ containsIdentity:identity]) {
+    // The identity was removed before async method was called. There is
+    // nothing to do.
+    return;
+  }
   std::move(callback).Run(FakeGetHostedDomainForIdentity(identity), nil);
 }
 
@@ -417,7 +439,11 @@
     const std::set<std::string>& names,
     FetchCapabilitiesCallback callback) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
-  DCHECK([storage_ containsIdentity:identity]);
+  if (![storage_ containsIdentity:identity]) {
+    // The identity was removed before async method was called. There is
+    // nothing to do.
+    return;
+  }
   FakeSystemIdentityDetails* details = [storage_ detailsForIdentity:identity];
 
   // Simulates the action to refresh the internal capability state with