Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/android/most_visited_sites.h" | 9 #include "chrome/browser/android/most_visited_sites.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 struct TitleURL { | 14 struct TitleURL { |
| 15 TitleURL(const std::string& title, | 15 TitleURL(const std::string& title, const std::string& url) |
| 16 const std::string& url, | 16 : title(base::UTF8ToUTF16(title)), url(url) {} |
| 17 const std::string& source) | 17 TitleURL(const base::string16& title, const std::string& url) |
| 18 : title(base::UTF8ToUTF16(title)), url(url), source(source) {} | 18 : title(title), url(url) {} |
| 19 | 19 |
| 20 base::string16 title; | 20 base::string16 title; |
| 21 std::string url; | 21 std::string url; |
| 22 std::string source; | 22 |
| 23 bool operator==(const TitleURL& other) const { | |
| 24 return title == other.title && url == other.url; | |
| 25 } | |
| 26 | |
| 27 MostVisitedSites::Suggestion* GetSuggestion(bool is_personal) const { | |
|
Marc Treib
2015/09/08 11:39:49
nit: Return a scoped_ptr to make ownership clear?
knn
2015/09/08 13:42:00
Done.
| |
| 28 return new MostVisitedSites::Suggestion( | |
| 29 title, url, | |
| 30 is_personal ? MostVisitedSites::TOP_SITES : MostVisitedSites::POPULAR); | |
| 31 } | |
| 23 }; | 32 }; |
| 24 | 33 |
| 25 std::vector<base::string16> GetTitles(const std::vector<TitleURL>& data) { | 34 static const size_t kNumSites = 4; |
| 26 std::vector<base::string16> titles; | |
| 27 for (const TitleURL& item : data) | |
| 28 titles.push_back(item.title); | |
| 29 return titles; | |
| 30 } | |
| 31 | |
| 32 std::vector<std::string> GetURLs(const std::vector<TitleURL>& data) { | |
| 33 std::vector<std::string> urls; | |
| 34 for (const TitleURL& item : data) | |
| 35 urls.push_back(item.url); | |
| 36 return urls; | |
| 37 } | |
| 38 | |
| 39 std::vector<std::string> GetSources(const std::vector<TitleURL>& data) { | |
| 40 std::vector<std::string> sources; | |
| 41 for (const TitleURL& item : data) | |
| 42 sources.push_back(item.source); | |
| 43 return sources; | |
| 44 } | |
| 45 | |
| 46 static const int kNumSites = 4; | |
| 47 | 35 |
| 48 } // namespace | 36 } // namespace |
| 49 | 37 |
| 38 // This a test for MostVisitedSites::MergeSuggestions(...) method, and thus has | |
| 39 // the same scope as the method itself. This includes: | |
| 40 // + Merge popular suggestions with personal suggestions. | |
| 41 // + Order the suggestions correctly based on the previous ordering. | |
| 42 // More importantly things out of the scope of testing presently: | |
| 43 // - Removing blacklisted suggestions. | |
| 44 // - Storing the current suggestion ordering. | |
| 45 // - Retrieving the previous ordering. | |
| 46 // - Correct Host extraction from the URL. | |
| 47 // - Ensuring popular suggestions don't contain personal ones. | |
| 50 class MostVisitedSitesTest : public testing::Test { | 48 class MostVisitedSitesTest : public testing::Test { |
| 51 protected: | 49 protected: |
| 52 void Check(const std::vector<TitleURL>& popular, | 50 void Check(const std::vector<TitleURL>& popular_sites, |
| 53 const std::vector<TitleURL>& personal, | 51 const std::vector<TitleURL>& personal_sites, |
| 54 const std::vector<TitleURL>& expected) { | 52 const std::vector<std::string>& old_sites_url, |
| 55 std::vector<base::string16> titles(GetTitles(personal)); | 53 const std::vector<bool>& old_sites_is_personal, |
| 56 std::vector<std::string> urls(GetURLs(personal)); | 54 const std::vector<bool>& expected_sites_is_personal, |
| 57 std::vector<std::string> sources(GetSources(personal)); | 55 const std::vector<TitleURL>& expected_sites) { |
| 58 | 56 ScopedVector<MostVisitedSites::Suggestion> personal_suggestions; |
| 59 std::vector<base::string16> popular_titles(GetTitles(popular)); | 57 personal_suggestions.reserve(personal_sites.size()); |
| 60 std::vector<std::string> popular_urls(GetURLs(popular)); | 58 for (const auto& site : personal_sites) |
| 61 | 59 personal_suggestions.push_back(site.GetSuggestion(true)); |
| 62 MostVisitedSites::AddPopularSitesImpl( | 60 ScopedVector<MostVisitedSites::Suggestion> popular_suggestions; |
| 63 kNumSites, popular_titles, popular_urls, &titles, &urls, &sources); | 61 popular_suggestions.reserve(popular_sites.size()); |
| 64 | 62 for (const auto& site : popular_sites) |
| 65 EXPECT_EQ(GetTitles(expected), titles); | 63 popular_suggestions.push_back(site.GetSuggestion(false)); |
| 66 EXPECT_EQ(GetURLs(expected), urls); | 64 ScopedVector<MostVisitedSites::Suggestion> result_suggestions; |
| 67 EXPECT_EQ(GetSources(expected), sources); | 65 MostVisitedSites::MergeSuggestions( |
| 66 &personal_suggestions, &popular_suggestions, old_sites_url, | |
| 67 old_sites_is_personal, &result_suggestions); | |
| 68 std::vector<TitleURL> result_sites; | |
| 69 std::vector<bool> result_is_personal; | |
| 70 result_sites.reserve(result_suggestions.size()); | |
| 71 result_is_personal.reserve(result_suggestions.size()); | |
| 72 for (const auto suggestion : result_suggestions) { | |
| 73 result_sites.push_back(TitleURL(suggestion->title, suggestion->url)); | |
| 74 result_is_personal.push_back(suggestion->source != | |
| 75 MostVisitedSites::POPULAR); | |
| 76 } | |
| 77 EXPECT_EQ(result_is_personal, expected_sites_is_personal); | |
| 78 EXPECT_EQ(result_sites, expected_sites); | |
| 68 } | 79 } |
| 69 }; | 80 }; |
| 70 | 81 |
| 71 TEST_F(MostVisitedSitesTest, PopularSitesAppend) { | 82 TEST_F(MostVisitedSitesTest, PersonalSitesDefaultOrder) { |
| 72 TitleURL popular[] = { | 83 TitleURL personal[] = { |
| 73 TitleURL("Site 1", "https://www.site1.com/", "popular"), | 84 TitleURL("Site 1", "https://www.site1.com/"), |
| 74 TitleURL("Site 2", "https://www.site2.com/", "popular"), | 85 TitleURL("Site 2", "https://www.site2.com/"), |
| 75 }; | 86 TitleURL("Site 3", "https://www.site3.com/"), |
| 76 TitleURL personal[] = { | 87 TitleURL("Site 4", "https://www.site4.com/"), |
| 77 TitleURL("Site 3", "https://www.site3.com/", "server8"), | 88 }; |
| 78 TitleURL("Site 4", "https://www.site4.com/", "server8"), | 89 std::vector<TitleURL> personal_sites(personal, |
| 79 }; | 90 personal + arraysize(personal)); |
| 80 // Popular suggestions should keep their positions, with personal suggestions | 91 std::vector<std::string> old_sites_url; |
| 81 // appended at the end. | 92 std::vector<bool> old_sites_source; |
| 82 TitleURL expected[] = { | 93 // Without any previous ordering or popular suggestions, the result after |
| 83 TitleURL("Site 1", "https://www.site1.com/", "popular"), | 94 // merge should be the personal suggestions themselves. |
| 84 TitleURL("Site 2", "https://www.site2.com/", "popular"), | 95 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/); |
| 85 TitleURL("Site 3", "https://www.site3.com/", "server8"), | 96 Check(std::vector<TitleURL>(), personal_sites, old_sites_url, |
| 86 TitleURL("Site 4", "https://www.site4.com/", "server8"), | 97 old_sites_source, expected_sites_source, personal_sites); |
| 87 }; | 98 } |
| 88 | 99 |
| 100 TEST_F(MostVisitedSitesTest, PersonalSitesDefinedOrder) { | |
| 101 TitleURL personal[] = { | |
| 102 TitleURL("Site 1", "https://www.site1.com/"), | |
| 103 TitleURL("Site 2", "https://www.site2.com/"), | |
| 104 TitleURL("Site 3", "https://www.site3.com/"), | |
| 105 TitleURL("Site 4", "https://www.site4.com/"), | |
| 106 }; | |
| 107 std::string old[] = { | |
| 108 "https://www.site4.com/", "https://www.site2.com/", | |
| 109 }; | |
| 110 std::vector<bool> old_sites_source(arraysize(old), true /*personal source*/); | |
| 111 TitleURL expected[] = { | |
| 112 TitleURL("Site 4", "https://www.site4.com/"), | |
| 113 TitleURL("Site 2", "https://www.site2.com/"), | |
| 114 TitleURL("Site 1", "https://www.site1.com/"), | |
| 115 TitleURL("Site 3", "https://www.site3.com/"), | |
| 116 }; | |
| 117 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/); | |
| 118 Check(std::vector<TitleURL>(), | |
| 119 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 120 std::vector<std::string>(old, old + arraysize(old)), old_sites_source, | |
| 121 expected_sites_source, | |
| 122 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 123 } | |
| 124 | |
| 125 TEST_F(MostVisitedSitesTest, PopularSitesDefaultOrder) { | |
| 126 TitleURL popular[] = { | |
| 127 TitleURL("Site 1", "https://www.site1.com/"), | |
| 128 TitleURL("Site 2", "https://www.site2.com/"), | |
| 129 TitleURL("Site 3", "https://www.site3.com/"), | |
| 130 TitleURL("Site 4", "https://www.site4.com/"), | |
| 131 }; | |
| 132 std::vector<TitleURL> popular_sites(popular, popular + arraysize(popular)); | |
| 133 std::vector<std::string> old_sites_url; | |
| 134 std::vector<bool> old_sites_source; | |
| 135 // Without any previous ordering or personal suggestions, the result after | |
| 136 // merge should be the popular suggestions themselves. | |
| 137 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/); | |
| 138 Check(popular_sites, std::vector<TitleURL>(), old_sites_url, old_sites_source, | |
| 139 expected_sites_source, popular_sites); | |
| 140 } | |
| 141 | |
| 142 TEST_F(MostVisitedSitesTest, PopularSitesDefinedOrder) { | |
| 143 TitleURL popular[] = { | |
| 144 TitleURL("Site 1", "https://www.site1.com/"), | |
| 145 TitleURL("Site 2", "https://www.site2.com/"), | |
| 146 TitleURL("Site 3", "https://www.site3.com/"), | |
| 147 TitleURL("Site 4", "https://www.site4.com/"), | |
| 148 }; | |
| 149 std::string old[] = { | |
| 150 "https://www.site4.com/", "https://www.site2.com/", | |
| 151 }; | |
| 152 std::vector<bool> old_sites_source(arraysize(old), false /*popular source*/); | |
| 153 TitleURL expected[] = { | |
| 154 TitleURL("Site 4", "https://www.site4.com/"), | |
| 155 TitleURL("Site 2", "https://www.site2.com/"), | |
| 156 TitleURL("Site 1", "https://www.site1.com/"), | |
| 157 TitleURL("Site 3", "https://www.site3.com/"), | |
| 158 }; | |
| 159 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/); | |
| 160 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 161 std::vector<TitleURL>(), | |
| 162 std::vector<std::string>(old, old + arraysize(old)), old_sites_source, | |
| 163 expected_sites_source, | |
| 164 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 165 } | |
| 166 | |
| 167 TEST_F(MostVisitedSitesTest, PopularAndPersonalDefaultOrder) { | |
| 168 TitleURL popular[] = { | |
| 169 TitleURL("Site 1", "https://www.site1.com/"), | |
| 170 TitleURL("Site 2", "https://www.site2.com/"), | |
| 171 }; | |
| 172 TitleURL personal[] = { | |
| 173 TitleURL("Site 3", "https://www.site3.com/"), | |
| 174 TitleURL("Site 4", "https://www.site4.com/"), | |
| 175 }; | |
| 176 // Without an explicit ordering, personal suggestions precede popular | |
| 177 // suggestions. | |
| 178 TitleURL expected[] = { | |
| 179 TitleURL("Site 3", "https://www.site3.com/"), | |
| 180 TitleURL("Site 4", "https://www.site4.com/"), | |
| 181 TitleURL("Site 1", "https://www.site1.com/"), | |
| 182 TitleURL("Site 2", "https://www.site2.com/"), | |
| 183 }; | |
| 184 bool expected_source_is_personal[] = {true, true, false, false}; | |
| 89 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 185 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 90 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 186 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 91 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 187 std::vector<std::string>(), std::vector<bool>(), |
| 92 } | 188 std::vector<bool>(expected_source_is_personal, |
| 93 | 189 expected_source_is_personal + |
| 94 TEST_F(MostVisitedSitesTest, PopularSitesOverflow) { | 190 arraysize(expected_source_is_personal)), |
| 95 TitleURL popular[] = { | 191 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 96 TitleURL("Site 1", "https://www.site1.com/", "popular"), | 192 } |
| 97 TitleURL("Site 2", "https://www.site2.com/", "popular"), | 193 |
| 98 TitleURL("Site 3", "https://www.site3.com/", "popular"), | 194 TEST_F(MostVisitedSitesTest, PopularAndPersonalDefinedOrder) { |
| 99 }; | 195 TitleURL popular[] = { |
| 100 TitleURL personal[] = { | 196 TitleURL("Site 1", "https://www.site1.com/"), |
| 101 TitleURL("Site 4", "https://www.site4.com/", "server8"), | 197 TitleURL("Site 2", "https://www.site2.com/"), |
| 102 TitleURL("Site 5", "https://www.site5.com/", "server8"), | 198 }; |
| 103 }; | 199 TitleURL personal[] = { |
| 104 // When there are more total suggestions than slots, the personal suggestions | 200 TitleURL("Site 3", "https://www.site3.com/"), |
| 105 // should win, with the remaining popular suggestions still retaining their | 201 TitleURL("Site 4", "https://www.site4.com/"), |
| 106 // positions. | 202 }; |
| 107 TitleURL expected[] = { | 203 std::string old[] = { |
| 108 TitleURL("Site 1", "https://www.site1.com/", "popular"), | 204 "https://www.site2.com/", "https://www.unknownsite.com/", |
| 109 TitleURL("Site 2", "https://www.site2.com/", "popular"), | 205 "https://www.site4.com/", |
| 110 TitleURL("Site 4", "https://www.site4.com/", "server8"), | 206 }; |
| 111 TitleURL("Site 5", "https://www.site5.com/", "server8"), | 207 std::vector<bool> old_sites_source(arraysize(old), false /*popular source*/); |
| 112 }; | 208 // Keep the order constant for previous suggestions, else personal suggestions |
| 113 | 209 // precede popular suggestions. |
| 210 TitleURL expected[] = { | |
| 211 TitleURL("Site 2", "https://www.site2.com/"), | |
| 212 TitleURL("Site 3", "https://www.site3.com/"), | |
| 213 TitleURL("Site 4", "https://www.site4.com/"), | |
| 214 TitleURL("Site 1", "https://www.site1.com/"), | |
| 215 }; | |
| 216 bool expected_source_is_personal[] = {false, true, true, false}; | |
| 114 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 217 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 115 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 218 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 116 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 219 std::vector<std::string>(old, old + arraysize(old)), old_sites_source, |
| 117 } | 220 std::vector<bool>(expected_source_is_personal, |
| 118 | 221 expected_source_is_personal + |
| 119 TEST_F(MostVisitedSitesTest, PopularSitesOverwrite) { | 222 arraysize(expected_source_is_personal)), |
| 120 TitleURL popular[] = { | 223 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 121 TitleURL("Site 1", "https://www.site1.com/", "popular"), | 224 } |
| 122 TitleURL("Site 2", "https://www.site2.com/", "popular"), | |
| 123 TitleURL("Site 3", "https://www.site3.com/", "popular"), | |
| 124 }; | |
| 125 TitleURL personal[] = { | |
| 126 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"), | |
| 127 }; | |
| 128 // When a personal suggestions matches the host of a popular one, it should | |
| 129 // overwrite that suggestion (in its original position). | |
| 130 TitleURL expected[] = { | |
| 131 TitleURL("Site 1", "https://www.site1.com/", "popular"), | |
| 132 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"), | |
| 133 TitleURL("Site 3", "https://www.site3.com/", "popular"), | |
| 134 }; | |
| 135 | |
| 136 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 137 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 138 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 139 } | |
| 140 | |
| 141 TEST_F(MostVisitedSitesTest, PopularSitesOrdering) { | |
| 142 TitleURL popular[] = { | |
| 143 TitleURL("Site 1", "https://www.site1.com/", "popular"), | |
| 144 TitleURL("Site 2", "https://www.site2.com/", "popular"), | |
| 145 TitleURL("Site 3", "https://www.site3.com/", "popular"), | |
| 146 TitleURL("Site 4", "https://www.site4.com/", "popular"), | |
| 147 }; | |
| 148 TitleURL personal[] = { | |
| 149 TitleURL("Site 3", "https://www.site3.com/", "server8"), | |
| 150 TitleURL("Site 4", "https://www.site4.com/", "server8"), | |
| 151 TitleURL("Site 1", "https://www.site1.com/", "server8"), | |
| 152 TitleURL("Site 2", "https://www.site2.com/", "server8"), | |
| 153 }; | |
| 154 // The personal sites should replace the popular ones, but the order of the | |
| 155 // popular sites should win (since presumably they were there first). | |
| 156 TitleURL expected[] = { | |
| 157 TitleURL("Site 1", "https://www.site1.com/", "server8"), | |
| 158 TitleURL("Site 2", "https://www.site2.com/", "server8"), | |
| 159 TitleURL("Site 3", "https://www.site3.com/", "server8"), | |
| 160 TitleURL("Site 4", "https://www.site4.com/", "server8"), | |
| 161 }; | |
| 162 | |
| 163 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 164 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 165 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 166 } | |
| 167 | |
| 168 TEST_F(MostVisitedSitesTest, PopularSitesComplex) { | |
| 169 TitleURL popular[] = { | |
| 170 TitleURL("Site 1", "https://www.site1.com/", "popular"), | |
| 171 TitleURL("Site 2", "https://www.site2.com/", "popular"), | |
| 172 TitleURL("Site 3", "https://www.site3.com/", "popular"), | |
| 173 }; | |
| 174 TitleURL personal[] = { | |
| 175 TitleURL("Site 3 subpage", "https://www.site3.com/path", "server8"), | |
| 176 TitleURL("Site 1 subpage", "https://www.site1.com/path", "server8"), | |
| 177 TitleURL("Site 5", "https://www.site5.com/", "server8"), | |
| 178 TitleURL("Site 6", "https://www.site6.com/", "server8"), | |
| 179 }; | |
| 180 // Combination of behaviors: Personal suggestions replace matching popular | |
| 181 // ones while keeping the position of the popular suggestion. Remaining | |
| 182 // personal suggestions evict popular ones and retain their relative order. | |
| 183 TitleURL expected[] = { | |
| 184 TitleURL("Site 1 subpage", "https://www.site1.com/path", "server8"), | |
| 185 TitleURL("Site 5", "https://www.site5.com/", "server8"), | |
| 186 TitleURL("Site 3 subpage", "https://www.site3.com/path", "server8"), | |
| 187 TitleURL("Site 6", "https://www.site6.com/", "server8"), | |
| 188 }; | |
| 189 | |
| 190 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | |
| 191 std::vector<TitleURL>(personal, personal + arraysize(personal)), | |
| 192 std::vector<TitleURL>(expected, expected + arraysize(expected))); | |
| 193 } | |
| OLD | NEW |