Commit 72e18d26 authored by Rohit Rao's avatar Rohit Rao Committed by Commit Bot

[ios] Reworks ShellEarlGrey to meet the new EG helper guidelines.

shell_earl_grey.h is converted to be C++ functions in the "shell_test_util"
namespace.  shell_earl_grey_app_interface.h is introduced to contain the
ObjC functions that will eventually make up the EG2 cross-process API.

BUG=922813

Change-Id: Ifb419383e9c1dc2e69327914d00cd749318ff889
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1554053
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#648647}
parent 79d39f2b
......@@ -79,6 +79,8 @@ source_set("earl_grey_test_support") {
"earl_grey/shell_actions.mm",
"earl_grey/shell_earl_grey.h",
"earl_grey/shell_earl_grey.mm",
"earl_grey/shell_earl_grey_app_interface.h",
"earl_grey/shell_earl_grey_app_interface.mm",
"earl_grey/shell_matchers.h",
"earl_grey/shell_matchers.mm",
"earl_grey/shell_matchers_shorthand.h",
......
......@@ -54,10 +54,10 @@ const char kHtmlFile[] =
const char linkText[] = "normal-link-text";
const GURL pageURL = _server.GetURL(kHtmlFile);
bool success = [ShellEarlGrey loadURL:pageURL];
bool success = shell_test_util::LoadUrl(pageURL);
GREYAssert(success, @"Page did not complete loading.");
success = [ShellEarlGrey waitForWebViewContainingText:linkText];
success = shell_test_util::WaitForWebViewContainingText(linkText);
GREYAssert(success, @"Failed waiting for web view containing '%s'", linkText);
[[EarlGrey selectElementWithMatcher:web::WebView()]
......@@ -85,9 +85,9 @@ const char kHtmlFile[] =
const char linkText[] = "no-webkit-link-text";
const GURL pageURL = _server.GetURL(kHtmlFile);
bool success = [ShellEarlGrey loadURL:pageURL];
bool success = shell_test_util::LoadUrl(pageURL);
GREYAssert(success, @"Page did not complete loading.");
success = [ShellEarlGrey waitForWebViewContainingText:linkText];
success = shell_test_util::WaitForWebViewContainingText(linkText);
GREYAssert(success, @"Failed waiting for web view containing '%s'", linkText);
[[EarlGrey selectElementWithMatcher:web::WebView()]
......
......@@ -13,18 +13,18 @@
// Test methods that perform actions on Web Shell. These methods may read or
// alter Web Shell's internal state programmatically or via the UI, but in both
// cases will properly synchronize the UI for Earl Grey tests.
@interface ShellEarlGrey : NSObject
namespace shell_test_util {
// Loads |URL| in the current WebState with transition of type
// ui::PAGE_TRANSITION_TYPED, and waits for the page to complete loading, or
// a timeout. Returns false if the page doesn't finish loading, true if it
// does.
+ (bool)loadURL:(const GURL&)URL WARN_UNUSED_RESULT;
bool LoadUrl(const GURL& url) WARN_UNUSED_RESULT;
// Waits for the current web view to contain |text|. If the condition is not met
// within a timeout, it returns false, otherwise, true.
+ (bool)waitForWebViewContainingText:(const std::string)text WARN_UNUSED_RESULT;
bool WaitForWebViewContainingText(const std::string& text) WARN_UNUSED_RESULT;
@end
} // namespace shell_test_util
#endif // IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_H_
......@@ -6,15 +6,11 @@
#import <EarlGrey/EarlGrey.h>
#import "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#import "ios/web/public/test/earl_grey/js_test_util.h"
#include "ios/web/public/test/element_selector.h"
#import "ios/web/public/test/navigation_test_util.h"
#import "ios/web/public/test/web_view_content_test_util.h"
#import "ios/web/public/test/web_view_interaction_test_util.h"
#include "ios/web/shell/test/app/navigation_test_util.h"
#import "ios/web/shell/test/app/web_shell_test_util.h"
#import "ios/web/shell/test/earl_grey/shell_earl_grey_app_interface.h"
using base::test::ios::kWaitForPageLoadTimeout;
using base::test::ios::kWaitForUIElementTimeout;
using base::test::ios::WaitUntilConditionOrTimeout;
......@@ -22,19 +18,22 @@ using base::test::ios::WaitUntilConditionOrTimeout;
#error "This file requires ARC support."
#endif
@implementation ShellEarlGrey
namespace shell_test_util {
+ (bool)loadURL:(const GURL&)URL {
web::shell_test_util::LoadUrl(URL);
web::WebState* webState = web::shell_test_util::GetCurrentWebState();
bool LoadUrl(const GURL& url) {
[ShellEarlGreyAppInterface loadURL:base::SysUTF8ToNSString(url.spec())];
if (!web::test::WaitForPageToFinishLoading(webState))
bool load_success = WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
return ![ShellEarlGreyAppInterface isCurrentWebStateLoading];
});
if (!load_success) {
return false;
}
if (webState->ContentIsHTML()) {
if (!web::WaitUntilWindowIdInjected(webState)) {
return false;
}
bool injection_success =
[ShellEarlGreyAppInterface waitForWindowIDInjectedInCurrentWebState];
if (!injection_success) {
return false;
}
// Ensure any UI elements handled by EarlGrey become idle for any subsequent
......@@ -43,11 +42,11 @@ using base::test::ios::WaitUntilConditionOrTimeout;
return true;
}
+ (bool)waitForWebViewContainingText:(std::string)text {
bool WaitForWebViewContainingText(const std::string& text) {
return WaitUntilConditionOrTimeout(kWaitForUIElementTimeout, ^bool {
return web::test::IsWebViewContainingText(
web::shell_test_util::GetCurrentWebState(), text);
return [ShellEarlGreyAppInterface
currentWebStateContainsText:base::SysUTF8ToNSString(text)];
});
}
@end
} // namespace shell_test_util
// Copyright 2019 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.
#ifndef IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_APP_INTERFACE_H_
#define IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_APP_INTERFACE_H_
#import <Foundation/Foundation.h>
#include "base/compiler_specific.h"
// Test methods that perform actions on Web Shell. These methods may read or
// alter Web Shell's internal state programmatically or via the UI, but in both
// cases will properly synchronize the UI for Earl Grey tests.
@interface ShellEarlGreyAppInterface : NSObject
// Loads |URL| in the current WebState with transition of type
// ui::PAGE_TRANSITION_TYPED and returns without waiting for the page to load.
+ (void)loadURL:(NSString*)spec;
// Returns YES if the current WebState is loading.
+ (BOOL)isCurrentWebStateLoading WARN_UNUSED_RESULT;
// Returns YES if the windowID has been injected into the current web state. If
// the WebState contains content that does not require windowID injection,
// returns YES immediately.
+ (BOOL)waitForWindowIDInjectedInCurrentWebState WARN_UNUSED_RESULT;
// Returns YES if the current WebState contains the given |text|.
+ (BOOL)currentWebStateContainsText:(NSString*)text WARN_UNUSED_RESULT;
@end
#endif // IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_APP_INTERFACE_H_
// Copyright 2019 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/earl_grey/shell_earl_grey_app_interface.h"
#import <EarlGrey/EarlGrey.h>
#import "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#import "ios/web/public/test/earl_grey/js_test_util.h"
#import "ios/web/public/test/navigation_test_util.h"
#import "ios/web/public/test/web_view_content_test_util.h"
#import "ios/web/public/web_state/web_state.h"
#import "ios/web/shell/test/app/web_shell_test_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using web::shell_test_util::GetCurrentWebState;
@implementation ShellEarlGreyAppInterface
+ (void)loadURL:(NSString*)spec {
web::test::LoadUrl(GetCurrentWebState(), GURL(base::SysNSStringToUTF8(spec)));
}
+ (BOOL)isCurrentWebStateLoading {
return GetCurrentWebState()->IsLoading();
}
+ (BOOL)waitForWindowIDInjectedInCurrentWebState {
return web::WaitUntilWindowIdInjected(GetCurrentWebState());
}
+ (BOOL)currentWebStateContainsText:(NSString*)text {
return web::test::IsWebViewContainingText(GetCurrentWebState(),
base::SysNSStringToUTF8(text));
}
@end
......@@ -55,7 +55,7 @@ void WaitForOffset(CGFloat y_offset) {
// be {0, 0} before returning.
void ScrollLongPageToTop(const GURL& url) {
// Load the page and swipe down.
bool success = [ShellEarlGrey loadURL:url];
bool success = shell_test_util::LoadUrl(url);
GREYAssert(success, @"Page did not complete loading.");
[[EarlGrey selectElementWithMatcher:web::WebViewScrollView()]
performAction:grey_scrollToContentEdge(kGREYContentEdgeTop)];
......@@ -113,7 +113,7 @@ void ScrollLongPageToTop(const GURL& url) {
- (void)testZeroContentOffsetAfterLoad {
// Set up the file-based server to load the tall page.
const GURL baseURL = _server.GetURL(kLongPage1);
bool success = [ShellEarlGrey loadURL:baseURL];
bool success = shell_test_util::LoadUrl(baseURL);
GREYAssert(success, @"Page did not complete loading.");
// Scroll the page and load again to verify that the new page's scroll offset
......@@ -127,8 +127,9 @@ void ScrollLongPageToTop(const GURL& url) {
// Add a query parameter so the next load creates another NavigationItem.
GURL::Replacements replacements;
replacements.SetQueryStr(base::NumberToString(i));
GREYAssert([ShellEarlGrey loadURL:baseURL.ReplaceComponents(replacements)],
@"Page did not complete loading.");
bool success =
shell_test_util::LoadUrl(baseURL.ReplaceComponents(replacements));
GREYAssert(success, @"Page did not complete loading.");
// Wait for the content offset to be set to {0, 0}.
WaitForOffset(0.0);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment