| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "chrome/browser/android/ntp/most_visited_sites.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 struct TitleURL { | |
| 20 TitleURL(const std::string& title, const std::string& url) | |
| 21 : title(base::UTF8ToUTF16(title)), url(url) {} | |
| 22 TitleURL(const base::string16& title, const std::string& url) | |
| 23 : title(title), url(url) {} | |
| 24 | |
| 25 base::string16 title; | |
| 26 std::string url; | |
| 27 | |
| 28 bool operator==(const TitleURL& other) const { | |
| 29 return title == other.title && url == other.url; | |
| 30 } | |
| 31 }; | |
| 32 | |
| 33 static const size_t kNumSites = 4; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 // This a test for MostVisitedSites::MergeSuggestions(...) method, and thus has | |
| 38 // the same scope as the method itself. This includes: | |
| 39 // + Merge popular suggestions with personal suggestions. | |
| 40 // + Order the suggestions correctly based on the previous ordering. | |
| 41 // More importantly things out of the scope of testing presently: | |
| 42 // - Removing blacklisted suggestions. | |
| 43 // - Storing the current suggestion ordering. | |
| 44 // - Retrieving the previous ordering. | |
| 45 // - Correct Host extraction from the URL. | |
| 46 // - Ensuring personal suggestions are not duplicated in popular suggestions. | |
| 47 class MostVisitedSitesTest : public testing::Test { | |
| 48 protected: | |
| 49 void Check(const std::vector<TitleURL>& popular_sites, | |
| 50 const std::vector<TitleURL>& whitelist_entry_points, | |
| 51 const std::vector<TitleURL>& personal_sites, | |
| 52 const std::vector<std::string>& old_sites_url, | |
| 53 const std::vector<bool>& old_sites_is_personal, | |
| 54 const std::vector<bool>& expected_sites_is_personal, | |
| 55 const std::vector<TitleURL>& expected_sites) { | |
| 56 MostVisitedSites::SuggestionsPtrVector personal_suggestions; | |
| 57 for (const TitleURL& site : personal_sites) | |
| 58 personal_suggestions.push_back(MakeSuggestionFrom(site, true, false)); | |
| 59 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; | |
| 60 for (const TitleURL& site : whitelist_entry_points) | |
| 61 whitelist_suggestions.push_back(MakeSuggestionFrom(site, false, true)); | |
| 62 MostVisitedSites::SuggestionsPtrVector popular_suggestions; | |
| 63 for (const TitleURL& site : popular_sites) | |
| 64 popular_suggestions.push_back(MakeSuggestionFrom(site, false, false)); | |
| 65 MostVisitedSites::SuggestionsPtrVector result_suggestions = | |
| 66 MostVisitedSites::MergeSuggestions( | |
| 67 &personal_suggestions, &whitelist_suggestions, &popular_suggestions, | |
| 68 old_sites_url, old_sites_is_personal); | |
| 69 std::vector<TitleURL> result_sites; | |
| 70 std::vector<bool> result_is_personal; | |
| 71 result_sites.reserve(result_suggestions.size()); | |
| 72 result_is_personal.reserve(result_suggestions.size()); | |
| 73 for (const auto& suggestion : result_suggestions) { | |
| 74 result_sites.push_back( | |
| 75 TitleURL(suggestion->title, suggestion->url.spec())); | |
| 76 result_is_personal.push_back(suggestion->source != | |
| 77 MostVisitedSites::POPULAR); | |
| 78 } | |
| 79 EXPECT_EQ(result_is_personal, expected_sites_is_personal); | |
| 80 EXPECT_EQ(result_sites, expected_sites); | |
| 81 } | |
| 82 static std::unique_ptr<MostVisitedSites::Suggestion> MakeSuggestionFrom( | |
| 83 const TitleURL& title_url, | |
| 84 bool is_personal, | |
| 85 bool whitelist) { | |
| 86 std::unique_ptr<MostVisitedSites::Suggestion> suggestion = | |
| 87 base::WrapUnique(new MostVisitedSites::Suggestion()); | |
| 88 suggestion->title = title_url.title; | |
| 89 suggestion->url = GURL(title_url.url); | |
| 90 suggestion->source = whitelist ? MostVisitedSites::WHITELIST | |
| 91 : (is_personal ? MostVisitedSites::TOP_SITES | |
| 92 : MostVisitedSites::POPULAR); | |
| 93 return suggestion; | |
| 94 } | |
| 95 }; | |
| 96 | |
| 97 TEST_F(MostVisitedSitesTest, PersonalSitesDefaultOrder) { | |
| 98 TitleURL personal[] = { | |
| 99 TitleURL("Site 1", "https://www.site1.com/"), | |
| 100 TitleURL("Site 2", "https://www.site2.com/"), | |
| 101 TitleURL("Site 3", "https://www.site3.com/"), | |
| 102 TitleURL("Site 4", "https://www.site4.com/"), | |
| 103 }; | |
| 104 std::vector<TitleURL> personal_sites(personal, | |
| 105 personal + arraysize(personal)); | |
| 106 std::vector<std::string> old_sites_url; | |
| 107 std::vector<bool> old_sites_source; | |
| 108 // Without any previous ordering or popular suggestions, the result after | |
| 109 // merge should be the personal suggestions themselves. | |
| 110 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/); | |
| 111 Check(std::vector<TitleURL>(), std::vector<TitleURL>(), personal_sites, | |
| 112 old_sites_url, old_sites_source, expected_sites_source, personal_sites); | |
| 113 } | |
| 114 | |
| 115 TEST_F(MostVisitedSitesTest, PersonalSitesDefinedOrder) { | |
| 116 TitleURL personal[] = { | |
| 117 TitleURL("Site 1", "https://www.site1.com/"), | |
| 118 TitleURL("Site 2", "https://www.site2.com/"), | |
| 119 TitleURL("Site 3", "https://www.site3.com/"), | |
| 120 TitleURL("Site 4", "https://www.site4.com/"), | |
| 121 }; | |
| 122 std::string old[] = { | |
| 123 "https://www.site4.com/", "https://www.site2.com/", | |
| 124 }; | |
| 125 std::vector<bool> old_sites_source(arraysize(old), true /*personal source*/); | |
| 126 TitleURL expected[] = { | |
| 127 TitleURL("Site 4", "https://www.site4.com/"), | |
| 128 TitleURL("Site 2", "https://www.site2.com/"), | |
| 129 TitleURL("Site 1", "https://www.site1.com/"), | |
| 130 TitleURL("Site 3", "https://www.site3.com/"), | |
| 131 }; | |
| 132 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/); | |
| 133 Check(std::vector<TitleURL>(), std::vector<TitleURL>(), | |
| 134 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 135 std::vector<std::string>(old, old + arraysize(old)), old_sites_source, | |
| 136 expected_sites_source, | |
| 137 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 138 } | |
| 139 | |
| 140 TEST_F(MostVisitedSitesTest, PopularSitesDefaultOrder) { | |
| 141 TitleURL popular[] = { | |
| 142 TitleURL("Site 1", "https://www.site1.com/"), | |
| 143 TitleURL("Site 2", "https://www.site2.com/"), | |
| 144 TitleURL("Site 3", "https://www.site3.com/"), | |
| 145 TitleURL("Site 4", "https://www.site4.com/"), | |
| 146 }; | |
| 147 std::vector<TitleURL> popular_sites(popular, popular + arraysize(popular)); | |
| 148 std::vector<std::string> old_sites_url; | |
| 149 std::vector<bool> old_sites_source; | |
| 150 // Without any previous ordering or personal suggestions, the result after | |
| 151 // merge should be the popular suggestions themselves. | |
| 152 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/); | |
| 153 Check(popular_sites, std::vector<TitleURL>(), std::vector<TitleURL>(), | |
| 154 old_sites_url, old_sites_source, expected_sites_source, popular_sites); | |
| 155 } | |
| 156 | |
| 157 TEST_F(MostVisitedSitesTest, PopularSitesDefinedOrder) { | |
| 158 TitleURL popular[] = { | |
| 159 TitleURL("Site 1", "https://www.site1.com/"), | |
| 160 TitleURL("Site 2", "https://www.site2.com/"), | |
| 161 TitleURL("Site 3", "https://www.site3.com/"), | |
| 162 TitleURL("Site 4", "https://www.site4.com/"), | |
| 163 }; | |
| 164 std::string old[] = { | |
| 165 "https://www.site4.com/", "https://www.site2.com/", | |
| 166 }; | |
| 167 std::vector<bool> old_sites_source(arraysize(old), false /*popular source*/); | |
| 168 TitleURL expected[] = { | |
| 169 TitleURL("Site 4", "https://www.site4.com/"), | |
| 170 TitleURL("Site 2", "https://www.site2.com/"), | |
| 171 TitleURL("Site 1", "https://www.site1.com/"), | |
| 172 TitleURL("Site 3", "https://www.site3.com/"), | |
| 173 }; | |
| 174 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/); | |
| 175 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 176 std::vector<TitleURL>(), std::vector<TitleURL>(), | |
| 177 std::vector<std::string>(old, old + arraysize(old)), old_sites_source, | |
| 178 expected_sites_source, | |
| 179 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 180 } | |
| 181 | |
| 182 TEST_F(MostVisitedSitesTest, PopularAndPersonalDefaultOrder) { | |
| 183 TitleURL popular[] = { | |
| 184 TitleURL("Site 1", "https://www.site1.com/"), | |
| 185 TitleURL("Site 2", "https://www.site2.com/"), | |
| 186 }; | |
| 187 TitleURL personal[] = { | |
| 188 TitleURL("Site 3", "https://www.site3.com/"), | |
| 189 TitleURL("Site 4", "https://www.site4.com/"), | |
| 190 }; | |
| 191 // Without an explicit ordering, personal suggestions precede popular | |
| 192 // suggestions. | |
| 193 TitleURL expected[] = { | |
| 194 TitleURL("Site 3", "https://www.site3.com/"), | |
| 195 TitleURL("Site 4", "https://www.site4.com/"), | |
| 196 TitleURL("Site 1", "https://www.site1.com/"), | |
| 197 TitleURL("Site 2", "https://www.site2.com/"), | |
| 198 }; | |
| 199 bool expected_source_is_personal[] = {true, true, false, false}; | |
| 200 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 201 std::vector<TitleURL>(), | |
| 202 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 203 std::vector<std::string>(), std::vector<bool>(), | |
| 204 std::vector<bool>(expected_source_is_personal, | |
| 205 expected_source_is_personal + | |
| 206 arraysize(expected_source_is_personal)), | |
| 207 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 208 } | |
| 209 | |
| 210 TEST_F(MostVisitedSitesTest, PopularAndPersonalDefinedOrder) { | |
| 211 TitleURL popular[] = { | |
| 212 TitleURL("Site 1", "https://www.site1.com/"), | |
| 213 TitleURL("Site 2", "https://www.site2.com/"), | |
| 214 }; | |
| 215 TitleURL personal[] = { | |
| 216 TitleURL("Site 3", "https://www.site3.com/"), | |
| 217 TitleURL("Site 4", "https://www.site4.com/"), | |
| 218 }; | |
| 219 std::string old[] = { | |
| 220 "https://www.site2.com/", "https://www.unknownsite.com/", | |
| 221 "https://www.site4.com/", | |
| 222 }; | |
| 223 std::vector<bool> old_sites_source(arraysize(old), false /*popular source*/); | |
| 224 // Keep the order constant for previous suggestions, else personal suggestions | |
| 225 // precede popular suggestions. | |
| 226 TitleURL expected[] = { | |
| 227 TitleURL("Site 2", "https://www.site2.com/"), | |
| 228 TitleURL("Site 3", "https://www.site3.com/"), | |
| 229 TitleURL("Site 4", "https://www.site4.com/"), | |
| 230 TitleURL("Site 1", "https://www.site1.com/"), | |
| 231 }; | |
| 232 bool expected_source_is_personal[] = {false, true, true, false}; | |
| 233 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 234 std::vector<TitleURL>(), | |
| 235 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 236 std::vector<std::string>(old, old + arraysize(old)), old_sites_source, | |
| 237 std::vector<bool>(expected_source_is_personal, | |
| 238 expected_source_is_personal + | |
| 239 arraysize(expected_source_is_personal)), | |
| 240 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 241 } | |
| OLD | NEW |