Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "base/string_util.h" | |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "chrome/browser/search_engines/template_url.h" | |
| 8 #include "chrome/browser/search_engines/template_url_service.h" | |
| 9 #include "chrome/browser/search_engines/util.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Creates a TemplateURL with default values except for the prepopulate ID, | |
| 15 // keyword and TemplateURLID. Only use this in tests if your tests do not | |
| 16 // care about other fields. The caller is the owner of this TemplateURL. | |
| 17 TemplateURL* CreatePrepopulateTemplateURL(int prepopulate_id, | |
| 18 const std::string& keyword, | |
| 19 TemplateURLID id) { | |
| 20 TemplateURLData data; | |
| 21 data.prepopulate_id = prepopulate_id; | |
| 22 data.SetKeyword(ASCIIToUTF16(keyword)); | |
| 23 data.id = id; | |
| 24 return new TemplateURL(NULL, data); | |
| 25 } | |
| 26 | |
| 27 }; // namespace | |
| 28 | |
| 29 TEST(TemplateURLServiceUtilTest, RemoveDuplicatePrepopulateIDs) { | |
|
SteveT
2012/05/17 13:34:54
This covers all the heuristics used by RemoveDupli
| |
| 30 std::vector<TemplateURL*> prepopulated_turls; | |
| 31 TemplateURLService::TemplateURLVector local_turls; | |
| 32 | |
| 33 prepopulated_turls.push_back(CreatePrepopulateTemplateURL(1, "winner4", 1)); | |
| 34 prepopulated_turls.push_back(CreatePrepopulateTemplateURL(2, "xxx", 2)); | |
| 35 prepopulated_turls.push_back(CreatePrepopulateTemplateURL(3, "yyy", 3)); | |
| 36 | |
| 37 // Create a sets of different TURLs grouped by prepopulate ID. Each group | |
| 38 // will test a different heuristic of RemoveDuplicatePrepopulateIDs. | |
| 39 // Ignored set - These should be left alone as they do not have valid | |
| 40 // prepopulate IDs. | |
| 41 local_turls.push_back(CreatePrepopulateTemplateURL(0, "winner1", 4)); | |
| 42 local_turls.push_back(CreatePrepopulateTemplateURL(0, "winner2", 5)); | |
| 43 local_turls.push_back(CreatePrepopulateTemplateURL(0, "winner3", 6)); | |
|
Peter Kasting
2012/05/17 20:12:17
If after this block you add a "size_t num_non_prep
SteveT
2012/05/17 21:27:22
Done.
| |
| 44 | |
| 45 // Keyword match set - Prefer the one that matches the keyword of the | |
| 46 // prepopulate ID. | |
| 47 local_turls.push_back(CreatePrepopulateTemplateURL(1, "loser1", 7)); | |
| 48 local_turls.push_back(CreatePrepopulateTemplateURL(1, "loser2", 8)); | |
| 49 local_turls.push_back(CreatePrepopulateTemplateURL(1, "winner4", 9)); | |
| 50 | |
| 51 // Default set - Prefer the default search engine over all other criteria. | |
| 52 // The last one is the default. It will be passed as the | |
| 53 // default_search_provider parameter to RemoveDuplicatePrepopulateIDs. | |
| 54 local_turls.push_back(CreatePrepopulateTemplateURL(2, "loser3", 10)); | |
| 55 local_turls.push_back(CreatePrepopulateTemplateURL(2, "loser4", 11)); | |
|
Peter Kasting
2012/05/17 20:12:17
Nit: Make this have keyword "xxx" to validate that
SteveT
2012/05/17 21:27:22
Done.
| |
| 56 TemplateURL* default_turl = CreatePrepopulateTemplateURL(2, "winner5", 12); | |
| 57 local_turls.push_back(default_turl); | |
| 58 | |
| 59 // ID set - Prefer the lowest TemplateURLID if the keywords don't match and if | |
| 60 // none are the default. | |
| 61 local_turls.push_back(CreatePrepopulateTemplateURL(3, "winner6", 13)); | |
| 62 local_turls.push_back(CreatePrepopulateTemplateURL(3, "loser5", 14)); | |
| 63 local_turls.push_back(CreatePrepopulateTemplateURL(3, "loser6", 15)); | |
| 64 | |
| 65 testing::TestRemoveDuplicatePrepopulateIDs(NULL, prepopulated_turls, | |
| 66 default_turl, &local_turls); | |
| 67 | |
| 68 // Verify that the expected local TURLs survived the process. | |
| 69 for (TemplateURLService::TemplateURLVector::const_iterator itr = | |
| 70 local_turls.begin(); itr != local_turls.end(); ++itr) { | |
|
Peter Kasting
2012/05/17 20:12:17
Nit: {} not necessary
SteveT
2012/05/17 21:27:22
Done.
| |
| 71 EXPECT_TRUE(StartsWith((*itr)->keyword(), ASCIIToUTF16("winner"), true)); | |
| 72 } | |
| 73 | |
| 74 // Manually clean up, since we own all the remaining TURLs. | |
| 75 for (std::vector<TemplateURL*>::iterator itr = prepopulated_turls.begin(); | |
| 76 itr != prepopulated_turls.end(); ++itr) | |
| 77 delete *itr; | |
| 78 for (TemplateURLService::TemplateURLVector::iterator itr = | |
| 79 local_turls.begin(); itr != local_turls.end(); ++itr) | |
| 80 delete *itr; | |
|
SteveT
2012/05/17 13:34:54
Manual cleanups suck, but sometimes they're necess
Peter Kasting
2012/05/17 20:12:17
Use ScopedVector for |prepopulated_turls|. Use ST
SteveT
2012/05/17 21:27:22
Awesome. Done.
| |
| 81 } | |
| OLD | NEW |