1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/search_engines/template_url_service_test_util.h"
#include <memory>
#include "base/command_line.h"
#include "components/country_codes/country_codes.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/os_crypt/async/browser/test_utils.h"
#include "components/regional_capabilities/regional_capabilities_prefs.h"
#include "components/regional_capabilities/regional_capabilities_service.h"
#include "components/regional_capabilities/regional_capabilities_switches.h"
#include "components/regional_capabilities/regional_capabilities_test_utils.h"
#include "components/regional_capabilities/regional_capabilities_utils.h"
#include "components/search_engines/keyword_table.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"
#include "components/search_engines/search_engines_test_util.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_prepopulate_data_resolver.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_service_client.h"
#include "components/webdata/common/web_database_service.h"
void RegisterPrefsForTemplateURLService(
user_prefs::PrefRegistrySyncable* registry) {
TemplateURLService::RegisterProfilePrefs(registry);
TemplateURLPrepopulateData::RegisterProfilePrefs(registry);
regional_capabilities::prefs::RegisterProfilePrefs(registry);
DefaultSearchManager::RegisterProfilePrefs(registry);
}
// -- TemplateURLServiceLoadWaiter --------------------------------------------
TemplateURLServiceLoadWaiter::TemplateURLServiceLoadWaiter() = default;
TemplateURLServiceLoadWaiter::~TemplateURLServiceLoadWaiter() = default;
void TemplateURLServiceLoadWaiter::WaitForLoadComplete(
TemplateURLService& template_url_service) {
ASSERT_TRUE(!scoped_template_url_service_observation_.IsObserving());
if (template_url_service.loaded()) {
return;
}
scoped_template_url_service_observation_.Observe(&template_url_service);
run_loop_.Run();
}
void TemplateURLServiceLoadWaiter::OnTemplateURLServiceChanged() {
UpdateStatus();
}
void TemplateURLServiceLoadWaiter::OnTemplateURLServiceShuttingDown() {
// Added for correctness. Not needed for the classic use cases where we wait
// for the service to finish loading after creation. But in case a test runs
// into some issue or a service is destroyed, this is intended to not keep
// blocking and risk hiding the underlying issue.
UpdateStatus(/*force_quit=*/true);
}
void TemplateURLServiceLoadWaiter::UpdateStatus(bool force_quit) {
ASSERT_TRUE(scoped_template_url_service_observation_.IsObserving());
if (force_quit ||
scoped_template_url_service_observation_.GetSource()->loaded()) {
scoped_template_url_service_observation_.Reset();
run_loop_.Quit();
}
}
// -- TemplateURLServiceUnitTestBase ------------------------------------------
TemplateURLServiceUnitTestBase::TemplateURLServiceUnitTestBase() = default;
TemplateURLServiceUnitTestBase::~TemplateURLServiceUnitTestBase() = default;
void TemplateURLServiceUnitTestBase::SetUp() {
RegisterPrefsForTemplateURLService(pref_service_.registry());
local_state_.registry()->RegisterBooleanPref(
metrics::prefs::kMetricsReportingEnabled, true);
// Bypass the country checks.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kSearchEngineChoiceCountry,
switches::kDefaultListCountryOverride);
regional_capabilities_service_ =
regional_capabilities::CreateServiceWithFakeClient(pref_service_);
prepopulate_data_resolver_ =
std::make_unique<TemplateURLPrepopulateData::Resolver>(
pref_service_, *regional_capabilities_service_.get());
search_engine_choice_service_ =
std::make_unique<search_engines::SearchEngineChoiceService>(
std::make_unique<FakeSearchEngineChoiceServiceClient>(),
pref_service_, &local_state_, *regional_capabilities_service_,
*prepopulate_data_resolver_);
template_url_service_ = CreateService();
}
std::unique_ptr<TemplateURLService>
TemplateURLServiceUnitTestBase::CreateService() {
return std::make_unique<TemplateURLService>(
pref_service_, *search_engine_choice_service_,
*prepopulate_data_resolver_.get(), std::make_unique<SearchTermsData>(),
nullptr /* KeywordWebDataService */,
nullptr /* TemplateURLServiceClient */, base::RepeatingClosure());
}
// -- LoadedTemplateURLServiceUnitTestBase ------------------------------------
LoadedTemplateURLServiceUnitTestBase::LoadedTemplateURLServiceUnitTestBase()
: os_crypt_(os_crypt_async::GetTestOSCryptAsyncForTesting()) {}
LoadedTemplateURLServiceUnitTestBase::~LoadedTemplateURLServiceUnitTestBase() =
default;
std::unique_ptr<TemplateURLService>
LoadedTemplateURLServiceUnitTestBase::CreateService() {
CHECK(!database_);
CHECK(!keyword_data_service_);
auto task_runner = task_environment.GetMainThreadTaskRunner();
database_ = base::MakeRefCounted<WebDatabaseService>(
base::FilePath(WebDatabase::kInMemoryPath),
/*ui_task_runner=*/task_runner,
/*db_task_runner=*/task_runner);
database_->AddTable(std::make_unique<KeywordTable>());
database_->LoadDatabase(os_crypt_.get());
keyword_data_service_ =
base::MakeRefCounted<KeywordWebDataService>(database_, task_runner);
keyword_data_service_->Init(base::DoNothing());
auto template_url_service = std::make_unique<TemplateURLService>(
pref_service(), search_engine_choice_service(),
prepopulate_data_resolver(), std::make_unique<SearchTermsData>(),
keyword_data_service_, nullptr /* TemplateURLServiceClient */,
base::RepeatingClosure());
return template_url_service;
}
void LoadedTemplateURLServiceUnitTestBase::SetUp() {
TemplateURLServiceUnitTestBase::SetUp();
ASSERT_FALSE(template_url_service().loaded());
template_url_service().Load();
template_url_service_load_waiter_.WaitForLoadComplete(template_url_service());
ASSERT_EQ(GetKeywordTemplateURLs().size(),
regional_capabilities::GetDefaultPrepopulatedEngines().size());
}
void LoadedTemplateURLServiceUnitTestBase::TearDown() {
template_url_service().Shutdown();
keyword_data_service_->ShutdownOnUISequence();
database_->ShutdownDatabase();
}
// Same as `TemplateURLService::GetTemplateURLs()`, but removes the starter
// pack entries.
TemplateURLService::TemplateURLVector
LoadedTemplateURLServiceUnitTestBase::GetKeywordTemplateURLs() {
TemplateURLService::TemplateURLVector turls =
template_url_service().GetTemplateURLs();
auto to_remove = std::ranges::remove_if(turls, [](const TemplateURL* turl) {
return turl->starter_pack_id() != 0;
});
turls.erase(to_remove.begin(), to_remove.end());
return turls;
}
// Returns `TemplateURLService::GetTemplateURLs()`, filtering to only entries
// that have `keyword` as their keyword. In general it should return a single
// elements, but in some cases there may be more.
TemplateURLService::TemplateURLVector
LoadedTemplateURLServiceUnitTestBase::GetTemplateURLsMatchingKeyword(
std::u16string keyword) {
TemplateURLService::TemplateURLVector matching_turls;
for (const auto& turl : template_url_service().GetTemplateURLs()) {
if (turl->keyword() == keyword) {
matching_turls.push_back(turl);
}
}
return matching_turls;
}
|