File: template_url_service_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (566 lines) | stat: -rw-r--r-- 24,527 bytes parent folder | download | duplicates (3)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Copyright 2014 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.h"

#include <stddef.h>

#include <memory>

#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_utils.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service_client.h"
#include "components/search_engines/template_url_service_observer.h"
#include "components/search_engines/template_url_service_test_util.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/search_engines_data/resources/definitions/prepopulated_engines.h"
#include "url/origin.h"

class TemplateURLServiceUnitTest : public TemplateURLServiceUnitTestBase {};

TEST_F(TemplateURLServiceUnitTest, SessionToken) {
  // Subsequent calls always get the same token.
  std::string token = template_url_service().GetSessionToken();
  std::string token2 = template_url_service().GetSessionToken();
  EXPECT_EQ(token, token2);
  EXPECT_FALSE(token.empty());

  // Calls do not regenerate a token.
  template_url_service().current_token_ = "PRE-EXISTING TOKEN";
  token = template_url_service().GetSessionToken();
  EXPECT_EQ(token, "PRE-EXISTING TOKEN");

  // ... unless the token has expired.
  template_url_service().current_token_.clear();
  const base::TimeDelta kSmallDelta = base::Milliseconds(1);
  template_url_service().token_expiration_time_ =
      base::TimeTicks::Now() - kSmallDelta;
  token = template_url_service().GetSessionToken();
  EXPECT_FALSE(token.empty());
  EXPECT_EQ(token, template_url_service().current_token_);

  // ... or cleared.
  template_url_service().current_token_.clear();
  template_url_service().ClearSessionToken();
  token = template_url_service().GetSessionToken();
  EXPECT_FALSE(token.empty());
  EXPECT_EQ(token, template_url_service().current_token_);

  // The expiration time is always updated.
  template_url_service().GetSessionToken();
  base::TimeTicks expiration_time_1 =
      template_url_service().token_expiration_time_;
  base::PlatformThread::Sleep(kSmallDelta);
  template_url_service().GetSessionToken();
  base::TimeTicks expiration_time_2 =
      template_url_service().token_expiration_time_;
  EXPECT_GT(expiration_time_2, expiration_time_1);
  EXPECT_GE(expiration_time_2, expiration_time_1 + kSmallDelta);
}

TEST_F(TemplateURLServiceUnitTest, GenerateSearchURL) {
  // Set the default search provider to a custom one.
  TemplateURLData template_url_data;
  template_url_data.SetURL("https://www.example.com/?q={searchTerms}");
  template_url_service().SetUserSelectedDefaultSearchProvider(
      template_url_service().Add(
          std::make_unique<TemplateURL>(template_url_data)));

  EXPECT_EQ(
      "https://www.example.com/?q=foo",
      template_url_service().GenerateSearchURLForDefaultSearchProvider(u"foo"));
  EXPECT_EQ(
      "https://www.example.com/?q=",
      template_url_service().GenerateSearchURLForDefaultSearchProvider(u""));
}

TEST_F(TemplateURLServiceUnitTest, ExtractSearchMetadata) {
  TemplateURLData template_url_data;
  template_url_data.SetURL("https://www.example.com/?q={searchTerms}");
  template_url_data.search_intent_params = {"gs_ssp", "si"};
  template_url_service().SetUserSelectedDefaultSearchProvider(
      template_url_service().Add(
          std::make_unique<TemplateURL>(template_url_data)));

  GURL input("https://www.example.com/?q=MyQuery&si=my_si&other_param=foobar");
  auto result = template_url_service().ExtractSearchMetadata(input);
  ASSERT_TRUE(result.has_value());

  EXPECT_EQ(result->normalized_url,
            "https://www.example.com/?si=my_si&q=myquery")
      << "q parameter and si parameter should have been preserved. other_param "
         "should be discarded.";
  EXPECT_EQ(result->search_terms, u"myquery");
}

TEST_F(TemplateURLServiceUnitTest, ValidDefaultSearchProviderOrigin) {
  const std::string dse_str = "https://www.example.com";
  url::Origin dse_origin = url::Origin::Create(GURL(dse_str));
  TemplateURLData template_url_data;
  template_url_data.SetURL(dse_str + "/?q={searchTerms}");
  template_url_service().SetUserSelectedDefaultSearchProvider(
      template_url_service().Add(
          std::make_unique<TemplateURL>(template_url_data)));

  EXPECT_EQ(template_url_service().GetDefaultSearchProviderOrigin(),
            dse_origin);
}

TEST_F(TemplateURLServiceUnitTest, InvalidDefaultSearchProviderOrigin) {
  const std::string dse_str = "https://invalid:test:site";
  url::Origin dse_origin = url::Origin::Create(GURL(dse_str));
  TemplateURLData template_url_data;
  template_url_data.SetURL(dse_str + "/?q={searchTerms}");
  template_url_service().SetUserSelectedDefaultSearchProvider(
      template_url_service().Add(
          std::make_unique<TemplateURL>(template_url_data)));

  EXPECT_NE(template_url_service().GetDefaultSearchProviderOrigin(),
            dse_origin);
  EXPECT_TRUE(template_url_service().GetDefaultSearchProviderOrigin().opaque());
}

TEST_F(
    TemplateURLServiceUnitTest,
    GetFeaturedEnterpriseSearchEngines_IgnoresUnfeaturedEnterpriseSearchEngine) {
  TemplateURLData sitesearch_turl_data;
  sitesearch_turl_data.SetKeyword(u"sitesearch");
  sitesearch_turl_data.SetShortName(u"sitesearch");
  sitesearch_turl_data.SetURL("https://www.sitesearch.com?q={searchTerms}");
  sitesearch_turl_data.policy_origin =
      TemplateURLData::PolicyOrigin::kSiteSearch;
  sitesearch_turl_data.enforced_by_policy = true;
  sitesearch_turl_data.featured_by_policy = false;
  sitesearch_turl_data.safe_for_autoreplace = false;
  template_url_service().Add(
      std::make_unique<TemplateURL>(sitesearch_turl_data));

  TemplateURLData searchaggregator_turl_data;
  searchaggregator_turl_data.SetKeyword(u"searchaggregator");
  searchaggregator_turl_data.SetShortName(u"searchaggregator");
  searchaggregator_turl_data.SetURL(
      "https://www.searchaggregator.com?q={searchTerms}");
  searchaggregator_turl_data.policy_origin =
      TemplateURLData::PolicyOrigin::kSearchAggregator;
  searchaggregator_turl_data.enforced_by_policy = true;
  searchaggregator_turl_data.featured_by_policy = false;
  searchaggregator_turl_data.safe_for_autoreplace = false;
  template_url_service().Add(
      std::make_unique<TemplateURL>(searchaggregator_turl_data));

  EXPECT_TRUE(
      template_url_service().GetFeaturedEnterpriseSearchEngines().empty());
}

TEST_F(
    TemplateURLServiceUnitTest,
    GetFeaturedEnterpriseSearchEngines_IncludesFeaturedEnterpriseSearchEngines) {
  TemplateURLData sitesearch_turl_data;
  sitesearch_turl_data.SetKeyword(u"@sitesearch");
  sitesearch_turl_data.SetShortName(u"sitesearch");
  sitesearch_turl_data.SetURL("https://www.sitesearch.com?q={searchTerms}");
  sitesearch_turl_data.policy_origin =
      TemplateURLData::PolicyOrigin::kSiteSearch;
  sitesearch_turl_data.enforced_by_policy = true;
  sitesearch_turl_data.featured_by_policy = true;
  sitesearch_turl_data.safe_for_autoreplace = false;
  template_url_service().Add(
      std::make_unique<TemplateURL>(sitesearch_turl_data));

  TemplateURLData searchaggregator_turl_data;
  searchaggregator_turl_data.SetKeyword(u"@searchaggregator");
  searchaggregator_turl_data.SetShortName(u"searchaggregator");
  searchaggregator_turl_data.SetURL(
      "https://www.searchaggregator.com?q={searchTerms}");
  searchaggregator_turl_data.policy_origin =
      TemplateURLData::PolicyOrigin::kSearchAggregator;
  searchaggregator_turl_data.enforced_by_policy = true;
  searchaggregator_turl_data.featured_by_policy = true;
  searchaggregator_turl_data.safe_for_autoreplace = false;
  template_url_service().Add(
      std::make_unique<TemplateURL>(searchaggregator_turl_data));

  EXPECT_EQ(
      static_cast<int>(
          template_url_service().GetFeaturedEnterpriseSearchEngines().size()),
      2);
  EXPECT_EQ(template_url_service()
                .GetFeaturedEnterpriseSearchEngines()[0]
                ->short_name(),
            u"sitesearch");
  EXPECT_EQ(
      template_url_service().GetFeaturedEnterpriseSearchEngines()[0]->url(),
      "https://www.sitesearch.com?q={searchTerms}");
  EXPECT_EQ(template_url_service()
                .GetFeaturedEnterpriseSearchEngines()[1]
                ->short_name(),
            u"searchaggregator");
  EXPECT_EQ(
      template_url_service().GetFeaturedEnterpriseSearchEngines()[1]->url(),
      "https://www.searchaggregator.com?q={searchTerms}");
}

TEST_F(TemplateURLServiceUnitTest, HiddenFromLists) {
  auto create_template_url_data =
      [](const std::u16string& keyword,
         TemplateURLData::PolicyOrigin policy_origin,
         bool featured_by_policy) -> TemplateURLData {
    TemplateURLData data;
    data.SetShortName(keyword);
    data.SetKeyword(keyword);
    data.SetURL("https://" + base::UTF16ToUTF8(keyword) + "/?q={searchTerms}");
    data.policy_origin = policy_origin;
    data.featured_by_policy = featured_by_policy;
    return data;
  };

  // Engines with no conflicts. Should NOT be hidden.
  {
    TemplateURL* turl = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"work", TemplateURLData::PolicyOrigin::kNoPolicy,
            /*featured_by_policy=*/false)));
    TemplateURL* turl_default_search_provider = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"default_search_provider",
            TemplateURLData::PolicyOrigin::kDefaultSearchProvider,
            /*featured_by_policy=*/false)));
    TemplateURL* turl_site_search = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"site_search", TemplateURLData::PolicyOrigin::kSiteSearch,
            /*featured_by_policy=*/false)));
    TemplateURL* turl_search_aggregator = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"search_aggregator",
            TemplateURLData::PolicyOrigin::kSearchAggregator,
            /*featured_by_policy=*/false)));
    ASSERT_FALSE(template_url_service().HiddenFromLists(turl));
    ASSERT_FALSE(
        template_url_service().HiddenFromLists(turl_default_search_provider));
    ASSERT_FALSE(template_url_service().HiddenFromLists(turl_site_search));
    ASSERT_FALSE(
        template_url_service().HiddenFromLists(turl_search_aggregator));
  }
  // User-defined engine and a nonfeatured policy engine exists with the same
  // keyword. User-defined engine should be hidden. Policy engine should not be
  // hidden.
  {
    TemplateURL* turl = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"conflict", TemplateURLData::PolicyOrigin::kNoPolicy,
            /*featured_by_policy=*/false)));
    TemplateURL* turl_policy = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"conflict", TemplateURLData::PolicyOrigin::kSiteSearch,
            /*featured_by_policy=*/false)));
    ASSERT_FALSE(template_url_service().HiddenFromLists(turl));
    ASSERT_TRUE(template_url_service().HiddenFromLists(turl_policy));
  }

  // User-defined engine and a featured policy engine exists with the same
  // keyword. User-defined engine should be hidden. Policy engine should not be
  // hidden.
  {
    TemplateURL* turl = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"@conflict", TemplateURLData::PolicyOrigin::kNoPolicy,
            /*featured_by_policy=*/false)));
    TemplateURL* turl_featured_policy = template_url_service().Add(
        std::make_unique<TemplateURL>(create_template_url_data(
            u"@conflict", TemplateURLData::PolicyOrigin::kSiteSearch,
            /*featured_by_policy=*/true)));
    ASSERT_TRUE(template_url_service().HiddenFromLists(turl));
    ASSERT_FALSE(template_url_service().HiddenFromLists(turl_featured_policy));
  }
}

#if BUILDFLAG(IS_ANDROID)

class TemplateURLServiceWithDatabaseUnitTest
    : public LoadedTemplateURLServiceUnitTestBase {
 protected:
  const std::u16string kOldPlayEngineKeyword = u"old_keyword";
  const std::u16string kNewPlayEngineKeyword = u"new_keyword";

  TemplateURLData CreatePlayAPITemplateURLData(const std::u16string& keyword) {
    return TemplateURLService::CreatePlayAPITemplateURLData(
        keyword, keyword,
        base::StringPrintf("https://%s.com/q={searchTerms}",
                           base::UTF16ToUTF8(keyword).c_str()));
  }

  // Uses the "legacy" API way of registering a search engine coming from Play.
  TemplateURL* AddPlayApiEngineLegacy(const std::u16string& keyword,
                                      bool set_as_default) {
    // Create a custom search engine declared as coming from Play.
    TemplateURL* template_url = template_url_service().Add(
        std::make_unique<TemplateURL>(CreatePlayAPITemplateURLData(keyword)));

    CHECK(template_url);
    CHECK(template_url->GetRegulatoryExtensionType() ==
          RegulatoryExtensionType::kAndroidEEA);
    CHECK_EQ(template_url,
             template_url_service().GetTemplateURLForKeyword(keyword));

    if (set_as_default) {
      template_url_service().SetUserSelectedDefaultSearchProvider(template_url);
      CHECK_EQ(template_url_service().GetDefaultSearchProvider()->keyword(),
               keyword);
    } else {
      CHECK_EQ(template_url_service().GetDefaultSearchProvider()->keyword(),
               TemplateURLPrepopulateData::google.keyword);
    }

    return template_url;
  }
};

TEST_F(TemplateURLServiceWithDatabaseUnitTest, ResetPlayAPISearchEngine) {
  const size_t initial_turl_count =
      template_url_service().GetTemplateURLs().size();

  // Create a custom search engine declared as coming from Play.
  AddPlayApiEngineLegacy(kOldPlayEngineKeyword,
                         /*set_as_default=*/false);

  // The (old) play engine is added.
  EXPECT_EQ(template_url_service().GetTemplateURLs().size(),
            initial_turl_count + 1);

  const auto new_play_engine_data =
      CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
  EXPECT_TRUE(
      template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));

  // The old DSE is not known anymore.
  EXPECT_FALSE(
      template_url_service().GetTemplateURLForKeyword(kOldPlayEngineKeyword));

  // The new engine is added, flagged as coming from Play, and set as default.
  auto* new_play_engine =
      template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
  EXPECT_TRUE(new_play_engine);
  ASSERT_EQ(new_play_engine->GetRegulatoryExtensionType(),
            RegulatoryExtensionType::kAndroidEEA);
  EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());

  // We still have the same number of engines.
  EXPECT_EQ(template_url_service().GetTemplateURLs().size(),
            initial_turl_count + 1);
}

TEST_F(TemplateURLServiceWithDatabaseUnitTest,
       ResetPlayAPISearchEngine_OldIsDefault) {
  AddPlayApiEngineLegacy(kOldPlayEngineKeyword, /*set_as_default=*/true);

  const auto new_play_engine_data =
      CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
  EXPECT_TRUE(
      template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));

  // The old DSE is not known anymore.
  EXPECT_FALSE(
      template_url_service().GetTemplateURLForKeyword(kOldPlayEngineKeyword));

  // The new engine is added, flagged as coming from Play, and set as default.
  auto* new_play_engine =
      template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
  EXPECT_TRUE(new_play_engine);
  ASSERT_EQ(new_play_engine->GetRegulatoryExtensionType(),
            RegulatoryExtensionType::kAndroidEEA);
  EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());
}

TEST_F(TemplateURLServiceWithDatabaseUnitTest,
       ResetPlayAPISearchEngine_NewIsPrepopulated) {
  const std::u16string overriden_keyword =
      TemplateURLPrepopulateData::bing.keyword;
  TemplateURL* old_prepopulated_engine =
      template_url_service().GetTemplateURLForKeyword(overriden_keyword);
  ASSERT_TRUE(old_prepopulated_engine);

  const std::string new_engine_url_from_play =
      "https://new.chromium?q={searchTerms}";
  const std::u16string new_engine_name_from_play = u"New Engine From Play";
  const auto new_engine_data = TemplateURLService::CreatePlayAPITemplateURLData(
      overriden_keyword, new_engine_name_from_play, new_engine_url_from_play);
  EXPECT_TRUE(template_url_service().ResetPlayAPISearchEngine(new_engine_data));

  // The new engine is added, flagged as coming from Play, and set as default.
  auto* new_play_engine =
      template_url_service().GetTemplateURLForKeyword(overriden_keyword);
  EXPECT_TRUE(new_play_engine);
  ASSERT_EQ(new_play_engine->GetRegulatoryExtensionType(),
            RegulatoryExtensionType::kAndroidEEA);
  EXPECT_EQ(new_play_engine->prepopulate_id(), /* bing_id */ 3);
  EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());

  // The properties are the ones coming from Chromium database: reconciliation
  // detects matching defiition and re-uses it.
  EXPECT_EQ(new_play_engine->url(), old_prepopulated_engine->url());
  EXPECT_EQ(new_play_engine->short_name(), new_engine_name_from_play);

  // Both the old prepopulated engine and the new one from play are registered.
  ASSERT_THAT(
      GetTemplateURLsMatchingKeyword(overriden_keyword),
      testing::UnorderedElementsAre(old_prepopulated_engine, new_play_engine));
}

TEST_F(TemplateURLServiceWithDatabaseUnitTest,
       ResetPlayAPISearchEngine_OldIsPrepopulated) {
  const std::u16string overriden_keyword =
      TemplateURLPrepopulateData::bing.keyword;

  // Asserting the legacy, baseline behaviour.

  // `overriden_keyword` is expected to be a preloaded engine.
  TemplateURL* old_prepopulated_engine =
      template_url_service().GetTemplateURLForKeyword(overriden_keyword);
  ASSERT_TRUE(old_prepopulated_engine);

  // When adding the old Play engine, both engines are still registered, but the
  // one from Play is now returned when looking up by keyword.
  TemplateURL* old_play_engine =
      AddPlayApiEngineLegacy(overriden_keyword,
                             /*set_as_default=*/true);
  ASSERT_THAT(
      GetTemplateURLsMatchingKeyword(overriden_keyword),
      testing::UnorderedElementsAre(old_prepopulated_engine, old_play_engine));
  ASSERT_EQ(old_play_engine,
            template_url_service().GetTemplateURLForKeyword(overriden_keyword));

  // Checking the behaviour of the new function

  const auto new_play_engine_data =
      CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
  EXPECT_TRUE(
      template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));

  // The new engine is added, flagged as coming from Play, and set as default.
  auto* new_play_engine =
      template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
  EXPECT_TRUE(new_play_engine);
  ASSERT_EQ(new_play_engine->GetRegulatoryExtensionType(),
            RegulatoryExtensionType::kAndroidEEA);
  EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());

  // The old prepopulated engine is still there and is exposed when looking up
  // the keyword.
  EXPECT_THAT(GetTemplateURLsMatchingKeyword(overriden_keyword),
              testing::UnorderedElementsAre(old_prepopulated_engine));
  EXPECT_EQ(old_prepopulated_engine,
            template_url_service().GetTemplateURLForKeyword(overriden_keyword));
}

TEST_F(TemplateURLServiceWithDatabaseUnitTest,
       ResetPlayAPISearchEngine_NewHasSameKeywordAsOld) {
  const std::u16string play_engine_keyword = u"play_keyword";
  AddPlayApiEngineLegacy(play_engine_keyword, /*set_as_default=*/false);

  const std::string new_engine_url_from_play =
      "https://play.chromium?q={searchTerms}&is_new=true";
  const std::u16string new_engine_name_from_play = u"Play Engine New";
  const auto new_play_engine_data =
      TemplateURLService::CreatePlayAPITemplateURLData(
          play_engine_keyword, new_engine_name_from_play,
          new_engine_url_from_play);

  EXPECT_TRUE(
      template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));

  // The new engine is added, flagged as coming from Play, and set as default.
  auto* new_play_engine =
      template_url_service().GetTemplateURLForKeyword(play_engine_keyword);
  EXPECT_TRUE(new_play_engine);
  ASSERT_EQ(new_play_engine->GetRegulatoryExtensionType(),
            RegulatoryExtensionType::kAndroidEEA);
  EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());

  // This is the only known engine matching this keyword.
  EXPECT_THAT(GetTemplateURLsMatchingKeyword(play_engine_keyword),
              testing::Contains(new_play_engine));

  // The properties are the ones coming from play, not the old ones.
  EXPECT_EQ(new_play_engine->url(), new_engine_url_from_play);
  EXPECT_EQ(new_play_engine->short_name(), new_engine_name_from_play);
}

TEST_F(TemplateURLServiceWithDatabaseUnitTest,
       ResetPlayAPISearchEngine_DseSetByPolicy) {
  TemplateURLData policy_engine_data;
  policy_engine_data.SetShortName(u"Chromium.org Search");
  policy_engine_data.SetKeyword(u"cr_search");
  policy_engine_data.SetURL("https://search.chromium.org?q={searchTerms}");

  template_url_service().ApplyDefaultSearchChangeForTesting(
      &policy_engine_data, DefaultSearchManager::FROM_POLICY);

  const TemplateURL* policy_engine =
      template_url_service().GetDefaultSearchProvider();
  ASSERT_EQ(policy_engine->keyword(), policy_engine_data.keyword());
  ASSERT_TRUE(policy_engine->CreatedByDefaultSearchProviderPolicy());

  const auto new_play_engine_data =
      CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);

  EXPECT_TRUE(
      template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
  // The new engine is added, flagged as coming from Play, but not set as
  // default.
  auto* new_play_engine =
      template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
  EXPECT_TRUE(new_play_engine);
  ASSERT_EQ(new_play_engine->GetRegulatoryExtensionType(),
            RegulatoryExtensionType::kAndroidEEA);

  EXPECT_NE(new_play_engine, template_url_service().GetDefaultSearchProvider());

  // The policy engine is still the default one.
  EXPECT_EQ(policy_engine, template_url_service().GetDefaultSearchProvider());
}

TEST_F(TemplateURLServiceWithDatabaseUnitTest,
       ResetPlayAPISearchEngine_NewHasSameKeywordAsPolicyEngine) {
  TemplateURLData policy_engine_data;
  policy_engine_data.SetShortName(u"Chromium.org Search");
  policy_engine_data.SetKeyword(u"cr_search");
  policy_engine_data.SetURL("https://search.chromium.org?q={searchTerms}");

  template_url_service().ApplyDefaultSearchChangeForTesting(
      &policy_engine_data, DefaultSearchManager::FROM_POLICY);

  const TemplateURL* policy_engine =
      template_url_service().GetDefaultSearchProvider();
  ASSERT_EQ(policy_engine->keyword(), policy_engine_data.keyword());
  ASSERT_TRUE(policy_engine->CreatedByDefaultSearchProviderPolicy());

  // Add the Play API engine using the same keyword as the policy engine.
  const auto new_play_engine_data =
      TemplateURLService::CreatePlayAPITemplateURLData(
          policy_engine_data.keyword(), u"title2",
          "https://example2.com/q={searchTerms}");

  // The operation should fail.
  EXPECT_FALSE(
      template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));

  // Only the policy engine is registered with the keyword, and it's still the
  // DSE.
  EXPECT_THAT(GetTemplateURLsMatchingKeyword(policy_engine_data.keyword()),
              testing::UnorderedElementsAre(policy_engine));
  EXPECT_EQ(policy_engine, template_url_service().GetDefaultSearchProvider());
}

#endif