Commit 78b9bb38 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Convert TranslateCollectionVC to UITableView

Also fixes bugs with the SwitchItem and adding test for the
ChromeTableViewControllerTest platform test helper.

Bug: 894791
Change-Id: Ib2d4631ac65ce380dbe18d43603a34e74baec66c
Reviewed-on: https://chromium-review.googlesource.com/c/1297977
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603121}
parent bfae0f8b
...@@ -99,8 +99,8 @@ source_set("settings") { ...@@ -99,8 +99,8 @@ source_set("settings") {
"table_cell_catalog_view_controller.mm", "table_cell_catalog_view_controller.mm",
"time_range_selector_collection_view_controller.h", "time_range_selector_collection_view_controller.h",
"time_range_selector_collection_view_controller.mm", "time_range_selector_collection_view_controller.mm",
"translate_collection_view_controller.h", "translate_table_view_controller.h",
"translate_collection_view_controller.mm", "translate_table_view_controller.mm",
"voicesearch_collection_view_controller.h", "voicesearch_collection_view_controller.h",
"voicesearch_collection_view_controller.mm", "voicesearch_collection_view_controller.mm",
] ]
...@@ -293,7 +293,7 @@ source_set("unit_tests") { ...@@ -293,7 +293,7 @@ source_set("unit_tests") {
"sync_encryption_passphrase_collection_view_controller_unittest.mm", "sync_encryption_passphrase_collection_view_controller_unittest.mm",
"sync_settings_collection_view_controller_unittest.mm", "sync_settings_collection_view_controller_unittest.mm",
"time_range_selector_collection_view_controller_unittest.mm", "time_range_selector_collection_view_controller_unittest.mm",
"translate_collection_view_controller_unittest.mm", "translate_table_view_controller_unittest.mm",
"voicesearch_collection_view_controller_unittest.mm", "voicesearch_collection_view_controller_unittest.mm",
] ]
deps = [ deps = [
......
...@@ -5,7 +5,12 @@ ...@@ -5,7 +5,12 @@
#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CELLS_CONSTANTS_H_ #ifndef IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CELLS_CONSTANTS_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CELLS_CONSTANTS_H_ #define IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CELLS_CONSTANTS_H_
#import <UIKit/UIKit.h>
// The color of the detail text for the settings cells. // The color of the detail text for the settings cells.
extern const int kSettingsCellsDetailTextColor; extern const int kSettingsCellsDetailTextColor;
// Default height for the settings cells.
extern const CGFloat kSettingsCellDefaultHeight;
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CELLS_CONSTANTS_H_ #endif // IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CELLS_CONSTANTS_H_
...@@ -9,3 +9,5 @@ ...@@ -9,3 +9,5 @@
#endif #endif
const int kSettingsCellsDetailTextColor = 0x767676; const int kSettingsCellsDetailTextColor = 0x767676;
const CGFloat kSettingsCellDefaultHeight = 70;
...@@ -153,9 +153,7 @@ const CGFloat kIconImageSize = 28; ...@@ -153,9 +153,7 @@ const CGFloat kIconImageSize = 28;
constant:-kTableViewHorizontalSpacing], constant:-kTableViewHorizontalSpacing],
[_iconImageView.centerYAnchor [_iconImageView.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor], constraintEqualToAnchor:_textLabel.centerYAnchor],
[_textLabel.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor],
_iconHiddenConstraint, _iconHiddenConstraint,
]]; ]];
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#import "ios/chrome/browser/ui/settings/cells/legacy/legacy_settings_detail_item.h" #import "ios/chrome/browser/ui/settings/cells/legacy/legacy_settings_detail_item.h"
#import "ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h" #import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/chrome/browser/ui/settings/translate_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/translate_table_view_controller.h"
#import "ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean.h" #import "ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean.h"
#import "ios/chrome/browser/web/mailto_handler_manager.h" #import "ios/chrome/browser/web/mailto_handler_manager.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
...@@ -225,8 +225,8 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -225,8 +225,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
break; break;
} }
case ItemTypeSettingsTranslate: { case ItemTypeSettingsTranslate: {
TranslateCollectionViewController* controller = TranslateTableViewController* controller =
[[TranslateCollectionViewController alloc] [[TranslateTableViewController alloc]
initWithPrefs:browserState_->GetPrefs()]; initWithPrefs:browserState_->GetPrefs()];
controller.dispatcher = self.dispatcher; controller.dispatcher = self.dispatcher;
[self.navigationController pushViewController:controller animated:YES]; [self.navigationController pushViewController:controller animated:YES];
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import "ios/chrome/browser/ui/commands/application_commands.h" #import "ios/chrome/browser/ui/commands/application_commands.h"
#import "ios/chrome/browser/ui/commands/open_new_tab_command.h" #import "ios/chrome/browser/ui/commands/open_new_tab_command.h"
#import "ios/chrome/browser/ui/material_components/utils.h" #import "ios/chrome/browser/ui/material_components/utils.h"
#import "ios/chrome/browser/ui/settings/cells/settings_cells_constants.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h" #import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h" #import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h" #import "ios/chrome/browser/ui/util/uikit_ui_util.h"
...@@ -26,6 +27,10 @@ ...@@ -26,6 +27,10 @@
self.styler.tableViewSectionHeaderBlurEffect = nil; self.styler.tableViewSectionHeaderBlurEffect = nil;
[super viewDidLoad]; [super viewDidLoad];
self.styler.cellBackgroundColor = [UIColor whiteColor]; self.styler.cellBackgroundColor = [UIColor whiteColor];
self.styler.cellTitleColor = [UIColor blackColor];
self.tableView.estimatedRowHeight = kSettingsCellDefaultHeight;
// Do not set the estimated height of the footer/header as if there is no
// header/footer, there is an empty space.
} }
- (void)viewWillAppear:(BOOL)animated { - (void)viewWillAppear:(BOOL)animated {
......
...@@ -2,24 +2,24 @@ ...@@ -2,24 +2,24 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_TRANSLATE_COLLECTION_VIEW_CONTROLLER_H_ #ifndef IOS_CHROME_BROWSER_UI_SETTINGS_TRANSLATE_TABLE_VIEW_CONTROLLER_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_TRANSLATE_COLLECTION_VIEW_CONTROLLER_H_ #define IOS_CHROME_BROWSER_UI_SETTINGS_TRANSLATE_TABLE_VIEW_CONTROLLER_H_
#import "ios/chrome/browser/ui/settings/settings_root_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"
class PrefService; class PrefService;
// Controller for the UI that allows the user to control Translate settings. // Controller for the UI that allows the user to control Translate settings.
@interface TranslateCollectionViewController @interface TranslateTableViewController : SettingsRootTableViewController
: SettingsRootCollectionViewController
// |prefs| must not be nil. // |prefs| must not be nil.
- (instancetype)initWithPrefs:(PrefService*)prefs NS_DESIGNATED_INITIALIZER; - (instancetype)initWithPrefs:(PrefService*)prefs NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithLayout:(UICollectionViewLayout*)layout - (instancetype)initWithTableViewStyle:(UITableViewStyle)style
style:(CollectionViewControllerStyle)style appBarStyle:
(ChromeTableViewControllerStyle)appBarStyle
NS_UNAVAILABLE; NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
@end @end
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_TRANSLATE_COLLECTION_VIEW_CONTROLLER_H_ #endif // IOS_CHROME_BROWSER_UI_SETTINGS_TRANSLATE_TABLE_VIEW_CONTROLLER_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import "ios/chrome/browser/ui/settings/translate_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/translate_table_view_controller.h"
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include <memory> #include <memory>
...@@ -15,19 +15,17 @@ ...@@ -15,19 +15,17 @@
#include "components/translate/core/browser/translate_prefs.h" #include "components/translate/core/browser/translate_prefs.h"
#include "ios/chrome/browser/application_context.h" #include "ios/chrome/browser/application_context.h"
#import "ios/chrome/browser/translate/chrome_ios_translate_client.h" #import "ios/chrome/browser/translate/chrome_ios_translate_client.h"
#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h" #import "ios/chrome/browser/ui/settings/cells/settings_cells_constants.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item.h" #import "ios/chrome/browser/ui/settings/cells/settings_switch_item.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
#import "ios/chrome/browser/ui/settings/cells/legacy/legacy_settings_switch_item.h"
#import "ios/chrome/browser/ui/settings/cells/settings_text_item.h"
#import "ios/chrome/browser/ui/settings/settings_utils.h" #import "ios/chrome/browser/ui/settings/settings_utils.h"
#import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_detail_text_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_link_header_footer_item.h"
#import "ios/chrome/browser/ui/table_view/table_view_model.h"
#include "ios/chrome/browser/ui/util/uikit_ui_util.h" #include "ios/chrome/browser/ui/util/uikit_ui_util.h"
#include "ios/chrome/grit/ios_chromium_strings.h" #include "ios/chrome/grit/ios_chromium_strings.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#import "ios/third_party/material_components_ios/src/components/Palettes/src/MaterialPalettes.h"
#import "ios/third_party/material_components_ios/src/components/Snackbar/src/MaterialSnackbar.h" #import "ios/third_party/material_components_ios/src/components/Snackbar/src/MaterialSnackbar.h"
#import "ios/third_party/material_components_ios/src/components/Typography/src/MaterialTypography.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -39,7 +37,6 @@ namespace { ...@@ -39,7 +37,6 @@ namespace {
typedef NS_ENUM(NSInteger, SectionIdentifier) { typedef NS_ENUM(NSInteger, SectionIdentifier) {
SectionIdentifierTranslate = kSectionIdentifierEnumZero, SectionIdentifierTranslate = kSectionIdentifierEnumZero,
SectionIdentifierFooter,
}; };
typedef NS_ENUM(NSInteger, ItemType) { typedef NS_ENUM(NSInteger, ItemType) {
...@@ -54,89 +51,88 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings"; ...@@ -54,89 +51,88 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings";
} // namespace } // namespace
@interface TranslateCollectionViewController ()<BooleanObserver> { @interface TranslateTableViewController ()<BooleanObserver> {
// Profile preferences. // Profile preferences.
PrefService* _prefs; // weak PrefService* _prefs; // weak
PrefBackedBoolean* _translationEnabled; PrefBackedBoolean* _translationEnabled;
// The item related to the switch for the translation setting. // The item related to the switch for the translation setting.
LegacySettingsSwitchItem* _translationItem; SettingsSwitchItem* _translationItem;
} }
@end @end
@implementation TranslateCollectionViewController @implementation TranslateTableViewController
#pragma mark - Initialization #pragma mark - Initialization
- (instancetype)initWithPrefs:(PrefService*)prefs { - (instancetype)initWithPrefs:(PrefService*)prefs {
DCHECK(prefs); DCHECK(prefs);
UICollectionViewLayout* layout = [[MDCCollectionViewFlowLayout alloc] init];
self = self =
[super initWithLayout:layout style:CollectionViewControllerStyleAppBar]; [super initWithTableViewStyle:UITableViewStyleGrouped
appBarStyle:ChromeTableViewControllerStyleWithAppBar];
if (self) { if (self) {
self.title = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING);
self.collectionViewAccessibilityIdentifier =
@"translate_settings_view_controller";
_prefs = prefs; _prefs = prefs;
_translationEnabled = [[PrefBackedBoolean alloc] _translationEnabled = [[PrefBackedBoolean alloc]
initWithPrefService:_prefs initWithPrefService:_prefs
prefName:prefs::kOfferTranslateEnabled]; prefName:prefs::kOfferTranslateEnabled];
[_translationEnabled setObserver:self]; [_translationEnabled setObserver:self];
// TODO(crbug.com/764578): -loadModel should not be called from
// initializer. A possible fix is to move this call to -viewDidLoad.
[self loadModel];
} }
return self; return self;
} }
#pragma mark - SettingsRootCollectionViewController - (void)viewDidLoad {
[super viewDidLoad];
self.title = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING);
self.tableView.accessibilityIdentifier =
@"translate_settings_view_controller";
[self loadModel];
self.tableView.estimatedSectionFooterHeight = kSettingsCellDefaultHeight;
}
#pragma mark - SettingsRootTableViewController
- (void)loadModel { - (void)loadModel {
[super loadModel]; [super loadModel];
CollectionViewModel* model = self.collectionViewModel; TableViewModel<TableViewItem*>* model = self.tableViewModel;
// Translate Section // Translate Section
[model addSectionWithIdentifier:SectionIdentifierTranslate]; [model addSectionWithIdentifier:SectionIdentifierTranslate];
_translationItem = _translationItem =
[[LegacySettingsSwitchItem alloc] initWithType:ItemTypeTranslate]; [[SettingsSwitchItem alloc] initWithType:ItemTypeTranslate];
_translationItem.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING); _translationItem.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING);
_translationItem.on = [_translationEnabled value]; _translationItem.on = [_translationEnabled value];
[model addItem:_translationItem [model addItem:_translationItem
toSectionWithIdentifier:SectionIdentifierTranslate]; toSectionWithIdentifier:SectionIdentifierTranslate];
SettingsTextItem* resetTranslate = TableViewDetailTextItem* resetTranslate =
[[SettingsTextItem alloc] initWithType:ItemTypeResetTranslate]; [[TableViewDetailTextItem alloc] initWithType:ItemTypeResetTranslate];
resetTranslate.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_RESET); resetTranslate.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_RESET);
resetTranslate.accessibilityTraits |= UIAccessibilityTraitButton; resetTranslate.accessibilityTraits |= UIAccessibilityTraitButton;
[model addItem:resetTranslate [model addItem:resetTranslate
toSectionWithIdentifier:SectionIdentifierTranslate]; toSectionWithIdentifier:SectionIdentifierTranslate];
// Footer Section TableViewLinkHeaderFooterItem* footer =
[model addSectionWithIdentifier:SectionIdentifierFooter]; [[TableViewLinkHeaderFooterItem alloc] initWithType:ItemTypeFooter];
CollectionViewFooterItem* footer =
[[CollectionViewFooterItem alloc] initWithType:ItemTypeFooter];
footer.cellStyle = CollectionViewCellStyle::kUIKit;
footer.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_DESCRIPTION); footer.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_DESCRIPTION);
footer.linkURL = google_util::AppendGoogleLocaleParam( footer.linkURL = google_util::AppendGoogleLocaleParam(
GURL(kTranslateLearnMoreUrl), GURL(kTranslateLearnMoreUrl),
GetApplicationContext()->GetApplicationLocale()); GetApplicationContext()->GetApplicationLocale());
footer.linkDelegate = self; [model setFooter:footer forSectionWithIdentifier:SectionIdentifierTranslate];
[model addItem:footer toSectionWithIdentifier:SectionIdentifierFooter];
} }
#pragma mark UICollectionViewDataSource #pragma mark - UITableViewDataSource
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView - (UITableViewCell*)tableView:(UITableView*)tableView
cellForItemAtIndexPath:(NSIndexPath*)indexPath { cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UICollectionViewCell* cell = UITableViewCell* cell =
[super collectionView:collectionView cellForItemAtIndexPath:indexPath]; [super tableView:tableView cellForRowAtIndexPath:indexPath];
NSInteger itemType = NSInteger itemType = [self.tableViewModel itemTypeForIndexPath:indexPath];
[self.collectionViewModel itemTypeForIndexPath:indexPath];
switch (itemType) { switch (itemType) {
case ItemTypeTranslate: { case ItemTypeTranslate: {
LegacySettingsSwitchCell* switchCell = cell.selectionStyle = UITableViewCellSelectionStyleNone;
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>(cell); SettingsSwitchCell* switchCell =
base::mac::ObjCCastStrict<SettingsSwitchCell>(cell);
[switchCell.switchView addTarget:self [switchCell.switchView addTarget:self
action:@selector(translateToggled:) action:@selector(translateToggled:)
forControlEvents:UIControlEventValueChanged]; forControlEvents:UIControlEventValueChanged];
...@@ -149,13 +145,24 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings"; ...@@ -149,13 +145,24 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings";
return cell; return cell;
} }
#pragma mark UICollectionViewDelegate #pragma mark - UITableViewDelegate
- (void)collectionView:(UICollectionView*)collectionView
didSelectItemAtIndexPath:(NSIndexPath*)indexPath { - (UIView*)tableView:(UITableView*)tableView
[super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; viewForFooterInSection:(NSInteger)section {
UIView* footerView =
[super tableView:tableView viewForFooterInSection:section];
if (SectionIdentifierTranslate ==
[self.tableViewModel sectionIdentifierForSection:section]) {
TableViewLinkHeaderFooterView* footer =
base::mac::ObjCCastStrict<TableViewLinkHeaderFooterView>(footerView);
footer.delegate = self;
}
return footerView;
}
NSInteger itemType = - (void)tableView:(UITableView*)tableView
[self.collectionViewModel itemTypeForIndexPath:indexPath]; didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
NSInteger itemType = [self.tableViewModel itemTypeForIndexPath:indexPath];
if (itemType == ItemTypeResetTranslate) { if (itemType == ItemTypeResetTranslate) {
std::unique_ptr<translate::TranslatePrefs> translatePrefs( std::unique_ptr<translate::TranslatePrefs> translatePrefs(
...@@ -169,56 +176,7 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings"; ...@@ -169,56 +176,7 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings";
message.category = kTranslateSettingsCategory; message.category = kTranslateSettingsCategory;
[MDCSnackbarManager showMessage:message]; [MDCSnackbarManager showMessage:message];
} }
} [tableView deselectRowAtIndexPath:indexPath animated:NO];
#pragma mark MDCCollectionViewStylingDelegate
- (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView
cellStyleForSection:(NSInteger)section {
NSInteger sectionIdentifier =
[self.collectionViewModel sectionIdentifierForSection:section];
if (sectionIdentifier == SectionIdentifierFooter) {
return MDCCollectionViewCellStyleDefault;
}
return self.styler.cellStyle;
}
- (BOOL)collectionView:(UICollectionView*)collectionView
shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath {
NSInteger sectionIdentifier =
[self.collectionViewModel sectionIdentifierForSection:indexPath.section];
if (sectionIdentifier == SectionIdentifierFooter) {
return YES;
}
return NO;
}
- (CGFloat)collectionView:(UICollectionView*)collectionView
cellHeightAtIndexPath:(NSIndexPath*)indexPath {
CollectionViewItem* item =
[self.collectionViewModel itemAtIndexPath:indexPath];
if (item.type == ItemTypeFooter)
return [MDCCollectionViewCell
cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
forItem:item];
return MDCCellDefaultOneLineHeight;
}
- (BOOL)collectionView:(UICollectionView*)collectionView
hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
switch (type) {
case ItemTypeFooter:
case ItemTypeTranslate:
return YES;
default:
return NO;
}
} }
#pragma mark - BooleanObserver #pragma mark - BooleanObserver
...@@ -236,16 +194,16 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings"; ...@@ -236,16 +194,16 @@ NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings";
#pragma mark - Actions #pragma mark - Actions
- (void)translateToggled:(id)sender { - (void)translateToggled:(id)sender {
NSIndexPath* switchPath = [self.collectionViewModel NSIndexPath* switchPath =
indexPathForItemType:ItemTypeTranslate [self.tableViewModel indexPathForItemType:ItemTypeTranslate
sectionIdentifier:SectionIdentifierTranslate]; sectionIdentifier:SectionIdentifierTranslate];
LegacySettingsSwitchItem* switchItem = SettingsSwitchItem* switchItem =
base::mac::ObjCCastStrict<LegacySettingsSwitchItem>( base::mac::ObjCCastStrict<SettingsSwitchItem>(
[self.collectionViewModel itemAtIndexPath:switchPath]); [self.tableViewModel itemAtIndexPath:switchPath]);
LegacySettingsSwitchCell* switchCell = SettingsSwitchCell* switchCell =
base::mac::ObjCCastStrict<LegacySettingsSwitchCell>( base::mac::ObjCCastStrict<SettingsSwitchCell>(
[self.collectionView cellForItemAtIndexPath:switchPath]); [self.tableView cellForRowAtIndexPath:switchPath]);
DCHECK_EQ(switchCell.switchView, sender); DCHECK_EQ(switchCell.switchView, sender);
BOOL isOn = switchCell.switchView.isOn; BOOL isOn = switchCell.switchView.isOn;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import "ios/chrome/browser/ui/settings/translate_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/translate_table_view_controller.h"
#include <memory> #include <memory>
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include "components/translate/core/browser/translate_prefs.h" #include "components/translate/core/browser/translate_prefs.h"
#include "ios/chrome/browser/pref_names.h" #include "ios/chrome/browser/pref_names.h"
#import "ios/chrome/browser/translate/chrome_ios_translate_client.h" #import "ios/chrome/browser/translate/chrome_ios_translate_client.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h" #import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h" #import "testing/gtest_mac.h"
...@@ -39,20 +39,19 @@ const char kBlacklistedSite[] = "http://blacklistedsite.com"; ...@@ -39,20 +39,19 @@ const char kBlacklistedSite[] = "http://blacklistedsite.com";
const char kLanguage1[] = "klingon"; const char kLanguage1[] = "klingon";
const char kLanguage2[] = "pirate"; const char kLanguage2[] = "pirate";
class TranslateCollectionViewControllerTest class TranslateTableViewControllerTest : public ChromeTableViewControllerTest {
: public CollectionViewControllerTest {
protected: protected:
TranslateCollectionViewControllerTest() TranslateTableViewControllerTest()
: scoped_task_environment_( : scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {} base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override { void SetUp() override {
CollectionViewControllerTest::SetUp(); ChromeTableViewControllerTest::SetUp();
pref_service_ = CreateLocalState(); pref_service_ = CreateLocalState();
} }
CollectionViewController* InstantiateController() override { ChromeTableViewController* InstantiateController() override {
return [[TranslateCollectionViewController alloc] return [[TranslateTableViewController alloc]
initWithPrefs:pref_service_.get()]; initWithPrefs:pref_service_.get()];
} }
...@@ -64,7 +63,7 @@ class TranslateCollectionViewControllerTest ...@@ -64,7 +63,7 @@ class TranslateCollectionViewControllerTest
registry->RegisterStringPref( registry->RegisterStringPref(
prefs::kAcceptLanguages, prefs::kAcceptLanguages,
l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES)); l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES));
base::FilePath path("TranslateCollectionViewControllerTest.pref"); base::FilePath path("TranslateTableViewControllerTest.pref");
sync_preferences::PrefServiceMockFactory factory; sync_preferences::PrefServiceMockFactory factory;
factory.SetUserPrefsFile(path, base::ThreadTaskRunnerHandle::Get().get()); factory.SetUserPrefsFile(path, base::ThreadTaskRunnerHandle::Get().get());
return factory.Create(registry.get()); return factory.Create(registry.get());
...@@ -74,27 +73,27 @@ class TranslateCollectionViewControllerTest ...@@ -74,27 +73,27 @@ class TranslateCollectionViewControllerTest
std::unique_ptr<PrefService> pref_service_; std::unique_ptr<PrefService> pref_service_;
}; };
TEST_F(TranslateCollectionViewControllerTest, TestModelTranslateOff) { TEST_F(TranslateTableViewControllerTest, TestModelTranslateOff) {
CreateController(); CreateController();
CheckController(); CheckController();
EXPECT_EQ(2, NumberOfSections()); EXPECT_EQ(1, NumberOfSections());
EXPECT_EQ(2, NumberOfItemsInSection(0)); EXPECT_EQ(2, NumberOfItemsInSection(0));
CheckSwitchCellStateAndTitleWithId(NO, IDS_IOS_TRANSLATE_SETTING, 0, 0); CheckSwitchCellStateAndTextWithId(NO, IDS_IOS_TRANSLATE_SETTING, 0, 0);
CheckTextCellTitleWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1); CheckTextCellTextWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1);
} }
TEST_F(TranslateCollectionViewControllerTest, TestModelTranslateOn) { TEST_F(TranslateTableViewControllerTest, TestModelTranslateOn) {
BooleanPrefMember translateEnabled; BooleanPrefMember translateEnabled;
translateEnabled.Init(prefs::kOfferTranslateEnabled, pref_service_.get()); translateEnabled.Init(prefs::kOfferTranslateEnabled, pref_service_.get());
translateEnabled.SetValue(true); translateEnabled.SetValue(true);
CreateController(); CreateController();
EXPECT_EQ(2, NumberOfSections()); EXPECT_EQ(1, NumberOfSections());
EXPECT_EQ(2, NumberOfItemsInSection(0)); EXPECT_EQ(2, NumberOfItemsInSection(0));
CheckSwitchCellStateAndTitleWithId(YES, IDS_IOS_TRANSLATE_SETTING, 0, 0); CheckSwitchCellStateAndTextWithId(YES, IDS_IOS_TRANSLATE_SETTING, 0, 0);
CheckTextCellTitleWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1); CheckTextCellTextWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1);
} }
TEST_F(TranslateCollectionViewControllerTest, TestClearPreferences) { TEST_F(TranslateTableViewControllerTest, TestClearPreferences) {
// Set some preferences. // Set some preferences.
std::unique_ptr<translate::TranslatePrefs> translate_prefs( std::unique_ptr<translate::TranslatePrefs> translate_prefs(
ChromeIOSTranslateClient::CreateTranslatePrefs(pref_service_.get())); ChromeIOSTranslateClient::CreateTranslatePrefs(pref_service_.get()));
...@@ -107,11 +106,11 @@ TEST_F(TranslateCollectionViewControllerTest, TestClearPreferences) { ...@@ -107,11 +106,11 @@ TEST_F(TranslateCollectionViewControllerTest, TestClearPreferences) {
translate_prefs->IsLanguagePairWhitelisted(kLanguage1, kLanguage2)); translate_prefs->IsLanguagePairWhitelisted(kLanguage1, kLanguage2));
// Reset the preferences through the UI. // Reset the preferences through the UI.
CreateController(); CreateController();
TranslateCollectionViewController* controller = TranslateTableViewController* controller =
static_cast<TranslateCollectionViewController*>(this->controller()); static_cast<TranslateTableViewController*>(this->controller());
// Simulate a tap on the "reset" item. // Simulate a tap on the "reset" item.
[controller collectionView:[controller collectionView] [controller tableView:controller.tableView
didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]]; didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
// Check that preferences are gone. // Check that preferences are gone.
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(kBlacklistedSite)); EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(kBlacklistedSite));
EXPECT_FALSE(translate_prefs->IsBlockedLanguage(kLanguage1)); EXPECT_FALSE(translate_prefs->IsBlockedLanguage(kLanguage1));
......
...@@ -83,20 +83,21 @@ class ChromeTableViewControllerTest : public BlockCleanupTest { ...@@ -83,20 +83,21 @@ class ChromeTableViewControllerTest : public BlockCleanupTest {
int section_id, int section_id,
int item_id); int item_id);
// Verifies that the switch cell at |item| in |section| has a title which // Verifies that the switch cell at |item| in |section| has a text property
// matches |expected_title| and is currently in |state|. // which matches |expected_title| and a isOn method which matches
void CheckSwitchCellStateAndTitle(BOOL expected_state, // |expected_state|.
NSString* expected_title, void CheckSwitchCellStateAndText(BOOL expected_state,
int section, NSString* expected_title,
int item); int section,
int item);
// Verifies that the switch cell at |item| in |section| has a title which
// matches the l10n string for |expected_title_id| and is currently in // Verifies that the switch cell at |item| in |section| has a text property
// |state|. // which matches the l10n string for |expected_title_id| and a isOn method
void CheckSwitchCellStateAndTitleWithId(BOOL expected_state, // which matches |expected_state|.
int expected_title_id, void CheckSwitchCellStateAndTextWithId(BOOL expected_state,
int section, int expected_title_id,
int item); int section,
int item);
// Verifies that the cell at |item| in |section| has the given // Verifies that the cell at |item| in |section| has the given
// |accessory_type|. // |accessory_type|.
......
...@@ -140,21 +140,24 @@ void ChromeTableViewControllerTest::CheckDetailItemTextWithIds( ...@@ -140,21 +140,24 @@ void ChromeTableViewControllerTest::CheckDetailItemTextWithIds(
[item detailText]); [item detailText]);
} }
void ChromeTableViewControllerTest::CheckSwitchCellStateAndTitle( void ChromeTableViewControllerTest::CheckSwitchCellStateAndText(
BOOL expected_state, BOOL expected_state,
NSString* expected_title, NSString* expected_title,
int section, int section,
int item) { int item) {
// TODO(crbug.com/894791): Implement this. id switch_item = GetTableViewItem(section, item);
NOTREACHED(); EXPECT_TRUE([switch_item respondsToSelector:@selector(text)]);
EXPECT_NSEQ(expected_title, [switch_item text]);
EXPECT_TRUE([switch_item respondsToSelector:@selector(isOn)]);
EXPECT_EQ(expected_state, [switch_item isOn]);
} }
void ChromeTableViewControllerTest::CheckSwitchCellStateAndTitleWithId( void ChromeTableViewControllerTest::CheckSwitchCellStateAndTextWithId(
BOOL expected_state, BOOL expected_state,
int expected_title_id, int expected_title_id,
int section, int section,
int item) { int item) {
CheckSwitchCellStateAndTitle( CheckSwitchCellStateAndText(
expected_state, l10n_util::GetNSString(expected_title_id), section, item); expected_state, l10n_util::GetNSString(expected_title_id), section, item);
} }
......
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