| Index: ios/chrome/test/app/static_html_view_test_util.mm
|
| diff --git a/ios/chrome/test/app/static_html_view_test_util.mm b/ios/chrome/test/app/static_html_view_test_util.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9daa7a0daeb907af6ea1442c03a46203a0e66a70
|
| --- /dev/null
|
| +++ b/ios/chrome/test/app/static_html_view_test_util.mm
|
| @@ -0,0 +1,62 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#import "ios/chrome/test/app/static_html_view_test_util.h"
|
| +
|
| +#import <Foundation/Foundation.h>
|
| +
|
| +#include "base/mac/foundation_util.h"
|
| +#include "base/strings/sys_string_conversions.h"
|
| +#import "ios/chrome/browser/ui/static_content/static_html_view_controller.h"
|
| +#import "ios/chrome/test/app/chrome_test_util.h"
|
| +#import "ios/testing/wait_util.h"
|
| +
|
| +namespace chrome_test_util {
|
| +
|
| +// Synchronously returns the result of executed JavaScript.
|
| +id ExecuteScriptInStaticController(
|
| + StaticHtmlViewController* html_view_controller,
|
| + NSString* script) {
|
| + __block id result = nil;
|
| + __block bool did_finish = false;
|
| + [html_view_controller executeJavaScript:script
|
| + completionHandler:^(id script_result, NSError* error) {
|
| + result = [script_result copy];
|
| + did_finish = true;
|
| + }];
|
| +
|
| + // If a timeout is reached, then return |result|, which should be nil;
|
| + testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, ^{
|
| + return did_finish;
|
| + });
|
| +
|
| + return [result autorelease];
|
| +}
|
| +
|
| +// Returns the StaticHtmlViewController for the given |web_state|. If none is
|
| +// found, it returns nil.
|
| +StaticHtmlViewController* GetStaticHtmlView(web::WebState* web_state) {
|
| + // The WKWebView in a static HTML view isn't part of a
|
| + // webState, but it does have the StaticHtmlViewController
|
| + // as its navigation delegate. The WKWebView is the only child subview
|
| + // of the web state's view.
|
| + UIView* web_state_view = chrome_test_util::GetCurrentWebState()->GetView();
|
| + WKWebView* web_view =
|
| + base::mac::ObjCCast<WKWebView>(web_state_view.subviews.firstObject);
|
| + return base::mac::ObjCCast<StaticHtmlViewController>(
|
| + web_view.navigationDelegate);
|
| +}
|
| +
|
| +bool StaticHtmlViewContainingText(web::WebState* web_state, std::string text) {
|
| + StaticHtmlViewController* html_view_controller = GetStaticHtmlView(web_state);
|
| + if (!html_view_controller) {
|
| + return false;
|
| + }
|
| +
|
| + id body = ExecuteScriptInStaticController(
|
| + html_view_controller, @"document.body ? document.body.textContent : ''");
|
| + return [body containsString:base::SysUTF8ToNSString(text)];
|
| + return false;
|
| +}
|
| +} // namespace chrome_test_util
|
|
|