Commit 44c61e5b authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

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: I78f180aca959c50996b1af932cc31c1690795038
Reviewed-on: https://chromium-review.googlesource.com/c/1343089
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarMoe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610079}
parent 96e464af
...@@ -165,7 +165,7 @@ void FormStructureBrowserTest::TearDown() { ...@@ -165,7 +165,7 @@ void FormStructureBrowserTest::TearDown() {
void FormStructureBrowserTest::GenerateResults(const std::string& input, void FormStructureBrowserTest::GenerateResults(const std::string& input,
std::string* output) { std::string* output) {
ASSERT_TRUE(LoadHtml(input)); ASSERT_TRUE(LoadHtmlWithoutSubresources(input));
base::TaskScheduler::GetInstance()->FlushForTesting(); base::TaskScheduler::GetInstance()->FlushForTesting();
web::WebFrame* frame = web::GetMainWebFrame(web_state()); web::WebFrame* frame = web::GetMainWebFrame(web_state());
AutofillManager* autofill_manager = AutofillManager* autofill_manager =
......
...@@ -47,6 +47,11 @@ class WebTestWithWebState : public WebTest, ...@@ -47,6 +47,11 @@ class WebTestWithWebState : public WebTest,
void LoadHtml(NSString* html); void LoadHtml(NSString* html);
// Loads the specified HTML content into the WebState, using test url name. // Loads the specified HTML content into the WebState, using test url name.
bool LoadHtml(const std::string& html) WARN_UNUSED_RESULT; 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 // Blocks until both known NSRunLoop-based and known message-loop-based
// background tasks have completed // background tasks have completed
void WaitForBackgroundTasks(); void WaitForBackgroundTasks();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#import "ios/web/public/test/web_test_with_web_state.h" #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/message_loop/message_loop_current.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
#include "ios/web/public/web_state/url_verification_constants.h" #include "ios/web/public/web_state/url_verification_constants.h"
#include "ios/web/public/web_state/web_state_observer.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/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" #import "ios/web/web_state/web_state_impl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -22,6 +24,7 @@ ...@@ -22,6 +24,7 @@
#endif #endif
using base::test::ios::WaitUntilConditionOrTimeout; using base::test::ios::WaitUntilConditionOrTimeout;
using base::test::ios::kWaitForActionTimeout;
using base::test::ios::kWaitForJSCompletionTimeout; using base::test::ios::kWaitForJSCompletionTimeout;
using base::test::ios::kWaitForPageLoadTimeout; using base::test::ios::kWaitForPageLoadTimeout;
...@@ -75,6 +78,55 @@ void WebTestWithWebState::AddTransientItem(const GURL& url) { ...@@ -75,6 +78,55 @@ void WebTestWithWebState::AddTransientItem(const GURL& url) {
.AddTransientItem(url); .AddTransientItem(url);
} }
bool WebTestWithWebState::LoadHtmlWithoutSubresources(const std::string& html) {
if (!base::ios::IsRunningOnIOS11OrLater()) {
return LoadHtml(html);
}
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, ^{
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);
web::WKWebViewConfigurationProvider& configuration_provider =
web::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;
}
void WebTestWithWebState::LoadHtml(NSString* html, const GURL& url) { void WebTestWithWebState::LoadHtml(NSString* html, const GURL& url) {
// Sets MIME type to "text/html" once navigation is committed. // Sets MIME type to "text/html" once navigation is committed.
class MimeTypeUpdater : public WebStateObserver { class MimeTypeUpdater : public WebStateObserver {
......
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