File: template_url_service_test_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (362 lines) | stat: -rw-r--r-- 13,719 bytes parent folder | download | duplicates (5)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/search_engines/template_url_service_test_util.h"

#include <memory>
#include <utility>

#include "base/check_deref.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/regional_capabilities/regional_capabilities_service_factory.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_service_factory.h"
#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
#include "chrome/browser/search_engines/template_url_prepopulate_data_resolver_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "chrome/browser/webdata_services/web_data_service_factory.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/regional_capabilities/regional_capabilities_service.h"
#include "components/regional_capabilities/regional_capabilities_test_utils.h"
#include "components/search_engines/keyword_table.h"
#include "components/search_engines/keyword_web_data_service.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/template_url_data_util.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/testing_search_terms_data.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/webdata/common/web_database_service.h"
#include "content/public/browser/browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
 public:
  TestingTemplateURLServiceClient(history::HistoryService* history_service,
                                  std::u16string* search_term)
      : ChromeTemplateURLServiceClient(history_service),
        search_term_(search_term) {}

  TestingTemplateURLServiceClient(const TestingTemplateURLServiceClient&) =
      delete;
  TestingTemplateURLServiceClient& operator=(
      const TestingTemplateURLServiceClient&) = delete;

  void SetKeywordSearchTermsForURL(const GURL& url,
                                   TemplateURLID id,
                                   const std::u16string& term) override {
    *search_term_ = term;
  }

 private:
  raw_ptr<std::u16string> search_term_;
};

}  // namespace

void SetManagedDefaultSearchPreferences(const TemplateURLData& managed_data,
                                        bool enabled,
                                        TestingProfile* profile) {
  base::Value::Dict dict = TemplateURLDataToDictionary(managed_data);
  dict.Set(DefaultSearchManager::kDisabledByPolicy, !enabled);

  profile->GetTestingPrefService()->SetManagedPref(
      DefaultSearchManager::kDefaultSearchProviderDataPrefName,
      std::move(dict));
}

void RemoveManagedDefaultSearchPreferences(TestingProfile* profile) {
  profile->GetTestingPrefService()->RemoveManagedPref(
      DefaultSearchManager::kDefaultSearchProviderDataPrefName);
}

void SetRecommendedDefaultSearchPreferences(const TemplateURLData& data,
                                            bool enabled,
                                            TestingProfile* profile) {
  base::Value::Dict dict = TemplateURLDataToDictionary(data);
  dict.Set(DefaultSearchManager::kDisabledByPolicy, !enabled);

  profile->GetTestingPrefService()->SetRecommendedPref(
      DefaultSearchManager::kDefaultSearchProviderDataPrefName,
      std::move(dict));
}

void SetManagedSearchSettingsPreference(
    const EnterpriseSearchManager::OwnedTemplateURLDataVector&
        enterprise_search_engines,
    TestingProfile* profile) {
  base::Value::List pref_value;
  for (auto& enterprise_search_engine : enterprise_search_engines) {
    pref_value.Append(
        base::Value(TemplateURLDataToDictionary(*enterprise_search_engine)));
  }

  profile->GetTestingPrefService()->SetManagedPref(
      EnterpriseSearchManager::kSiteSearchSettingsPrefName,
      std::move(pref_value));
}

std::unique_ptr<TemplateURL> CreateTestTemplateURL(
    const std::u16string& keyword,
    const std::string& url,
    const std::string& guid,
    base::Time last_modified,
    bool safe_for_autoreplace,
    TemplateURLData::PolicyOrigin policy_origin,
    int prepopulate_id,
    int starter_pack_id,
    TemplateURLData::ActiveStatus is_active) {
  DCHECK(!base::StartsWith(guid, "key"))
      << "Don't use test GUIDs with the form \"key1\". Use \"guid1\" instead "
         "for clarity.";

  TemplateURLData data;
  data.SetShortName(u"unittest");
  data.SetKeyword(keyword);
  data.SetURL(url);
  data.favicon_url = GURL("http://favicon.url");
  data.safe_for_autoreplace = safe_for_autoreplace;
  data.date_created = base::Time::FromTimeT(100);
  data.last_modified = last_modified;
  data.policy_origin = policy_origin;
  data.prepopulate_id = prepopulate_id;
  data.starter_pack_id = starter_pack_id;
  data.is_active = is_active;
  if (!guid.empty()) {
    data.sync_guid = guid;
  }
  return std::make_unique<TemplateURL>(data);
}

TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
    : TemplateURLServiceTestUtil(TestingProfile::TestingFactories()) {}

TemplateURLServiceTestUtil::TemplateURLServiceTestUtil(PrefService& local_state)
    : TemplateURLServiceTestUtil(TestingProfile::TestingFactories(),
                                 &local_state) {}

TemplateURLServiceTestUtil::TemplateURLServiceTestUtil(
    TestingProfile::TestingFactories testing_factories,
    PrefService* local_state)
    : local_state_(local_state) {
  if (!local_state_) {
    if (g_browser_process->local_state()) {
      local_state_ = g_browser_process->local_state();
    } else {
      // `g_browser_process->local_state()` might be null in unit tests.
      owned_local_state_ = std::make_unique<ScopedTestingLocalState>(
          TestingBrowserProcess::GetGlobal());
      local_state_ = owned_local_state_->Get();
    }
  }
  CHECK(local_state_);

  TestingProfile::Builder profile_builder;
  profile_builder.AddTestingFactories(std::move(testing_factories));
  profile_builder.AddTestingFactories(
      TemplateURLServiceTestUtil::SetUpRequiredServicesWithCustomLocalState(
          local_state_));

  profile_ = profile_builder.Build();

  scoped_refptr<WebDatabaseService> web_database_service =
      new WebDatabaseService(profile_->GetPath().AppendASCII("webdata"),
                             base::SingleThreadTaskRunner::GetCurrentDefault(),
                             base::SingleThreadTaskRunner::GetCurrentDefault());
  web_database_service->AddTable(
      std::unique_ptr<WebDatabaseTable>(new KeywordTable()));
  web_database_service->LoadDatabase(g_browser_process->os_crypt_async());

  web_data_service_ = new KeywordWebDataService(
      web_database_service.get(),
      base::SingleThreadTaskRunner::GetCurrentDefault());
  web_data_service_->Init(base::NullCallback());

  ResetModel(false);
}

TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
  ClearModel();
  web_data_service_->ShutdownOnUISequence();
  profile_.reset();

  // Flush the message loop to make application verifiers happy.
  base::RunLoop().RunUntilIdle();
}

// static
TestingProfile::TestingFactories
TemplateURLServiceTestUtil::SetUpRequiredServicesWithCustomLocalState(
    PrefService* local_state_override) {
  TestingProfile::TestingFactories testing_factories;

  testing_factories.push_back({
      search_engines::SearchEngineChoiceServiceFactory::GetInstance(),
      base::BindLambdaForTesting(
          [local_state_override](content::BrowserContext* browser_context)
              -> std::unique_ptr<KeyedService> {
            Profile* profile = Profile::FromBrowserContext(browser_context);
            regional_capabilities::RegionalCapabilitiesService*
                regional_capabilities =
                    regional_capabilities::RegionalCapabilitiesServiceFactory::
                        GetInstance()
                            ->GetForProfile(profile);
            PrefService* local_state = local_state_override
                                           ? local_state_override
                                           : g_browser_process->local_state();
            CHECK(local_state);

            return std::make_unique<search_engines::SearchEngineChoiceService>(
                std::make_unique<FakeSearchEngineChoiceServiceClient>(),
                *profile->GetPrefs(), local_state, *regional_capabilities,
                CHECK_DEREF(
                    TemplateURLPrepopulateData::ResolverFactory::GetInstance()
                        ->GetForProfile(profile)));
          }),
  });

  return testing_factories;
}

// static
BrowserContextKeyedServiceFactory::TestingFactory
TemplateURLServiceTestUtil::GetTemplateURLServiceTestingFactory() {
  return base::BindRepeating(
      [](content::BrowserContext* context) -> std::unique_ptr<KeyedService> {
        return TemplateURLServiceTestUtil::CreateTemplateURLServiceForTesting(
            Profile::FromBrowserContext(context));
      });
}

// static
std::unique_ptr<TemplateURLService>
TemplateURLServiceTestUtil::CreateTemplateURLServiceForTesting(
    Profile* profile,
    std::unique_ptr<SearchTermsData> search_terms_data,
    scoped_refptr<KeywordWebDataService> web_data_service,
    std::unique_ptr<TemplateURLServiceClient> client,
    base::RepeatingClosure dsp_change_callback) {
  return std::make_unique<TemplateURLService>(
      CHECK_DEREF(profile->GetPrefs()),
      CHECK_DEREF(
          search_engines::SearchEngineChoiceServiceFactory::GetForProfile(
              profile)),
      CHECK_DEREF(
          TemplateURLPrepopulateData::ResolverFactory::GetForProfile(profile)),
      std::move(search_terms_data), web_data_service, std::move(client),
      std::move(dsp_change_callback));
}

// static
std::unique_ptr<TemplateURLService>
TemplateURLServiceTestUtil::CreateTemplateURLServiceForTesting(
    Profile* profile,
    base::span<const TemplateURLService::Initializer> initializers) {
  return std::make_unique<TemplateURLService>(
      CHECK_DEREF(profile->GetPrefs()),
      CHECK_DEREF(
          search_engines::SearchEngineChoiceServiceFactory::GetForProfile(
              profile)),
      CHECK_DEREF(
          TemplateURLPrepopulateData::ResolverFactory::GetForProfile(profile)),
      initializers);
}

void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
  changed_count_++;
}

int TemplateURLServiceTestUtil::GetObserverCount() {
  return changed_count_;
}

void TemplateURLServiceTestUtil::ResetObserverCount() {
  changed_count_ = 0;
}

void TemplateURLServiceTestUtil::VerifyLoad() {
  ASSERT_FALSE(model()->loaded());
  model()->Load();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1, GetObserverCount());
  ResetObserverCount();
}

void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
  model()->ChangeToLoadedState();
  // Initialize the web data service so that the database gets updated with
  // any changes made.

  model()->web_data_service_ = web_data_service_;
  base::RunLoop().RunUntilIdle();
}

void TemplateURLServiceTestUtil::ClearModel() {
  model_->Shutdown();
  model_.reset();
}

void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
  if (model_) {
    ClearModel();
  }
  model_ = CreateTemplateURLServiceForTesting(
      profile(),
      std::make_unique<TestingSearchTermsData>("http://www.google.com/"),
      web_data_service_.get(),
      std::unique_ptr<TemplateURLServiceClient>(
          new TestingTemplateURLServiceClient(
              HistoryServiceFactory::GetForProfileIfExists(
                  profile(), ServiceAccessType::EXPLICIT_ACCESS),
              &search_term_)),
      base::BindLambdaForTesting([&] { ++dsp_set_to_google_callback_count_; }));
  model()->AddObserver(this);
  changed_count_ = 0;
  if (verify_load) {
    VerifyLoad();
  }
}

std::u16string TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
  std::u16string search_term;
  search_term.swap(search_term_);
  return search_term;
}

TemplateURL* TemplateURLServiceTestUtil::AddExtensionControlledTURL(
    std::unique_ptr<TemplateURL> extension_turl) {
  TemplateURL* result = model()->Add(std::move(extension_turl));
  DCHECK(result);
  DCHECK(result->GetExtensionInfo());
  if (result->GetExtensionInfo()->wants_to_be_default_engine) {
    SetExtensionDefaultSearchInPrefs(profile()->GetTestingPrefService(),
                                     result->data());
  }
  return result;
}

void TemplateURLServiceTestUtil::RemoveExtensionControlledTURL(
    const std::string& extension_id) {
  TemplateURL* turl = model()->FindTemplateURLForExtension(
      extension_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
  ASSERT_TRUE(turl);
  ASSERT_TRUE(turl->GetExtensionInfo());
  if (turl->GetExtensionInfo()->wants_to_be_default_engine) {
    RemoveExtensionDefaultSearchFromPrefs(profile()->GetTestingPrefService());
  }
  model()->RemoveExtensionControlledTURL(
      extension_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
}