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..2edd3dd6bfb8af302efa55b83633c87c38a7d5be |
| --- /dev/null |
| +++ b/ios/web/shell/test/utils/web_view_egutil.mm |
| @@ -0,0 +1,84 @@ |
| +// 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> |
| + |
| +#import "base/test/ios/wait_util.h" |
| +#import "ios/web/shell/view_controller.h" |
| +#include "ios/web/shell/test/utils/eg_util.h" |
| + |
| +namespace { |
| + |
| +// Constant for UI wait loop in seconds. |
| +const NSTimeInterval kSpinDelaySeconds = 0.01; |
| + |
| +// Constant for timeout in seconds while waiting for web element. |
| +const NSTimeInterval kWaitForWebElementTimeout = 4.0; |
| + |
| +// Script that returns document.body as a string. |
| +NSString* const kGetDocumentBodyJavaScript = |
| + @"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; |
| + } |
| + |
| + CRWWebController* webController = earl_grey_util::GetCurrentWebController(); |
| + DCHECK(webController); |
| + |
| + __block id script = nil; |
|
Eugene But (OOO till 7-30)
2016/04/16 00:38:40
This is not |script|...
baxley
2016/04/21 16:09:17
A lot of this logic went away as I switched to Web
|
| + __block BOOL didSuceed = NO; |
| + __block BOOL timeout = NO; |
| + NSDate* deadline = |
| + [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; |
| + script = ^(NSString* evaluationResult, NSError* error) { |
|
Eugene But (OOO till 7-30)
2016/04/16 00:38:40
Please log |error| if any.
baxley
2016/04/21 16:09:17
N/A with new API
|
| + if (![evaluationResult containsString:text]) { |
| + if ([[NSDate date] compare:deadline] == NSOrderedDescending) { |
| + timeout = YES; |
| + return; |
| + } |
| + [webController evaluateJavaScript:kGetDocumentBodyJavaScript |
| + stringResultHandler:script]; |
| + } else { |
| + didSuceed = YES; |
| + } |
| + }; |
| + |
| + [webController evaluateJavaScript:kGetDocumentBodyJavaScript |
| + stringResultHandler:script]; |
| + while (!didSuceed && !timeout) { |
| + base::test::ios::SpinRunLoopWithMaxDelay( |
| + base::TimeDelta::FromSecondsD(kSpinDelaySeconds)); |
| + } |
| + return didSuceed; |
| + |
| + }; |
| + |
| + DescribeToBlock describe = ^void(id<GREYDescription> description) { |
| + [description |
| + appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
| + }; |
| + |
| + base::scoped_nsobject<GREYElementMatcherBlock> matcherBlock( |
|
Eugene But (OOO till 7-30)
2016/04/16 00:38:40
base::mac::ScopedBlock
Eugene But (OOO till 7-30)
2016/04/16 02:00:51
Oh, this is not a block... nevermind....
|
| + [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| + descriptionBlock:describe] retain]); |
|
Eugene But (OOO till 7-30)
2016/04/16 00:38:40
Blocks should be copied, not retained.
Eugene But (OOO till 7-30)
2016/04/16 00:38:40
This is a memory leak
Eugene But (OOO till 7-30)
2016/04/16 02:00:51
Ignore this as well :)
EG is so confusing. They u
rohitrao (ping after 24h)
2016/04/18 11:44:25
Isn't this still a leak? Alloc/init will return a
Eugene But (OOO till 7-30)
2016/04/18 15:22:54
"Ignore this as well" was related to copy vs. reta
baxley
2016/04/21 16:09:17
Done.
baxley
2016/04/21 16:09:17
Done.
|
| + return matcherBlock; |
| +} |
| + |
| +@end |
| + |
| +#if !(GREY_DISABLE_SHORTHAND) |
| + |
| +id<GREYMatcher> shell_webViewContainingText(NSString* text) { |
| + return [GREYMatchers matcherForWebViewContainingText:text]; |
| +} |
| + |
| +#endif |