Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/chrome/test/app/static_html_view_test_util.h" | |
| 6 | |
| 7 #import <Foundation/Foundation.h> | |
| 8 | |
| 9 #include "base/mac/foundation_util.h" | |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 11 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" | |
| 12 #import "ios/chrome/test/app/chrome_test_util.h" | |
| 13 #import "ios/testing/wait_util.h" | |
| 14 | |
| 15 namespace chrome_test_util { | |
| 16 | |
| 17 // Script that returns document.body as a string. | |
| 18 NSString* const kGetDocumentBodyJavaScript = | |
| 19 @"document.body ? document.body.textContent : null"; | |
| 20 | |
| 21 // Synchronously returns the result of executed JavaScript. | |
| 22 id ExecuteScriptInStaticController( | |
| 23 StaticHtmlViewController* html_view_controller, | |
| 24 NSString* script) { | |
| 25 __block id result = nil; | |
| 26 __block bool did_finish = false; | |
| 27 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.
| |
| 28 ^(id script_result, NSError* error) { | |
| 29 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!
| |
| 30 did_finish = true; | |
| 31 }; | |
| 32 [html_view_controller executeJavaScript:script | |
| 33 completionHandler:completion_handler]; | |
| 34 | |
| 35 // If a timeout is reached, then return |result|, which should be nil; | |
| 36 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, ^{ | |
| 37 return did_finish; | |
| 38 }); | |
| 39 | |
| 40 return result; | |
| 41 } | |
| 42 | |
| 43 // Returns the StaticHtmlViewController for the given |web_state|. If none is | |
| 44 // found, it returns nil. | |
| 45 StaticHtmlViewController* GetStaticHtmlView(web::WebState* web_state) { | |
| 46 // The WKWebView in a static HTML view isn't part of a | |
| 47 // webState, but it does have the StaticHtmlViewController | |
| 48 // as its navigation delegate. The WKWebView is the only child subview | |
| 49 // of the web state's view. | |
| 50 UIView* web_state_view = chrome_test_util::GetCurrentWebState()->GetView(); | |
| 51 if (web_state_view.subviews.count == 0) { | |
| 52 return nil; | |
| 53 } | |
| 54 WKWebView* web_view = | |
| 55 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
| |
| 56 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.
| |
| 57 if (web_view && [web_view.navigationDelegate | |
| 58 isKindOfClass:[StaticHtmlViewController class]]) { | |
| 59 html_view_controller = base::mac::ObjCCast<StaticHtmlViewController>( | |
| 60 web_view.navigationDelegate); | |
| 61 } | |
| 62 return html_view_controller; | |
| 63 } | |
| 64 | |
| 65 bool StaticHtmlViewContainingText(web::WebState* web_state, std::string text) { | |
| 66 StaticHtmlViewController* html_view_controller = GetStaticHtmlView(web_state); | |
| 67 if (!html_view_controller) { | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 id result = ExecuteScriptInStaticController(html_view_controller, | |
| 72 kGetDocumentBodyJavaScript); | |
| 73 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.
| |
| 74 return [result containsString:base::SysUTF8ToNSString(text)]; | |
| 75 } | |
| 76 return false; | |
| 77 } | |
| 78 } // namespace chrome_test_util | |
| OLD | NEW |