123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // 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/web_view/test/web_view_test_util.h"
- #import "ios/web_view/public/cwv_web_view.h"
- #import "ios/web_view/public/cwv_web_view_configuration.h"
- #import "base/test/ios/wait_util.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::kWaitForPageLoadTimeout;
- using base::test::ios::kWaitForJSCompletionTimeout;
- using base::test::ios::kWaitForUIElementTimeout;
- namespace ios_web_view {
- namespace test {
- CWVWebView* CreateWebView() {
- return [[CWVWebView alloc]
- initWithFrame:UIScreen.mainScreen.bounds
- configuration:[CWVWebViewConfiguration defaultConfiguration]];
- }
- bool LoadUrl(CWVWebView* web_view, NSURL* url) {
- [web_view loadRequest:[NSURLRequest requestWithURL:url]];
- return WaitForWebViewLoadCompletionOrTimeout(web_view);
- }
- bool TapWebViewElementWithId(CWVWebView* web_view, NSString* element_id) {
- NSString* script = [NSString
- stringWithFormat:@"(function() {"
- " var element = document.getElementById('%@');"
- " if (element) {"
- " element.click();"
- " return true;"
- " }"
- " return false;"
- "})();",
- element_id];
- return [EvaluateJavaScript(web_view, script) boolValue];
- }
- id EvaluateJavaScript(CWVWebView* web_view, NSString* script, bool* success) {
- __block bool callback_called = false;
- __block id evaluation_result = nil;
- __block bool evaluation_success = false;
- [web_view evaluateJavaScript:script
- completion:^(id local_result, BOOL local_success) {
- callback_called = true;
- evaluation_result = [local_result copy];
- evaluation_success = local_success;
- }];
- bool completed = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
- return callback_called;
- });
- if (success) {
- *success = evaluation_success;
- }
- return completed ? evaluation_result : nil;
- }
- bool WaitForWebViewContainingTextOrTimeout(CWVWebView* web_view,
- NSString* text) {
- // Wait for load to stop because a new load may have just started.
- if (!WaitForWebViewLoadCompletionOrTimeout(web_view)) {
- return false;
- }
- return WaitUntilConditionOrTimeout(kWaitForUIElementTimeout, ^{
- id body = ios_web_view::test::EvaluateJavaScript(
- web_view, @"document.body ? document.body.textContent : null");
- return [body isKindOfClass:[NSString class]] && [body containsString:text];
- });
- }
- bool WaitForWebViewLoadCompletionOrTimeout(CWVWebView* web_view) {
- return WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
- return !web_view.loading;
- });
- }
- void CopyWebViewState(CWVWebView* source_web_view,
- CWVWebView* destination_web_view) {
- NSKeyedArchiver* archiver =
- [[NSKeyedArchiver alloc] initRequiringSecureCoding:NO];
- [source_web_view encodeRestorableStateWithCoder:archiver];
- NSKeyedUnarchiver* unarchiver =
- [[NSKeyedUnarchiver alloc] initForReadingFromData:[archiver encodedData]
- error:nil];
- unarchiver.requiresSecureCoding = NO;
- [destination_web_view decodeRestorableStateWithCoder:unarchiver];
- }
- } // namespace test
- } // namespace ios_web_view
|