From 7cbbce326c5e6dfb6c8c14f6fe2e396b8997f015 Mon Sep 17 00:00:00 2001 From: Olivier Robin Date: Thu, 22 Nov 2018 14:26:15 +0000 Subject: [PATCH] Reland "[Reland] Block resources in FormStructureBrowserTest." This is a reland of d15dd85fbe28ee8991802c55314bc97c0ee8a7cf Original change's description: > [Reland] Block resources in FormStructureBrowserTest. > > The files used for FormStructureBrowserTest are snapshots of real web > sites. They can have link to online resources but should not need them > for the test to pass. > But if the network is really bad, the page can wait for the resource > loading to finish and timeout. > Preventing the resource loading will make the test faster and more > robust. > > Bug: 896173 > Change-Id: I16b02e1d7defd4d950e6ee4cd21bd0133b9ac957 > Reviewed-on: https://chromium-review.googlesource.com/c/1346462 > Commit-Queue: Olivier Robin > Reviewed-by: Eugene But > Cr-Commit-Position: refs/heads/master@{#610366} Tbr: eugenebut@chromium.org Bug: 896173 Change-Id: I713942404ec5ab47fb653f4fc2c04214f66fa866 Reviewed-on: https://chromium-review.googlesource.com/c/1348032 Reviewed-by: Olivier Robin Commit-Queue: Olivier Robin Cr-Commit-Position: refs/heads/master@{#610407} --- .../autofill/form_structure_browsertest.mm | 2 +- ios/web/public/test/web_test_with_web_state.h | 5 ++ .../public/test/web_test_with_web_state.mm | 52 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/ios/chrome/browser/autofill/form_structure_browsertest.mm b/ios/chrome/browser/autofill/form_structure_browsertest.mm index 1783bbe4666db8..7fa794119eb9f6 100644 --- a/ios/chrome/browser/autofill/form_structure_browsertest.mm +++ b/ios/chrome/browser/autofill/form_structure_browsertest.mm @@ -165,7 +165,7 @@ void FormStructureBrowserTest::GenerateResults(const std::string& input, std::string* output) { - ASSERT_TRUE(LoadHtml(input)); + ASSERT_TRUE(LoadHtmlWithoutSubresources(input)); base::TaskScheduler::GetInstance()->FlushForTesting(); web::WebFrame* frame = web::GetMainWebFrame(web_state()); AutofillManager* autofill_manager = diff --git a/ios/web/public/test/web_test_with_web_state.h b/ios/web/public/test/web_test_with_web_state.h index 0e5a0ba967fe20..bb08f6417dc085 100644 --- a/ios/web/public/test/web_test_with_web_state.h +++ b/ios/web/public/test/web_test_with_web_state.h @@ -47,6 +47,11 @@ class WebTestWithWebState : public WebTest, void LoadHtml(NSString* html); // Loads the specified HTML content into the WebState, using test url name. bool LoadHtml(const std::string& html) WARN_UNUSED_RESULT; + // Loads the specified HTML content with URL into the WebState. None of the + // subresources will be fetched. + // This function is only supported on iOS11+. On iOS10, this function simply + // calls |LoadHtml|. + bool LoadHtmlWithoutSubresources(const std::string& html); // Blocks until both known NSRunLoop-based and known message-loop-based // background tasks have completed void WaitForBackgroundTasks(); diff --git a/ios/web/public/test/web_test_with_web_state.mm b/ios/web/public/test/web_test_with_web_state.mm index b47b23d44161d0..6c85ea6335f839 100644 --- a/ios/web/public/test/web_test_with_web_state.mm +++ b/ios/web/public/test/web_test_with_web_state.mm @@ -4,6 +4,7 @@ #import "ios/web/public/test/web_test_with_web_state.h" +#include "base/ios/ios_util.h" #include "base/message_loop/message_loop_current.h" #include "base/run_loop.h" #include "base/scoped_observer.h" @@ -15,6 +16,7 @@ #include "ios/web/public/web_state/url_verification_constants.h" #include "ios/web/public/web_state/web_state_observer.h" #import "ios/web/web_state/ui/crw_web_controller.h" +#import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" #import "ios/web/web_state/web_state_impl.h" #if !defined(__has_feature) || !__has_feature(objc_arc) @@ -22,6 +24,7 @@ #endif using base::test::ios::WaitUntilConditionOrTimeout; +using base::test::ios::kWaitForActionTimeout; using base::test::ios::kWaitForJSCompletionTimeout; using base::test::ios::kWaitForPageLoadTimeout; @@ -75,6 +78,55 @@ .AddTransientItem(url); } +bool WebTestWithWebState::LoadHtmlWithoutSubresources(const std::string& html) { + if (@available(iOS 11, *)) { + NSString* block_all = @"[{" + " \"trigger\": {" + " \"url-filter\": \".*\"" + " }," + " \"action\": {" + " \"type\": \"block\"" + " }" + "}]"; + __block WKContentRuleList* content_rule_list = nil; + __block NSError* error = nil; + __block BOOL rule_compilation_completed = NO; + [WKContentRuleListStore.defaultStore + compileContentRuleListForIdentifier:@"block_everything" + encodedContentRuleList:block_all + completionHandler:^(WKContentRuleList* rule_list, + NSError* err) { + error = err; + content_rule_list = rule_list; + rule_compilation_completed = YES; + }]; + + bool success = WaitUntilConditionOrTimeout(kWaitForActionTimeout, ^bool { + return rule_compilation_completed; + }); + if (!success) { + DLOG(WARNING) << "ContentRuleList compilation timed out."; + return false; + } + if (error) { + DLOG(WARNING) << "ContentRuleList compilation failed with error: " + << base::SysNSStringToUTF8(error.description); + return false; + } + DCHECK(content_rule_list); + WKWebViewConfigurationProvider& configuration_provider = + WKWebViewConfigurationProvider::FromBrowserState(GetBrowserState()); + WKWebViewConfiguration* configuration = + configuration_provider.GetWebViewConfiguration(); + [configuration.userContentController addContentRuleList:content_rule_list]; + bool result = LoadHtml(html); + [configuration.userContentController + removeContentRuleList:content_rule_list]; + return result; + } + return LoadHtml(html); +} + void WebTestWithWebState::LoadHtml(NSString* html, const GURL& url) { // Sets MIME type to "text/html" once navigation is committed. class MimeTypeUpdater : public WebStateObserver {