1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_vector.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/search_engines/search_terms_data.h"
9 #include "components/search_engines/template_url.h"
10 #include "components/search_engines/template_url_service.h"
11 #include "components/search_engines/util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 scoped_ptr
<TemplateURLData
> CreatePrepopulateTemplateURLData(
18 const std::string
& keyword
,
20 scoped_ptr
<TemplateURLData
> data(new TemplateURLData
);
21 data
->prepopulate_id
= prepopulate_id
;
22 data
->SetKeyword(base::ASCIIToUTF16(keyword
));
27 // Creates a TemplateURL with default values except for the prepopulate ID,
28 // keyword and TemplateURLID. Only use this in tests if your tests do not
29 // care about other fields.
30 scoped_ptr
<TemplateURL
> CreatePrepopulateTemplateURL(int prepopulate_id
,
31 const std::string
& keyword
,
33 return make_scoped_ptr(new TemplateURL(
34 *CreatePrepopulateTemplateURLData(prepopulate_id
, keyword
, id
)));
39 TEST(TemplateURLServiceUtilTest
, RemoveDuplicatePrepopulateIDs
) {
40 ScopedVector
<TemplateURLData
> prepopulated_turls
;
41 TemplateURLService::TemplateURLVector local_turls
;
42 STLElementDeleter
<TemplateURLService::TemplateURLVector
> local_turls_deleter(
45 prepopulated_turls
.push_back(
46 CreatePrepopulateTemplateURLData(1, "winner4", 1).release());
47 prepopulated_turls
.push_back(
48 CreatePrepopulateTemplateURLData(2, "xxx", 2).release());
49 prepopulated_turls
.push_back(
50 CreatePrepopulateTemplateURLData(3, "yyy", 3).release());
52 // Create a sets of different TURLs grouped by prepopulate ID. Each group
53 // will test a different heuristic of RemoveDuplicatePrepopulateIDs.
54 // Ignored set - These should be left alone as they do not have valid
56 local_turls
.push_back(
57 CreatePrepopulateTemplateURL(0, "winner1", 4).release());
58 local_turls
.push_back(
59 CreatePrepopulateTemplateURL(0, "winner2", 5).release());
60 local_turls
.push_back(
61 CreatePrepopulateTemplateURL(0, "winner3", 6).release());
62 size_t num_non_prepopulated_urls
= local_turls
.size();
64 // Keyword match set - Prefer the one that matches the keyword of the
66 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "loser1", 7).release());
67 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "loser2", 8).release());
68 local_turls
.push_back(
69 CreatePrepopulateTemplateURL(1, "winner4", 9).release());
71 // Default set - Prefer the default search engine over all other criteria.
72 // The last one is the default. It will be passed as the
73 // default_search_provider parameter to RemoveDuplicatePrepopulateIDs.
74 local_turls
.push_back(
75 CreatePrepopulateTemplateURL(2, "loser3", 10).release());
76 local_turls
.push_back(CreatePrepopulateTemplateURL(2, "xxx", 11).release());
77 TemplateURL
* default_turl
=
78 CreatePrepopulateTemplateURL(2, "winner5", 12).release();
79 local_turls
.push_back(default_turl
);
81 // ID set - Prefer the lowest TemplateURLID if the keywords don't match and if
82 // none are the default.
83 local_turls
.push_back(
84 CreatePrepopulateTemplateURL(3, "winner6", 13).release());
85 local_turls
.push_back(
86 CreatePrepopulateTemplateURL(3, "loser5", 14).release());
87 local_turls
.push_back(
88 CreatePrepopulateTemplateURL(3, "loser6", 15).release());
90 RemoveDuplicatePrepopulateIDs(NULL
, prepopulated_turls
, default_turl
,
91 &local_turls
, SearchTermsData(), NULL
);
93 // Verify that the expected local TURLs survived the process.
94 EXPECT_EQ(local_turls
.size(),
95 prepopulated_turls
.size() + num_non_prepopulated_urls
);
96 for (TemplateURLService::TemplateURLVector::const_iterator itr
=
97 local_turls
.begin(); itr
!= local_turls
.end(); ++itr
) {
98 EXPECT_TRUE(base::StartsWith((*itr
)->keyword(),
99 base::ASCIIToUTF16("winner"),
100 base::CompareCase::SENSITIVE
));