Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: chrome/browser/android/most_visited_sites_unittest.cc

Issue 1330773002: [Android] Persist ordering of NTP suggestions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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)
18 : title(base::UTF8ToUTF16(title)), url(url), source(source) {}
19 17
20 base::string16 title; 18 base::string16 title;
21 std::string url; 19 std::string url;
22 std::string source;
23 }; 20 };
24 21
25 std::vector<base::string16> GetTitles(const std::vector<TitleURL>& data) { 22 std::vector<base::string16> GetTitles(const std::vector<TitleURL>& data) {
26 std::vector<base::string16> titles; 23 std::vector<base::string16> titles;
27 for (const TitleURL& item : data) 24 for (const TitleURL& item : data)
28 titles.push_back(item.title); 25 titles.push_back(item.title);
29 return titles; 26 return titles;
30 } 27 }
31 28
32 std::vector<std::string> GetURLs(const std::vector<TitleURL>& data) { 29 std::vector<std::string> GetURLs(const std::vector<TitleURL>& data) {
33 std::vector<std::string> urls; 30 std::vector<std::string> urls;
34 for (const TitleURL& item : data) 31 for (const TitleURL& item : data)
35 urls.push_back(item.url); 32 urls.push_back(item.url);
36 return urls; 33 return urls;
37 } 34 }
38 35
39 std::vector<std::string> GetSources(const std::vector<TitleURL>& data) { 36 std::vector<std::string> GetHosts(const std::vector<TitleURL>& data) {
40 std::vector<std::string> sources; 37 std::vector<std::string> hosts;
41 for (const TitleURL& item : data) 38 for (const TitleURL& item : data)
42 sources.push_back(item.source); 39 hosts.emplace_back(GURL(item.url).host());
43 return sources; 40 return hosts;
44 } 41 }
45 42
46 static const int kNumSites = 4; 43 static const size_t kNumSites = 4;
47 44
48 } // namespace 45 } // namespace
49 46
47 // This a test for MostVisitedSites::MergeSuggestions(...) method, and thus has
48 // the same scope as the method itself. This includes:
49 // + Merge popular suggestions with personal suggestions.
50 // + Order the suggestions correctly based on the previous ordering.
51 // More importantly things out of the scope of testing presently:
52 // - Removing blacklisted suggestions.
Marc Treib 2015/09/07 11:13:00 Also, blacklisting suggestions in the first place.
knn 2015/09/08 10:48:56 Acknowledged.
53 // - Storing the current suggestion ordering.
54 // - Retrieving the previous ordering.
55 // - Correct Host extraction from the URL.
Marc Treib 2015/09/07 11:13:00 This is just a feature of GURL and (hopefully) tes
knn 2015/09/08 10:48:56 No that is below. Agreed it is tested elsewhere, j
56 // - Ensuring popular suggestions don't contain personal ones.
Marc Treib 2015/09/07 11:13:00 What does this mean?
knn 2015/09/08 10:48:56 This is duplicate elimination.
Marc Treib 2015/09/08 11:39:49 Then please say that :) I find the current comment
knn 2015/09/08 13:42:00 There shouldn't be overlap between the two right?
Marc Treib 2015/09/08 13:56:55 After MergeSuggestions, right. The comment IMO doe
knn 2015/09/08 14:17:32 I'm not sure I understand, there is no "after" for
Marc Treib 2015/09/08 14:24:27 Or maybe I'm missing something? What I mean is: Wh
50 class MostVisitedSitesTest : public testing::Test { 57 class MostVisitedSitesTest : public testing::Test {
51 protected: 58 protected:
52 void Check(const std::vector<TitleURL>& popular, 59 void Check(const std::vector<TitleURL>& popular_sites,
53 const std::vector<TitleURL>& personal, 60 const std::vector<TitleURL>& personal_sites,
54 const std::vector<TitleURL>& expected) { 61 const std::vector<std::string>& old_sites_url,
55 std::vector<base::string16> titles(GetTitles(personal)); 62 const std::vector<bool>& old_sites_source,
56 std::vector<std::string> urls(GetURLs(personal)); 63 const std::vector<bool>& expected_sites_source,
57 std::vector<std::string> sources(GetSources(personal)); 64 const std::vector<TitleURL>& expected_sites) {
65 std::vector<base::string16> personal_titles(GetTitles(personal_sites));
66 std::vector<std::string> personal_urls(GetURLs(personal_sites));
67 std::vector<std::string> personal_hosts(GetHosts(personal_sites));
58 68
59 std::vector<base::string16> popular_titles(GetTitles(popular)); 69 std::vector<base::string16> popular_titles(GetTitles(popular_sites));
60 std::vector<std::string> popular_urls(GetURLs(popular)); 70 std::vector<std::string> popular_urls(GetURLs(popular_sites));
71 std::vector<std::string> popular_hosts(GetHosts(popular_sites));
61 72
62 MostVisitedSites::AddPopularSitesImpl( 73 std::vector<bool> result_sources;
63 kNumSites, popular_titles, popular_urls, &titles, &urls, &sources); 74 std::vector<std::string> result_urls;
75 std::vector<base::string16> result_titles;
64 76
65 EXPECT_EQ(GetTitles(expected), titles); 77 MostVisitedSites::MergeSuggestions(
66 EXPECT_EQ(GetURLs(expected), urls); 78 &result_sources, &result_urls, &result_titles, kNumSites,
67 EXPECT_EQ(GetSources(expected), sources); 79 personal_sites.size(), popular_sites.size(), &personal_urls,
80 &personal_titles, &popular_urls, &popular_titles, personal_hosts,
81 popular_hosts, old_sites_url, old_sites_source);
82
83 EXPECT_EQ(GetTitles(expected_sites), result_titles);
84 EXPECT_EQ(GetURLs(expected_sites), result_urls);
85 EXPECT_EQ(expected_sites_source, result_sources);
68 } 86 }
69 }; 87 };
70 88
71 TEST_F(MostVisitedSitesTest, PopularSitesAppend) { 89 TEST_F(MostVisitedSitesTest, PersonalSitesDefaultOrder) {
72 TitleURL popular[] = { 90 TitleURL personal[] = {
73 TitleURL("Site 1", "https://www.site1.com/", "popular"), 91 TitleURL("Site 1", "https://www.site1.com/"),
74 TitleURL("Site 2", "https://www.site2.com/", "popular"), 92 TitleURL("Site 2", "https://www.site2.com/"),
93 TitleURL("Site 3", "https://www.site3.com/"),
94 TitleURL("Site 4", "https://www.site4.com/"),
75 }; 95 };
96 std::vector<TitleURL> personal_sites(personal,
97 personal + arraysize(personal));
98 std::vector<std::string> old_sites_url;
99 std::vector<bool> old_sites_source;
100 // Without any previous ordering or popular suggestions, the result after
101 // merge should be the personal suggestions themselves.
102 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/);
103 Check(std::vector<TitleURL>(), personal_sites, old_sites_url,
104 old_sites_source, expected_sites_source, personal_sites);
105 }
106
107 TEST_F(MostVisitedSitesTest, PersonalSitesDefinedOrder) {
76 TitleURL personal[] = { 108 TitleURL personal[] = {
77 TitleURL("Site 3", "https://www.site3.com/", "server8"), 109 TitleURL("Site 1", "https://www.site1.com/"),
78 TitleURL("Site 4", "https://www.site4.com/", "server8"), 110 TitleURL("Site 2", "https://www.site2.com/"),
111 TitleURL("Site 3", "https://www.site3.com/"),
112 TitleURL("Site 4", "https://www.site4.com/"),
79 }; 113 };
80 // Popular suggestions should keep their positions, with personal suggestions 114 std::string old[] = {
81 // appended at the end. 115 "https://www.site4.com/", "https://www.site2.com/",
116 };
117 std::vector<bool> old_sites_source(arraysize(old), true /*personal source*/);
82 TitleURL expected[] = { 118 TitleURL expected[] = {
83 TitleURL("Site 1", "https://www.site1.com/", "popular"), 119 TitleURL("Site 4", "https://www.site4.com/"),
84 TitleURL("Site 2", "https://www.site2.com/", "popular"), 120 TitleURL("Site 2", "https://www.site2.com/"),
85 TitleURL("Site 3", "https://www.site3.com/", "server8"), 121 TitleURL("Site 1", "https://www.site1.com/"),
86 TitleURL("Site 4", "https://www.site4.com/", "server8"), 122 TitleURL("Site 3", "https://www.site3.com/"),
87 }; 123 };
88 124 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/);
89 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), 125 Check(std::vector<TitleURL>(),
90 std::vector<TitleURL>(personal, personal + arraysize(personal)), 126 std::vector<TitleURL>(personal, personal + arraysize(personal)),
127 std::vector<std::string>(old, old + arraysize(old)), old_sites_source,
128 expected_sites_source,
91 std::vector<TitleURL>(expected, expected + arraysize(expected))); 129 std::vector<TitleURL>(expected, expected + arraysize(expected)));
92 } 130 }
93 131
94 TEST_F(MostVisitedSitesTest, PopularSitesOverflow) { 132 TEST_F(MostVisitedSitesTest, PopularSitesDefaultOrder) {
95 TitleURL popular[] = { 133 TitleURL popular[] = {
96 TitleURL("Site 1", "https://www.site1.com/", "popular"), 134 TitleURL("Site 1", "https://www.site1.com/"),
97 TitleURL("Site 2", "https://www.site2.com/", "popular"), 135 TitleURL("Site 2", "https://www.site2.com/"),
98 TitleURL("Site 3", "https://www.site3.com/", "popular"), 136 TitleURL("Site 3", "https://www.site3.com/"),
137 TitleURL("Site 4", "https://www.site4.com/"),
99 }; 138 };
100 TitleURL personal[] = { 139 std::vector<TitleURL> popular_sites(popular, popular + arraysize(popular));
101 TitleURL("Site 4", "https://www.site4.com/", "server8"), 140 std::vector<std::string> old_sites_url;
102 TitleURL("Site 5", "https://www.site5.com/", "server8"), 141 std::vector<bool> old_sites_source;
142 // Without any previous ordering or personal suggestions, the result after
143 // merge should be the popular suggestions themselves.
144 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/);
145 Check(popular_sites, std::vector<TitleURL>(), old_sites_url, old_sites_source,
146 expected_sites_source, popular_sites);
147 }
148
149 TEST_F(MostVisitedSitesTest, PopularSitesDefinedOrder) {
150 TitleURL popular[] = {
151 TitleURL("Site 1", "https://www.site1.com/"),
152 TitleURL("Site 2", "https://www.site2.com/"),
153 TitleURL("Site 3", "https://www.site3.com/"),
154 TitleURL("Site 4", "https://www.site4.com/"),
103 }; 155 };
104 // When there are more total suggestions than slots, the personal suggestions 156 std::string old[] = {
105 // should win, with the remaining popular suggestions still retaining their 157 "https://www.site4.com/", "https://www.site2.com/",
106 // positions. 158 };
159 std::vector<bool> old_sites_source(arraysize(old), false /*popular source*/);
107 TitleURL expected[] = { 160 TitleURL expected[] = {
108 TitleURL("Site 1", "https://www.site1.com/", "popular"), 161 TitleURL("Site 4", "https://www.site4.com/"),
109 TitleURL("Site 2", "https://www.site2.com/", "popular"), 162 TitleURL("Site 2", "https://www.site2.com/"),
110 TitleURL("Site 4", "https://www.site4.com/", "server8"), 163 TitleURL("Site 1", "https://www.site1.com/"),
111 TitleURL("Site 5", "https://www.site5.com/", "server8"), 164 TitleURL("Site 3", "https://www.site3.com/"),
112 }; 165 };
113 166 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/);
114 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), 167 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)),
115 std::vector<TitleURL>(personal, personal + arraysize(personal)), 168 std::vector<TitleURL>(),
169 std::vector<std::string>(old, old + arraysize(old)), old_sites_source,
170 expected_sites_source,
116 std::vector<TitleURL>(expected, expected + arraysize(expected))); 171 std::vector<TitleURL>(expected, expected + arraysize(expected)));
117 } 172 }
118 173
119 TEST_F(MostVisitedSitesTest, PopularSitesOverwrite) { 174 TEST_F(MostVisitedSitesTest, PopularAndPersonalDefaultOrder) {
120 TitleURL popular[] = { 175 TitleURL popular[] = {
121 TitleURL("Site 1", "https://www.site1.com/", "popular"), 176 TitleURL("Site 1", "https://www.site1.com/"),
122 TitleURL("Site 2", "https://www.site2.com/", "popular"), 177 TitleURL("Site 2", "https://www.site2.com/"),
123 TitleURL("Site 3", "https://www.site3.com/", "popular"),
124 }; 178 };
125 TitleURL personal[] = { 179 TitleURL personal[] = {
126 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"), 180 TitleURL("Site 3", "https://www.site3.com/"),
181 TitleURL("Site 4", "https://www.site4.com/"),
127 }; 182 };
128 // When a personal suggestions matches the host of a popular one, it should 183 // Without an explicit ordering, personal suggestions precede popular
129 // overwrite that suggestion (in its original position). 184 // suggestions.
130 TitleURL expected[] = { 185 TitleURL expected[] = {
131 TitleURL("Site 1", "https://www.site1.com/", "popular"), 186 TitleURL("Site 3", "https://www.site3.com/"),
132 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"), 187 TitleURL("Site 4", "https://www.site4.com/"),
133 TitleURL("Site 3", "https://www.site3.com/", "popular"), 188 TitleURL("Site 1", "https://www.site1.com/"),
189 TitleURL("Site 2", "https://www.site2.com/"),
134 }; 190 };
135 191 bool expected_source_is_personal[] = {true, true, false, false};
136 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), 192 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)),
137 std::vector<TitleURL>(personal, personal + arraysize(personal)), 193 std::vector<TitleURL>(personal, personal + arraysize(personal)),
194 std::vector<std::string>(), std::vector<bool>(),
195 std::vector<bool>(expected_source_is_personal,
196 expected_source_is_personal +
197 arraysize(expected_source_is_personal)),
138 std::vector<TitleURL>(expected, expected + arraysize(expected))); 198 std::vector<TitleURL>(expected, expected + arraysize(expected)));
139 } 199 }
140 200
141 TEST_F(MostVisitedSitesTest, PopularSitesOrdering) { 201 TEST_F(MostVisitedSitesTest, PopularAndPersonalDefinedOrder) {
142 TitleURL popular[] = { 202 TitleURL popular[] = {
143 TitleURL("Site 1", "https://www.site1.com/", "popular"), 203 TitleURL("Site 1", "https://www.site1.com/"),
144 TitleURL("Site 2", "https://www.site2.com/", "popular"), 204 TitleURL("Site 2", "https://www.site2.com/"),
145 TitleURL("Site 3", "https://www.site3.com/", "popular"),
146 TitleURL("Site 4", "https://www.site4.com/", "popular"),
147 }; 205 };
148 TitleURL personal[] = { 206 TitleURL personal[] = {
149 TitleURL("Site 3", "https://www.site3.com/", "server8"), 207 TitleURL("Site 3", "https://www.site3.com/"),
150 TitleURL("Site 4", "https://www.site4.com/", "server8"), 208 TitleURL("Site 4", "https://www.site4.com/"),
151 TitleURL("Site 1", "https://www.site1.com/", "server8"),
152 TitleURL("Site 2", "https://www.site2.com/", "server8"),
153 }; 209 };
154 // The personal sites should replace the popular ones, but the order of the 210 std::string old[] = {
155 // popular sites should win (since presumably they were there first). 211 "https://www.site2.com/", "https://www.unknownsite.com/",
212 "https://www.site4.com/",
213 };
214 std::vector<bool> old_sites_source(arraysize(old), false /*popular source*/);
215 // Keep the order constant for previous suggestions, else personal suggestions
216 // precede popular suggestions.
156 TitleURL expected[] = { 217 TitleURL expected[] = {
157 TitleURL("Site 1", "https://www.site1.com/", "server8"), 218 TitleURL("Site 2", "https://www.site2.com/"),
158 TitleURL("Site 2", "https://www.site2.com/", "server8"), 219 TitleURL("Site 3", "https://www.site3.com/"),
159 TitleURL("Site 3", "https://www.site3.com/", "server8"), 220 TitleURL("Site 4", "https://www.site4.com/"),
160 TitleURL("Site 4", "https://www.site4.com/", "server8"), 221 TitleURL("Site 1", "https://www.site1.com/"),
161 }; 222 };
162 223 bool expected_source_is_personal[] = {false, true, true, false};
163 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), 224 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)),
164 std::vector<TitleURL>(personal, personal + arraysize(personal)), 225 std::vector<TitleURL>(personal, personal + arraysize(personal)),
226 std::vector<std::string>(old, old + arraysize(old)), old_sites_source,
227 std::vector<bool>(expected_source_is_personal,
228 expected_source_is_personal +
229 arraysize(expected_source_is_personal)),
165 std::vector<TitleURL>(expected, expected + arraysize(expected))); 230 std::vector<TitleURL>(expected, expected + arraysize(expected)));
166 } 231 }
167
168 TEST_F(MostVisitedSitesTest, PopularSitesComplex) {
Marc Treib 2015/09/07 11:13:00 Why did you remove this test? I don't think your c
Marc Treib 2015/09/09 12:15:01 I think this hasn't been addressed yet - do we hav
knn 2015/09/09 13:54:30 Oops missed this. I did not remove any particular
Marc Treib 2015/09/09 14:00:47 Ah, right. Okay then, I guess.
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698