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

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

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

Powered by Google App Engine
This is Rietveld 408576698