Commit 1c8da5b5 authored by Ali Juma's avatar Ali Juma Committed by Commit Bot

Make window_open_by_dom_egtest.mm compatible with EG2

This adds the file as an EG2 target, updates #includes, and replaces
calls to helpers that can't be used in EG2.

This also adds a webStateLastCommittedURL method to ChromeEarlGrey.

Bug: 987646
Change-Id: Ib98baad37b422b36a216b7915a0594b0e76c4062
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1947048Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Ali Juma <ajuma@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721126}
parent 576773e7
...@@ -398,6 +398,7 @@ source_set("eg2_tests") { ...@@ -398,6 +398,7 @@ source_set("eg2_tests") {
"stop_loading_egtest.mm", "stop_loading_egtest.mm",
"tab_order_egtest.mm", "tab_order_egtest.mm",
"visible_url_egtest.mm", "visible_url_egtest.mm",
"window_open_by_dom_egtest.mm",
] ]
deps = [ deps = [
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import <EarlGrey/EarlGrey.h>
#include "base/format_macros.h" #include "base/format_macros.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
...@@ -11,27 +9,21 @@ ...@@ -11,27 +9,21 @@
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
#include "components/content_settings/core/common/content_settings.h" #include "components/content_settings/core/common/content_settings.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "ios/chrome/test/app/tab_test_util.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h" #import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h" #import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h" #import "ios/chrome/test/earl_grey/chrome_test_case.h"
#include "ios/net/url_test_util.h" #include "ios/net/url_test_util.h"
#import "ios/web/public/test/earl_grey/web_view_actions.h" #import "ios/testing/earl_grey/earl_grey_test.h"
#import "ios/web/public/test/earl_grey/web_view_matchers.h"
#include "ios/web/public/test/element_selector.h" #include "ios/web/public/test/element_selector.h"
#import "ios/web/public/test/http_server/http_server.h" #import "ios/web/public/test/http_server/http_server.h"
#include "ios/web/public/test/http_server/http_server_util.h" #include "ios/web/public/test/http_server/http_server_util.h"
#import "ios/web/public/web_state.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
using chrome_test_util::GetCurrentWebState;
using chrome_test_util::OmniboxText; using chrome_test_util::OmniboxText;
using chrome_test_util::WebViewMatcher;
using web::test::HttpServer; using web::test::HttpServer;
...@@ -55,8 +47,21 @@ id<GREYMatcher> PopupBlocker() { ...@@ -55,8 +47,21 @@ id<GREYMatcher> PopupBlocker() {
@implementation WindowOpenByDOMTestCase @implementation WindowOpenByDOMTestCase
#if defined(CHROME_EARL_GREY_2)
+ (void)setUpForTestCase {
[super setUpForTestCase];
[self setUpHelper];
}
#elif defined(CHROME_EARL_GREY_1)
+ (void)setUp { + (void)setUp {
[super setUp]; [super setUp];
[self setUpHelper];
}
#else
#error Not an EarlGrey Test
#endif
+ (void)setUpHelper {
[ChromeEarlGrey setContentSettings:CONTENT_SETTING_ALLOW]; [ChromeEarlGrey setContentSettings:CONTENT_SETTING_ALLOW];
web::test::SetUpFileBasedHttpServer(); web::test::SetUpFileBasedHttpServer();
} }
...@@ -86,11 +91,8 @@ id<GREYMatcher> PopupBlocker() { ...@@ -86,11 +91,8 @@ id<GREYMatcher> PopupBlocker() {
// target="_blank" links. // target="_blank" links.
- (void)testLinkWithBlankTargetSessionStorage { - (void)testLinkWithBlankTargetSessionStorage {
[ChromeEarlGrey executeJavaScript:@"sessionStorage.setItem('key', 'value');"]; [ChromeEarlGrey executeJavaScript:@"sessionStorage.setItem('key', 'value');"];
const char ID[] = "webScenarioWindowOpenSameURLWithBlankTarget"; [ChromeEarlGrey
[[EarlGrey selectElementWithMatcher:WebViewMatcher()] tapWebStateElementWithID:@"webScenarioWindowOpenSameURLWithBlankTarget"];
performAction:web::WebViewTapElement(
GetCurrentWebState(),
[ElementSelector selectorWithElementID:ID])];
[ChromeEarlGrey waitForMainTabCount:2]; [ChromeEarlGrey waitForMainTabCount:2];
[ChromeEarlGrey waitForWebStateContainingText:"Expected result"]; [ChromeEarlGrey waitForWebStateContainingText:"Expected result"];
...@@ -102,21 +104,14 @@ id<GREYMatcher> PopupBlocker() { ...@@ -102,21 +104,14 @@ id<GREYMatcher> PopupBlocker() {
// Tests tapping a link with target="_blank". // Tests tapping a link with target="_blank".
- (void)testLinkWithBlankTarget { - (void)testLinkWithBlankTarget {
const char ID[] = "webScenarioWindowOpenRegularLink"; [ChromeEarlGrey tapWebStateElementWithID:@"webScenarioWindowOpenRegularLink"];
[[EarlGrey selectElementWithMatcher:WebViewMatcher()]
performAction:web::WebViewTapElement(
GetCurrentWebState(),
[ElementSelector selectorWithElementID:ID])];
[ChromeEarlGrey waitForMainTabCount:2]; [ChromeEarlGrey waitForMainTabCount:2];
} }
// Tests opening a window with URL that ends with /..; // Tests opening a window with URL that ends with /..;
- (void)testWindowOpenWithSpecialURL { - (void)testWindowOpenWithSpecialURL {
const char ID[] = "webScenarioWindowOpenWithSpecialURL"; [ChromeEarlGrey
[[EarlGrey selectElementWithMatcher:WebViewMatcher()] tapWebStateElementWithID:@"webScenarioWindowOpenWithSpecialURL"];
performAction:web::WebViewTapElement(
GetCurrentWebState(),
[ElementSelector selectorWithElementID:ID])];
if (@available(iOS 13, *)) { if (@available(iOS 13, *)) {
// Starting from iOS 13 WebKit does not rewrite URL that ends with /..; // Starting from iOS 13 WebKit does not rewrite URL that ends with /..;
[ChromeEarlGrey waitForMainTabCount:2]; [ChromeEarlGrey waitForMainTabCount:2];
...@@ -141,19 +136,14 @@ id<GREYMatcher> PopupBlocker() { ...@@ -141,19 +136,14 @@ id<GREYMatcher> PopupBlocker() {
// Tests a link with target="_blank" multiple times. // Tests a link with target="_blank" multiple times.
- (void)testLinkWithBlankTargetMultipleTimes { - (void)testLinkWithBlankTargetMultipleTimes {
const char ID[] = "webScenarioWindowOpenRegularLinkMultipleTimes"; [ChromeEarlGrey tapWebStateElementWithID:
web::WebState* test_page_web_state = GetCurrentWebState(); @"webScenarioWindowOpenRegularLinkMultipleTimes"];
id<GREYMatcher> test_page_matcher = WebViewMatcher();
id<GREYAction> link_tap = web::WebViewTapElement(
test_page_web_state, [ElementSelector selectorWithElementID:ID]);
[[EarlGrey selectElementWithMatcher:test_page_matcher]
performAction:link_tap];
[ChromeEarlGrey waitForMainTabCount:2]; [ChromeEarlGrey waitForMainTabCount:2];
[ChromeEarlGrey openNewTab]; [ChromeEarlGrey openNewTab];
[ChromeEarlGrey waitForMainTabCount:3]; [ChromeEarlGrey waitForMainTabCount:3];
[ChromeEarlGrey selectTabAtIndex:0]; [ChromeEarlGrey selectTabAtIndex:0];
[[EarlGrey selectElementWithMatcher:test_page_matcher] [ChromeEarlGrey tapWebStateElementWithID:
performAction:link_tap]; @"webScenarioWindowOpenRegularLinkMultipleTimes"];
[ChromeEarlGrey waitForMainTabCount:4]; [ChromeEarlGrey waitForMainTabCount:4];
} }
...@@ -207,11 +197,8 @@ id<GREYMatcher> PopupBlocker() { ...@@ -207,11 +197,8 @@ id<GREYMatcher> PopupBlocker() {
// Tests that opening a window with target=_blank which closes itself after 1 // Tests that opening a window with target=_blank which closes itself after 1
// second delay. // second delay.
- (void)testLinkWithBlankTargetWithDelayedClose { - (void)testLinkWithBlankTargetWithDelayedClose {
const char ID[] = "webScenarioWindowOpenWithDelayedClose"; [ChromeEarlGrey
[[EarlGrey selectElementWithMatcher:WebViewMatcher()] tapWebStateElementWithID:@"webScenarioWindowOpenWithDelayedClose"];
performAction:web::WebViewTapElement(
GetCurrentWebState(),
[ElementSelector selectorWithElementID:ID])];
[ChromeEarlGrey waitForMainTabCount:2]; [ChromeEarlGrey waitForMainTabCount:2];
base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSecondsD(1)); base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSecondsD(1));
[ChromeEarlGrey waitForMainTabCount:1]; [ChromeEarlGrey waitForMainTabCount:1];
...@@ -245,7 +232,7 @@ id<GREYMatcher> PopupBlocker() { ...@@ -245,7 +232,7 @@ id<GREYMatcher> PopupBlocker() {
// Since about scheme URLs are also trimmed to about:blank, check the url // Since about scheme URLs are also trimmed to about:blank, check the url
// directly instead. // directly instead.
DCHECK_EQ(GURL("about:blank%23hash"), DCHECK_EQ(GURL("about:blank%23hash"),
chrome_test_util::GetCurrentWebState()->GetLastCommittedURL()); [ChromeEarlGrey webStateLastCommittedURL]);
// And confirm the location bar only shows about:blank. // And confirm the location bar only shows about:blank.
[[EarlGrey selectElementWithMatcher:OmniboxText("about:blank")] [[EarlGrey selectElementWithMatcher:OmniboxText("about:blank")]
assertWithMatcher:grey_notNil()]; assertWithMatcher:grey_notNil()];
......
...@@ -360,6 +360,9 @@ id ExecuteJavaScript(NSString* javascript, NSError* __autoreleasing* out_error); ...@@ -360,6 +360,9 @@ id ExecuteJavaScript(NSString* javascript, NSError* __autoreleasing* out_error);
// Returns the current web state's VisibleURL. // Returns the current web state's VisibleURL.
- (GURL)webStateVisibleURL; - (GURL)webStateVisibleURL;
// Returns the current web state's last committed URL.
- (GURL)webStateLastCommittedURL;
// Purges cached web view pages, so the next time back navigation will not use // Purges cached web view pages, so the next time back navigation will not use
// a cached page. Browsers don't have to use a fresh version for back/forward // a cached page. Browsers don't have to use a fresh version for back/forward
// navigation for HTTP pages and may serve a version from the cache even if the // navigation for HTTP pages and may serve a version from the cache even if the
......
...@@ -510,6 +510,11 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ChromeEarlGreyAppInterface) ...@@ -510,6 +510,11 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ChromeEarlGreyAppInterface)
base::SysNSStringToUTF8([ChromeEarlGreyAppInterface webStateVisibleURL])); base::SysNSStringToUTF8([ChromeEarlGreyAppInterface webStateVisibleURL]));
} }
- (GURL)webStateLastCommittedURL {
return GURL(base::SysNSStringToUTF8(
[ChromeEarlGreyAppInterface webStateLastCommittedURL]));
}
- (void)purgeCachedWebViewPages { - (void)purgeCachedWebViewPages {
[ChromeEarlGreyAppInterface purgeCachedWebViewPages]; [ChromeEarlGreyAppInterface purgeCachedWebViewPages];
[self waitForRestoreSessionToFinish]; [self waitForRestoreSessionToFinish];
......
...@@ -184,6 +184,9 @@ ...@@ -184,6 +184,9 @@
// Returns the current WebState's VisibleURL. // Returns the current WebState's VisibleURL.
+ (NSString*)webStateVisibleURL; + (NSString*)webStateVisibleURL;
// Returns the current WebState's last committed URL.
+ (NSString*)webStateLastCommittedURL;
// Purges cached web view pages in the current web state, so the next time back // Purges cached web view pages in the current web state, so the next time back
// navigation will not use a cached page. Browsers don't have to use a fresh // navigation will not use a cached page. Browsers don't have to use a fresh
// version for back/forward navigation for HTTP pages and may serve a version // version for back/forward navigation for HTTP pages and may serve a version
......
...@@ -331,6 +331,11 @@ using chrome_test_util::BrowserCommandDispatcherForMainBVC; ...@@ -331,6 +331,11 @@ using chrome_test_util::BrowserCommandDispatcherForMainBVC;
chrome_test_util::GetCurrentWebState()->GetVisibleURL().spec()); chrome_test_util::GetCurrentWebState()->GetVisibleURL().spec());
} }
+ (NSString*)webStateLastCommittedURL {
return base::SysUTF8ToNSString(
chrome_test_util::GetCurrentWebState()->GetLastCommittedURL().spec());
}
+ (void)purgeCachedWebViewPages { + (void)purgeCachedWebViewPages {
web::WebState* web_state = chrome_test_util::GetCurrentWebState(); web::WebState* web_state = chrome_test_util::GetCurrentWebState();
web_state->SetWebUsageEnabled(false); web_state->SetWebUsageEnabled(false);
......
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