Commit 868a311f authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Update EG matchers for new Settings cells

This CL updates the chrome matchers and actions functions to be
compatible with the new type of cells in the Settings.

Bug: 894791
Change-Id: I10d899a3e4946b9e171bdf8909d10ce0f50c8b2a
Reviewed-on: https://chromium-review.googlesource.com/c/1333368
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608336}
parent 4c30279e
......@@ -212,7 +212,7 @@ NSString* const kCreditCardLabelTemplate = @"Test User, %@";
[[EarlGrey
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"cardItem_switch", YES, YES)]
performAction:chrome_test_util::TurnSettingsSwitchOn(NO)];
performAction:chrome_test_util::TurnLegacySettingsSwitchOn(NO)];
// Expect Autofill credit cards to remain visible.
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
......@@ -223,7 +223,7 @@ NSString* const kCreditCardLabelTemplate = @"Test User, %@";
[[EarlGrey
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"cardItem_switch", NO, YES)]
performAction:chrome_test_util::TurnSettingsSwitchOn(YES)];
performAction:chrome_test_util::TurnLegacySettingsSwitchOn(YES)];
// Expect Autofill credit cards to remain visible.
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(
......
......@@ -278,7 +278,7 @@ NSString* GetTextFieldForID(int categoryId) {
[[EarlGrey
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"addressItem_switch", YES, YES)]
performAction:chrome_test_util::TurnSettingsSwitchOn(NO)];
performAction:chrome_test_util::TurnLegacySettingsSwitchOn(NO)];
// Expect Autofill profiles to remain visible.
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(kProfileLabel)]
......@@ -288,7 +288,7 @@ NSString* GetTextFieldForID(int categoryId) {
[[EarlGrey
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"addressItem_switch", NO, YES)]
performAction:chrome_test_util::TurnSettingsSwitchOn(YES)];
performAction:chrome_test_util::TurnLegacySettingsSwitchOn(YES)];
// Expect Autofill profiles to remain visible.
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(kProfileLabel)]
......
......@@ -211,7 +211,7 @@ class ScopedBlockPopupsException {
[[EarlGrey
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"blockPopupsContentView_switch", YES)]
performAction:chrome_test_util::TurnSettingsSwitchOn(NO)];
performAction:chrome_test_util::TurnLegacySettingsSwitchOn(NO)];
[[EarlGrey selectElementWithMatcher:grey_text(base::SysUTF8ToNSString(
allowedPattern))]
assertWithMatcher:grey_notVisible()];
......@@ -227,7 +227,7 @@ class ScopedBlockPopupsException {
[[EarlGrey
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"blockPopupsContentView_switch", NO)]
performAction:chrome_test_util::TurnSettingsSwitchOn(YES)];
performAction:chrome_test_util::TurnLegacySettingsSwitchOn(YES)];
[[EarlGrey selectElementWithMatcher:grey_text(base::SysUTF8ToNSString(
allowedPattern))]
assertWithMatcher:grey_sufficientlyVisible()];
......
......@@ -59,6 +59,7 @@ using chrome_test_util::SettingsDoneButton;
using chrome_test_util::SettingsMenuBackButton;
using chrome_test_util::SetUpAndReturnMockReauthenticationModule;
using chrome_test_util::SetUpAndReturnMockReauthenticationModuleForExport;
using chrome_test_util::TurnLegacySettingsSwitchOn;
using web::test::ElementSelector;
namespace {
......@@ -1276,7 +1277,7 @@ PasswordForm CreateSampleFormWithIndex(int index) {
[GetInteractionForListItem(chrome_test_util::LegacySettingsSwitchCell(
@"savePasswordsItem_switch", expected_state),
kGREYDirectionUp)
performAction:chrome_test_util::TurnSettingsSwitchOn(!expected_state)];
performAction:TurnLegacySettingsSwitchOn(!expected_state)];
// Check the stored items. Scroll down if needed.
[GetInteractionForPasswordEntry(@"example.com, concrete username")
assertWithMatcher:grey_notNil()];
......@@ -1303,8 +1304,7 @@ PasswordForm CreateSampleFormWithIndex(int index) {
selectElementWithMatcher:chrome_test_util::LegacySettingsSwitchCell(
@"savePasswordsItem_switch",
expected_initial_state)]
performAction:chrome_test_util::TurnSettingsSwitchOn(
!expected_initial_state)];
performAction:TurnLegacySettingsSwitchOn(!expected_initial_state)];
ios::ChromeBrowserState* browserState =
chrome_test_util::GetOriginalBrowserState();
const bool expected_final_state = !expected_initial_state;
......
......@@ -25,6 +25,10 @@ id<GREYAction> LongPressElementForContextMenu(
// Action to turn the switch of a LegacySettingsSwitchCell to the given |on|
// state.
// TODO(crbug.com/894800): Remove this.
id<GREYAction> TurnLegacySettingsSwitchOn(BOOL on);
// Action to turn the switch of a SettingsSwitchCell to the given |on| state.
id<GREYAction> TurnSettingsSwitchOn(BOOL on);
// Action to turn the switch of a SyncSwitchCell to the given |on| state.
......
......@@ -7,6 +7,7 @@
#import "base/mac/foundation_util.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h"
#import "ios/chrome/browser/ui/settings/cells/legacy/legacy_settings_switch_item.h"
#import "ios/chrome/browser/ui/settings/cells/settings_switch_item.h"
#import "ios/chrome/browser/ui/settings/cells/sync_switch_item.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "ios/web/public/test/earl_grey/web_view_actions.h"
......@@ -15,6 +16,10 @@
#error "This file requires ARC support."
#endif
namespace {
NSString* kChromeActionsErrorDomain = @"ChromeActionsError";
} // namespace
namespace chrome_test_util {
id<GREYAction> LongPressElementForContextMenu(
......@@ -27,22 +32,66 @@ id<GREYAction> LongPressElementForContextMenu(
id<GREYAction> TurnSettingsSwitchOn(BOOL on) {
id<GREYMatcher> constraints = grey_not(grey_systemAlertViewShown());
NSString* actionName =
NSString* action_name =
[NSString stringWithFormat:@"Turn settings switch to %@ state",
on ? @"ON" : @"OFF"];
return [GREYActionBlock
actionWithName:actionName
actionWithName:action_name
constraints:constraints
performBlock:^BOOL(id collectionViewCell,
__strong NSError** errorOrNil) {
LegacySettingsSwitchCell* switchCell =
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>(
collectionViewCell);
UISwitch* switchView = switchCell.switchView;
if (switchView.on != on) {
id<GREYAction> longPressAction = [GREYActions
performBlock:^BOOL(id collection_view_cell,
__strong NSError** error_or_nil) {
SettingsSwitchCell* switch_cell =
base::mac::ObjCCast<SettingsSwitchCell>(collection_view_cell);
if (!switch_cell) {
*error_or_nil = [NSError
errorWithDomain:kChromeActionsErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey : @"The element isn't of "
@"the expected type "
@"(SettingsSwitchCell)."
}];
return NO;
}
UISwitch* switch_view = switch_cell.switchView;
if (switch_view.on != on) {
id<GREYAction> long_press_action = [GREYActions
actionForLongPressWithDuration:kGREYLongPressDefaultDuration];
return [long_press_action perform:switch_view error:error_or_nil];
}
return YES;
}];
}
id<GREYAction> TurnLegacySettingsSwitchOn(BOOL on) {
id<GREYMatcher> constraints = grey_not(grey_systemAlertViewShown());
NSString* action_name =
[NSString stringWithFormat:@"Turn settings switch to %@ state",
on ? @"ON" : @"OFF"];
return [GREYActionBlock
actionWithName:action_name
constraints:constraints
performBlock:^BOOL(id collection_view_cell,
__strong NSError** error_or_nil) {
LegacySettingsSwitchCell* switch_cell =
base::mac::ObjCCast<LegacySettingsSwitchCell>(
collection_view_cell);
if (!switch_cell) {
*error_or_nil =
[NSError errorWithDomain:kChromeActionsErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey :
@"The element isn't of the expected type "
@"(LegacySettingsSwitchCell)."
}];
return NO;
}
UISwitch* switch_view = switch_cell.switchView;
if (switch_view.on != on) {
id<GREYAction> long_press_action = [GREYActions
actionForLongPressWithDuration:kGREYLongPressDefaultDuration];
return [longPressAction perform:switchView error:errorOrNil];
return [long_press_action perform:switch_view error:error_or_nil];
}
return YES;
}];
......@@ -55,14 +104,15 @@ id<GREYAction> TurnSyncSwitchOn(BOOL on) {
return [GREYActionBlock
actionWithName:actionName
constraints:constraints
performBlock:^BOOL(id syncSwitchCell, __strong NSError** errorOrNil) {
SyncSwitchCell* switchCell =
base::mac::ObjCCastStrict<SyncSwitchCell>(syncSwitchCell);
UISwitch* switchView = switchCell.switchView;
if (switchView.on != on) {
id<GREYAction> longPressAction = [GREYActions
performBlock:^BOOL(id sync_switch_cell,
__strong NSError** error_or_nil) {
SyncSwitchCell* switch_cell =
base::mac::ObjCCastStrict<SyncSwitchCell>(sync_switch_cell);
UISwitch* switch_view = switch_cell.switchView;
if (switch_view.on != on) {
id<GREYAction> long_press_action = [GREYActions
actionForLongPressWithDuration:kGREYLongPressDefaultDuration];
return [longPressAction perform:switchView error:errorOrNil];
return [long_press_action perform:switch_view error:error_or_nil];
}
return YES;
}];
......
......@@ -89,14 +89,25 @@ id<GREYMatcher> TabletTabSwitcherOpenButton();
// Matcher for show tabs button.
id<GREYMatcher> ShowTabsButton();
// Matcher for SettingsSwitchCell.
id<GREYMatcher> SettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on);
// Matcher for SettingsSwitchCell.
id<GREYMatcher> SettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on,
BOOL is_enabled);
// Matcher for LegacySettingsSwitchCell.
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibilityIdentifier,
BOOL isToggledOn);
// TODO(crbug.com/894800): Remove this.
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on);
// Matcher for LegacySettingsSwitchCell.
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibilityIdentifier,
BOOL isToggledOn,
BOOL isEnabled);
// TODO(crbug.com/894800): Remove this.
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on,
BOOL is_enabled);
// Matcher for SyncSwitchCell.
id<GREYMatcher> SyncSwitchCell(NSString* accessibilityLabel, BOOL isToggledOn);
......
......@@ -26,6 +26,7 @@
#import "ios/chrome/browser/ui/settings/accounts_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/cells/clear_browsing_data_constants.h"
#import "ios/chrome/browser/ui/settings/cells/legacy/legacy_settings_switch_item.h"
#import "ios/chrome/browser/ui/settings/cells/settings_switch_item.h"
#import "ios/chrome/browser/ui/settings/cells/sync_switch_item.h"
#import "ios/chrome/browser/ui/settings/clear_browsing_data_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/clear_browsing_data_ui_constants.h"
......@@ -48,35 +49,53 @@
namespace {
id<GREYMatcher> SettingsSwitchIsToggledOn(BOOL isToggledOn) {
// TODO(crbug.com/894800): Remove |use_new_cell|.
id<GREYMatcher> SettingsSwitchIsToggledOn(BOOL is_toggled_on,
BOOL use_new_cell) {
MatchesBlock matches = ^BOOL(id element) {
LegacySettingsSwitchCell* switch_cell =
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>(element);
UISwitch* switch_view = switch_cell.switchView;
return (switch_view.on && isToggledOn) || (!switch_view.on && !isToggledOn);
UISwitch* switch_view;
if (use_new_cell) {
SettingsSwitchCell* switch_cell =
base::mac::ObjCCastStrict<SettingsSwitchCell>(element);
switch_view = switch_cell.switchView;
} else {
LegacySettingsSwitchCell* switch_cell =
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>(element);
switch_view = switch_cell.switchView;
}
return (switch_view.on && is_toggled_on) ||
(!switch_view.on && !is_toggled_on);
};
DescribeToBlock describe = ^void(id<GREYDescription> description) {
NSString* name =
[NSString stringWithFormat:@"settingsSwitchToggledState(%@)",
isToggledOn ? @"ON" : @"OFF"];
is_toggled_on ? @"ON" : @"OFF"];
[description appendText:name];
};
return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
descriptionBlock:describe];
}
id<GREYMatcher> SettingsSwitchIsEnabled(BOOL isEnabled) {
// TODO(crbug.com/894800): Remove |use_new_cell|.
id<GREYMatcher> SettingsSwitchIsEnabled(BOOL is_enabled, BOOL use_new_cell) {
MatchesBlock matches = ^BOOL(id element) {
LegacySettingsSwitchCell* switch_cell =
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>(element);
UISwitch* switch_view = switch_cell.switchView;
return (switch_view.enabled && isEnabled) ||
(!switch_view.enabled && !isEnabled);
UISwitch* switch_view;
if (use_new_cell) {
SettingsSwitchCell* switch_cell =
base::mac::ObjCCastStrict<SettingsSwitchCell>(element);
switch_view = switch_cell.switchView;
} else {
LegacySettingsSwitchCell* switch_cell =
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>(element);
switch_view = switch_cell.switchView;
}
return (switch_view.enabled && is_enabled) ||
(!switch_view.enabled && !is_enabled);
};
DescribeToBlock describe = ^void(id<GREYDescription> description) {
NSString* name =
[NSString stringWithFormat:@"settingsSwitchEnabledState(%@)",
isEnabled ? @"YES" : @"NO"];
is_enabled ? @"YES" : @"NO"];
[description appendText:name];
};
return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches
......@@ -254,26 +273,41 @@ id<GREYMatcher> ShowTabsButton() {
grey_sufficientlyVisible(), nil);
}
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibilityIdentifier,
BOOL isToggledOn) {
return LegacySettingsSwitchCell(accessibilityIdentifier, isToggledOn, YES);
id<GREYMatcher> SettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on) {
return SettingsSwitchCell(accessibility_identifier, is_toggled_on, YES);
}
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibilityIdentifier,
BOOL isToggledOn,
BOOL isEnabled) {
return grey_allOf(grey_accessibilityID(accessibilityIdentifier),
SettingsSwitchIsToggledOn(isToggledOn),
SettingsSwitchIsEnabled(isEnabled),
id<GREYMatcher> SettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on,
BOOL is_enabled) {
return grey_allOf(grey_accessibilityID(accessibility_identifier),
SettingsSwitchIsToggledOn(is_toggled_on, YES),
SettingsSwitchIsEnabled(is_enabled, YES),
grey_sufficientlyVisible(), nil);
}
id<GREYMatcher> SyncSwitchCell(NSString* accessibilityLabel, BOOL isToggledOn) {
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on) {
return LegacySettingsSwitchCell(accessibility_identifier, is_toggled_on, YES);
}
id<GREYMatcher> LegacySettingsSwitchCell(NSString* accessibility_identifier,
BOOL is_toggled_on,
BOOL is_enabled) {
return grey_allOf(grey_accessibilityID(accessibility_identifier),
SettingsSwitchIsToggledOn(is_toggled_on, NO),
SettingsSwitchIsEnabled(is_enabled, NO),
grey_sufficientlyVisible(), nil);
}
id<GREYMatcher> SyncSwitchCell(NSString* accessibilityLabel,
BOOL is_toggled_on) {
return grey_allOf(
grey_accessibilityLabel(accessibilityLabel),
grey_accessibilityValue(
isToggledOn ? l10n_util::GetNSString(IDS_IOS_SETTING_ON)
: l10n_util::GetNSString(IDS_IOS_SETTING_OFF)),
is_toggled_on ? l10n_util::GetNSString(IDS_IOS_SETTING_ON)
: l10n_util::GetNSString(IDS_IOS_SETTING_OFF)),
grey_sufficientlyVisible(), nil);
}
......
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