Commit 4177d2f1 authored by Kurt Horimoto's avatar Kurt Horimoto Committed by Commit Bot

[iOS][EG2] Convert BVC EGTests to EG2.

In addition to adding an application-level open URL utility, this CL
also converts the WebState visibility check to a unittest.

Bug: 987646
Change-Id: Ia2c99573dcabf64db0b06c094c95d5aa6d44c3df
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1900690
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Auto-Submit: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723491}
parent 73416a91
......@@ -245,6 +245,7 @@ source_set("unit_tests") {
}
source_set("eg_tests") {
defines = [ "CHROME_EARL_GREY_1" ]
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
......@@ -265,9 +266,9 @@ source_set("eg_tests") {
"//ios/chrome/browser/ui/util",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/earl_grey:test_support",
"//ios/testing/earl_grey:earl_grey_support",
"//ios/third_party/earl_grey:earl_grey+link",
"//ios/third_party/webkit",
"//ios/web:earl_grey_test_support",
"//ios/web/public/test",
"//ios/web/public/test/http_server",
"//ui/base",
......@@ -278,3 +279,30 @@ source_set("eg_tests") {
"XCTest.framework",
]
}
source_set("eg2_tests") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"browser_view_controller_egtest.mm",
]
deps = [
"//base",
"//base/test:test_support",
"//components/strings",
"//ios/chrome/app/strings",
"//ios/chrome/browser/ntp:features",
"//ios/chrome/browser/ui/popup_menu:constants",
"//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib",
"//ios/web/public/test/http_server",
"//ui/base",
"//url",
]
libs = [ "UIKit.framework" ]
}
......@@ -4,25 +4,17 @@
#include <map>
#import <EarlGrey/EarlGrey.h>
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#import <XCTest/XCTest.h>
#include "base/feature_list.h"
#include "base/strings/sys_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/ui/util/ui_util.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_ui.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#include "ios/web/public/test/http_server/html_response_provider.h"
#import "ios/web/public/test/http_server/http_server.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"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -74,8 +66,7 @@
destinationURL.GetContent())];
// Verifies that the navigation to the destination page happened.
GREYAssertEqual(destinationURL,
chrome_test_util::GetCurrentWebState()->GetVisibleURL(),
GREYAssertEqual(destinationURL, [ChromeEarlGrey webStateVisibleURL],
@"Did not navigate to the destination url.");
// Verifies that the destination page is shown.
......@@ -111,11 +102,7 @@
// Tests that BVC properly handles open URL. When NTP is visible, the URL
// should be opened in the same tab (not create a new tab).
- (void)testOpenURLFromNTP {
id<UIApplicationDelegate> appDelegate =
[[UIApplication sharedApplication] delegate];
[appDelegate application:[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"https://anything"]
options:[NSDictionary dictionary]];
[ChromeEarlGrey applicationOpenURL:GURL("https://anything")];
[[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
"https://anything")]
assertWithMatcher:grey_notNil()];
......@@ -131,11 +118,7 @@
// tab, the URL should be opened in a new tab, adding to the tab count.
- (void)testOpenURLFromTab {
[ChromeEarlGrey loadURL:GURL("https://invalid")];
id<UIApplicationDelegate> appDelegate =
[[UIApplication sharedApplication] delegate];
[appDelegate application:[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"https://anything"]
options:[NSDictionary dictionary]];
[ChromeEarlGrey applicationOpenURL:GURL("https://anything")];
[[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
"https://anything")]
assertWithMatcher:grey_notNil()];
......@@ -147,54 +130,11 @@
- (void)testOpenURLFromTabSwitcher {
[ChromeEarlGrey closeCurrentTab];
[ChromeEarlGrey waitForMainTabCount:0];
id<UIApplicationDelegate> appDelegate =
[[UIApplication sharedApplication] delegate];
[appDelegate application:[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"https://anything"]
options:[NSDictionary dictionary]];
[ChromeEarlGrey applicationOpenURL:GURL("https://anything")];
[[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
"https://anything")]
assertWithMatcher:grey_notNil()];
[ChromeEarlGrey waitForMainTabCount:1];
}
#pragma mark - WebState visibility
// Tests that WebStates are properly marked as shown or hidden when switching
// tabs.
- (void)testWebStateVisibilityAfterTabSwitch {
const GURL testURL = web::test::HttpServer::MakeUrl("http://origin");
const std::string testPageContents("Test Page");
std::map<GURL, std::string> responses;
responses[testURL] = testPageContents;
web::test::SetUpSimpleHttpServer(responses);
// Load the test page.
[ChromeEarlGrey loadURL:testURL];
[ChromeEarlGrey waitForWebStateContainingText:testPageContents];
web::WebState* firstWebState = chrome_test_util::GetCurrentWebState();
// And do the same in a second tab.
[ChromeEarlGrey openNewTab];
[ChromeEarlGrey loadURL:testURL];
[ChromeEarlGrey waitForWebStateContainingText:testPageContents];
web::WebState* secondWebState = chrome_test_util::GetCurrentWebState();
// Check visibility before and after switching tabs.
GREYAssert(secondWebState->IsVisible(), @"secondWebState not visible");
GREYAssert(!firstWebState->IsVisible(),
@"firstWebState unexpectedly visible");
[ChromeEarlGrey selectTabAtIndex:0];
GREYAssert(firstWebState->IsVisible(), @"firstWebState not visible");
GREYAssert(!secondWebState->IsVisible(),
@"secondWebState unexpectedly visible");
[ChromeEarlGrey selectTabAtIndex:1];
GREYAssert(secondWebState->IsVisible(), @"secondWebState not visible");
GREYAssert(!firstWebState->IsVisible(),
@"firstWebState unexpectedly visible");
}
@end
......@@ -250,4 +250,25 @@ TEST_F(BrowserViewControllerTest, TestFocusNextPrevious) {
EXPECT_EQ(web_state_list->active_index(), 0);
}
// Tests that WebState::WasShown() and WebState::WasHidden() is properly called
// for WebState activations in the BrowserViewController's WebStateList.
TEST_F(BrowserViewControllerTest, UpdateWebStateVisibility) {
WebStateList* web_state_list = tabModel_.webStateList;
ASSERT_EQ(3, web_state_list->count());
// Activate each WebState in the list and check the visibility.
web_state_list->ActivateWebStateAt(0);
EXPECT_EQ(web_state_list->GetWebStateAt(0)->IsVisible(), true);
EXPECT_EQ(web_state_list->GetWebStateAt(1)->IsVisible(), false);
EXPECT_EQ(web_state_list->GetWebStateAt(2)->IsVisible(), false);
web_state_list->ActivateWebStateAt(1);
EXPECT_EQ(web_state_list->GetWebStateAt(0)->IsVisible(), false);
EXPECT_EQ(web_state_list->GetWebStateAt(1)->IsVisible(), true);
EXPECT_EQ(web_state_list->GetWebStateAt(2)->IsVisible(), false);
web_state_list->ActivateWebStateAt(2);
EXPECT_EQ(web_state_list->GetWebStateAt(0)->IsVisible(), false);
EXPECT_EQ(web_state_list->GetWebStateAt(1)->IsVisible(), false);
EXPECT_EQ(web_state_list->GetWebStateAt(2)->IsVisible(), true);
}
} // namespace
......@@ -81,6 +81,10 @@ id ExecuteJavaScript(NSString* javascript, NSError* __autoreleasing* out_error);
#pragma mark - Navigation Utilities (EG2)
// Instructs the application delegate to open |URL| with default opening
// options.
- (void)applicationOpenURL:(const GURL&)URL;
// Loads |URL| in the current WebState with transition type
// ui::PAGE_TRANSITION_TYPED, and if waitForCompletion is YES
// waits for the loading to complete within a timeout.
......
......@@ -284,6 +284,11 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ChromeEarlGreyAppInterface)
EG_TEST_HELPER_ASSERT_TRUE(pageLoaded, kWaitForPageToFinishLoadingError);
}
- (void)applicationOpenURL:(const GURL&)URL {
NSString* spec = base::SysUTF8ToNSString(URL.spec());
[ChromeEarlGreyAppInterface applicationOpenURL:spec];
}
- (void)loadURL:(const GURL&)URL waitForCompletion:(BOOL)wait {
NSString* spec = base::SysUTF8ToNSString(URL.spec());
[ChromeEarlGreyAppInterface startLoadingURL:spec];
......
......@@ -28,6 +28,9 @@
// the operation failed.
+ (NSError*)removeBrowsingCache;
// Opens |URL| using the application delegate.
+ (void)applicationOpenURL:(NSString*)spec;
// Loads the URL |spec| in the current WebState with transition type
// ui::PAGE_TRANSITION_TYPED and returns without waiting for the page to load.
+ (void)startLoadingURL:(NSString*)spec;
......
......@@ -74,6 +74,13 @@ using chrome_test_util::BrowserCommandDispatcherForMainBVC;
@"Clearing browser cache for main tabs timed out");
}
+ (void)applicationOpenURL:(NSString*)spec {
UIApplication* application = UIApplication.sharedApplication;
[application.delegate application:application
openURL:[NSURL URLWithString:spec]
options:[NSDictionary dictionary]];
}
+ (void)startLoadingURL:(NSString*)spec {
chrome_test_util::LoadUrl(GURL(base::SysNSStringToUTF8(spec)));
}
......
......@@ -72,6 +72,7 @@ chrome_ios_eg2_test("ios_chrome_ui_eg2tests_module") {
deps = [
"//ios/chrome/browser/ui/activity_services:eg2_tests",
"//ios/chrome/browser/ui/browser_view:eg2_tests",
"//ios/chrome/browser/ui/dialogs:eg2_tests",
"//ios/chrome/browser/ui/download:eg2_tests",
"//ios/chrome/browser/ui/find_bar:eg2_tests",
......
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