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 // Creates a TemplateURL with default values except for the prepopulate ID,
16 // keyword and TemplateURLID. Only use this in tests if your tests do not
17 // care about other fields. The caller is the owner of this TemplateURL.
18 TemplateURL
* CreatePrepopulateTemplateURL(int prepopulate_id
,
19 const std::string
& keyword
,
22 data
.prepopulate_id
= prepopulate_id
;
23 data
.SetKeyword(base::ASCIIToUTF16(keyword
));
25 return new TemplateURL(NULL
, data
);
30 TEST(TemplateURLServiceUtilTest
, RemoveDuplicatePrepopulateIDs
) {
31 ScopedVector
<TemplateURL
> prepopulated_turls
;
32 TemplateURLService::TemplateURLVector local_turls
;
33 STLElementDeleter
<TemplateURLService::TemplateURLVector
> local_turls_deleter(
36 prepopulated_turls
.push_back(CreatePrepopulateTemplateURL(1, "winner4", 1));
37 prepopulated_turls
.push_back(CreatePrepopulateTemplateURL(2, "xxx", 2));
38 prepopulated_turls
.push_back(CreatePrepopulateTemplateURL(3, "yyy", 3));
40 // Create a sets of different TURLs grouped by prepopulate ID. Each group
41 // will test a different heuristic of RemoveDuplicatePrepopulateIDs.
42 // Ignored set - These should be left alone as they do not have valid
44 local_turls
.push_back(CreatePrepopulateTemplateURL(0, "winner1", 4));
45 local_turls
.push_back(CreatePrepopulateTemplateURL(0, "winner2", 5));
46 local_turls
.push_back(CreatePrepopulateTemplateURL(0, "winner3", 6));
47 size_t num_non_prepopulated_urls
= local_turls
.size();
49 // Keyword match set - Prefer the one that matches the keyword of the
51 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "loser1", 7));
52 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "loser2", 8));
53 local_turls
.push_back(CreatePrepopulateTemplateURL(1, "winner4", 9));
55 // Default set - Prefer the default search engine over all other criteria.
56 // The last one is the default. It will be passed as the
57 // default_search_provider parameter to RemoveDuplicatePrepopulateIDs.
58 local_turls
.push_back(CreatePrepopulateTemplateURL(2, "loser3", 10));
59 local_turls
.push_back(CreatePrepopulateTemplateURL(2, "xxx", 11));
60 TemplateURL
* default_turl
= CreatePrepopulateTemplateURL(2, "winner5", 12);
61 local_turls
.push_back(default_turl
);
63 // ID set - Prefer the lowest TemplateURLID if the keywords don't match and if
64 // none are the default.
65 local_turls
.push_back(CreatePrepopulateTemplateURL(3, "winner6", 13));
66 local_turls
.push_back(CreatePrepopulateTemplateURL(3, "loser5", 14));
67 local_turls
.push_back(CreatePrepopulateTemplateURL(3, "loser6", 15));
69 RemoveDuplicatePrepopulateIDs(NULL
, prepopulated_turls
, default_turl
,
72 // Verify that the expected local TURLs survived the process.
73 EXPECT_EQ(local_turls
.size(),
74 prepopulated_turls
.size() + num_non_prepopulated_urls
);
75 for (TemplateURLService::TemplateURLVector::const_iterator itr
=
76 local_turls
.begin(); itr
!= local_turls
.end(); ++itr
) {
78 StartsWith((*itr
)->keyword(), base::ASCIIToUTF16("winner"), true));