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