1 // Copyright (c) 2012 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 "chrome/browser/search_engines/template_url.h"
9 #include "chrome/browser/search_engines/template_url_service.h"
10 #include "chrome/browser/search_engines/util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 scoped_ptr
<TemplateURLData
> CreatePrepopulateTemplateURLData(
17 const std::string
& keyword
,
19 scoped_ptr
<TemplateURLData
> data(new TemplateURLData
);
20 data
->prepopulate_id
= prepopulate_id
;
21 data
->SetKeyword(base::ASCIIToUTF16(keyword
));
26 // Creates a TemplateURL with default values except for the prepopulate ID,
27 // keyword and TemplateURLID. Only use this in tests if your tests do not
28 // care about other fields.
29 scoped_ptr
<TemplateURL
> CreatePrepopulateTemplateURL(int prepopulate_id
,
30 const std::string
& keyword
,
32 return make_scoped_ptr(new TemplateURL(
33 NULL
, *CreatePrepopulateTemplateURLData(prepopulate_id
, keyword
, id
)));
38 TEST(TemplateURLServiceUtilTest
, RemoveDuplicatePrepopulateIDs
) {
39 ScopedVector
<TemplateURLData
> prepopulated_turls
;
40 TemplateURLService::TemplateURLVector local_turls
;
41 STLElementDeleter
<TemplateURLService::TemplateURLVector
> local_turls_deleter(
44 prepopulated_turls
.push_back(
45 CreatePrepopulateTemplateURLData(1, "winner4", 1).release());
46 prepopulated_turls
.push_back(
47 CreatePrepopulateTemplateURLData(2, "xxx", 2).release());
48 prepopulated_turls
.push_back(
49 CreatePrepopulateTemplateURLData(3, "yyy", 3).release());
51 // Create a sets of different TURLs grouped by prepopulate ID. Each group
52 // will test a different heuristic of RemoveDuplicatePrepopulateIDs.
53 // Ignored set - These should be left alone as they do not have valid
55 local_turls
.push_back(
56 CreatePrepopulateTemplateURL(0, "winner1", 4).release());
57 local_turls
.push_back(
58 CreatePrepopulateTemplateURL(0, "winner2", 5).release());
59 local_turls
.push_back(
60 CreatePrepopulateTemplateURL(0, "winner3", 6).release());
61 size_t num_non_prepopulated_urls
= local_turls
.size();
63 // Keyword match set - Prefer the one that matches the keyword of the
65 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "loser1", 7).release());
66 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "loser2", 8).release());
67 local_turls
.push_back(
68 CreatePrepopulateTemplateURL(1, "winner4", 9).release());
70 // Default set - Prefer the default search engine over all other criteria.
71 // The last one is the default. It will be passed as the
72 // default_search_provider parameter to RemoveDuplicatePrepopulateIDs.
73 local_turls
.push_back(
74 CreatePrepopulateTemplateURL(2, "loser3", 10).release());
75 local_turls
.push_back(CreatePrepopulateTemplateURL(2, "xxx", 11).release());
76 TemplateURL
* default_turl
=
77 CreatePrepopulateTemplateURL(2, "winner5", 12).release();
78 local_turls
.push_back(default_turl
);
80 // ID set - Prefer the lowest TemplateURLID if the keywords don't match and if
81 // none are the default.
82 local_turls
.push_back(
83 CreatePrepopulateTemplateURL(3, "winner6", 13).release());
84 local_turls
.push_back(
85 CreatePrepopulateTemplateURL(3, "loser5", 14).release());
86 local_turls
.push_back(
87 CreatePrepopulateTemplateURL(3, "loser6", 15).release());
89 RemoveDuplicatePrepopulateIDs(NULL
, prepopulated_turls
, default_turl
,
92 // Verify that the expected local TURLs survived the process.
93 EXPECT_EQ(local_turls
.size(),
94 prepopulated_turls
.size() + num_non_prepopulated_urls
);
95 for (TemplateURLService::TemplateURLVector::const_iterator itr
=
96 local_turls
.begin(); itr
!= local_turls
.end(); ++itr
) {
98 StartsWith((*itr
)->keyword(), base::ASCIIToUTF16("winner"), true));