Skip to content

Commit

Permalink
Reland "[Reland] Block resources in FormStructureBrowserTest."
Browse files Browse the repository at this point in the history
This is a reland of d15dd85

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 <[email protected]>
> Reviewed-by: Eugene But <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#610366}

Tbr: [email protected]
Bug: 896173
Change-Id: I713942404ec5ab47fb653f4fc2c04214f66fa866
Reviewed-on: https://chromium-review.googlesource.com/c/1348032
Reviewed-by: Olivier Robin <[email protected]>
Commit-Queue: Olivier Robin <[email protected]>
Cr-Commit-Position: refs/heads/master@{#610407}
  • Loading branch information
Olivier Robin authored and Commit Bot committed Nov 22, 2018
1 parent f0bf740 commit 7cbbce3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ios/chrome/browser/autofill/form_structure_browsertest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
5 changes: 5 additions & 0 deletions ios/web/public/test/web_test_with_web_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
52 changes: 52 additions & 0 deletions ios/web/public/test/web_test_with_web_state.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,13 +16,15 @@
#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)
#error "This file requires ARC support."
#endif

using base::test::ios::WaitUntilConditionOrTimeout;
using base::test::ios::kWaitForActionTimeout;
using base::test::ios::kWaitForJSCompletionTimeout;
using base::test::ios::kWaitForPageLoadTimeout;

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7cbbce3

Please sign in to comment.