Commit 135b143a authored by Chris Lu's avatar Chris Lu Committed by Commit Bot

[ios] Convert InfobarEGTest to EG2

Creates InfobarManagerAppInterface to allow for retrieval of InfobarManager
state.

Bug: 987646
Change-Id: Ic518fe7ffaaeec27b08f6a3e4a37a12dc84387fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1914535
Commit-Queue: Chris Lu <thegreenfrog@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715898}
parent 45cc3fe3
......@@ -137,7 +137,68 @@ source_set("unit_tests") {
]
}
source_set("eg2_tests") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"infobar_egtest.mm",
]
deps = [
":eg_test_support+eg2",
"//base",
"//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",
"//url",
]
libs = [ "UIKit.framework" ]
}
source_set("eg_tests") {
defines = [ "CHROME_EARL_GREY_1" ]
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"infobar_egtest.mm",
]
deps = [
":eg_test_support",
"//base",
"//ios/chrome/test/earl_grey:test_support",
"//ios/testing/earl_grey:earl_grey_support",
"//ios/web/public/test/http_server",
"//url",
]
libs = [ "XCTest.framework" ]
}
source_set("eg_test_support") {
defines = [ "CHROME_EARL_GREY_1" ]
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"infobar_manager_app_interface.h",
"infobar_manager_app_interface.mm",
]
deps = [
":test_support",
"//base",
"//components/infobars/core",
"//ios/chrome/app:app_internal",
"//ios/chrome/browser/infobars",
"//ios/chrome/browser/tabs",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/test/app:test_support",
]
}
source_set("test_support") {
defines = [ "CHROME_EARL_GREY_1" ]
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
......@@ -150,14 +211,20 @@ source_set("test_support") {
]
}
source_set("eg_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
source_set("eg_app_support+eg2") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"infobar_egtest.mm",
"infobar_manager_app_interface.h",
"infobar_manager_app_interface.mm",
"test_infobar_delegate.h",
"test_infobar_delegate.mm",
]
deps = [
":test_support",
"//base",
"//components/infobars/core",
"//ios/chrome/app:app_internal",
......@@ -165,10 +232,17 @@ source_set("eg_tests") {
"//ios/chrome/browser/tabs",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/earl_grey:test_support",
"//ios/third_party/earl_grey:earl_grey+link",
"//ios/web/public/test/http_server",
"//url",
]
libs = [ "XCTest.framework" ]
}
source_set("eg_test_support+eg2") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"infobar_manager_app_interface.h",
]
}
......@@ -2,21 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <EarlGrey/EarlGrey.h>
#import <XCTest/XCTest.h>
#include "base/strings/sys_string_conversions.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_manager.h"
#import "ios/chrome/app/main_controller.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/ui/infobars/test_infobar_delegate.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "ios/chrome/browser/ui/infobars/infobar_manager_app_interface.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_test_case.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#import "ios/web/public/test/http_server/http_server.h"
#include "ios/web/public/test/http_server/http_server_util.h"
#include "url/gurl.h"
......@@ -26,33 +19,20 @@
#error "This file requires ARC support."
#endif
#if defined(CHROME_EARL_GREY_2)
// TODO(crbug.com/1015113): The EG2 macro is breaking indexing for some reason
// without the trailing semicolon. For now, disable the extra semi warning
// so Xcode indexing works for the egtest.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat-extra-semi"
GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(InfobarManagerAppInterface);
#endif // defined(CHROME_EARL_GREY_2)
namespace {
// Timeout for how long to wait for an infobar to appear or disapper.
const CFTimeInterval kTimeout = 4.0;
// Returns the InfoBarManager for the current tab. Only works in normal
// (non-incognito) mode.
infobars::InfoBarManager* GetCurrentInfoBarManager() {
MainController* main_controller = chrome_test_util::GetMainController();
id<BrowserInterface> interface =
main_controller.interfaceProvider.mainInterface;
web::WebState* web_state =
interface.tabModel ? interface.tabModel.webStateList->GetActiveWebState()
: nullptr;
if (web_state) {
return InfoBarManagerImpl::FromWebState(web_state);
}
return nullptr;
}
// Adds a TestInfoBar with |message| to the current tab.
bool AddTestInfoBarToCurrentTabWithMessage(NSString* message) {
infobars::InfoBarManager* manager = GetCurrentInfoBarManager();
TestInfoBarDelegate* test_infobar_delegate = new TestInfoBarDelegate(message);
return test_infobar_delegate->Create(manager);
}
// Verifies that a single TestInfoBar with |message| is either present or absent
// on the current tab.
void VerifyTestInfoBarVisibleForCurrentTab(bool visible, NSString* message) {
......@@ -61,6 +41,7 @@ void VerifyTestInfoBarVisibleForCurrentTab(bool visible, NSString* message) {
NSString* condition_name =
visible ? @"Waiting for infobar to show" : @"Waiting for infobar to hide";
#if defined(CHROME_EARL_GREY_1)
[[GREYCondition
conditionWithName:condition_name
block:^BOOL {
......@@ -72,14 +53,24 @@ void VerifyTestInfoBarVisibleForCurrentTab(bool visible, NSString* message) {
error:&error];
return error == nil;
}] waitWithTimeout:kTimeout];
}
// Verifies the number of Infobar currently in the InfobarManager (Thus in the
// InfobarContainer) is |number_of_infobars|.
void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
infobars::InfoBarManager* manager = GetCurrentInfoBarManager();
GREYAssertEqual(number_of_infobars, manager->infobar_count(),
@"Incorrect number of infobars.");
#elif defined(CHROME_EARL_GREY_2)
GREYAssertTrue(
[[GREYCondition
conditionWithName:condition_name
block:^BOOL {
NSError* error = nil;
[[EarlGrey
selectElementWithMatcher:
chrome_test_util::
StaticTextWithAccessibilityLabel(message)]
assertWithMatcher:expected_visibility
error:&error];
return error == nil;
}] waitWithTimeout:kTimeout],
condition_name);
#else
#error Must define either CHROME_EARL_GREY_1 or CHROME_EARL_GREY_2.
#endif
}
} // namespace
......@@ -105,16 +96,19 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a test infobar to the current tab. Verify that the infobar is present
// in the model and that the infobar view is visible on screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(infoBarMessage),
GREYAssertTrue([InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:infoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, infoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
// Navigate to a different page. Verify that the infobar is dismissed and no
// longer visible on screen.
[ChromeEarlGrey loadURL:GURL(url::kAboutBlankURL)];
VerifyTestInfoBarVisibleForCurrentTab(false, infoBarMessage);
VerifyNumberOfInfobarsInManager(0);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:0],
@"Incorrect number of infobars.");
}
// Tests that page infobars persist only on the tabs they are opened on, and
......@@ -138,16 +132,20 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
[ChromeEarlGrey loadURL:ponyURL];
[ChromeEarlGrey waitForMainTabCount:2];
VerifyTestInfoBarVisibleForCurrentTab(false, infoBarMessage);
VerifyNumberOfInfobarsInManager(0);
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(infoBarMessage),
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:0],
@"Incorrect number of infobars.");
GREYAssertTrue([InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:infoBarMessage],
@"Failed to add infobar to second tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, infoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
// Switch back to the first tab and make sure no infobar is visible.
[ChromeEarlGrey selectTabAtIndex:0U];
VerifyTestInfoBarVisibleForCurrentTab(false, infoBarMessage);
VerifyNumberOfInfobarsInManager(0);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:0],
@"Incorrect number of infobars.");
// Navigate to a different URL in the first tab, to verify that this
// navigation does not hide the infobar in the second tab.
......@@ -158,7 +156,8 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
[ChromeEarlGrey closeCurrentTab];
[ChromeEarlGrey waitForMainTabCount:1];
VerifyTestInfoBarVisibleForCurrentTab(true, infoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
}
// Tests that the Infobar dissapears once the "OK" button is tapped.
......@@ -176,10 +175,12 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a test infobar to the current tab. Verify that the infobar is present
// in the model and that the infobar view is visible on screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(infoBarMessage),
GREYAssertTrue([InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:infoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, infoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
// Tap on "OK" which should dismiss the Infobar.
[[EarlGrey
......@@ -187,7 +188,8 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
grey_sufficientlyVisible(), nil)]
performAction:grey_tap()];
VerifyTestInfoBarVisibleForCurrentTab(false, infoBarMessage);
VerifyNumberOfInfobarsInManager(0);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:0],
@"Incorrect number of infobars.");
}
// Tests adding an Infobar on top of an existing one.
......@@ -205,10 +207,12 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a test infobar to the current tab. Verify that the infobar is present
// in the model and that the infobar view is visible on screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(firstInfoBarMessage),
GREYAssertTrue([InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:firstInfoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
// Second Infobar Message
NSString* secondInfoBarMessage = @"TestSecondInfoBar";
......@@ -216,11 +220,14 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a second test infobar to the current tab. Verify that the infobar is
// present in the model, and that only this second infobar is now visible on
// screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(secondInfoBarMessage),
GREYAssertTrue(
[InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:secondInfoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, secondInfoBarMessage);
VerifyTestInfoBarVisibleForCurrentTab(false, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(2);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:2],
@"Incorrect number of infobars.");
}
// Tests that a taller Infobar layout is correct and the OK button is tappable.
......@@ -241,10 +248,12 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a test infobar to the current tab. Verify that the infobar is present
// in the model and that the infobar view is visible on screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(firstInfoBarMessage),
GREYAssertTrue([InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:firstInfoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
// Dismiss the Infobar.
[[EarlGrey
......@@ -252,7 +261,8 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
grey_sufficientlyVisible(), nil)]
performAction:grey_tap()];
VerifyTestInfoBarVisibleForCurrentTab(false, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(0);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:0],
@"Incorrect number of infobars.");
}
// Tests that adding an Infobar of lower height on top of a taller Infobar only
......@@ -275,10 +285,12 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a test infobar to the current tab. Verify that the infobar is present
// in the model and that the infobar view is visible on screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(firstInfoBarMessage),
GREYAssertTrue([InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:firstInfoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
// Second Infobar Message
NSString* secondInfoBarMessage = @"TestSecondInfoBar";
......@@ -286,11 +298,14 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
// Add a second test infobar to the current tab. Verify that the infobar is
// present in the model, and that only this second infobar is now visible on
// screen.
GREYAssert(AddTestInfoBarToCurrentTabWithMessage(secondInfoBarMessage),
GREYAssertTrue(
[InfobarManagerAppInterface
addTestInfoBarToCurrentTabWithMessage:secondInfoBarMessage],
@"Failed to add infobar to test tab.");
VerifyTestInfoBarVisibleForCurrentTab(true, secondInfoBarMessage);
VerifyTestInfoBarVisibleForCurrentTab(false, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(2);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:2],
@"Incorrect number of infobars.");
// Dismiss the second Infobar.
[[EarlGrey
......@@ -299,7 +314,8 @@ void VerifyNumberOfInfobarsInManager(size_t number_of_infobars) {
performAction:grey_tap()];
VerifyTestInfoBarVisibleForCurrentTab(false, secondInfoBarMessage);
VerifyTestInfoBarVisibleForCurrentTab(true, firstInfoBarMessage);
VerifyNumberOfInfobarsInManager(1);
GREYAssertTrue([InfobarManagerAppInterface verifyInfobarCount:1],
@"Incorrect number of infobars.");
}
@end
// 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_CHROME_BROWSER_UI_INFOBARS_INFOBAR_MANAGER_APP_INTERFACE_H_
#define IOS_CHROME_BROWSER_UI_INFOBARS_INFOBAR_MANAGER_APP_INTERFACE_H_
#import <UIKit/UIKit.h>
// InfobarManagerAppInterface contains the app-side
// implementation for helpers. These helpers are compiled into
// the app binary and can be called from either app or test code.
@interface InfobarManagerAppInterface : NSObject
// Verifies that there are |totalInfobars| in the InfobarManager of the current
// active WebState.
+ (BOOL)verifyInfobarCount:(NSInteger)totalInfobars;
// Adds a TestInfoBar with |message| to the current active WebState.
+ (BOOL)addTestInfoBarToCurrentTabWithMessage:(NSString*)message;
@end
#endif // IOS_CHROME_BROWSER_UI_INFOBARS_INFOBAR_MANAGER_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/chrome/browser/ui/infobars/infobar_manager_app_interface.h"
#include "components/infobars/core/infobar_manager.h"
#import "ios/chrome/app/main_controller.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/ui/infobars/test_infobar_delegate.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation InfobarManagerAppInterface
+ (BOOL)verifyInfobarCount:(NSInteger)totalInfobars {
MainController* mainController = chrome_test_util::GetMainController();
id<BrowserInterface> interface =
mainController.interfaceProvider.mainInterface;
web::WebState* webState =
interface.tabModel.webStateList->GetActiveWebState();
infobars::InfoBarManager* manager =
InfoBarManagerImpl::FromWebState(webState);
return totalInfobars == (NSInteger)manager->infobar_count();
}
+ (BOOL)addTestInfoBarToCurrentTabWithMessage:(NSString*)message {
MainController* mainController = chrome_test_util::GetMainController();
id<BrowserInterface> interface =
mainController.interfaceProvider.mainInterface;
web::WebState* webState =
interface.tabModel.webStateList->GetActiveWebState();
infobars::InfoBarManager* manager =
InfoBarManagerImpl::FromWebState(webState);
TestInfoBarDelegate* testInfobarDelegate = new TestInfoBarDelegate(message);
return testInfobarDelegate->Create(manager);
}
@end
......@@ -177,7 +177,7 @@ source_set("eg_app_support+eg2") {
]
deps = [
"//ios/chrome/browser/infobars",
"//ios/chrome/browser/ui/infobars:test_support",
"//ios/chrome/browser/ui/infobars:eg_app_support+eg2",
"//ios/chrome/test/app:test_support",
"//ios/testing:nserror_support",
]
......
......@@ -382,6 +382,8 @@ source_set("eg_app_support+eg2") {
"//ios/chrome/browser/ui/first_run:eg_app_support+eg2",
"//ios/chrome/browser/ui/fullscreen/test:eg_app_support+eg2",
"//ios/chrome/browser/ui/history:constants",
"//ios/chrome/browser/ui/infobars",
"//ios/chrome/browser/ui/infobars:eg_app_support+eg2",
"//ios/chrome/browser/ui/location_bar:location_bar",
"//ios/chrome/browser/ui/omnibox:omnibox_internal",
"//ios/chrome/browser/ui/payments:payments_ui",
......
......@@ -75,6 +75,7 @@ chrome_ios_eg2_test("ios_chrome_ui_eg2tests_module") {
"//ios/chrome/browser/ui/download:eg2_tests",
"//ios/chrome/browser/ui/first_run:eg2_tests",
"//ios/chrome/browser/ui/fullscreen:eg2_tests",
"//ios/chrome/browser/ui/infobars:eg2_tests",
"//ios/chrome/browser/ui/keyboard:eg2_tests",
"//ios/chrome/browser/ui/ntp:eg2_tests",
"//ios/chrome/browser/ui/omnibox/popup: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