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

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

Issue 1972923002: MostVisitedSites: Remove unused order-persisting code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: one more TODO, and rebase Created 4 years, 7 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/ntp/most_visited_sites.cc ('k') | no next file » | 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 "chrome/browser/android/ntp/most_visited_sites.h" 5 #include "chrome/browser/android/ntp/most_visited_sites.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <vector> 10 #include <vector>
(...skipping 17 matching lines...) Expand all
28 bool operator==(const TitleURL& other) const { 28 bool operator==(const TitleURL& other) const {
29 return title == other.title && url == other.url; 29 return title == other.title && url == other.url;
30 } 30 }
31 }; 31 };
32 32
33 static const size_t kNumSites = 4; 33 static const size_t kNumSites = 4;
34 34
35 } // namespace 35 } // namespace
36 36
37 // This a test for MostVisitedSites::MergeSuggestions(...) method, and thus has 37 // This a test for MostVisitedSites::MergeSuggestions(...) method, and thus has
38 // the same scope as the method itself. This includes: 38 // the same scope as the method itself. This tests merging popular suggestions
39 // + Merge popular suggestions with personal suggestions. 39 // with personal suggestions.
40 // + Order the suggestions correctly based on the previous ordering. 40 // More important things out of the scope of testing presently:
41 // More importantly things out of the scope of testing presently:
42 // - Removing blacklisted suggestions. 41 // - Removing blacklisted suggestions.
43 // - Storing the current suggestion ordering. 42 // - Correct host extraction from the URL.
44 // - Retrieving the previous ordering.
45 // - Correct Host extraction from the URL.
46 // - Ensuring personal suggestions are not duplicated in popular suggestions. 43 // - Ensuring personal suggestions are not duplicated in popular suggestions.
47 class MostVisitedSitesTest : public testing::Test { 44 class MostVisitedSitesTest : public testing::Test {
48 protected: 45 protected:
49 void Check(const std::vector<TitleURL>& popular_sites, 46 void Check(const std::vector<TitleURL>& popular_sites,
50 const std::vector<TitleURL>& whitelist_entry_points, 47 const std::vector<TitleURL>& whitelist_entry_points,
51 const std::vector<TitleURL>& personal_sites, 48 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, 49 const std::vector<bool>& expected_sites_is_personal,
55 const std::vector<TitleURL>& expected_sites) { 50 const std::vector<TitleURL>& expected_sites) {
56 MostVisitedSites::SuggestionsPtrVector personal_suggestions; 51 MostVisitedSites::SuggestionsPtrVector personal_suggestions;
57 for (const TitleURL& site : personal_sites) 52 for (const TitleURL& site : personal_sites)
58 personal_suggestions.push_back(MakeSuggestionFrom(site, true, false)); 53 personal_suggestions.push_back(MakeSuggestionFrom(site, true, false));
59 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; 54 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions;
60 for (const TitleURL& site : whitelist_entry_points) 55 for (const TitleURL& site : whitelist_entry_points)
61 whitelist_suggestions.push_back(MakeSuggestionFrom(site, false, true)); 56 whitelist_suggestions.push_back(MakeSuggestionFrom(site, false, true));
62 MostVisitedSites::SuggestionsPtrVector popular_suggestions; 57 MostVisitedSites::SuggestionsPtrVector popular_suggestions;
63 for (const TitleURL& site : popular_sites) 58 for (const TitleURL& site : popular_sites)
64 popular_suggestions.push_back(MakeSuggestionFrom(site, false, false)); 59 popular_suggestions.push_back(MakeSuggestionFrom(site, false, false));
65 MostVisitedSites::SuggestionsPtrVector result_suggestions = 60 MostVisitedSites::SuggestionsPtrVector result_suggestions =
66 MostVisitedSites::MergeSuggestions( 61 MostVisitedSites::MergeSuggestions(&personal_suggestions,
67 &personal_suggestions, &whitelist_suggestions, &popular_suggestions, 62 &whitelist_suggestions,
68 old_sites_url, old_sites_is_personal); 63 &popular_suggestions);
69 std::vector<TitleURL> result_sites; 64 std::vector<TitleURL> result_sites;
70 std::vector<bool> result_is_personal; 65 std::vector<bool> result_is_personal;
71 result_sites.reserve(result_suggestions.size()); 66 result_sites.reserve(result_suggestions.size());
72 result_is_personal.reserve(result_suggestions.size()); 67 result_is_personal.reserve(result_suggestions.size());
73 for (const auto& suggestion : result_suggestions) { 68 for (const auto& suggestion : result_suggestions) {
74 result_sites.push_back( 69 result_sites.push_back(
75 TitleURL(suggestion->title, suggestion->url.spec())); 70 TitleURL(suggestion->title, suggestion->url.spec()));
76 result_is_personal.push_back(suggestion->source != 71 result_is_personal.push_back(suggestion->source !=
77 MostVisitedSites::POPULAR); 72 MostVisitedSites::POPULAR);
78 } 73 }
79 EXPECT_EQ(result_is_personal, expected_sites_is_personal); 74 EXPECT_EQ(expected_sites_is_personal, result_is_personal);
80 EXPECT_EQ(result_sites, expected_sites); 75 EXPECT_EQ(expected_sites, result_sites);
81 } 76 }
82 static std::unique_ptr<MostVisitedSites::Suggestion> MakeSuggestionFrom( 77 static std::unique_ptr<MostVisitedSites::Suggestion> MakeSuggestionFrom(
83 const TitleURL& title_url, 78 const TitleURL& title_url,
84 bool is_personal, 79 bool is_personal,
85 bool whitelist) { 80 bool whitelist) {
86 std::unique_ptr<MostVisitedSites::Suggestion> suggestion = 81 std::unique_ptr<MostVisitedSites::Suggestion> suggestion =
87 base::WrapUnique(new MostVisitedSites::Suggestion()); 82 base::WrapUnique(new MostVisitedSites::Suggestion());
88 suggestion->title = title_url.title; 83 suggestion->title = title_url.title;
89 suggestion->url = GURL(title_url.url); 84 suggestion->url = GURL(title_url.url);
90 suggestion->source = whitelist ? MostVisitedSites::WHITELIST 85 suggestion->source = whitelist ? MostVisitedSites::WHITELIST
91 : (is_personal ? MostVisitedSites::TOP_SITES 86 : (is_personal ? MostVisitedSites::TOP_SITES
92 : MostVisitedSites::POPULAR); 87 : MostVisitedSites::POPULAR);
93 return suggestion; 88 return suggestion;
94 } 89 }
95 }; 90 };
96 91
97 TEST_F(MostVisitedSitesTest, PersonalSitesDefaultOrder) { 92 TEST_F(MostVisitedSitesTest, PersonalSites) {
98 TitleURL personal[] = { 93 std::vector<TitleURL> personal_sites{
99 TitleURL("Site 1", "https://www.site1.com/"), 94 TitleURL("Site 1", "https://www.site1.com/"),
100 TitleURL("Site 2", "https://www.site2.com/"), 95 TitleURL("Site 2", "https://www.site2.com/"),
101 TitleURL("Site 3", "https://www.site3.com/"), 96 TitleURL("Site 3", "https://www.site3.com/"),
102 TitleURL("Site 4", "https://www.site4.com/"), 97 TitleURL("Site 4", "https://www.site4.com/"),
103 }; 98 };
104 std::vector<TitleURL> personal_sites(personal, 99 // Without any popular suggestions, the result after merge should be the
105 personal + arraysize(personal)); 100 // personal suggestions.
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*/); 101 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/);
111 Check(std::vector<TitleURL>(), std::vector<TitleURL>(), personal_sites, 102 Check(std::vector<TitleURL>(), std::vector<TitleURL>(), personal_sites,
112 old_sites_url, old_sites_source, expected_sites_source, personal_sites); 103 expected_sites_source, personal_sites);
113 } 104 }
114 105
115 TEST_F(MostVisitedSitesTest, PersonalSitesDefinedOrder) { 106 TEST_F(MostVisitedSitesTest, PopularSites) {
116 TitleURL personal[] = { 107 std::vector<TitleURL> popular_sites{
117 TitleURL("Site 1", "https://www.site1.com/"), 108 TitleURL("Site 1", "https://www.site1.com/"),
118 TitleURL("Site 2", "https://www.site2.com/"), 109 TitleURL("Site 2", "https://www.site2.com/"),
119 TitleURL("Site 3", "https://www.site3.com/"), 110 TitleURL("Site 3", "https://www.site3.com/"),
120 TitleURL("Site 4", "https://www.site4.com/"), 111 TitleURL("Site 4", "https://www.site4.com/"),
121 }; 112 };
122 std::string old[] = { 113 // Without any personal suggestions, the result after merge should be the
123 "https://www.site4.com/", "https://www.site2.com/", 114 // popular suggestions.
124 }; 115 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/);
125 std::vector<bool> old_sites_source(arraysize(old), true /*personal source*/); 116 Check(popular_sites, std::vector<TitleURL>(), std::vector<TitleURL>(),
126 TitleURL expected[] = { 117 expected_sites_source, popular_sites);
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 } 118 }
139 119
140 TEST_F(MostVisitedSitesTest, PopularSitesDefaultOrder) { 120 TEST_F(MostVisitedSitesTest, PersonalPrecedePopularSites) {
141 TitleURL popular[] = { 121 std::vector<TitleURL> popular_sites{
142 TitleURL("Site 1", "https://www.site1.com/"), 122 TitleURL("Site 1", "https://www.site1.com/"),
143 TitleURL("Site 2", "https://www.site2.com/"), 123 TitleURL("Site 2", "https://www.site2.com/"),
124 };
125 std::vector<TitleURL> personal_sites{
144 TitleURL("Site 3", "https://www.site3.com/"), 126 TitleURL("Site 3", "https://www.site3.com/"),
145 TitleURL("Site 4", "https://www.site4.com/"), 127 TitleURL("Site 4", "https://www.site4.com/"),
146 }; 128 };
147 std::vector<TitleURL> popular_sites(popular, popular + arraysize(popular)); 129 // Personal suggestions should precede popular suggestions.
148 std::vector<std::string> old_sites_url; 130 std::vector<TitleURL> expected_sites{
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/"), 131 TitleURL("Site 3", "https://www.site3.com/"),
195 TitleURL("Site 4", "https://www.site4.com/"), 132 TitleURL("Site 4", "https://www.site4.com/"),
196 TitleURL("Site 1", "https://www.site1.com/"), 133 TitleURL("Site 1", "https://www.site1.com/"),
197 TitleURL("Site 2", "https://www.site2.com/"), 134 TitleURL("Site 2", "https://www.site2.com/"),
198 }; 135 };
199 bool expected_source_is_personal[] = {true, true, false, false}; 136 std::vector<bool> expected_sites_source{true, true, false, false};
200 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), 137 Check(popular_sites, std::vector<TitleURL>(), personal_sites,
201 std::vector<TitleURL>(), 138 expected_sites_source, expected_sites);
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 } 139 }
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 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/most_visited_sites.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698