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") {
"table_cell_catalog_view_controller.mm",
"time_range_selector_collection_view_controller.h",
"time_range_selector_collection_view_controller.mm",
"translate_collection_view_controller.h",
"translate_collection_view_controller.mm",
"translate_table_view_controller.h",
"translate_table_view_controller.mm",
"voicesearch_collection_view_controller.h",
"voicesearch_collection_view_controller.mm",
]
......@@ -293,7 +293,7 @@ source_set("unit_tests") {
"sync_encryption_passphrase_collection_view_controller_unittest.mm",
"sync_settings_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",
]
deps = [
......
......@@ -5,7 +5,12 @@
#ifndef 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.
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_
......@@ -9,3 +9,5 @@
#endif
const int kSettingsCellsDetailTextColor = 0x767676;
const CGFloat kSettingsCellDefaultHeight = 70;
......@@ -153,9 +153,7 @@ const CGFloat kIconImageSize = 28;
constant:-kTableViewHorizontalSpacing],
[_iconImageView.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor],
[_textLabel.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor],
constraintEqualToAnchor:_textLabel.centerYAnchor],
_iconHiddenConstraint,
]];
......
......@@ -22,7 +22,7 @@
#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/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/web/mailto_handler_manager.h"
#include "ios/chrome/grit/ios_strings.h"
......@@ -225,8 +225,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
break;
}
case ItemTypeSettingsTranslate: {
TranslateCollectionViewController* controller =
[[TranslateCollectionViewController alloc]
TranslateTableViewController* controller =
[[TranslateTableViewController alloc]
initWithPrefs:browserState_->GetPrefs()];
controller.dispatcher = self.dispatcher;
[self.navigationController pushViewController:controller animated:YES];
......
......@@ -8,6 +8,7 @@
#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/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/table_view/chrome_table_view_styler.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
......@@ -26,6 +27,10 @@
self.styler.tableViewSectionHeaderBlurEffect = nil;
[super viewDidLoad];
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 {
......
......@@ -2,24 +2,24 @@
// 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_SETTINGS_TRANSLATE_COLLECTION_VIEW_CONTROLLER_H_
#define 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_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;
// Controller for the UI that allows the user to control Translate settings.
@interface TranslateCollectionViewController
: SettingsRootCollectionViewController
@interface TranslateTableViewController : SettingsRootTableViewController
// |prefs| must not be nil.
- (instancetype)initWithPrefs:(PrefService*)prefs NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithLayout:(UICollectionViewLayout*)layout
style:(CollectionViewControllerStyle)style
- (instancetype)initWithTableViewStyle:(UITableViewStyle)style
appBarStyle:
(ChromeTableViewControllerStyle)appBarStyle
NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
@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 @@
// 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/settings/translate_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/translate_table_view_controller.h"
#include <memory>
......@@ -19,7 +19,7 @@
#include "components/translate/core/browser/translate_prefs.h"
#include "ios/chrome/browser/pref_names.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 "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
......@@ -39,20 +39,19 @@ const char kBlacklistedSite[] = "http://blacklistedsite.com";
const char kLanguage1[] = "klingon";
const char kLanguage2[] = "pirate";
class TranslateCollectionViewControllerTest
: public CollectionViewControllerTest {
class TranslateTableViewControllerTest : public ChromeTableViewControllerTest {
protected:
TranslateCollectionViewControllerTest()
TranslateTableViewControllerTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override {
CollectionViewControllerTest::SetUp();
ChromeTableViewControllerTest::SetUp();
pref_service_ = CreateLocalState();
}
CollectionViewController* InstantiateController() override {
return [[TranslateCollectionViewController alloc]
ChromeTableViewController* InstantiateController() override {
return [[TranslateTableViewController alloc]
initWithPrefs:pref_service_.get()];
}
......@@ -64,7 +63,7 @@ class TranslateCollectionViewControllerTest
registry->RegisterStringPref(
prefs::kAcceptLanguages,
l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES));
base::FilePath path("TranslateCollectionViewControllerTest.pref");
base::FilePath path("TranslateTableViewControllerTest.pref");
sync_preferences::PrefServiceMockFactory factory;
factory.SetUserPrefsFile(path, base::ThreadTaskRunnerHandle::Get().get());
return factory.Create(registry.get());
......@@ -74,27 +73,27 @@ class TranslateCollectionViewControllerTest
std::unique_ptr<PrefService> pref_service_;
};
TEST_F(TranslateCollectionViewControllerTest, TestModelTranslateOff) {
TEST_F(TranslateTableViewControllerTest, TestModelTranslateOff) {
CreateController();
CheckController();
EXPECT_EQ(2, NumberOfSections());
EXPECT_EQ(1, NumberOfSections());
EXPECT_EQ(2, NumberOfItemsInSection(0));
CheckSwitchCellStateAndTitleWithId(NO, IDS_IOS_TRANSLATE_SETTING, 0, 0);
CheckTextCellTitleWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1);
CheckSwitchCellStateAndTextWithId(NO, IDS_IOS_TRANSLATE_SETTING, 0, 0);
CheckTextCellTextWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1);
}
TEST_F(TranslateCollectionViewControllerTest, TestModelTranslateOn) {
TEST_F(TranslateTableViewControllerTest, TestModelTranslateOn) {
BooleanPrefMember translateEnabled;
translateEnabled.Init(prefs::kOfferTranslateEnabled, pref_service_.get());
translateEnabled.SetValue(true);
CreateController();
EXPECT_EQ(2, NumberOfSections());
EXPECT_EQ(1, NumberOfSections());
EXPECT_EQ(2, NumberOfItemsInSection(0));
CheckSwitchCellStateAndTitleWithId(YES, IDS_IOS_TRANSLATE_SETTING, 0, 0);
CheckTextCellTitleWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1);
CheckSwitchCellStateAndTextWithId(YES, IDS_IOS_TRANSLATE_SETTING, 0, 0);
CheckTextCellTextWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1);
}
TEST_F(TranslateCollectionViewControllerTest, TestClearPreferences) {
TEST_F(TranslateTableViewControllerTest, TestClearPreferences) {
// Set some preferences.
std::unique_ptr<translate::TranslatePrefs> translate_prefs(
ChromeIOSTranslateClient::CreateTranslatePrefs(pref_service_.get()));
......@@ -107,11 +106,11 @@ TEST_F(TranslateCollectionViewControllerTest, TestClearPreferences) {
translate_prefs->IsLanguagePairWhitelisted(kLanguage1, kLanguage2));
// Reset the preferences through the UI.
CreateController();
TranslateCollectionViewController* controller =
static_cast<TranslateCollectionViewController*>(this->controller());
TranslateTableViewController* controller =
static_cast<TranslateTableViewController*>(this->controller());
// Simulate a tap on the "reset" item.
[controller collectionView:[controller collectionView]
didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
[controller tableView:controller.tableView
didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
// Check that preferences are gone.
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(kBlacklistedSite));
EXPECT_FALSE(translate_prefs->IsBlockedLanguage(kLanguage1));
......
......@@ -83,20 +83,21 @@ class ChromeTableViewControllerTest : public BlockCleanupTest {
int section_id,
int item_id);
// Verifies that the switch cell at |item| in |section| has a title which
// matches |expected_title| and is currently in |state|.
void CheckSwitchCellStateAndTitle(BOOL expected_state,
NSString* expected_title,
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
// |state|.
void CheckSwitchCellStateAndTitleWithId(BOOL expected_state,
int expected_title_id,
int section,
int item);
// Verifies that the switch cell at |item| in |section| has a text property
// which matches |expected_title| and a isOn method which matches
// |expected_state|.
void CheckSwitchCellStateAndText(BOOL expected_state,
NSString* expected_title,
int section,
int item);
// Verifies that the switch cell at |item| in |section| has a text property
// which matches the l10n string for |expected_title_id| and a isOn method
// which matches |expected_state|.
void CheckSwitchCellStateAndTextWithId(BOOL expected_state,
int expected_title_id,
int section,
int item);
// Verifies that the cell at |item| in |section| has the given
// |accessory_type|.
......
......@@ -140,21 +140,24 @@ void ChromeTableViewControllerTest::CheckDetailItemTextWithIds(
[item detailText]);
}
void ChromeTableViewControllerTest::CheckSwitchCellStateAndTitle(
void ChromeTableViewControllerTest::CheckSwitchCellStateAndText(
BOOL expected_state,
NSString* expected_title,
int section,
int item) {
// TODO(crbug.com/894791): Implement this.
NOTREACHED();
id switch_item = GetTableViewItem(section, item);
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,
int expected_title_id,
int section,
int item) {
CheckSwitchCellStateAndTitle(
CheckSwitchCellStateAndText(
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