Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Unified Diff: ios/web/shell/test/utils/web_view_egutil.mm

Issue 1890333002: Web shell test to go back and forward. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bd0ae3ad57ac48b9f9dc163ffc0e2608c6b50140
--- /dev/null
+++ b/ios/web/shell/test/utils/web_view_egutil.mm
@@ -0,0 +1,83 @@
+// 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.
+const NSTimeInterval kSpinDelaySeconds = 0.01;
+
+// Constant for timeout while waiting for web element.
+const NSTimeInterval kMaxTimeout = 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;
+ __block BOOL didSuceed = NO;
+ __block BOOL timeout = NO;
+ NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:kMaxTimeout];
+ script = ^(NSString* evaluationResult, NSError* error) {
+ 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(
+ [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
+ descriptionBlock:describe] retain]);
+ return matcherBlock;
+}
+
+@end
+
+#if !(GREY_DISABLE_SHORTHAND)
+
+id<GREYMatcher> shell_webViewContainingText(NSString* text) {
+ return [GREYMatchers matcherForWebViewContainingText:text];
+}
+
+#endif

Powered by Google App Engine
This is Rietveld 408576698