Commit b59fb043 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Migrate SearchEngineSettingsCollectionViewController from

CollectionViewController to ChromeTableViewController, and show search
engines' keywords in Settings.

This CL migrates the SearchEngineSettingsCollectionViewController from
CollectionViewController, which is based on MDCCollectionViewController,
to ChromeTableViewController, which is based on UITableViewController.

This CL also enables showing search engines' keywords in Settings for
security considerations.

Bug: 894791, 433824
Change-Id: I0eb6cf7081159d13cf389750f99ca13ee162c9d6
Reviewed-on: https://chromium-review.googlesource.com/c/1344134
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avataredchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609781}
parent 27a03725
......@@ -74,8 +74,8 @@ source_set("settings") {
"reauthentication_protocol.h",
"save_passwords_collection_view_controller.h",
"save_passwords_collection_view_controller.mm",
"search_engine_settings_collection_view_controller.h",
"search_engine_settings_collection_view_controller.mm",
"search_engine_table_view_controller.h",
"search_engine_table_view_controller.mm",
"settings_collection_view_controller.h",
"settings_collection_view_controller.mm",
"settings_navigation_controller.h",
......@@ -285,7 +285,7 @@ source_set("unit_tests") {
"privacy_table_view_controller_unittest.mm",
"reauthentication_module_unittest.mm",
"save_passwords_collection_view_controller_unittest.mm",
"search_engine_settings_collection_view_controller_unittest.mm",
"search_engine_table_view_controller_unittest.mm",
"settings_navigation_controller_unittest.mm",
"settings_root_collection_view_controller_unittest.mm",
"settings_root_table_view_controller_unittest.mm",
......
......@@ -2,27 +2,26 @@
// 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_SEARCH_ENGINE_SETTINGS_COLLECTION_VIEW_CONTROLLER_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_SETTINGS_COLLECTION_VIEW_CONTROLLER_H_
#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_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"
namespace ios {
class ChromeBrowserState;
} // namespace ios
// This class is the table view for the Search Engine settings.
@interface SearchEngineSettingsCollectionViewController
: SettingsRootCollectionViewController
@interface SearchEngineTableViewController : SettingsRootTableViewController
// The designated initializer. |browserState| must not be nil.
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithLayout:(UICollectionViewLayout*)layout
style:(CollectionViewControllerStyle)style
- (instancetype)initWithTableViewStyle:(UITableViewStyle)style
appBarStyle:
(ChromeTableViewControllerStyle)appBarStyle
NS_UNAVAILABLE;
@end
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_SETTINGS_COLLECTION_VIEW_CONTROLLER_H_
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_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/search_engine_settings_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/search_engine_table_view_controller.h"
#include <memory>
......@@ -14,11 +14,9 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#import "ios/chrome/browser/search_engines/search_engine_observer_bridge.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
#import "ios/chrome/browser/ui/settings/cells/settings_text_item.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_text_header_footer_item.h"
#include "ios/chrome/grit/ios_strings.h"
#import "ios/third_party/material_components_ios/src/components/CollectionCells/src/MaterialCollectionCells.h"
#import "ios/third_party/material_components_ios/src/components/Palettes/src/MaterialPalettes.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -45,11 +43,10 @@ const char kUmaSelectDefaultSearchEngine[] =
} // namespace
@interface SearchEngineSettingsCollectionViewController ()<
SearchEngineObserving>
@interface SearchEngineTableViewController ()<SearchEngineObserving>
@end
@implementation SearchEngineSettingsCollectionViewController {
@implementation SearchEngineTableViewController {
TemplateURLService* _templateURLService; // weak
std::unique_ptr<SearchEngineObserverBridge> _observer;
// Prevent unnecessary notifications when we write to the setting.
......@@ -67,9 +64,9 @@ const char kUmaSelectDefaultSearchEngine[] =
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState {
DCHECK(browserState);
UICollectionViewLayout* layout = [[MDCCollectionViewFlowLayout alloc] init];
self =
[super initWithLayout:layout style:CollectionViewControllerStyleAppBar];
[super initWithTableViewStyle:UITableViewStyleGrouped
appBarStyle:ChromeTableViewControllerStyleWithAppBar];
if (self) {
_templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(browserState);
......@@ -77,17 +74,23 @@ const char kUmaSelectDefaultSearchEngine[] =
std::make_unique<SearchEngineObserverBridge>(self, _templateURLService);
_templateURLService->Load();
[self setTitle:l10n_util::GetNSString(IDS_IOS_SEARCH_ENGINE_SETTING_TITLE)];
[self setCollectionViewAccessibilityIdentifier:@"Search Engine"];
// 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;
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadModel];
}
#pragma mark - ChromeTableViewController
- (void)loadModel {
[super loadModel];
CollectionViewModel* model = self.collectionViewModel;
TableViewModel* model = self.tableViewModel;
[self loadSearchEngines];
// Add prior search engines.
......@@ -95,11 +98,12 @@ const char kUmaSelectDefaultSearchEngine[] =
[model addSectionWithIdentifier:SectionIdentifierPriorSearchEngines];
for (TemplateURL* url : _priorSearchEngines) {
SettingsTextItem* engine = [[SettingsTextItem alloc]
TableViewDetailTextItem* engine = [[TableViewDetailTextItem alloc]
initWithType:ItemTypePriorSearchEnginesEngine];
[engine setText:base::SysUTF16ToNSString(url->short_name())];
engine.text = base::SysUTF16ToNSString(url->short_name());
engine.detailText = base::SysUTF16ToNSString(url->keyword());
if (url == _templateURLService->GetDefaultSearchProvider()) {
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark];
[engine setAccessoryType:UITableViewCellAccessoryCheckmark];
}
[model addItem:engine
toSectionWithIdentifier:SectionIdentifierPriorSearchEngines];
......@@ -110,20 +114,21 @@ const char kUmaSelectDefaultSearchEngine[] =
if (_customSearchEngines.size() > 0) {
[model addSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
SettingsTextItem* header = [[SettingsTextItem alloc]
initWithType:ItemTypeCustomSearchEnginesEngineHeader];
TableViewTextHeaderFooterItem* header =
[[TableViewTextHeaderFooterItem alloc]
initWithType:ItemTypeCustomSearchEnginesEngineHeader];
header.text = l10n_util::GetNSString(
IDS_IOS_SEARCH_ENGINE_SETTING_CUSTOM_SECTION_HEADER);
header.textColor = [[MDCPalette greyPalette] tint500];
[model setHeader:header
forSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
for (TemplateURL* url : _customSearchEngines) {
SettingsTextItem* engine = [[SettingsTextItem alloc]
TableViewDetailTextItem* engine = [[TableViewDetailTextItem alloc]
initWithType:ItemTypeCustomSearchEnginesEngine];
[engine setText:base::SysUTF16ToNSString(url->short_name())];
engine.text = base::SysUTF16ToNSString(url->short_name());
engine.detailText = base::SysUTF16ToNSString(url->keyword());
if (url == _templateURLService->GetDefaultSearchProvider()) {
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark];
[engine setAccessoryType:UITableViewCellAccessoryCheckmark];
}
[model addItem:engine
toSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
......@@ -131,25 +136,24 @@ const char kUmaSelectDefaultSearchEngine[] =
}
}
#pragma mark UICollectionViewDelegate
#pragma mark UITableViewDelegate
- (void)collectionView:(UICollectionView*)collectionView
didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
[super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
CollectionViewModel* model = self.collectionViewModel;
- (void)tableView:(UITableView*)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
TableViewModel* model = self.tableViewModel;
// Only handle taps on search engine items.
CollectionViewItem* selectedItem = [model itemAtIndexPath:indexPath];
TableViewItem* selectedItem = [model itemAtIndexPath:indexPath];
if (selectedItem.type != ItemTypePriorSearchEnginesEngine &&
selectedItem.type != ItemTypeCustomSearchEnginesEngine) {
return;
}
// Do nothing if the tapped engine was already the default.
SettingsTextItem* selectedTextItem =
base::mac::ObjCCastStrict<SettingsTextItem>(selectedItem);
if (selectedTextItem.accessoryType ==
MDCCollectionViewCellAccessoryCheckmark) {
TableViewDetailTextItem* selectedTextItem =
base::mac::ObjCCastStrict<TableViewDetailTextItem>(selectedItem);
if (selectedTextItem.accessoryType == UITableViewCellAccessoryCheckmark) {
return;
}
......@@ -158,42 +162,42 @@ const char kUmaSelectDefaultSearchEngine[] =
// Iterate through the engines and remove the checkmark from any that have it.
if ([model
hasSectionForSectionIdentifier:SectionIdentifierPriorSearchEngines]) {
for (CollectionViewItem* item in
for (TableViewItem* item in
[model itemsInSectionWithIdentifier:
SectionIdentifierPriorSearchEngines]) {
if (item.type != ItemTypePriorSearchEnginesEngine) {
continue;
}
SettingsTextItem* textItem =
base::mac::ObjCCastStrict<SettingsTextItem>(item);
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone;
TableViewDetailTextItem* textItem =
base::mac::ObjCCastStrict<TableViewDetailTextItem>(item);
if (textItem.accessoryType == UITableViewCellAccessoryCheckmark) {
textItem.accessoryType = UITableViewCellAccessoryNone;
[modifiedItems addObject:textItem];
}
}
}
if ([model hasSectionForSectionIdentifier:
SectionIdentifierCustomSearchEngines]) {
for (CollectionViewItem* item in
for (TableViewItem* item in
[model itemsInSectionWithIdentifier:
SectionIdentifierCustomSearchEngines]) {
if (item.type != ItemTypeCustomSearchEnginesEngine) {
continue;
}
SettingsTextItem* textItem =
base::mac::ObjCCastStrict<SettingsTextItem>(item);
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone;
TableViewDetailTextItem* textItem =
base::mac::ObjCCastStrict<TableViewDetailTextItem>(item);
if (textItem.accessoryType == UITableViewCellAccessoryCheckmark) {
textItem.accessoryType = UITableViewCellAccessoryNone;
[modifiedItems addObject:textItem];
}
}
}
// Show the checkmark on the new default engine.
SettingsTextItem* newDefaultEngine =
base::mac::ObjCCastStrict<SettingsTextItem>(
TableViewDetailTextItem* newDefaultEngine =
base::mac::ObjCCastStrict<TableViewDetailTextItem>(
[model itemAtIndexPath:indexPath]);
newDefaultEngine.accessoryType = MDCCollectionViewCellAccessoryCheckmark;
newDefaultEngine.accessoryType = UITableViewCellAccessoryCheckmark;
[modifiedItems addObject:newDefaultEngine];
// Set the new engine as the default.
......@@ -205,6 +209,7 @@ const char kUmaSelectDefaultSearchEngine[] =
[model indexInItemTypeForIndexPath:indexPath]];
[self reconfigureCellsForItems:modifiedItems];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark Internal methods
......
......@@ -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/search_engine_settings_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/search_engine_table_view_controller.h"
#include <memory>
......@@ -17,9 +17,8 @@
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h"
#import "ios/chrome/browser/ui/settings/cells/settings_text_item.h"
#import "ios/third_party/material_components_ios/src/components/CollectionCells/src/MaterialCollectionCells.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_detail_text_item.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h"
#include "ios/web/public/test/test_web_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
......@@ -36,11 +35,11 @@ namespace {
const char kUmaSelectDefaultSearchEngine[] =
"Search.iOS.SelectDefaultSearchEngine";
class SearchEngineSettingsCollectionViewControllerTest
: public CollectionViewControllerTest {
class SearchEngineTableViewControllerTest
: public ChromeTableViewControllerTest {
protected:
void SetUp() override {
CollectionViewControllerTest::SetUp();
ChromeTableViewControllerTest::SetUp();
TestChromeBrowserState::Builder test_cbs_builder;
test_cbs_builder.AddTestingFactory(
ios::TemplateURLServiceFactory::GetInstance(),
......@@ -52,18 +51,20 @@ class SearchEngineSettingsCollectionViewControllerTest
template_url_service_->Load();
}
CollectionViewController* InstantiateController() override {
return [[SearchEngineSettingsCollectionViewController alloc]
ChromeTableViewController* InstantiateController() override {
return [[SearchEngineTableViewController alloc]
initWithBrowserState:chrome_browser_state_.get()];
}
// Adds a prepopulated search engine to TemplateURLService.
TemplateURL* AddPriorSearchEngine(const std::string& short_name,
const std::string& keyword,
int prepopulate_id,
bool set_default) {
TemplateURLData data;
data.SetShortName(base::ASCIIToUTF16(short_name));
data.SetURL("https://chromium.test/index.php?q={searchTerms}");
data.SetKeyword(base::ASCIIToUTF16(keyword));
data.prepopulate_id = prepopulate_id;
TemplateURL* url =
template_url_service_->Add(std::make_unique<TemplateURL>(data));
......@@ -74,11 +75,13 @@ class SearchEngineSettingsCollectionViewControllerTest
// Adds a custom search engine to TemplateURLService.
TemplateURL* AddCustomSearchEngine(const std::string& short_name,
const std::string& keyword,
base::Time last_visited_time,
bool set_default) {
TemplateURLData data;
data.SetShortName(base::ASCIIToUTF16(short_name));
data.SetURL("https://chromium.test/index.php?q={searchTerms}");
data.SetKeyword(base::ASCIIToUTF16(keyword));
data.last_visited = last_visited_time;
TemplateURL* url =
template_url_service_->Add(std::make_unique<TemplateURL>(data));
......@@ -87,13 +90,14 @@ class SearchEngineSettingsCollectionViewControllerTest
return url;
}
// Checks if a text cell in the CollectionView has a check mark.
// Checks if a text cell in the TableView has a check mark.
void CheckTextCellChecked(bool expect_checked, int section, int item) {
SettingsTextItem* text_item = base::mac::ObjCCastStrict<SettingsTextItem>(
GetCollectionViewItem(section, item));
TableViewDetailTextItem* text_item =
base::mac::ObjCCastStrict<TableViewDetailTextItem>(
GetTableViewItem(section, item));
ASSERT_TRUE(text_item);
EXPECT_EQ(expect_checked ? MDCCollectionViewCellAccessoryCheckmark
: MDCCollectionViewCellAccessoryNone,
EXPECT_EQ(expect_checked ? UITableViewCellAccessoryCheckmark
: UITableViewCellAccessoryNone,
text_item.accessoryType);
}
......@@ -104,7 +108,7 @@ class SearchEngineSettingsCollectionViewControllerTest
};
// Tests that no items are shown if TemplateURLService is empty.
TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestNoUrl) {
TEST_F(SearchEngineTableViewControllerTest, TestNoUrl) {
CreateController();
CheckController();
EXPECT_EQ(0, NumberOfSections());
......@@ -112,110 +116,119 @@ TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestNoUrl) {
// Tests that items are displayed correctly when TemplateURLService is filled
// and a prepopulated search engine is selected as default.
TEST_F(SearchEngineSettingsCollectionViewControllerTest,
TEST_F(SearchEngineTableViewControllerTest,
TestUrlsLoadedWithPrepopulatedSearchEngineAsDefault) {
AddPriorSearchEngine("prepopulated-3", 3, false);
AddPriorSearchEngine("prepopulated-1", 1, false);
AddPriorSearchEngine("prepopulated-2", 2, true);
AddCustomSearchEngine(
"custom-4", base::Time::Now() - base::TimeDelta::FromDays(10), false);
AddCustomSearchEngine(
"custom-1", base::Time::Now() - base::TimeDelta::FromSeconds(10), false);
AddCustomSearchEngine(
"custom-3", base::Time::Now() - base::TimeDelta::FromHours(10), false);
AddCustomSearchEngine(
"custom-2", base::Time::Now() - base::TimeDelta::FromMinutes(10), false);
AddPriorSearchEngine("prepopulated-3", "p3.com", 3, false);
AddPriorSearchEngine("prepopulated-1", "p1.com", 1, false);
AddPriorSearchEngine("prepopulated-2", "p2.com", 2, true);
AddCustomSearchEngine("custom-4", "c4.com",
base::Time::Now() - base::TimeDelta::FromDays(10),
false);
AddCustomSearchEngine("custom-1", "c1.com",
base::Time::Now() - base::TimeDelta::FromSeconds(10),
false);
AddCustomSearchEngine("custom-3", "c3.com",
base::Time::Now() - base::TimeDelta::FromHours(10),
false);
AddCustomSearchEngine("custom-2", "c2.com",
base::Time::Now() - base::TimeDelta::FromMinutes(10),
false);
CreateController();
CheckController();
ASSERT_EQ(2, NumberOfSections());
ASSERT_EQ(3, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellTextAndDetailText(@"prepopulated-1", @"p1.com", 0, 0);
CheckTextCellChecked(false, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellTextAndDetailText(@"prepopulated-2", @"p2.com", 0, 1);
CheckTextCellChecked(true, 0, 1);
CheckTextCellTitle(@"prepopulated-3", 0, 2);
CheckTextCellTextAndDetailText(@"prepopulated-3", @"p3.com", 0, 2);
CheckTextCellChecked(false, 0, 2);
ASSERT_EQ(3, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellTextAndDetailText(@"custom-1", @"c1.com", 1, 0);
CheckTextCellChecked(false, 1, 0);
CheckTextCellTitle(@"custom-2", 1, 1);
CheckTextCellTextAndDetailText(@"custom-2", @"c2.com", 1, 1);
CheckTextCellChecked(false, 1, 1);
CheckTextCellTitle(@"custom-3", 1, 2);
CheckTextCellTextAndDetailText(@"custom-3", @"c3.com", 1, 2);
CheckTextCellChecked(false, 1, 2);
}
// Tests that items are displayed correctly when TemplateURLService is filled
// and a custom search engine is selected as default.
TEST_F(SearchEngineSettingsCollectionViewControllerTest,
TEST_F(SearchEngineTableViewControllerTest,
TestUrlsLoadedWithCustomSearchEngineAsDefault) {
AddPriorSearchEngine("prepopulated-3", 3, false);
AddPriorSearchEngine("prepopulated-1", 1, false);
AddPriorSearchEngine("prepopulated-2", 2, false);
AddCustomSearchEngine(
"custom-4", base::Time::Now() - base::TimeDelta::FromDays(10), false);
AddCustomSearchEngine(
"custom-1", base::Time::Now() - base::TimeDelta::FromSeconds(10), false);
AddCustomSearchEngine(
"custom-3", base::Time::Now() - base::TimeDelta::FromHours(10), false);
AddCustomSearchEngine(
"custom-2", base::Time::Now() - base::TimeDelta::FromMinutes(10), true);
AddPriorSearchEngine("prepopulated-3", "p3.com", 3, false);
AddPriorSearchEngine("prepopulated-1", "p1.com", 1, false);
AddPriorSearchEngine("prepopulated-2", "p2.com", 2, false);
AddCustomSearchEngine("custom-4", "c4.com",
base::Time::Now() - base::TimeDelta::FromDays(10),
false);
AddCustomSearchEngine("custom-1", "c1.com",
base::Time::Now() - base::TimeDelta::FromSeconds(10),
false);
AddCustomSearchEngine("custom-3", "c3.com",
base::Time::Now() - base::TimeDelta::FromHours(10),
false);
AddCustomSearchEngine("custom-2", "c2.com",
base::Time::Now() - base::TimeDelta::FromMinutes(10),
true);
CreateController();
CheckController();
ASSERT_EQ(2, NumberOfSections());
ASSERT_EQ(4, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellTextAndDetailText(@"prepopulated-1", @"p1.com", 0, 0);
CheckTextCellChecked(false, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellTextAndDetailText(@"prepopulated-2", @"p2.com", 0, 1);
CheckTextCellChecked(false, 0, 1);
CheckTextCellTitle(@"prepopulated-3", 0, 2);
CheckTextCellTextAndDetailText(@"prepopulated-3", @"p3.com", 0, 2);
CheckTextCellChecked(false, 0, 2);
CheckTextCellTitle(@"custom-2", 0, 3);
CheckTextCellTextAndDetailText(@"custom-2", @"c2.com", 0, 3);
CheckTextCellChecked(true, 0, 3);
ASSERT_EQ(2, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellTextAndDetailText(@"custom-1", @"c1.com", 1, 0);
CheckTextCellChecked(false, 1, 0);
CheckTextCellTitle(@"custom-3", 1, 1);
CheckTextCellTextAndDetailText(@"custom-3", @"c3.com", 1, 1);
CheckTextCellChecked(false, 1, 1);
}
// Tests that when TemplateURLService add or remove TemplateURLs, or update
// default search engine, the controller will update the displayed items.
TEST_F(SearchEngineSettingsCollectionViewControllerTest,
TestUrlModifiedByService) {
TemplateURL* url_p1 = AddPriorSearchEngine("prepopulated-1", 1, true);
TEST_F(SearchEngineTableViewControllerTest, TestUrlModifiedByService) {
TemplateURL* url_p1 =
AddPriorSearchEngine("prepopulated-1", "p1.com", 1, true);
CreateController();
CheckController();
ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(1, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellTextAndDetailText(@"prepopulated-1", @"p1.com", 0, 0);
CheckTextCellChecked(true, 0, 0);
TemplateURL* url_p2 = AddPriorSearchEngine("prepopulated-2", 2, false);
TemplateURL* url_p2 =
AddPriorSearchEngine("prepopulated-2", "p2.com", 2, false);
ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellTextAndDetailText(@"prepopulated-1", @"p1.com", 0, 0);
CheckTextCellChecked(true, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellTextAndDetailText(@"prepopulated-2", @"p2.com", 0, 1);
CheckTextCellChecked(false, 0, 1);
template_url_service_->SetUserSelectedDefaultSearchProvider(url_p2);
ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellTextAndDetailText(@"prepopulated-1", @"p1.com", 0, 0);
CheckTextCellChecked(false, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellTextAndDetailText(@"prepopulated-2", @"p2.com", 0, 1);
CheckTextCellChecked(true, 0, 1);
template_url_service_->SetUserSelectedDefaultSearchProvider(url_p1);
......@@ -223,13 +236,13 @@ TEST_F(SearchEngineSettingsCollectionViewControllerTest,
ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(1, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellTextAndDetailText(@"prepopulated-1", @"p1.com", 0, 0);
CheckTextCellChecked(true, 0, 0);
}
// Tests that when user change default search engine, all items can be displayed
// correctly and the change can be synced to the prefs.
TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestChangeProvider) {
TEST_F(SearchEngineTableViewControllerTest, TestChangeProvider) {
// This test also needs to test the UMA, so load some real prepopulated search
// engines to ensure the SearchEngineType is logged correctly. Don't use any
// literal symbol(e.g. "google" or "AOL") from
......@@ -256,32 +269,35 @@ TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestChangeProvider) {
// Also add some custom search engines.
TemplateURL* url_c1 =
AddCustomSearchEngine("custom-1", base::Time::Now(), false);
AddCustomSearchEngine(
"custom-2", base::Time::Now() - base::TimeDelta::FromSeconds(10), false);
AddCustomSearchEngine("custom-1", "c1.com", base::Time::Now(), false);
AddCustomSearchEngine("custom-2", "c2.com",
base::Time::Now() - base::TimeDelta::FromSeconds(10),
false);
CreateController();
CheckController();
// Choose url_p1 as default.
[controller() collectionView:[controller() collectionView]
didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:url_p1_index
inSection:0]];
[controller() tableView:[controller() tableView]
didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:url_p1_index
inSection:0]];
ASSERT_EQ(2, NumberOfSections());
// Check first list.
ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(base::SysUTF16ToNSString(url_p1->short_name()), 0,
url_p1_index);
CheckTextCellTextAndDetailText(base::SysUTF16ToNSString(url_p1->short_name()),
base::SysUTF16ToNSString(url_p1->keyword()), 0,
url_p1_index);
CheckTextCellChecked(true, 0, url_p1_index);
CheckTextCellTitle(base::SysUTF16ToNSString(url_p2->short_name()), 0,
url_p2_index);
CheckTextCellTextAndDetailText(base::SysUTF16ToNSString(url_p2->short_name()),
base::SysUTF16ToNSString(url_p2->keyword()), 0,
url_p2_index);
CheckTextCellChecked(false, 0, url_p2_index);
// Check second list.
ASSERT_EQ(2, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellTextAndDetailText(@"custom-1", @"c1.com", 1, 0);
CheckTextCellChecked(false, 1, 0);
CheckTextCellTitle(@"custom-2", 1, 1);
CheckTextCellTextAndDetailText(@"custom-2", @"c2.com", 1, 1);
CheckTextCellChecked(false, 1, 1);
// Check default search engine.
EXPECT_EQ(url_p1, template_url_service_->GetDefaultSearchProvider());
......@@ -291,24 +307,26 @@ TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestChangeProvider) {
url_p1->GetEngineType(template_url_service_->search_terms_data()), 1);
// Choose url_p2 as default.
[controller() collectionView:[controller() collectionView]
didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:url_p2_index
inSection:0]];
[controller() tableView:[controller() tableView]
didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:url_p2_index
inSection:0]];
ASSERT_EQ(2, NumberOfSections());
// Check first list.
ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(base::SysUTF16ToNSString(url_p1->short_name()), 0,
url_p1_index);
CheckTextCellTextAndDetailText(base::SysUTF16ToNSString(url_p1->short_name()),
base::SysUTF16ToNSString(url_p1->keyword()), 0,
url_p1_index);
CheckTextCellChecked(false, 0, url_p1_index);
CheckTextCellTitle(base::SysUTF16ToNSString(url_p2->short_name()), 0,
url_p2_index);
CheckTextCellTextAndDetailText(base::SysUTF16ToNSString(url_p2->short_name()),
base::SysUTF16ToNSString(url_p2->keyword()), 0,
url_p2_index);
CheckTextCellChecked(true, 0, url_p2_index);
// Check second list.
ASSERT_EQ(2, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellTextAndDetailText(@"custom-1", @"c1.com", 1, 0);
CheckTextCellChecked(false, 1, 0);
CheckTextCellTitle(@"custom-2", 1, 1);
CheckTextCellTextAndDetailText(@"custom-2", @"c2.com", 1, 1);
CheckTextCellChecked(false, 1, 1);
// Check default search engine.
EXPECT_EQ(url_p2, template_url_service_->GetDefaultSearchProvider());
......@@ -322,23 +340,25 @@ TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestChangeProvider) {
histogram_tester_.ExpectTotalCount(kUmaSelectDefaultSearchEngine, 2);
// Choose url_c1 as default.
[controller() collectionView:[controller() collectionView]
didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
[controller() tableView:[controller() tableView]
didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
ASSERT_EQ(2, NumberOfSections());
// Check first list.
ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(base::SysUTF16ToNSString(url_p1->short_name()), 0,
url_p1_index);
CheckTextCellTextAndDetailText(base::SysUTF16ToNSString(url_p1->short_name()),
base::SysUTF16ToNSString(url_p1->keyword()), 0,
url_p1_index);
CheckTextCellChecked(false, 0, url_p1_index);
CheckTextCellTitle(base::SysUTF16ToNSString(url_p2->short_name()), 0,
url_p2_index);
CheckTextCellTextAndDetailText(base::SysUTF16ToNSString(url_p2->short_name()),
base::SysUTF16ToNSString(url_p2->keyword()), 0,
url_p2_index);
CheckTextCellChecked(false, 0, url_p2_index);
// Check second list.
ASSERT_EQ(2, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellTextAndDetailText(@"custom-1", @"c1.com", 1, 0);
CheckTextCellChecked(true, 1, 0);
CheckTextCellTitle(@"custom-2", 1, 1);
CheckTextCellTextAndDetailText(@"custom-2", @"c2.com", 1, 1);
CheckTextCellChecked(false, 1, 1);
// Check default search engine.
EXPECT_EQ(url_c1, template_url_service_->GetDefaultSearchProvider());
......
......@@ -56,7 +56,7 @@
#import "ios/chrome/browser/ui/settings/material_cell_catalog_view_controller.h"
#import "ios/chrome/browser/ui/settings/privacy_table_view_controller.h"
#import "ios/chrome/browser/ui/settings/save_passwords_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/search_engine_settings_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/search_engine_table_view_controller.h"
#import "ios/chrome/browser/ui/settings/sync_utils/sync_util.h"
#import "ios/chrome/browser/ui/settings/table_cell_catalog_view_controller.h"
#import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
......@@ -855,7 +855,7 @@ void IdentityObserverBridge::OnPrimaryAccountCleared(
[self showSyncGoogleService];
break;
case ItemTypeSearchEngine:
controller = [[SearchEngineSettingsCollectionViewController alloc]
controller = [[SearchEngineTableViewController alloc]
initWithBrowserState:_browserState];
break;
case ItemTypeSavedPasswords:
......
......@@ -29,6 +29,10 @@
// using shouldHideDoneButton to know if it should display the edit button.
- (void)updateEditButton;
// Reloads the table view model with |loadModel| and then reloads the
// table view data.
- (void)reloadData;
@end
// Subclasses of SettingsRootTableViewController should implement the
......
......@@ -47,6 +47,11 @@
}
}
- (void)reloadData {
[self loadModel];
[self.tableView reloadData];
}
#pragma mark - Property
- (UIBarButtonItem*)deleteButton {
......
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