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
, const std::string
& url
)
16 : title(base::UTF8ToUTF16(title
)), url(url
) {}
22 std::vector
<base::string16
> GetTitles(const std::vector
<TitleURL
>& data
) {
23 std::vector
<base::string16
> titles
;
24 for (const TitleURL
& item
: data
)
25 titles
.push_back(item
.title
);
29 std::vector
<std::string
> GetURLs(const std::vector
<TitleURL
>& data
) {
30 std::vector
<std::string
> urls
;
31 for (const TitleURL
& item
: data
)
32 urls
.push_back(item
.url
);
36 static const int kNumSites
= 4;
40 class MostVisitedSitesTest
: public testing::Test
{
42 void Check(const std::vector
<TitleURL
>& popular
,
43 const std::vector
<TitleURL
>& personal
,
44 const std::vector
<TitleURL
>& expected
) {
45 std::vector
<base::string16
> titles(GetTitles(personal
));
46 std::vector
<std::string
> urls(GetURLs(personal
));
48 std::vector
<base::string16
> popular_titles(GetTitles(popular
));
49 std::vector
<std::string
> popular_urls(GetURLs(popular
));
51 MostVisitedSites::AddPopularSitesImpl(
52 kNumSites
, &titles
, &urls
, popular_titles
, popular_urls
);
54 EXPECT_EQ(GetTitles(expected
), titles
);
55 EXPECT_EQ(GetURLs(expected
), urls
);
59 TEST_F(MostVisitedSitesTest
, PopularSitesAppend
) {
60 TitleURL popular
[] = {
61 TitleURL("Site 1", "https://www.site1.com/"),
62 TitleURL("Site 2", "https://www.site2.com/"),
64 TitleURL personal
[] = {
65 TitleURL("Site 3", "https://www.site3.com/"),
66 TitleURL("Site 4", "https://www.site4.com/"),
68 // Popular suggestions should keep their positions, with personal suggestions
69 // appended at the end.
70 TitleURL expected
[] = {
71 TitleURL("Site 1", "https://www.site1.com/"),
72 TitleURL("Site 2", "https://www.site2.com/"),
73 TitleURL("Site 3", "https://www.site3.com/"),
74 TitleURL("Site 4", "https://www.site4.com/"),
77 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
78 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
79 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
82 TEST_F(MostVisitedSitesTest
, PopularSitesOverflow
) {
83 TitleURL popular
[] = {
84 TitleURL("Site 1", "https://www.site1.com/"),
85 TitleURL("Site 2", "https://www.site2.com/"),
86 TitleURL("Site 3", "https://www.site3.com/"),
88 TitleURL personal
[] = {
89 TitleURL("Site 4", "https://www.site4.com/"),
90 TitleURL("Site 5", "https://www.site5.com/"),
92 // When there are more total suggestions than slots, the personal suggestions
93 // should win, with the remaining popular suggestions still retaining their
95 TitleURL expected
[] = {
96 TitleURL("Site 1", "https://www.site1.com/"),
97 TitleURL("Site 2", "https://www.site2.com/"),
98 TitleURL("Site 4", "https://www.site4.com/"),
99 TitleURL("Site 5", "https://www.site5.com/"),
102 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
103 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
104 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
107 TEST_F(MostVisitedSitesTest
, PopularSitesOverwrite
) {
108 TitleURL popular
[] = {
109 TitleURL("Site 1", "https://www.site1.com/"),
110 TitleURL("Site 2", "https://www.site2.com/"),
111 TitleURL("Site 3", "https://www.site3.com/"),
113 TitleURL personal
[] = {
114 TitleURL("Site 2 subpage", "https://www.site2.com/path"),
116 // When a personal suggestions matches the host of a popular one, it should
117 // overwrite that suggestion (in its original position).
118 TitleURL expected
[] = {
119 TitleURL("Site 1", "https://www.site1.com/"),
120 TitleURL("Site 2 subpage", "https://www.site2.com/path"),
121 TitleURL("Site 3", "https://www.site3.com/"),
124 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
125 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
126 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
129 TEST_F(MostVisitedSitesTest
, PopularSitesOrdering
) {
130 TitleURL popular
[] = {
131 TitleURL("Site 1", "https://www.site1.com/"),
132 TitleURL("Site 2", "https://www.site2.com/"),
133 TitleURL("Site 3", "https://www.site3.com/"),
134 TitleURL("Site 4", "https://www.site4.com/"),
136 TitleURL personal
[] = {
137 TitleURL("Site 3", "https://www.site3.com/"),
138 TitleURL("Site 4", "https://www.site4.com/"),
139 TitleURL("Site 1", "https://www.site1.com/"),
140 TitleURL("Site 2", "https://www.site2.com/"),
142 // The order of the popular sites should win (since presumably they were
144 TitleURL expected
[] = {
145 TitleURL("Site 1", "https://www.site1.com/"),
146 TitleURL("Site 2", "https://www.site2.com/"),
147 TitleURL("Site 3", "https://www.site3.com/"),
148 TitleURL("Site 4", "https://www.site4.com/"),
151 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
152 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
153 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));
156 TEST_F(MostVisitedSitesTest
, PopularSitesComplex
) {
157 TitleURL popular
[] = {
158 TitleURL("Site 1", "https://www.site1.com/"),
159 TitleURL("Site 2", "https://www.site2.com/"),
160 TitleURL("Site 3", "https://www.site3.com/"),
162 TitleURL personal
[] = {
163 TitleURL("Site 3 subpage", "https://www.site3.com/path"),
164 TitleURL("Site 1 subpage", "https://www.site1.com/path"),
165 TitleURL("Site 5", "https://www.site5.com/"),
166 TitleURL("Site 6", "https://www.site6.com/"),
168 // Combination of behaviors: Personal suggestions replace matching popular
169 // ones while keeping the position of the popular suggestion. Remaining
170 // personal suggestions evict popular ones and retain their relative order.
171 TitleURL expected
[] = {
172 TitleURL("Site 1 subpage", "https://www.site1.com/path"),
173 TitleURL("Site 5", "https://www.site5.com/"),
174 TitleURL("Site 3 subpage", "https://www.site3.com/path"),
175 TitleURL("Site 6", "https://www.site6.com/"),
178 Check(std::vector
<TitleURL
>(popular
, popular
+ arraysize(popular
)),
179 std::vector
<TitleURL
>(personal
, personal
+ arraysize(personal
)),
180 std::vector
<TitleURL
>(expected
, expected
+ arraysize(expected
)));