Commit 2b549d86 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Add custom search engines list in Search Engine Settings.

This CL changes how search engines are displayed in Search Engine Settings.

Previous(1 list):
  1. search engines that are prepopulated or created by policy.

Current(2 lists):
  1. Search engines that are prepopulated or created by policy, and possibly
     one custom search engine at the end if it's selected as default search
     engine;
  2. All remaining search engines.

Unittests are also updated.

Bug: 433824
Change-Id: Ib70a18f0544101760c331bb349ee9818956ed231
Reviewed-on: https://chromium-review.googlesource.com/c/1323509Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606776}
parent 6105631e
...@@ -1313,6 +1313,9 @@ Handoff must also be enabled in the General section of Settings, and your device ...@@ -1313,6 +1313,9 @@ Handoff must also be enabled in the General section of Settings, and your device
<message name="IDS_IOS_SEARCH_ENGINE_SETTING_TITLE" desc="The title for Search Engine selection setting [iOS only]"> <message name="IDS_IOS_SEARCH_ENGINE_SETTING_TITLE" desc="The title for Search Engine selection setting [iOS only]">
Search Engine Search Engine
</message> </message>
<message name="IDS_IOS_SEARCH_ENGINE_SETTING_CUSTOM_SECTION_HEADER" desc="The header for custom search engines section in Search Engine selection setting [iOS only]">
Recently Visited
</message>
<message name="IDS_IOS_SETTINGS_SITE_COPY_BUTTON" desc="Button that the user can press to copy the site to clipboard. [Length: 12em]"> <message name="IDS_IOS_SETTINGS_SITE_COPY_BUTTON" desc="Button that the user can press to copy the site to clipboard. [Length: 12em]">
Copy Copy
</message> </message>
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#import "ios/chrome/browser/ui/settings/cells/settings_text_item.h" #import "ios/chrome/browser/ui/settings/cells/settings_text_item.h"
#include "ios/chrome/grit/ios_strings.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/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" #include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -26,13 +27,19 @@ ...@@ -26,13 +27,19 @@
namespace { namespace {
typedef NS_ENUM(NSInteger, SectionIdentifier) { typedef NS_ENUM(NSInteger, SectionIdentifier) {
SectionIdentifierSearchEngines = kSectionIdentifierEnumZero, SectionIdentifierPriorSearchEngines = kSectionIdentifierEnumZero,
SectionIdentifierCustomSearchEngines,
}; };
typedef NS_ENUM(NSInteger, ItemType) { typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeSearchEnginesEngine = kItemTypeEnumZero, ItemTypePriorSearchEnginesEngine = kItemTypeEnumZero,
ItemTypeCustomSearchEnginesEngineHeader,
ItemTypeCustomSearchEnginesEngine,
}; };
constexpr base::TimeDelta kMaxVisitAge = base::TimeDelta::FromDays(2);
const size_t kMaxcustomSearchEngines = 3;
} // namespace } // namespace
@interface SearchEngineSettingsCollectionViewController ()< @interface SearchEngineSettingsCollectionViewController ()<
...@@ -40,10 +47,17 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -40,10 +47,17 @@ typedef NS_ENUM(NSInteger, ItemType) {
@end @end
@implementation SearchEngineSettingsCollectionViewController { @implementation SearchEngineSettingsCollectionViewController {
TemplateURLService* templateURLService_; // weak TemplateURLService* _templateURLService; // weak
std::unique_ptr<SearchEngineObserverBridge> observer_; std::unique_ptr<SearchEngineObserverBridge> _observer;
// Prevent unnecessary notifications when we write to the setting. // Prevent unnecessary notifications when we write to the setting.
BOOL updatingBackend_; BOOL _updatingBackend;
// The first list in the page which contains prepopulted search engines and
// search engines that are created by policy, and possibly one custom search
// engine if it's selected as default search engine.
std::vector<TemplateURL*> _priorSearchEngines;
// The second list in the page which contains all remaining custom search
// engines.
std::vector<TemplateURL*> _customSearchEngines;
} }
#pragma mark Initialization #pragma mark Initialization
...@@ -54,11 +68,11 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -54,11 +68,11 @@ typedef NS_ENUM(NSInteger, ItemType) {
self = self =
[super initWithLayout:layout style:CollectionViewControllerStyleAppBar]; [super initWithLayout:layout style:CollectionViewControllerStyleAppBar];
if (self) { if (self) {
templateURLService_ = _templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(browserState); ios::TemplateURLServiceFactory::GetForBrowserState(browserState);
observer_ = _observer =
std::make_unique<SearchEngineObserverBridge>(self, templateURLService_); std::make_unique<SearchEngineObserverBridge>(self, _templateURLService);
templateURLService_->Load(); _templateURLService->Load();
[self setTitle:l10n_util::GetNSString(IDS_IOS_SEARCH_ENGINE_SETTING_TITLE)]; [self setTitle:l10n_util::GetNSString(IDS_IOS_SEARCH_ENGINE_SETTING_TITLE)];
[self setCollectionViewAccessibilityIdentifier:@"Search Engine"]; [self setCollectionViewAccessibilityIdentifier:@"Search Engine"];
// TODO(crbug.com/764578): -loadModel should not be called from // TODO(crbug.com/764578): -loadModel should not be called from
...@@ -71,26 +85,46 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -71,26 +85,46 @@ typedef NS_ENUM(NSInteger, ItemType) {
- (void)loadModel { - (void)loadModel {
[super loadModel]; [super loadModel];
CollectionViewModel* model = self.collectionViewModel; CollectionViewModel* model = self.collectionViewModel;
NSArray* values = [self allValues]; [self loadSearchEngines];
// Do not add any sections if there are no search engines. // Add prior search engines.
if (![values count]) { if (_priorSearchEngines.size() > 0) {
return; [model addSectionWithIdentifier:SectionIdentifierPriorSearchEngines];
for (TemplateURL* url : _priorSearchEngines) {
SettingsTextItem* engine = [[SettingsTextItem alloc]
initWithType:ItemTypePriorSearchEnginesEngine];
[engine setText:base::SysUTF16ToNSString(url->short_name())];
if (url == _templateURLService->GetDefaultSearchProvider()) {
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark];
}
[model addItem:engine
toSectionWithIdentifier:SectionIdentifierPriorSearchEngines];
}
} }
[model addSectionWithIdentifier:SectionIdentifierSearchEngines]; // Add custom search engines.
for (NSUInteger i = 0; i < values.count; i++) { if (_customSearchEngines.size() > 0) {
NSString* value = values[i]; [model addSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
BOOL checked = [value isEqualToString:[self currentValue]];
SettingsTextItem* engine = SettingsTextItem* header = [[SettingsTextItem alloc]
[[SettingsTextItem alloc] initWithType:ItemTypeSearchEnginesEngine]; initWithType:ItemTypeCustomSearchEnginesEngineHeader];
[engine setText:value]; header.text = l10n_util::GetNSString(
if (checked) { IDS_IOS_SEARCH_ENGINE_SETTING_CUSTOM_SECTION_HEADER);
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; header.textColor = [[MDCPalette greyPalette] tint500];
[model setHeader:header
forSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
for (TemplateURL* url : _customSearchEngines) {
SettingsTextItem* engine = [[SettingsTextItem alloc]
initWithType:ItemTypeCustomSearchEnginesEngine];
[engine setText:base::SysUTF16ToNSString(url->short_name())];
if (url == _templateURLService->GetDefaultSearchProvider()) {
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark];
}
[model addItem:engine
toSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
} }
[model addItem:engine
toSectionWithIdentifier:SectionIdentifierSearchEngines];
} }
} }
...@@ -103,7 +137,8 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -103,7 +137,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
// Only handle taps on search engine items. // Only handle taps on search engine items.
CollectionViewItem* selectedItem = [model itemAtIndexPath:indexPath]; CollectionViewItem* selectedItem = [model itemAtIndexPath:indexPath];
if (selectedItem.type != ItemTypeSearchEnginesEngine) { if (selectedItem.type != ItemTypePriorSearchEnginesEngine &&
selectedItem.type != ItemTypeCustomSearchEnginesEngine) {
return; return;
} }
...@@ -115,19 +150,39 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -115,19 +150,39 @@ typedef NS_ENUM(NSInteger, ItemType) {
return; return;
} }
// Iterate through the engines and remove the checkmark from any that have it.
NSMutableArray* modifiedItems = [NSMutableArray array]; NSMutableArray* modifiedItems = [NSMutableArray array];
for (CollectionViewItem* item in
[model itemsInSectionWithIdentifier:SectionIdentifierSearchEngines]) {
if (item.type != ItemTypeSearchEnginesEngine) {
continue;
}
SettingsTextItem* textItem = // Iterate through the engines and remove the checkmark from any that have it.
base::mac::ObjCCastStrict<SettingsTextItem>(item); if ([model
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { hasSectionForSectionIdentifier:SectionIdentifierPriorSearchEngines]) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone; for (CollectionViewItem* item in
[modifiedItems addObject:textItem]; [model itemsInSectionWithIdentifier:
SectionIdentifierPriorSearchEngines]) {
if (item.type != ItemTypePriorSearchEnginesEngine) {
continue;
}
SettingsTextItem* textItem =
base::mac::ObjCCastStrict<SettingsTextItem>(item);
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone;
[modifiedItems addObject:textItem];
}
}
}
if ([model hasSectionForSectionIdentifier:
SectionIdentifierCustomSearchEngines]) {
for (CollectionViewItem* item in
[model itemsInSectionWithIdentifier:
SectionIdentifierCustomSearchEngines]) {
if (item.type != ItemTypeCustomSearchEnginesEngine) {
continue;
}
SettingsTextItem* textItem =
base::mac::ObjCCastStrict<SettingsTextItem>(item);
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone;
[modifiedItems addObject:textItem];
}
} }
} }
...@@ -139,39 +194,89 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -139,39 +194,89 @@ typedef NS_ENUM(NSInteger, ItemType) {
[modifiedItems addObject:newDefaultEngine]; [modifiedItems addObject:newDefaultEngine];
// Set the new engine as the default. // Set the new engine as the default.
[self setValueFromIndex:[model indexInItemTypeForIndexPath:indexPath]]; if (selectedItem.type == ItemTypePriorSearchEnginesEngine)
[self setDefaultToPriorSearchEngineAtIndex:
[model indexInItemTypeForIndexPath:indexPath]];
else
[self setDefaultToCustomSearchEngineAtIndex:
[model indexInItemTypeForIndexPath:indexPath]];
[self reconfigureCellsForItems:modifiedItems]; [self reconfigureCellsForItems:modifiedItems];
} }
#pragma mark Internal methods #pragma mark Internal methods
- (NSArray*)allValues { // Loads all TemplateURLs from TemplateURLService and classifies them into
std::vector<TemplateURL*> urls = templateURLService_->GetTemplateURLs(); // |_priorSearchEngines| and |_customSearchEngines|. If a TemplateURL is
NSMutableArray* items = [NSMutableArray arrayWithCapacity:urls.size()]; // prepopulated, created by policy or the default search engine, it will get
for (std::vector<TemplateURL*>::const_iterator iter = urls.begin(); // into the first list, otherwise the second list.
iter != urls.end(); ++iter) { - (void)loadSearchEngines {
[items addObject:base::SysUTF16ToNSString((*iter)->short_name())]; std::vector<TemplateURL*> urls = _templateURLService->GetTemplateURLs();
_priorSearchEngines.clear();
_priorSearchEngines.reserve(urls.size());
_customSearchEngines.clear();
_customSearchEngines.reserve(urls.size());
// Classify TemplateURLs.
for (TemplateURL* url : urls) {
if (_templateURLService->IsPrepopulatedOrCreatedByPolicy(url) ||
url == _templateURLService->GetDefaultSearchProvider())
_priorSearchEngines.push_back(url);
else
_customSearchEngines.push_back(url);
} }
return items; // Sort |fixedCutomeSearchEngines_| by TemplateURL's prepopulate_id. If
// prepopulated_id == 0, it's a custom search engine and should be put in the
// end of the list.
std::sort(_priorSearchEngines.begin(), _priorSearchEngines.end(),
[](const TemplateURL* lhs, const TemplateURL* rhs) {
if (lhs->prepopulate_id() == 0)
return false;
if (rhs->prepopulate_id() == 0)
return true;
return lhs->prepopulate_id() < rhs->prepopulate_id();
});
// Partially sort |_customSearchEngines| by TemplateURL's last_visited time.
auto begin = _customSearchEngines.begin();
auto end = _customSearchEngines.end();
auto pivot =
begin + std::min(kMaxcustomSearchEngines, _customSearchEngines.size());
std::partial_sort(begin, pivot, end,
[](const TemplateURL* lhs, const TemplateURL* rhs) {
return lhs->last_visited() > rhs->last_visited();
});
// Keep the search engines visited within |kMaxVisitAge| and erase others.
const base::Time cutoff = base::Time::Now() - kMaxVisitAge;
auto cutBegin = std::find_if(begin, pivot, [cutoff](const TemplateURL* url) {
return url->last_visited() < cutoff;
});
_customSearchEngines.erase(cutBegin, end);
} }
- (NSString*)currentValue { // Sets the search engine at |index| in prior section as default search engine.
return base::SysUTF16ToNSString( - (void)setDefaultToPriorSearchEngineAtIndex:(NSUInteger)index {
templateURLService_->GetDefaultSearchProvider()->short_name()); DCHECK_GE(index, 0U);
DCHECK_LT(index, _priorSearchEngines.size());
_updatingBackend = YES;
_templateURLService->SetUserSelectedDefaultSearchProvider(
_priorSearchEngines[index]);
_updatingBackend = NO;
} }
- (void)setValueFromIndex:(NSUInteger)index { // Sets the search engine at |index| in custom section as default search engine.
std::vector<TemplateURL*> urls = templateURLService_->GetTemplateURLs(); - (void)setDefaultToCustomSearchEngineAtIndex:(NSUInteger)index {
DCHECK_GE(index, 0U); DCHECK_GE(index, 0U);
DCHECK_LT(index, urls.size()); DCHECK_LT(index, _customSearchEngines.size());
updatingBackend_ = YES; _updatingBackend = YES;
templateURLService_->SetUserSelectedDefaultSearchProvider(urls[index]); _templateURLService->SetUserSelectedDefaultSearchProvider(
updatingBackend_ = NO; _customSearchEngines[index]);
_updatingBackend = NO;
} }
- (void)searchEngineChanged { - (void)searchEngineChanged {
if (!updatingBackend_) if (!_updatingBackend)
[self reloadData]; [self reloadData];
} }
......
...@@ -48,38 +48,44 @@ class SearchEngineSettingsCollectionViewControllerTest ...@@ -48,38 +48,44 @@ class SearchEngineSettingsCollectionViewControllerTest
initWithBrowserState:chrome_browser_state_.get()]; initWithBrowserState:chrome_browser_state_.get()];
} }
std::unique_ptr<TemplateURL> NewTemplateUrl(const std::string& shortName) { // Adds a prepopulated search engine to TemplateURLService.
TemplateURL* AddPriorSearchEngine(const std::string& short_name,
int prepopulate_id,
bool set_default) {
TemplateURLData data; TemplateURLData data;
data.SetShortName(base::ASCIIToUTF16(shortName)); data.SetShortName(base::ASCIIToUTF16(short_name));
return std::unique_ptr<TemplateURL>(new TemplateURL(data)); data.SetURL("https://chromium.test/index.php?q={searchTerms}");
data.prepopulate_id = prepopulate_id;
TemplateURL* url =
template_url_service_->Add(std::make_unique<TemplateURL>(data));
if (set_default)
template_url_service_->SetUserSelectedDefaultSearchProvider(url);
return url;
} }
void FillTemplateUrlService() { // Adds a custom search engine to TemplateURLService.
TemplateURL* defaultProvider = TemplateURL* AddCustomSearchEngine(const std::string& short_name,
template_url_service_->Add(NewTemplateUrl("first_url")); base::Time last_visited_time,
template_url_service_->SetUserSelectedDefaultSearchProvider( bool set_default) {
defaultProvider); TemplateURLData data;
template_url_service_->Add(NewTemplateUrl("second_url")); data.SetShortName(base::ASCIIToUTF16(short_name));
template_url_service_->Add(NewTemplateUrl("third_url")); data.SetURL("https://chromium.test/index.php?q={searchTerms}");
data.last_visited = last_visited_time;
TemplateURL* url =
template_url_service_->Add(std::make_unique<TemplateURL>(data));
if (set_default)
template_url_service_->SetUserSelectedDefaultSearchProvider(url);
return url;
} }
void CheckModelMatchesTemplateURLs() { // Checks if a text cell in the CollectionView has a check mark.
TemplateURLService::TemplateURLVector urls = void CheckTextCellChecked(bool expect_checked, int section, int item) {
template_url_service_->GetTemplateURLs(); SettingsTextItem* text_item = base::mac::ObjCCastStrict<SettingsTextItem>(
EXPECT_EQ(1, NumberOfSections()); GetCollectionViewItem(section, item));
ASSERT_EQ(urls.size(), ASSERT_TRUE(text_item);
static_cast<unsigned int>(NumberOfItemsInSection(0))); EXPECT_EQ(expect_checked ? MDCCollectionViewCellAccessoryCheckmark
for (unsigned int i = 0; i < urls.size(); ++i) { : MDCCollectionViewCellAccessoryNone,
BOOL isDefault = text_item.accessoryType);
template_url_service_->GetDefaultSearchProvider() == urls[i];
CheckTextCellTitle(base::SysUTF16ToNSString(urls[i]->short_name()), 0, i);
SettingsTextItem* textItem = base::mac::ObjCCastStrict<SettingsTextItem>(
GetCollectionViewItem(0, i));
EXPECT_EQ(isDefault ? MDCCollectionViewCellAccessoryCheckmark
: MDCCollectionViewCellAccessoryNone,
textItem.accessoryType);
}
} }
web::TestWebThreadBundle thread_bundle_; web::TestWebThreadBundle thread_bundle_;
...@@ -87,60 +93,170 @@ class SearchEngineSettingsCollectionViewControllerTest ...@@ -87,60 +93,170 @@ class SearchEngineSettingsCollectionViewControllerTest
TemplateURLService* template_url_service_; // weak TemplateURLService* template_url_service_; // weak
}; };
// Tests that no items are shown if TemplateURLService is empty.
TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestNoUrl) { TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestNoUrl) {
CreateController(); CreateController();
CheckController(); CheckController();
EXPECT_EQ(0, NumberOfSections()); EXPECT_EQ(0, NumberOfSections());
} }
TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestWithUrlsLoaded) { // Tests that items are displayed correctly when TemplateURLService is filled
FillTemplateUrlService(); // and a prepopulated search engine is selected as default.
TEST_F(SearchEngineSettingsCollectionViewControllerTest,
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);
CreateController();
CheckController();
ASSERT_EQ(2, NumberOfSections());
ASSERT_EQ(3, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellChecked(false, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellChecked(true, 0, 1);
CheckTextCellTitle(@"prepopulated-3", 0, 2);
CheckTextCellChecked(false, 0, 2);
ASSERT_EQ(3, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellChecked(false, 1, 0);
CheckTextCellTitle(@"custom-2", 1, 1);
CheckTextCellChecked(false, 1, 1);
CheckTextCellTitle(@"custom-3", 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,
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);
CreateController(); CreateController();
CheckModelMatchesTemplateURLs(); CheckController();
ASSERT_EQ(2, NumberOfSections());
ASSERT_EQ(4, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellChecked(false, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellChecked(false, 0, 1);
CheckTextCellTitle(@"prepopulated-3", 0, 2);
CheckTextCellChecked(false, 0, 2);
CheckTextCellTitle(@"custom-2", 0, 3);
CheckTextCellChecked(true, 0, 3);
ASSERT_EQ(2, NumberOfItemsInSection(1));
CheckTextCellTitle(@"custom-1", 1, 0);
CheckTextCellChecked(false, 1, 0);
CheckTextCellTitle(@"custom-3", 1, 1);
CheckTextCellChecked(false, 1, 1);
} }
TEST_F(SearchEngineSettingsCollectionViewControllerTest, TestWithAddedUrl) { // Tests that when TemplateURLService add or remove TemplateURLs, or update
FillTemplateUrlService(); // default search engine, the controller will update the displayed items.
TEST_F(SearchEngineSettingsCollectionViewControllerTest,
TestUrlModifiedByService) {
TemplateURL* url_1 = AddPriorSearchEngine("prepopulated-1", 1, true);
CreateController(); CreateController();
CheckController();
ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(1, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellChecked(true, 0, 0);
TemplateURL* url_2 = AddPriorSearchEngine("prepopulated-2", 2, false);
TemplateURL* newUrl = template_url_service_->Add(NewTemplateUrl("new_url")); ASSERT_EQ(1, NumberOfSections());
CheckModelMatchesTemplateURLs(); ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellChecked(true, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellChecked(false, 0, 1);
template_url_service_->SetUserSelectedDefaultSearchProvider(newUrl); template_url_service_->SetUserSelectedDefaultSearchProvider(url_2);
CheckModelMatchesTemplateURLs();
DCHECK(newUrl != template_url_service_->GetTemplateURLs()[0]); ASSERT_EQ(1, NumberOfSections());
template_url_service_->SetUserSelectedDefaultSearchProvider( ASSERT_EQ(2, NumberOfItemsInSection(0));
template_url_service_->GetTemplateURLs()[0]); CheckTextCellTitle(@"prepopulated-1", 0, 0);
template_url_service_->Remove(newUrl); CheckTextCellChecked(false, 0, 0);
CheckModelMatchesTemplateURLs(); CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellChecked(true, 0, 1);
template_url_service_->SetUserSelectedDefaultSearchProvider(url_1);
template_url_service_->Remove(url_2);
ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(1, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 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(SearchEngineSettingsCollectionViewControllerTest, TestChangeProvider) {
FillTemplateUrlService(); TemplateURL* url_1 = AddPriorSearchEngine("prepopulated-1", 1, false);
TemplateURL* url_2 = AddPriorSearchEngine("prepopulated-2", 2, true);
CreateController(); CreateController();
CheckController();
[controller() collectionView:[controller() collectionView] [controller() collectionView:[controller() collectionView]
didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
EXPECT_EQ(template_url_service_->GetTemplateURLs()[0],
template_url_service_->GetDefaultSearchProvider()); ASSERT_EQ(1, NumberOfSections());
CheckModelMatchesTemplateURLs(); ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellChecked(true, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellChecked(false, 0, 1);
EXPECT_EQ(url_1, template_url_service_->GetDefaultSearchProvider());
[controller() collectionView:[controller() collectionView] [controller() collectionView:[controller() collectionView]
didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]; didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
TemplateURL* url = template_url_service_->GetTemplateURLs()[1];
EXPECT_EQ(url, template_url_service_->GetDefaultSearchProvider()); ASSERT_EQ(1, NumberOfSections());
ASSERT_EQ(2, NumberOfItemsInSection(0));
CheckTextCellTitle(@"prepopulated-1", 0, 0);
CheckTextCellChecked(false, 0, 0);
CheckTextCellTitle(@"prepopulated-2", 0, 1);
CheckTextCellChecked(true, 0, 1);
EXPECT_EQ(url_2, template_url_service_->GetDefaultSearchProvider());
// Check that the selection was written back to the prefs. // Check that the selection was written back to the prefs.
const base::DictionaryValue* searchProviderDict = const base::DictionaryValue* searchProviderDict =
chrome_browser_state_->GetTestingPrefService()->GetDictionary( chrome_browser_state_->GetTestingPrefService()->GetDictionary(
DefaultSearchManager::kDefaultSearchProviderDataPrefName); DefaultSearchManager::kDefaultSearchProviderDataPrefName);
EXPECT_TRUE(searchProviderDict); ASSERT_TRUE(searchProviderDict);
base::string16 shortName; base::string16 short_name;
EXPECT_TRUE(searchProviderDict->GetString(DefaultSearchManager::kShortName, EXPECT_TRUE(searchProviderDict->GetString(DefaultSearchManager::kShortName,
&shortName)); &short_name));
EXPECT_EQ(url->short_name(), shortName); EXPECT_EQ(url_2->short_name(), short_name);
CheckModelMatchesTemplateURLs();
} }
} // namespace } // namespace
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