| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/android/most_visited_sites.h" | 9 #include "chrome/browser/android/most_visited_sites.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 struct TitleURL { | 14 struct TitleURL { |
| 15 TitleURL(const std::string& title, const std::string& url) | 15 TitleURL(const std::string& title, |
| 16 : title(base::UTF8ToUTF16(title)), url(url) {} | 16 const std::string& url, |
| 17 const std::string& source) |
| 18 : title(base::UTF8ToUTF16(title)), url(url), source(source) {} |
| 17 | 19 |
| 18 base::string16 title; | 20 base::string16 title; |
| 19 std::string url; | 21 std::string url; |
| 22 std::string source; |
| 20 }; | 23 }; |
| 21 | 24 |
| 22 std::vector<base::string16> GetTitles(const std::vector<TitleURL>& data) { | 25 std::vector<base::string16> GetTitles(const std::vector<TitleURL>& data) { |
| 23 std::vector<base::string16> titles; | 26 std::vector<base::string16> titles; |
| 24 for (const TitleURL& item : data) | 27 for (const TitleURL& item : data) |
| 25 titles.push_back(item.title); | 28 titles.push_back(item.title); |
| 26 return titles; | 29 return titles; |
| 27 } | 30 } |
| 28 | 31 |
| 29 std::vector<std::string> GetURLs(const std::vector<TitleURL>& data) { | 32 std::vector<std::string> GetURLs(const std::vector<TitleURL>& data) { |
| 30 std::vector<std::string> urls; | 33 std::vector<std::string> urls; |
| 31 for (const TitleURL& item : data) | 34 for (const TitleURL& item : data) |
| 32 urls.push_back(item.url); | 35 urls.push_back(item.url); |
| 33 return urls; | 36 return urls; |
| 34 } | 37 } |
| 35 | 38 |
| 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); |
| 43 return sources; |
| 44 } |
| 45 |
| 36 static const int kNumSites = 4; | 46 static const int kNumSites = 4; |
| 37 | 47 |
| 38 } // namespace | 48 } // namespace |
| 39 | 49 |
| 40 class MostVisitedSitesTest : public testing::Test { | 50 class MostVisitedSitesTest : public testing::Test { |
| 41 protected: | 51 protected: |
| 42 void Check(const std::vector<TitleURL>& popular, | 52 void Check(const std::vector<TitleURL>& popular, |
| 43 const std::vector<TitleURL>& personal, | 53 const std::vector<TitleURL>& personal, |
| 44 const std::vector<TitleURL>& expected) { | 54 const std::vector<TitleURL>& expected) { |
| 45 std::vector<base::string16> titles(GetTitles(personal)); | 55 std::vector<base::string16> titles(GetTitles(personal)); |
| 46 std::vector<std::string> urls(GetURLs(personal)); | 56 std::vector<std::string> urls(GetURLs(personal)); |
| 57 std::vector<std::string> sources(GetSources(personal)); |
| 47 | 58 |
| 48 std::vector<base::string16> popular_titles(GetTitles(popular)); | 59 std::vector<base::string16> popular_titles(GetTitles(popular)); |
| 49 std::vector<std::string> popular_urls(GetURLs(popular)); | 60 std::vector<std::string> popular_urls(GetURLs(popular)); |
| 50 | 61 |
| 51 MostVisitedSites::AddPopularSitesImpl( | 62 MostVisitedSites::AddPopularSitesImpl( |
| 52 kNumSites, &titles, &urls, popular_titles, popular_urls); | 63 kNumSites, popular_titles, popular_urls, &titles, &urls, &sources); |
| 53 | 64 |
| 54 EXPECT_EQ(GetTitles(expected), titles); | 65 EXPECT_EQ(GetTitles(expected), titles); |
| 55 EXPECT_EQ(GetURLs(expected), urls); | 66 EXPECT_EQ(GetURLs(expected), urls); |
| 67 EXPECT_EQ(GetSources(expected), sources); |
| 56 } | 68 } |
| 57 }; | 69 }; |
| 58 | 70 |
| 59 TEST_F(MostVisitedSitesTest, PopularSitesAppend) { | 71 TEST_F(MostVisitedSitesTest, PopularSitesAppend) { |
| 60 TitleURL popular[] = { | 72 TitleURL popular[] = { |
| 61 TitleURL("Site 1", "https://www.site1.com/"), | 73 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 62 TitleURL("Site 2", "https://www.site2.com/"), | 74 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 63 }; | 75 }; |
| 64 TitleURL personal[] = { | 76 TitleURL personal[] = { |
| 65 TitleURL("Site 3", "https://www.site3.com/"), | 77 TitleURL("Site 3", "https://www.site3.com/", "server8"), |
| 66 TitleURL("Site 4", "https://www.site4.com/"), | 78 TitleURL("Site 4", "https://www.site4.com/", "server8"), |
| 67 }; | 79 }; |
| 68 // Popular suggestions should keep their positions, with personal suggestions | 80 // Popular suggestions should keep their positions, with personal suggestions |
| 69 // appended at the end. | 81 // appended at the end. |
| 70 TitleURL expected[] = { | 82 TitleURL expected[] = { |
| 71 TitleURL("Site 1", "https://www.site1.com/"), | 83 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 72 TitleURL("Site 2", "https://www.site2.com/"), | 84 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 73 TitleURL("Site 3", "https://www.site3.com/"), | 85 TitleURL("Site 3", "https://www.site3.com/", "server8"), |
| 74 TitleURL("Site 4", "https://www.site4.com/"), | 86 TitleURL("Site 4", "https://www.site4.com/", "server8"), |
| 75 }; | 87 }; |
| 76 | 88 |
| 77 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 89 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 78 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 90 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 79 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 91 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 80 } | 92 } |
| 81 | 93 |
| 82 TEST_F(MostVisitedSitesTest, PopularSitesOverflow) { | 94 TEST_F(MostVisitedSitesTest, PopularSitesOverflow) { |
| 83 TitleURL popular[] = { | 95 TitleURL popular[] = { |
| 84 TitleURL("Site 1", "https://www.site1.com/"), | 96 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 85 TitleURL("Site 2", "https://www.site2.com/"), | 97 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 86 TitleURL("Site 3", "https://www.site3.com/"), | 98 TitleURL("Site 3", "https://www.site3.com/", "popular"), |
| 87 }; | 99 }; |
| 88 TitleURL personal[] = { | 100 TitleURL personal[] = { |
| 89 TitleURL("Site 4", "https://www.site4.com/"), | 101 TitleURL("Site 4", "https://www.site4.com/", "server8"), |
| 90 TitleURL("Site 5", "https://www.site5.com/"), | 102 TitleURL("Site 5", "https://www.site5.com/", "server8"), |
| 91 }; | 103 }; |
| 92 // When there are more total suggestions than slots, the personal suggestions | 104 // When there are more total suggestions than slots, the personal suggestions |
| 93 // should win, with the remaining popular suggestions still retaining their | 105 // should win, with the remaining popular suggestions still retaining their |
| 94 // positions. | 106 // positions. |
| 95 TitleURL expected[] = { | 107 TitleURL expected[] = { |
| 96 TitleURL("Site 1", "https://www.site1.com/"), | 108 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 97 TitleURL("Site 2", "https://www.site2.com/"), | 109 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 98 TitleURL("Site 4", "https://www.site4.com/"), | 110 TitleURL("Site 4", "https://www.site4.com/", "server8"), |
| 99 TitleURL("Site 5", "https://www.site5.com/"), | 111 TitleURL("Site 5", "https://www.site5.com/", "server8"), |
| 100 }; | 112 }; |
| 101 | 113 |
| 102 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 114 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 103 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 115 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 104 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 116 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 105 } | 117 } |
| 106 | 118 |
| 107 TEST_F(MostVisitedSitesTest, PopularSitesOverwrite) { | 119 TEST_F(MostVisitedSitesTest, PopularSitesOverwrite) { |
| 108 TitleURL popular[] = { | 120 TitleURL popular[] = { |
| 109 TitleURL("Site 1", "https://www.site1.com/"), | 121 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 110 TitleURL("Site 2", "https://www.site2.com/"), | 122 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 111 TitleURL("Site 3", "https://www.site3.com/"), | 123 TitleURL("Site 3", "https://www.site3.com/", "popular"), |
| 112 }; | 124 }; |
| 113 TitleURL personal[] = { | 125 TitleURL personal[] = { |
| 114 TitleURL("Site 2 subpage", "https://www.site2.com/path"), | 126 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"), |
| 115 }; | 127 }; |
| 116 // When a personal suggestions matches the host of a popular one, it should | 128 // When a personal suggestions matches the host of a popular one, it should |
| 117 // overwrite that suggestion (in its original position). | 129 // overwrite that suggestion (in its original position). |
| 118 TitleURL expected[] = { | 130 TitleURL expected[] = { |
| 119 TitleURL("Site 1", "https://www.site1.com/"), | 131 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 120 TitleURL("Site 2 subpage", "https://www.site2.com/path"), | 132 TitleURL("Site 2 subpage", "https://www.site2.com/path", "server8"), |
| 121 TitleURL("Site 3", "https://www.site3.com/"), | 133 TitleURL("Site 3", "https://www.site3.com/", "popular"), |
| 122 }; | 134 }; |
| 123 | 135 |
| 124 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 136 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 125 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 137 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 126 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 138 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 127 } | 139 } |
| 128 | 140 |
| 129 TEST_F(MostVisitedSitesTest, PopularSitesOrdering) { | 141 TEST_F(MostVisitedSitesTest, PopularSitesOrdering) { |
| 130 TitleURL popular[] = { | 142 TitleURL popular[] = { |
| 131 TitleURL("Site 1", "https://www.site1.com/"), | 143 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 132 TitleURL("Site 2", "https://www.site2.com/"), | 144 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 133 TitleURL("Site 3", "https://www.site3.com/"), | 145 TitleURL("Site 3", "https://www.site3.com/", "popular"), |
| 134 TitleURL("Site 4", "https://www.site4.com/"), | 146 TitleURL("Site 4", "https://www.site4.com/", "popular"), |
| 135 }; | 147 }; |
| 136 TitleURL personal[] = { | 148 TitleURL personal[] = { |
| 137 TitleURL("Site 3", "https://www.site3.com/"), | 149 TitleURL("Site 3", "https://www.site3.com/", "server8"), |
| 138 TitleURL("Site 4", "https://www.site4.com/"), | 150 TitleURL("Site 4", "https://www.site4.com/", "server8"), |
| 139 TitleURL("Site 1", "https://www.site1.com/"), | 151 TitleURL("Site 1", "https://www.site1.com/", "server8"), |
| 140 TitleURL("Site 2", "https://www.site2.com/"), | 152 TitleURL("Site 2", "https://www.site2.com/", "server8"), |
| 141 }; | 153 }; |
| 142 // The order of the popular sites should win (since presumably they were | 154 // The personal sites should replace the popular ones, but the order of the |
| 143 // there first). | 155 // popular sites should win (since presumably they were there first). |
| 144 TitleURL expected[] = { | 156 TitleURL expected[] = { |
| 145 TitleURL("Site 1", "https://www.site1.com/"), | 157 TitleURL("Site 1", "https://www.site1.com/", "server8"), |
| 146 TitleURL("Site 2", "https://www.site2.com/"), | 158 TitleURL("Site 2", "https://www.site2.com/", "server8"), |
| 147 TitleURL("Site 3", "https://www.site3.com/"), | 159 TitleURL("Site 3", "https://www.site3.com/", "server8"), |
| 148 TitleURL("Site 4", "https://www.site4.com/"), | 160 TitleURL("Site 4", "https://www.site4.com/", "server8"), |
| 149 }; | 161 }; |
| 150 | 162 |
| 151 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 163 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 152 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 164 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 153 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 165 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 154 } | 166 } |
| 155 | 167 |
| 156 TEST_F(MostVisitedSitesTest, PopularSitesComplex) { | 168 TEST_F(MostVisitedSitesTest, PopularSitesComplex) { |
| 157 TitleURL popular[] = { | 169 TitleURL popular[] = { |
| 158 TitleURL("Site 1", "https://www.site1.com/"), | 170 TitleURL("Site 1", "https://www.site1.com/", "popular"), |
| 159 TitleURL("Site 2", "https://www.site2.com/"), | 171 TitleURL("Site 2", "https://www.site2.com/", "popular"), |
| 160 TitleURL("Site 3", "https://www.site3.com/"), | 172 TitleURL("Site 3", "https://www.site3.com/", "popular"), |
| 161 }; | 173 }; |
| 162 TitleURL personal[] = { | 174 TitleURL personal[] = { |
| 163 TitleURL("Site 3 subpage", "https://www.site3.com/path"), | 175 TitleURL("Site 3 subpage", "https://www.site3.com/path", "server8"), |
| 164 TitleURL("Site 1 subpage", "https://www.site1.com/path"), | 176 TitleURL("Site 1 subpage", "https://www.site1.com/path", "server8"), |
| 165 TitleURL("Site 5", "https://www.site5.com/"), | 177 TitleURL("Site 5", "https://www.site5.com/", "server8"), |
| 166 TitleURL("Site 6", "https://www.site6.com/"), | 178 TitleURL("Site 6", "https://www.site6.com/", "server8"), |
| 167 }; | 179 }; |
| 168 // Combination of behaviors: Personal suggestions replace matching popular | 180 // Combination of behaviors: Personal suggestions replace matching popular |
| 169 // ones while keeping the position of the popular suggestion. Remaining | 181 // ones while keeping the position of the popular suggestion. Remaining |
| 170 // personal suggestions evict popular ones and retain their relative order. | 182 // personal suggestions evict popular ones and retain their relative order. |
| 171 TitleURL expected[] = { | 183 TitleURL expected[] = { |
| 172 TitleURL("Site 1 subpage", "https://www.site1.com/path"), | 184 TitleURL("Site 1 subpage", "https://www.site1.com/path", "server8"), |
| 173 TitleURL("Site 5", "https://www.site5.com/"), | 185 TitleURL("Site 5", "https://www.site5.com/", "server8"), |
| 174 TitleURL("Site 3 subpage", "https://www.site3.com/path"), | 186 TitleURL("Site 3 subpage", "https://www.site3.com/path", "server8"), |
| 175 TitleURL("Site 6", "https://www.site6.com/"), | 187 TitleURL("Site 6", "https://www.site6.com/", "server8"), |
| 176 }; | 188 }; |
| 177 | 189 |
| 178 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), | 190 Check(std::vector<TitleURL>(popular, popular + arraysize(popular)), |
| 179 std::vector<TitleURL>(personal, personal + arraysize(personal)), | 191 std::vector<TitleURL>(personal, personal + arraysize(personal)), |
| 180 std::vector<TitleURL>(expected, expected + arraysize(expected))); | 192 std::vector<TitleURL>(expected, expected + arraysize(expected))); |
| 181 } | 193 } |
| OLD | NEW |