Chromium Code Reviews| 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..65f2fad943e116c40d6c830831c03183ae612b90 |
| --- /dev/null |
| +++ b/ios/chrome/test/app/static_html_view_test_util.mm |
| @@ -0,0 +1,78 @@ |
| +// 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 { |
| + |
| +// Script that returns document.body as a string. |
| +NSString* const kGetDocumentBodyJavaScript = |
| + @"document.body ? document.body.textContent : null"; |
| + |
| +// Synchronously returns the result of executed JavaScript. |
| +id ExecuteScriptInStaticController( |
| + StaticHtmlViewController* html_view_controller, |
| + NSString* script) { |
| + __block id result = nil; |
| + __block bool did_finish = false; |
| + web::JavaScriptResultBlock completion_handler = |
|
Eugene But (OOO till 7-30)
2017/03/31 22:19:17
Optional nit: do you want to fold this block into
baxley
2017/04/03 18:10:41
Done.
|
| + ^(id script_result, NSError* error) { |
| + result = [script_result copy]; |
|
Eugene But (OOO till 7-30)
2017/03/31 22:19:17
This looks like a leak to me
baxley
2017/04/03 18:10:41
Done. Nice Catch!
|
| + did_finish = true; |
| + }; |
| + [html_view_controller executeJavaScript:script |
| + completionHandler:completion_handler]; |
| + |
| + // If a timeout is reached, then return |result|, which should be nil; |
| + testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, ^{ |
| + return did_finish; |
| + }); |
| + |
| + return result; |
| +} |
| + |
| +// 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(); |
| + if (web_state_view.subviews.count == 0) { |
| + return nil; |
| + } |
| + WKWebView* web_view = |
| + base::mac::ObjCCast<WKWebView>(web_state_view.subviews[0]); |
|
Eugene But (OOO till 7-30)
2017/03/31 22:19:17
Do you want to do web_state_view.subviews.forstObj
baxley
2017/04/03 18:10:41
I had the if condition above to prevent that, but
|
| + StaticHtmlViewController* html_view_controller = nil; |
|
Eugene But (OOO till 7-30)
2017/03/31 22:19:17
I think the following code will work correctly for
baxley
2017/04/03 18:10:41
Done.
|
| + if (web_view && [web_view.navigationDelegate |
| + isKindOfClass:[StaticHtmlViewController class]]) { |
| + html_view_controller = base::mac::ObjCCast<StaticHtmlViewController>( |
| + web_view.navigationDelegate); |
| + } |
| + return html_view_controller; |
| +} |
| + |
| +bool StaticHtmlViewContainingText(web::WebState* web_state, std::string text) { |
| + StaticHtmlViewController* html_view_controller = GetStaticHtmlView(web_state); |
| + if (!html_view_controller) { |
| + return false; |
| + } |
| + |
| + id result = ExecuteScriptInStaticController(html_view_controller, |
| + kGetDocumentBodyJavaScript); |
| + if ([result isKindOfClass:[NSString class]]) { |
|
Eugene But (OOO till 7-30)
2017/03/31 22:19:17
You can simplify this code as follows:
id body =
baxley
2017/04/03 18:10:41
Done.
|
| + return [result containsString:base::SysUTF8ToNSString(text)]; |
| + } |
| + return false; |
| +} |
| +} // namespace chrome_test_util |