OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/web/shell/test/utils/web_view_egutil.h" | |
6 | |
7 #import <WebKit/WebKit.h> | |
8 | |
9 #include "base/mac/bind_objc_block.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #import "base/test/ios/wait_util.h" | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
s/import/include
baxley
2016/04/21 21:52:51
Done.
| |
12 #include "base/values.h" | |
13 #import "ios/web/shell/view_controller.h" | |
14 #include "ios/web/shell/test/utils/navigation_egutil.h" | |
15 #include "ios/web/web_state/web_state_impl.h" | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Import only public API. Also this is import, not i
baxley
2016/04/21 21:52:51
Done.
| |
16 | |
17 namespace { | |
18 | |
19 // Constant for UI wait loop in seconds. | |
20 const NSTimeInterval kSpinDelaySeconds = 0.01; | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Can we move this to a common place? Seems like som
baxley
2016/04/21 21:52:52
After discussion, okay to leave here for now.
| |
21 | |
22 // Constant for timeout in seconds while waiting for web element. | |
23 const NSTimeInterval kWaitForWebElementTimeout = 4.0; | |
24 | |
25 // Script that returns document.body as a string. | |
26 std::string kGetDocumentBodyJavaScript = | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:56
Global objects are not allowed per C++ Style guide
baxley
2016/04/21 21:52:52
Done.
| |
27 "document.body ? document.body.textContent : null"; | |
28 } | |
29 | |
30 @implementation GREYMatchers (WebShellAdditions) | |
31 | |
32 + (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { | |
33 MatchesBlock matches = ^BOOL(UIView* view) { | |
34 if (![view isKindOfClass:[WKWebView class]]) { | |
35 return NO; | |
36 } | |
37 | |
38 ViewController* viewController = | |
39 (ViewController*)[[[[UIApplication sharedApplication] delegate] window] | |
40 rootViewController]; | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Can we have move finding view controller to a comm
baxley
2016/04/21 21:52:51
Done.
| |
41 DCHECK(viewController); | |
42 web::WebState* webState = [viewController webState]; | |
43 DCHECK(webState); | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
NIT: No need for this DCHECK, code will crash anyw
baxley
2016/04/21 21:52:51
Done.
| |
44 | |
45 __block BOOL didSucceed = NO; | |
46 void (^searchForTextBlock)(const base::Value* value) = | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Optional NIT: Is it possible to move block inline?
baxley
2016/04/21 21:52:51
Done.
| |
47 ^(const base::Value* value) { | |
48 std::string response; | |
49 if (value && value->IsType(base::Value::TYPE_STRING)) { | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:55
if (value && value->IsType(base::Value::TYPE_STRIN
baxley
2016/04/21 21:52:51
Done.
| |
50 value->GetAsString(&response); | |
51 if (response.find([text UTF8String]) == std::string::npos) { | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Please use sys_string_conversions instead of UTF8S
baxley
2016/04/21 21:52:51
Done.
| |
52 } else { | |
53 didSucceed = YES; | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:56
didSucceed = response.find([text UTF8String]) != s
baxley
2016/04/21 21:52:51
Done. I don't know what I was doing with this if b
| |
54 } | |
55 } | |
56 }; | |
57 NSDate* deadline = | |
58 [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; | |
59 while (!([[NSDate date] compare:deadline] == NSOrderedDescending) && | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
[[NSDate date] compare:deadline] != NSOrderedDesce
baxley
2016/04/21 21:52:51
Done.
| |
60 !didSucceed) { | |
61 webState->ExecuteJavaScript(base::UTF8ToUTF16(kGetDocumentBodyJavaScript), | |
62 base::BindBlock(searchForTextBlock)); | |
63 base::test::ios::SpinRunLoopWithMaxDelay( | |
64 base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); | |
65 } | |
66 return didSucceed; | |
67 }; | |
68 | |
69 DescribeToBlock describe = ^void(id<GREYDescription> description) { | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Optional NIT: Drop void for consistency with exist
baxley
2016/04/21 21:52:52
Done.
| |
70 [description | |
71 appendText:[NSString stringWithFormat:@"web view containing %@", text]]; | |
72 }; | |
73 | |
74 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
75 descriptionBlock:describe] | |
76 autorelease]; | |
Eugene But (OOO till 7-30)
2016/04/21 17:07:54
Optional NIT: Should we add a convenience function
baxley
2016/04/21 21:52:51
After offline discussion, leave as is for now.
| |
77 } | |
78 | |
79 @end | |
80 | |
81 #if !(GREY_DISABLE_SHORTHAND) | |
82 | |
83 id<GREYMatcher> shell_webViewContainingText(NSString* text) { | |
84 return [GREYMatchers matcherForWebViewContainingText:text]; | |
85 } | |
86 | |
87 #endif | |
OLD | NEW |