[iOS] Fix test hangs when clearing WKWebView data

ios_web_inttests has frequent test timeouts during tear down, while
clearing WKWebView data.

This CL attempts to address this flake by using the workaround
in WebStateObserverTest.StopFinishedNavigation more generally
rather than just for that test. This workaround uses a private API
to close the WKWebView before clearing data.

Change-Id: I0de76477a58f37496c2b2321749a780990e0bf0d
Bug: 1405455
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4177998
Reviewed-by: Justin Cohen <justincohen@chromium.org>
Commit-Queue: Ali Juma <ajuma@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1094499}
diff --git a/ios/web/test/web_int_test.mm b/ios/web/test/web_int_test.mm
index d31e470..15baab0 100644
--- a/ios/web/test/web_int_test.mm
+++ b/ios/web/test/web_int_test.mm
@@ -5,6 +5,7 @@
 #import "ios/web/test/web_int_test.h"
 
 #import "base/ios/block_types.h"
+#import "base/mac/foundation_util.h"
 #import "base/memory/ptr_util.h"
 #import "base/scoped_observation.h"
 #import "base/strings/sys_string_conversions.h"
@@ -15,6 +16,7 @@
 #import "ios/web/public/test/js_test_util.h"
 #import "ios/web/public/test/web_view_interaction_test_util.h"
 #import "ios/web/public/web_state_observer.h"
+#import "ios/web/web_state/web_state_impl.h"
 
 #if DCHECK_IS_ON()
 #import "ui/display/screen_base.h"
@@ -82,6 +84,18 @@
 }
 
 void WebIntTest::TearDown() {
+  // Tests can create an unresponsive WebProcess. WebIntTest::TearDown will
+  // call ClearBrowingData, which can take a very long time with an unresponsive
+  // WebProcess. Work around this problem by force closing WKWebView via a
+  // private API.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wundeclared-selector"
+  WKWebView* web_view = base::mac::ObjCCast<WKWebView>(
+      web::WebStateImpl::FromWebState(web_state())
+          ->GetWebViewNavigationProxy());
+  [web_view performSelector:@selector(_close)];
+#pragma clang diagnostic pop
+
   RemoveWKWebViewCreatedData([WKWebsiteDataStore defaultDataStore],
                              [WKWebsiteDataStore allWebsiteDataTypes]);
 
diff --git a/ios/web/web_state/web_state_observer_inttest.mm b/ios/web/web_state/web_state_observer_inttest.mm
index 8ce68af..0845c97 100644
--- a/ios/web/web_state/web_state_observer_inttest.mm
+++ b/ios/web/web_state/web_state_observer_inttest.mm
@@ -2440,18 +2440,6 @@
   // Stop the loading.
   web_state()->Stop();
   ASSERT_TRUE(test::WaitForPageToFinishLoading(web_state()));
-
-  // This test will create an unresponsive WebProcess. WebIntTest::TearDown will
-  // call ClearBrowingData, which can take a very long time with an unresponsive
-  // WebProcess. Work around this problem by force closing WKWebView via a
-  // private API.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wundeclared-selector"
-  WKWebView* web_view = base::mac::ObjCCast<WKWebView>(
-      web::WebStateImpl::FromWebState(web_state())
-          ->GetWebViewNavigationProxy());
-  [web_view performSelector:@selector(_close)];
-#pragma clang diagnostic pop
 }
 
 // Tests that iframe navigation triggers DidChangeBackForwardState.