Commit 74a7a39a authored by Eric Aleshire's avatar Eric Aleshire Committed by Commit Bot

Move GREYAsserts from tab_usage_recorder_test_util into the corresponding EG test.

This is part of EG1 to EG2 migration, which involves moving EarlGrey code from
app-side helpers into test code. For GREYAsserts, the policy is to take assert-
containing-helpers, modify them to return NSError* with localizedDescription
containing the assert failure (in the event of a failure), and then assert
on the result of the helper on the test side.

I add a new helper which assists in creating NSErrors.

Bug: 922813
Change-Id: Ib2fc2bb0b016afc416ccbbdae19c4acbcaf48350
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1553085
Commit-Queue: ericale <ericale@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#649718}
parent 90e248ac
......@@ -150,12 +150,17 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
kSelectedTabHistogramName, TabUsageRecorder::IN_MEMORY, 1, failureBlock);
// Evict the tab.
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
GREYAssertTrue(chrome_test_util::IsIncognitoMode(),
@"Failed to switch to incognito mode");
// Switch back to the normal tabs. Should be on tab one.
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
[ChromeEarlGrey waitForWebViewContainingText:kURL1FirstWord];
histogramTester.ExpectTotalCount(kSelectedTabHistogramName, 2, failureBlock);
......@@ -216,11 +221,14 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
// Evict the tab. Create a dummy tab so that switching back to normal mode
// does not trigger a reload immediately.
[ChromeEarlGrey openNewTab];
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
[ChromeEarlGrey waitForIncognitoTabCount:1];
// Switch back to the normal tabs. Should be on tab one.
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
chrome_test_util::SelectTabAtIndexInCurrentMode(0);
[ChromeEarlGrey waitForWebViewContainingText:kURL1FirstWord];
......@@ -254,12 +262,17 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
@"Fail to state tabs as cold start tabs");
// Open two incognito tabs with urls, clearing normal tabs from memory.
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* firstTabError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(firstTabError, firstTabError.localizedDescription);
NSError* secondTabError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(secondTabError, secondTabError.localizedDescription);
[ChromeEarlGrey waitForIncognitoTabCount:2];
// Switch back to the normal tabs.
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
[ChromeEarlGrey waitForWebViewContainingText:kURL2FirstWord];
// Select the other one so it also reloads.
......@@ -308,13 +321,16 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
@"Fail to simulate tab backgrounding.");
// Open incognito and clear normal tabs from memory.
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
GREYAssertTrue(chrome_test_util::IsIncognitoMode(),
@"Failed to switch to incognito mode");
histogramTester.ExpectTotalCount(kEvictedTabReloadTime, 0, failureBlock);
// Switch back to the normal tabs.
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
[ChromeEarlGrey waitForWebViewContainingText:kURL2FirstWord];
const GURL url1 = web::test::HttpServer::MakeUrl(kTestUrl1);
......@@ -345,8 +361,12 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
[ChromeEarlGrey closeAllTabsInCurrentMode];
GURL URL = web::test::HttpServer::MakeUrl(kTestUrl1);
NewMainTabWithURL(URL, kURL1FirstWord);
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
SwitchToNormalMode();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
[ChromeEarlGrey waitForWebViewContainingText:kURL1FirstWord];
[ChromeEarlGrey waitForMainTabCount:1];
......@@ -378,12 +398,18 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
[ChromeEarlGrey openNewTab];
[ChromeEarlGrey openNewTab];
chrome_test_util::LoadUrl(slowURL);
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
web::test::SetUpHttpServer(std::make_unique<web::DelayedResponseProvider>(
std::make_unique<HtmlResponseProvider>(responses), kSlowURLDelay));
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
// TODO(crbug.com/951600): We avoid asserting directly unless the test fails,
// due to timing issues.
if (switchError != nil) {
GREYAssert(false, switchError.localizedDescription);
}
// Turn off synchronization of GREYAssert to test the pending states.
[[GREYConfiguration sharedInstance]
......@@ -425,11 +451,17 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
NewMainTabWithURL(slowURL, "Slow");
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
web::test::SetUpHttpServer(std::make_unique<web::DelayedResponseProvider>(
std::make_unique<HtmlResponseProvider>(responses), kSlowURLDelay));
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
// TODO(crbug.com/951600): We avoid asserting directly unless the test fails,
// due to timing issues.
if (switchError != nil) {
GREYAssert(false, switchError.localizedDescription);
}
// Letting page load start.
base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSecondsD(0.5));
......@@ -470,9 +502,12 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
};
NewMainTabWithURL(slowURL, responses[slowURL]);
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
SwitchToNormalMode();
[ChromeEarlGreyUI openSettingsMenu];
[ChromeEarlGreyUI tapSettingsMenuButton:SettingsMenuPrivacyButton()];
[[EarlGrey selectElementWithMatcher:SettingsDoneButton()]
......@@ -500,11 +535,17 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
[ChromeEarlGrey openNewTab];
chrome_test_util::LoadUrl(slowURL);
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
web::test::SetUpHttpServer(std::make_unique<web::DelayedResponseProvider>(
std::make_unique<HtmlResponseProvider>(responses), kSlowURLDelay));
SwitchToNormalMode();
NSError* switchError = SwitchToNormalMode();
// TODO(crbug.com/951600): We avoid asserting directly unless the test fails,
// due to timing issues.
if (switchError != nil) {
GREYAssert(false, switchError.localizedDescription);
}
// Letting page load start.
base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSecondsD(0.5));
......@@ -585,8 +626,12 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
NSUInteger tabIndex = chrome_test_util::GetMainTabCount() - 1;
[ChromeEarlGrey openNewTab];
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
SwitchToNormalMode();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
chrome_test_util::SelectTabAtIndexInCurrentMode(tabIndex);
[ChromeEarlGrey waitForWebViewContainingText:"arrived"];
......@@ -632,8 +677,12 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
[ChromeEarlGrey waitForWebViewContainingText:"Whee"];
NSUInteger tabIndex = chrome_test_util::GetMainTabCount() - 1;
[ChromeEarlGrey openNewTab];
OpenNewIncognitoTabUsingUIAndEvictMainTabs();
SwitchToNormalMode();
NSError* openError = OpenNewIncognitoTabUsingUIAndEvictMainTabs();
GREYAssertNil(openError, openError.localizedDescription);
NSError* switchError = SwitchToNormalMode();
GREYAssertNil(switchError, switchError.localizedDescription);
chrome_test_util::SelectTabAtIndexInCurrentMode(tabIndex);
[ChromeEarlGrey waitForWebViewContainingText:"Whee"];
......@@ -665,11 +714,11 @@ void CloseTabAtIndexAndSync(NSUInteger i) {
web::test::HttpServer::MakeUrl("http://destination");
// Make the link that cover the whole page so that long pressing the web view
// will trigger the link context menu.
responses[initialURL] = base::StringPrintf(
"<body style='width:auto; height:auto;'><a href='%s' "
"id='link'><div style='width:100%%; "
"height:100%%;'>link</div></a></body>",
destinationURL.spec().c_str());
responses[initialURL] =
base::StringPrintf("<body style='width:auto; height:auto;'><a href='%s' "
"id='link'><div style='width:100%%; "
"height:100%%;'>link</div></a></body>",
destinationURL.spec().c_str());
responses[destinationURL] = "Whee!";
web::test::SetUpHttpServer(std::make_unique<HtmlResponseProvider>(responses));
chrome_test_util::HistogramTester histogramTester;
......
......@@ -5,14 +5,16 @@
#ifndef IOS_CHROME_BROWSER_METRICS_TAB_USAGE_RECORDER_TEST_UTIL_H_
#define IOS_CHROME_BROWSER_METRICS_TAB_USAGE_RECORDER_TEST_UTIL_H_
@class NSError;
namespace tab_usage_recorder_test_util {
// Opens a new incognito tab using the UI and evicts any main tab model tabs.
void OpenNewIncognitoTabUsingUIAndEvictMainTabs();
NSError* OpenNewIncognitoTabUsingUIAndEvictMainTabs();
// Switches to normal mode using the tab switcher and selects the
// previously-selected normal tab. Assumes current mode is Incognito.
void SwitchToNormalMode();
NSError* SwitchToNormalMode();
} // namespace tab_usage_recorder_test_util
......
......@@ -21,6 +21,7 @@
#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_error_util.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#include "ui/base/l10n/l10n_util_mac.h"
......@@ -35,7 +36,7 @@ const NSTimeInterval kWaitElementTimeout = 3;
// Shows the tab switcher by tapping the switcher button. Works on both phone
// and tablet.
void ShowTabSwitcher() {
bool ShowTabSwitcher() {
id<GREYMatcher> matcher = chrome_test_util::TabGridOpenButton();
// Perform a tap with a timeout. Occasionally EG doesn't sync up properly to
// the animations of tab switcher, so it is necessary to poll here.
......@@ -50,15 +51,14 @@ void ShowTabSwitcher() {
}];
// Wait until 2 seconds for the tap.
BOOL hasClicked = [tapTabSwitcher waitWithTimeout:2];
GREYAssertTrue(hasClicked, @"Tab switcher could not be tapped.");
return [tapTabSwitcher waitWithTimeout:2];
}
} // namespace
namespace tab_usage_recorder_test_util {
void OpenNewIncognitoTabUsingUIAndEvictMainTabs() {
NSError* OpenNewIncognitoTabUsingUIAndEvictMainTabs() {
int nb_incognito_tab = chrome_test_util::GetIncognitoTabCount();
[ChromeEarlGreyUI openToolsMenu];
id<GREYMatcher> new_incognito_tab_button_matcher =
......@@ -69,18 +69,29 @@ void OpenNewIncognitoTabUsingUIAndEvictMainTabs() {
ConditionBlock condition = ^bool {
return chrome_test_util::IsIncognitoMode();
};
GREYAssert(base::test::ios::WaitUntilConditionOrTimeout(kWaitElementTimeout,
condition),
@"Waiting switch to incognito mode.");
bool success = base::test::ios::WaitUntilConditionOrTimeout(
kWaitElementTimeout, condition);
if (!success) {
return chrome_test_util::NSErrorWithLocalizedDescription(
@"Waiting switch to incognito mode.");
}
chrome_test_util::EvictOtherTabModelTabs();
return nil;
}
void SwitchToNormalMode() {
GREYAssertTrue(chrome_test_util::IsIncognitoMode(),
@"Switching to normal mode is only allowed from Incognito.");
NSError* SwitchToNormalMode() {
if (!chrome_test_util::IsIncognitoMode()) {
return chrome_test_util::NSErrorWithLocalizedDescription(
@"Switching to normal mode is only allowed from Incognito.");
}
// Enter the tab grid to switch modes.
ShowTabSwitcher();
if (!ShowTabSwitcher()) {
return chrome_test_util::NSErrorWithLocalizedDescription(
@"Tab switcher could not be tapped.");
}
// Switch modes and exit the tab grid.
TabModel* model = chrome_test_util::GetMainController()
......@@ -99,13 +110,17 @@ void SwitchToNormalMode() {
ConditionBlock condition = ^bool {
return !chrome_test_util::IsIncognitoMode();
};
GREYAssert(base::test::ios::WaitUntilConditionOrTimeout(kWaitElementTimeout,
condition),
@"Waiting switch to normal mode.");
if (!base::test::ios::WaitUntilConditionOrTimeout(kWaitElementTimeout,
condition)) {
return chrome_test_util::NSErrorWithLocalizedDescription(
@"Waiting switch to normal mode.");
}
[[GREYConfiguration sharedInstance]
setValue:@(YES)
forConfigKey:kGREYConfigKeySynchronizationEnabled];
return nil;
}
} // namespace tab_usage_recorder_test_util
......@@ -227,6 +227,8 @@ source_set("test_support") {
"chrome_earl_grey.mm",
"chrome_earl_grey_ui.h",
"chrome_earl_grey_ui.mm",
"chrome_error_util.h",
"chrome_error_util.mm",
"chrome_matchers.h",
"chrome_matchers.mm",
"chrome_matchers_shorthand.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.
#ifndef IOS_CHROME_TEST_EARL_GREY_CHROME_ERROR_UTIL_H_
#define IOS_CHROME_TEST_EARL_GREY_CHROME_ERROR_UTIL_H_
@class NSError;
@class NSString;
namespace chrome_test_util {
// Returns a NSError with generic domain and error code, and the provided string
// as localizedDescription.
NSError* NSErrorWithLocalizedDescription(NSString* error_description);
} // namespace chrome_test_util
#endif // IOS_CHROME_TEST_EARL_GREY_CHROME_ERROR_UTIL_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/test/earl_grey/chrome_error_util.h"
#import <Foundation/Foundation.h>
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace chrome_test_util {
NSError* NSErrorWithLocalizedDescription(NSString* error_description) {
NSDictionary* userInfo = @{
NSLocalizedDescriptionKey : error_description,
};
return [[NSError alloc] initWithDomain:@"com.google.chrome.errorDomain"
code:0
userInfo:userInfo];
}
} // namespace chrome_test_util
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