OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/test/app/static_html_view_test_util.h" | 5 #import "ios/chrome/test/app/static_html_view_test_util.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" | 11 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" |
12 #import "ios/chrome/test/app/chrome_test_util.h" | 12 #import "ios/chrome/test/app/chrome_test_util.h" |
13 #import "ios/testing/wait_util.h" | 13 #import "ios/testing/wait_util.h" |
14 | 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
15 namespace chrome_test_util { | 19 namespace chrome_test_util { |
16 | 20 |
17 // Synchronously returns the result of executed JavaScript. | 21 // Synchronously returns the result of executed JavaScript. |
18 id ExecuteScriptInStaticController( | 22 id ExecuteScriptInStaticController( |
19 StaticHtmlViewController* html_view_controller, | 23 StaticHtmlViewController* html_view_controller, |
20 NSString* script) { | 24 NSString* script) { |
21 __block id result = nil; | 25 __block id result = nil; |
22 __block bool did_finish = false; | 26 __block bool did_finish = false; |
23 [html_view_controller executeJavaScript:script | 27 [html_view_controller executeJavaScript:script |
24 completionHandler:^(id script_result, NSError* error) { | 28 completionHandler:^(id script_result, NSError* error) { |
25 result = [script_result copy]; | 29 result = [script_result copy]; |
26 did_finish = true; | 30 did_finish = true; |
27 }]; | 31 }]; |
28 | 32 |
29 // If a timeout is reached, then return |result|, which should be nil; | 33 // If a timeout is reached, then return |result|, which should be nil; |
30 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, ^{ | 34 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, ^{ |
31 return did_finish; | 35 return did_finish; |
32 }); | 36 }); |
33 | 37 |
34 return [result autorelease]; | 38 return result; |
35 } | 39 } |
36 | 40 |
37 // Returns the StaticHtmlViewController for the given |web_state|. If none is | 41 // Returns the StaticHtmlViewController for the given |web_state|. If none is |
38 // found, it returns nil. | 42 // found, it returns nil. |
39 StaticHtmlViewController* GetStaticHtmlView(web::WebState* web_state) { | 43 StaticHtmlViewController* GetStaticHtmlView(web::WebState* web_state) { |
40 // The WKWebView in a static HTML view isn't part of a | 44 // The WKWebView in a static HTML view isn't part of a |
41 // webState, but it does have the StaticHtmlViewController | 45 // webState, but it does have the StaticHtmlViewController |
42 // as its navigation delegate. The WKWebView is the only child subview | 46 // as its navigation delegate. The WKWebView is the only child subview |
43 // of the web state's view. | 47 // of the web state's view. |
44 UIView* web_state_view = chrome_test_util::GetCurrentWebState()->GetView(); | 48 UIView* web_state_view = chrome_test_util::GetCurrentWebState()->GetView(); |
45 WKWebView* web_view = | 49 WKWebView* web_view = |
46 base::mac::ObjCCast<WKWebView>(web_state_view.subviews.firstObject); | 50 base::mac::ObjCCast<WKWebView>(web_state_view.subviews.firstObject); |
47 return base::mac::ObjCCast<StaticHtmlViewController>( | 51 return base::mac::ObjCCast<StaticHtmlViewController>( |
48 web_view.navigationDelegate); | 52 web_view.navigationDelegate); |
49 } | 53 } |
50 | 54 |
51 bool StaticHtmlViewContainingText(web::WebState* web_state, std::string text) { | 55 bool StaticHtmlViewContainingText(web::WebState* web_state, std::string text) { |
52 StaticHtmlViewController* html_view_controller = GetStaticHtmlView(web_state); | 56 StaticHtmlViewController* html_view_controller = GetStaticHtmlView(web_state); |
53 if (!html_view_controller) { | 57 if (!html_view_controller) { |
54 return false; | 58 return false; |
55 } | 59 } |
56 | 60 |
57 id body = ExecuteScriptInStaticController( | 61 id body = ExecuteScriptInStaticController( |
58 html_view_controller, @"document.body ? document.body.textContent : ''"); | 62 html_view_controller, @"document.body ? document.body.textContent : ''"); |
59 return [body containsString:base::SysUTF8ToNSString(text)]; | 63 return [body containsString:base::SysUTF8ToNSString(text)]; |
60 return false; | 64 return false; |
61 } | 65 } |
62 } // namespace chrome_test_util | 66 } // namespace chrome_test_util |
OLD | NEW |