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.
7 #include "base/macros.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/android/most_visited_sites.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 TitleURL(const std::string
& title
, const std::string
& url
)
17 : title(base::UTF8ToUTF16(title
)), url(url
) {}
18 TitleURL(const base::string16
& title
, const std::string
& url
)
19 : title(title
), url(url
) {}
24 bool operator==(const TitleURL
& other
) const {
25 return title
== other
.title
&& url
== other
.url
;
29 static const size_t kNumSites
= 4;
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.
43 class MostVisitedSitesTest
: public testing::Test
{
45 void Check(const std::vector
<TitleURL
>& popular_sites
,
46 const std::vector
<TitleURL
>& personal_sites
,
47 const std::vector
<std::string
>& old_sites_url
,
48 const std::vector
<bool>& old_sites_is_personal
,
49 const std::vector
<bool>& expected_sites_is_personal
,
50 const std::vector
<TitleURL
>& expected_sites
) {
51 ScopedVector
<MostVisitedSites::Suggestion
> personal_suggestions
;
52 personal_suggestions
.reserve(personal_sites
.size());
53 for (const TitleURL
& site
: personal_sites
)
54 personal_suggestions
.push_back(MakeSuggestionFrom(site
, true));
55 ScopedVector
<MostVisitedSites::Suggestion
> popular_suggestions
;
56 popular_suggestions
.reserve(popular_sites
.size());
57 for (const TitleURL
& site
: popular_sites
)
58 popular_suggestions
.push_back(MakeSuggestionFrom(site
, false));
59 ScopedVector
<MostVisitedSites::Suggestion
> result_suggestions
=
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
);
73 EXPECT_EQ(result_is_personal
, expected_sites_is_personal
);
74 EXPECT_EQ(result_sites
, expected_sites
);
76 static scoped_ptr
<MostVisitedSites::Suggestion
> MakeSuggestionFrom(
77 const TitleURL
& title_url
,
79 return make_scoped_ptr(new MostVisitedSites::Suggestion(
80 title_url
.title
, title_url
.url
,
81 is_personal
? MostVisitedSites::TOP_SITES
: MostVisitedSites::POPULAR
));
85 TEST_F(MostVisitedSitesTest
, PersonalSitesDefaultOrder
) {
86 TitleURL personal
[] = {
87 TitleURL("Site 1", "https://www.site1.com/"),
88 TitleURL("Site 2", "https://www.site2.com/"),
89 TitleURL("Site 3", "https://www.site3.com/"),
90 TitleURL("Site 4", "https://www.site4.com/"),
92 std::vector
<TitleURL
> personal_sites(personal
,
93 personal
+ arraysize(personal
));
94 std::vector
<std::string
> old_sites_url
;
95 std::vector
<bool> old_sites_source
;
96 // Without any previous ordering or popular suggestions, the result after
97 // merge should be the personal suggestions themselves.
98 std::vector
<bool> expected_sites_source(kNumSites
, true /*personal source*/);
99 Check(std::vector
<TitleURL
>(), personal_sites
, old_sites_url
,
100 old_sites_source
, expected_sites_source
, personal_sites
);
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/"),
110 std::string old
[] = {
111 "https://www.site4.com/", "https://www.site2.com/",
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/"),
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
)));
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/"),
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
);
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/"),
152 std::string old
[] = {
153 "https://www.site4.com/", "https://www.site2.com/",
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/"),
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
)));
170 TEST_F(MostVisitedSitesTest
, PopularAndPersonalDefaultOrder
) {
171 TitleURL popular
[] = {
172 TitleURL("Site 1", "https://www.site1.com/"),
173 TitleURL("Site 2", "https://www.site2.com/"),
175 TitleURL personal
[] = {
176 TitleURL("Site 3", "https://www.site3.com/"),
177 TitleURL("Site 4", "https://www.site4.com/"),
179 // Without an explicit ordering, personal suggestions precede popular
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/"),
187 bool expected_source_is_personal
[] = {true, true, false, false};
188 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
189 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
190 std::vector
<std::string
>(), std::vector
<bool>(),
191 std::vector
<bool>(expected_source_is_personal
,
192 expected_source_is_personal
+
193 arraysize(expected_source_is_personal
)),
194 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
197 TEST_F(MostVisitedSitesTest
, PopularAndPersonalDefinedOrder
) {
198 TitleURL popular
[] = {
199 TitleURL("Site 1", "https://www.site1.com/"),
200 TitleURL("Site 2", "https://www.site2.com/"),
202 TitleURL personal
[] = {
203 TitleURL("Site 3", "https://www.site3.com/"),
204 TitleURL("Site 4", "https://www.site4.com/"),
206 std::string old
[] = {
207 "https://www.site2.com/", "https://www.unknownsite.com/",
208 "https://www.site4.com/",
210 std::vector
<bool> old_sites_source(arraysize(old
), false /*popular source*/);
211 // Keep the order constant for previous suggestions, else personal suggestions
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/"),
219 bool expected_source_is_personal
[] = {false, true, true, false};
220 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
221 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
222 std::vector
<std::string
>(old
, old
+ arraysize(old
)), old_sites_source
,
223 std::vector
<bool>(expected_source_is_personal
,
224 expected_source_is_personal
+
225 arraysize(expected_source_is_personal
)),
226 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));