Commit 47eff284 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

Add EG test for the adaptive toolbar

This CL adds Earl Grey tests for the adaptive toolbar, testing the
different buttons displayed in the toolbars.

Bug: 804726
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: If878383cfe8847bddaab251716680bd1d1f9ac6b
Reviewed-on: https://chromium-review.googlesource.com/915347
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536965}
parent 01c629b5
......@@ -88,9 +88,12 @@ source_set("eg_tests") {
"//base",
"//components/strings",
"//ios/chrome/app/strings",
"//ios/chrome/browser/ui:ui_util",
"//ios/chrome/browser/ui/toolbar/public",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/earl_grey:test_support",
"//ios/testing/earl_grey:earl_grey_support",
"//ios/web:earl_grey_test_support",
"//ui/base",
]
libs = [ "XCTest.framework" ]
......
......@@ -6,10 +6,20 @@
#import <XCTest/XCTest.h>
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/ui/toolbar/public/toolbar_controller_constants.h"
#include "ios/chrome/browser/ui/ui_util.h"
#import "ios/chrome/browser/ui/uikit_ui_util.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ios/chrome/test/app/bookmarks_test_util.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "ios/chrome/test/earl_grey/chrome_actions.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"
#include "ios/testing/earl_grey/disabled_test_macros.h"
#import "ios/web/public/test/earl_grey/web_view_matchers.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -17,15 +27,45 @@
#endif
namespace {
// Returns the trait collection for this application main window.
UITraitCollection* TraitCollection() {
return [UIApplication sharedApplication].keyWindow.traitCollection;
const char kPageURL[] = "/test-page.html";
const char kPageURL2[] = "/test-page-2.html";
const char kPageURL3[] = "/test-page-3.html";
const char kLinkID[] = "linkID";
const char kPageLoadedString[] = "Page loaded!";
// Provides responses for redirect and changed window location URLs.
std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
const net::test_server::HttpRequest& request) {
std::unique_ptr<net::test_server::BasicHttpResponse> http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->set_content(
"<html><body><p>" + std::string(kPageLoadedString) + "</p><a href=\"" +
kPageURL3 + "\" id=\"" + kLinkID + "\">link!</a></body></html>");
return std::move(http_response);
}
// Whether the application has a compact width size class.
BOOL IsCompactWidth() {
return TraitCollection().horizontalSizeClass ==
UIUserInterfaceSizeClassCompact;
id<GREYMatcher> BookmarkButton() {
return chrome_test_util::ButtonWithAccessibilityLabelId(IDS_TOOLTIP_STAR);
}
id<GREYMatcher> ShareButton() {
return grey_allOf(grey_accessibilityID(kToolbarShareButtonIdentifier),
grey_sufficientlyVisible(), nil);
}
id<GREYMatcher> firstResponder() {
MatchesBlock matches = ^BOOL(UIResponder* responder) {
return [responder isFirstResponder];
};
DescribeToBlock describe = ^void(id<GREYDescription> description) {
[description appendText:@"first responder"];
};
return grey_allOf(
grey_kindOfClass([UIResponder class]),
[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe],
nil);
}
}
......@@ -38,6 +78,159 @@ BOOL IsCompactWidth() {
@implementation AdaptiveToolbarTestCase
// Tests that bookmarks button is selected for the bookmarked pages.
- (void)testBookmarkButton {
// Setup the bookmarks.
[ChromeEarlGrey waitForBookmarksToFinishLoading];
GREYAssert(chrome_test_util::ClearBookmarks(),
@"Not all bookmarks were removed.");
// Setup the server.
self.testServer->RegisterRequestHandler(
base::BindRepeating(&StandardResponse));
GREYAssertTrue(self.testServer->Start(), @"Test server failed to start.");
// Navigate to a page and check the bookmark button is not selected.
[ChromeEarlGrey loadURL:self.testServer->GetURL(kPageURL)];
[[EarlGrey selectElementWithMatcher:BookmarkButton()]
assertWithMatcher:grey_not(grey_selected())];
// Bookmark the page.
[[EarlGrey selectElementWithMatcher:BookmarkButton()]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:BookmarkButton()]
assertWithMatcher:grey_selected()];
// Navigate to a different page and check the button is not selected.
[ChromeEarlGrey loadURL:self.testServer->GetURL(kPageURL2)];
[[EarlGrey selectElementWithMatcher:BookmarkButton()]
assertWithMatcher:grey_not(grey_selected())];
// Navigate back to the bookmarked page and check the button.
[ChromeEarlGrey loadURL:self.testServer->GetURL(kPageURL)];
[[EarlGrey selectElementWithMatcher:BookmarkButton()]
assertWithMatcher:grey_selected()];
// Clean the bookmarks
GREYAssert(chrome_test_util::ClearBookmarks(),
@"Not all bookmarks were removed.");
}
// Tests that tapping a button cancels the focus on the omnibox.
- (void)testCancelOmniboxEdit {
if (IsCompactWidth()) {
EARL_GREY_TEST_SKIPPED(@"No button to tap in compact width.");
}
// Navigate to a page to enable the back button.
[ChromeEarlGrey loadURL:GURL("chrome://version")];
// Focus the omnibox.
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:firstResponder()];
// Tap the back button and check the omnibox is unfocused.
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:grey_not(firstResponder())];
}
// Verifies that the back/forward buttons are working and are correctly enabled
// during navigations.
- (void)testNavigationButtons {
// Setup the server.
self.testServer->RegisterRequestHandler(
base::BindRepeating(&StandardResponse));
GREYAssertTrue(self.testServer->Start(), @"Test server failed to start.");
// Loads two url and check the navigation buttons status.
[ChromeEarlGrey loadURL:self.testServer->GetURL(kPageURL)];
[ChromeEarlGrey loadURL:self.testServer->GetURL(kPageURL2)];
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
assertWithMatcher:grey_interactable()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::ForwardButton()]
assertWithMatcher:grey_not(grey_enabled())];
// Check the navigation to the second page occurred.
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:chrome_test_util::OmniboxContainingText(kPageURL2)];
// Go back.
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:chrome_test_util::OmniboxContainingText(kPageURL)];
// Check the buttons status.
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
assertWithMatcher:grey_interactable()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::ForwardButton()]
assertWithMatcher:grey_interactable()];
// Go forward.
[[EarlGrey selectElementWithMatcher:chrome_test_util::ForwardButton()]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:chrome_test_util::OmniboxContainingText(kPageURL2)];
// Check the buttons status.
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
assertWithMatcher:grey_interactable()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::ForwardButton()]
assertWithMatcher:grey_not(grey_enabled())];
// Open a page in a new incognito tab to have the focus.
[[EarlGrey
selectElementWithMatcher:web::WebViewInWebState(
chrome_test_util::GetCurrentWebState())]
performAction:chrome_test_util::LongPressElementForContextMenu(
kLinkID, true /* menu should appear */)];
[[EarlGrey selectElementWithMatcher:
chrome_test_util::StaticTextWithAccessibilityLabelId(
IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWINCOGNITOTAB)]
performAction:grey_tap()];
// Check the buttons status.
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
assertWithMatcher:grey_not(grey_enabled())];
[[EarlGrey selectElementWithMatcher:chrome_test_util::ForwardButton()]
assertWithMatcher:grey_not(grey_enabled())];
}
// Tests that tapping the omnibox button focuses the omnibox.
- (void)testOmniboxButton {
if (!IsSplitToolbarMode()) {
EARL_GREY_TEST_SKIPPED(@"No omnibox button to tap.");
}
[[EarlGrey selectElementWithMatcher:grey_accessibilityID(
kToolbarOmniboxButtonIdentifier)]
performAction:grey_tap()];
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:firstResponder()];
}
// Tests share button is enabled only on pages that can be shared.
- (void)testShareButton {
// Setup the server.
self.testServer->RegisterRequestHandler(
base::BindRepeating(&StandardResponse));
GREYAssertTrue(self.testServer->Start(), @"Test server failed to start.");
const GURL pageURL = self.testServer->GetURL(kPageURL);
// The button is disabled on the NTP.
[[EarlGrey selectElementWithMatcher:ShareButton()]
assertWithMatcher:grey_not(grey_enabled())];
// Navigate to another page and check that the share button is enabled.
[ChromeEarlGrey loadURL:pageURL];
[[EarlGrey selectElementWithMatcher:ShareButton()]
assertWithMatcher:grey_interactable()];
}
// Verifies the existence and state of toolbar UI elements.
- (void)testToolbarUI {
id<GREYMatcher> bookmarkButton =
......
......@@ -328,6 +328,7 @@ const int styleCount = 2;
[omniboxButton addTarget:self.dispatcher
action:@selector(focusOmnibox)
forControlEvents:UIControlEventTouchUpInside];
omniboxButton.accessibilityIdentifier = kToolbarOmniboxButtonIdentifier;
omniboxButton.visibilityMask =
self.visibilityConfiguration.omniboxButtonVisibility;
......
......@@ -29,6 +29,8 @@ extern NSString* const kToolbarToolsMenuButtonIdentifier;
extern NSString* const kToolbarStackButtonIdentifier;
// Accessibility identifier of the share button.
extern NSString* const kToolbarShareButtonIdentifier;
// Accessibility identifier of the omnibox button.
extern NSString* const kToolbarOmniboxButtonIdentifier;
// The maximum number to display in the tab switcher button.
extern NSInteger const kStackButtonMaxTabCount;
......
......@@ -21,6 +21,8 @@ NSString* const kToolbarStackButtonIdentifier =
@"kToolbarStackButtonIdentifier";
NSString* const kToolbarShareButtonIdentifier =
@"kToolbarShareButtonIdentifier";
NSString* const kToolbarOmniboxButtonIdentifier =
@"kToolbarOmniboxButtonIdentifier";
const NSInteger kStackButtonMaxTabCount = 99;
......
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