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/strings/utf_string_conversions.h"
9 #include "chrome/browser/android/most_visited_sites.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 TitleURL(const std::string
& title
,
16 const std::string
& url
,
17 const std::string
& source
)
18 : title(base::UTF8ToUTF16(title
)), url(url
), source(source
) {}
25 std::vector
<base::string16
> GetTitles(const std::vector
<TitleURL
>& data
) {
26 std::vector
<base::string16
> titles
;
27 for (const TitleURL
& item
: data
)
28 titles
.push_back(item
.title
);
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
);
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
);
46 static const int kNumSites
= 4;
50 class MostVisitedSitesTest
: public testing::Test
{
52 void Check(const std::vector
<TitleURL
>& popular
,
53 const std::vector
<TitleURL
>& personal
,
54 const std::vector
<TitleURL
>& expected
) {
55 std::vector
<base::string16
> titles(GetTitles(personal
));
56 std::vector
<std::string
> urls(GetURLs(personal
));
57 std::vector
<std::string
> sources(GetSources(personal
));
59 std::vector
<base::string16
> popular_titles(GetTitles(popular
));
60 std::vector
<std::string
> popular_urls(GetURLs(popular
));
62 MostVisitedSites::AddPopularSitesImpl(
63 kNumSites
, popular_titles
, popular_urls
, &titles
, &urls
, &sources
);
65 EXPECT_EQ(GetTitles(expected
), titles
);
66 EXPECT_EQ(GetURLs(expected
), urls
);
67 EXPECT_EQ(GetSources(expected
), sources
);
71 TEST_F(MostVisitedSitesTest
, PopularSitesAppend
) {
72 TitleURL popular
[] = {
73 TitleURL("Site 1", "https://www.site1.com/", "popular"),
74 TitleURL("Site 2", "https://www.site2.com/", "popular"),
76 TitleURL personal
[] = {
77 TitleURL("Site 3", "https://www.site3.com/", "server8"),
78 TitleURL("Site 4", "https://www.site4.com/", "server8"),
80 // Popular suggestions should keep their positions, with personal suggestions
81 // appended at the end.
82 TitleURL expected
[] = {
83 TitleURL("Site 1", "https://www.site1.com/", "popular"),
84 TitleURL("Site 2", "https://www.site2.com/", "popular"),
85 TitleURL("Site 3", "https://www.site3.com/", "server8"),
86 TitleURL("Site 4", "https://www.site4.com/", "server8"),
89 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
90 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
91 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
94 TEST_F(MostVisitedSitesTest
, PopularSitesOverflow
) {
95 TitleURL popular
[] = {
96 TitleURL("Site 1", "https://www.site1.com/", "popular"),
97 TitleURL("Site 2", "https://www.site2.com/", "popular"),
98 TitleURL("Site 3", "https://www.site3.com/", "popular"),
100 TitleURL personal
[] = {
101 TitleURL("Site 4", "https://www.site4.com/", "server8"),
102 TitleURL("Site 5", "https://www.site5.com/", "server8"),
104 // When there are more total suggestions than slots, the personal suggestions
105 // should win, with the remaining popular suggestions still retaining their
107 TitleURL expected
[] = {
108 TitleURL("Site 1", "https://www.site1.com/", "popular"),
109 TitleURL("Site 2", "https://www.site2.com/", "popular"),
110 TitleURL("Site 4", "https://www.site4.com/", "server8"),
111 TitleURL("Site 5", "https://www.site5.com/", "server8"),
114 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
115 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
116 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
119 TEST_F(MostVisitedSitesTest
, PopularSitesOverwrite
) {
120 TitleURL popular
[] = {
121 TitleURL("Site 1", "https://www.site1.com/", "popular"),
122 TitleURL("Site 2", "https://www.site2.com/", "popular"),
123 TitleURL("Site 3", "https://www.site3.com/", "popular"),
125 TitleURL personal
[] = {
126 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"),
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"),
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
)));
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"),
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"),
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"),
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
)));
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"),
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"),
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"),
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
)));