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" |
+#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" |
+ |
+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. |
+std::string 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; |
+ } |
+ |
+ ViewController* viewController = |
+ (ViewController*)[[[[UIApplication sharedApplication] delegate] window] |
+ rootViewController]; |
+ DCHECK(viewController); |
+ web::WebState* webState = [viewController webState]; |
+ DCHECK(webState); |
+ |
+ __block BOOL didSucceed = NO; |
+ void (^searchForTextBlock)(const base::Value* value) = |
+ ^(const base::Value* value) { |
+ std::string response; |
+ if (value && value->IsType(base::Value::TYPE_STRING)) { |
+ value->GetAsString(&response); |
+ if (response.find([text UTF8String]) == std::string::npos) { |
+ } else { |
+ didSucceed = YES; |
+ } |
+ } |
+ }; |
+ NSDate* deadline = |
+ [NSDate dateWithTimeIntervalSinceNow:kWaitForWebElementTimeout]; |
+ while (!([[NSDate date] compare:deadline] == NSOrderedDescending) && |
+ !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) { |
+ [description |
+ appendText:[NSString stringWithFormat:@"web view containing %@", text]]; |
+ }; |
+ |
+ return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
+ descriptionBlock:describe] |
+ autorelease]; |
+} |
+ |
+@end |
+ |
+#if !(GREY_DISABLE_SHORTHAND) |
+ |
+id<GREYMatcher> shell_webViewContainingText(NSString* text) { |
+ return [GREYMatchers matcherForWebViewContainingText:text]; |
+} |
+ |
+#endif |