Chromium Code Reviews| Index: ios/web/shell/test/utils/web_view_egutil.mm |
| diff --git a/ios/web/shell/test/utils/web_view_egutil.mm b/ios/web/shell/test/utils/web_view_egutil.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cbb88fa53a9c8a3b04aa8912117f04a78a17ead8 |
| --- /dev/null |
| +++ b/ios/web/shell/test/utils/web_view_egutil.mm |
| @@ -0,0 +1,87 @@ |
| +// Copyright 2016 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/shell/test/utils/web_view_egutil.h" |
| + |
| +#import <WebKit/WebKit.h> |
| + |
| +#include "base/mac/bind_objc_block.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#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.
|
| +#include "base/values.h" |
| +#import "ios/web/shell/view_controller.h" |
| +#include "ios/web/shell/test/utils/navigation_egutil.h" |
| +#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.
|
| + |
| +namespace { |
| + |
| +// Constant for UI wait loop in seconds. |
| +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.
|
| + |
| +// Constant for timeout in seconds while waiting for web element. |
| +const NSTimeInterval kWaitForWebElementTimeout = 4.0; |
| + |
| +// Script that returns document.body as a string. |
| +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.
|
| + "document.body ? document.body.textContent : null"; |
| +} |
| + |
| +@implementation GREYMatchers (WebShellAdditions) |
| + |
| ++ (id<GREYMatcher>)matcherForWebViewContainingText:(NSString*)text { |
| + MatchesBlock matches = ^BOOL(UIView* view) { |
| + if (![view isKindOfClass:[WKWebView class]]) { |
| + return NO; |
| + } |
| + |
| + ViewController* viewController = |
| + (ViewController*)[[[[UIApplication sharedApplication] delegate] window] |
| + 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.
|
| + DCHECK(viewController); |
| + web::WebState* webState = [viewController webState]; |
| + 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.
|
| + |
| + __block BOOL didSucceed = NO; |
| + 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.
|
| + ^(const base::Value* value) { |
| + std::string response; |
| + 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.
|
| + value->GetAsString(&response); |
| + 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.
|
| + } else { |
| + 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
|
| + } |
| + } |
| + }; |
| + NSDate* deadline = |
| + [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; |
| + 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.
|
| + !didSucceed) { |
| + webState->ExecuteJavaScript(base::UTF8ToUTF16(kGetDocumentBodyJavaScript), |
| + base::BindBlock(searchForTextBlock)); |
| + base::test::ios::SpinRunLoopWithMaxDelay( |
| + base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); |
| + } |
| + return didSucceed; |
| + }; |
| + |
| + 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.
|
| + [description |
| + appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
| + }; |
| + |
| + return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| + descriptionBlock:describe] |
| + 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.
|
| +} |
| + |
| +@end |
| + |
| +#if !(GREY_DISABLE_SHORTHAND) |
| + |
| +id<GREYMatcher> shell_webViewContainingText(NSString* text) { |
| + return [GREYMatchers matcherForWebViewContainingText:text]; |
| +} |
| + |
| +#endif |