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

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

Powered by Google App Engine
This is Rietveld 408576698