| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/android/ntp/most_visited_sites.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 struct TitleURL { | |
| 20 TitleURL(const std::string& title, const std::string& url) | |
| 21 : title(base::UTF8ToUTF16(title)), url(url) {} | |
| 22 TitleURL(const base::string16& title, const std::string& url) | |
| 23 : title(title), url(url) {} | |
| 24 | |
| 25 base::string16 title; | |
| 26 std::string url; | |
| 27 | |
| 28 bool operator==(const TitleURL& other) const { | |
| 29 return title == other.title && url == other.url; | |
| 30 } | |
| 31 }; | |
| 32 | |
| 33 static const size_t kNumSites = 4; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 // This a test for MostVisitedSites::MergeSuggestions(...) method, and thus has | |
| 38 // the same scope as the method itself. This tests merging popular suggestions | |
| 39 // with personal suggestions. | |
| 40 // More important things out of the scope of testing presently: | |
| 41 // - Removing blacklisted suggestions. | |
| 42 // - Correct host extraction from the URL. | |
| 43 // - Ensuring personal suggestions are not duplicated in popular suggestions. | |
| 44 class MostVisitedSitesTest : public testing::Test { | |
| 45 protected: | |
| 46 void Check(const std::vector<TitleURL>& popular_sites, | |
| 47 const std::vector<TitleURL>& whitelist_entry_points, | |
| 48 const std::vector<TitleURL>& personal_sites, | |
| 49 const std::vector<bool>& expected_sites_is_personal, | |
| 50 const std::vector<TitleURL>& expected_sites) { | |
| 51 MostVisitedSites::SuggestionsPtrVector personal_suggestions; | |
| 52 for (const TitleURL& site : personal_sites) | |
| 53 personal_suggestions.push_back(MakeSuggestionFrom(site, true, false)); | |
| 54 MostVisitedSites::SuggestionsPtrVector whitelist_suggestions; | |
| 55 for (const TitleURL& site : whitelist_entry_points) | |
| 56 whitelist_suggestions.push_back(MakeSuggestionFrom(site, false, true)); | |
| 57 MostVisitedSites::SuggestionsPtrVector popular_suggestions; | |
| 58 for (const TitleURL& site : popular_sites) | |
| 59 popular_suggestions.push_back(MakeSuggestionFrom(site, false, false)); | |
| 60 MostVisitedSites::SuggestionsPtrVector result_suggestions = | |
| 61 MostVisitedSites::MergeSuggestions(&personal_suggestions, | |
| 62 &whitelist_suggestions, | |
| 63 &popular_suggestions); | |
| 64 std::vector<TitleURL> result_sites; | |
| 65 std::vector<bool> result_is_personal; | |
| 66 result_sites.reserve(result_suggestions.size()); | |
| 67 result_is_personal.reserve(result_suggestions.size()); | |
| 68 for (const auto& suggestion : result_suggestions) { | |
| 69 result_sites.push_back( | |
| 70 TitleURL(suggestion->title, suggestion->url.spec())); | |
| 71 result_is_personal.push_back(suggestion->source != | |
| 72 MostVisitedSites::POPULAR); | |
| 73 } | |
| 74 EXPECT_EQ(expected_sites_is_personal, result_is_personal); | |
| 75 EXPECT_EQ(expected_sites, result_sites); | |
| 76 } | |
| 77 static std::unique_ptr<MostVisitedSites::Suggestion> MakeSuggestionFrom( | |
| 78 const TitleURL& title_url, | |
| 79 bool is_personal, | |
| 80 bool whitelist) { | |
| 81 std::unique_ptr<MostVisitedSites::Suggestion> suggestion = | |
| 82 base::WrapUnique(new MostVisitedSites::Suggestion()); | |
| 83 suggestion->title = title_url.title; | |
| 84 suggestion->url = GURL(title_url.url); | |
| 85 suggestion->source = whitelist ? MostVisitedSites::WHITELIST | |
| 86 : (is_personal ? MostVisitedSites::TOP_SITES | |
| 87 : MostVisitedSites::POPULAR); | |
| 88 return suggestion; | |
| 89 } | |
| 90 }; | |
| 91 | |
| 92 TEST_F(MostVisitedSitesTest, PersonalSites) { | |
| 93 std::vector<TitleURL> personal_sites{ | |
| 94 TitleURL("Site 1", "https://www.site1.com/"), | |
| 95 TitleURL("Site 2", "https://www.site2.com/"), | |
| 96 TitleURL("Site 3", "https://www.site3.com/"), | |
| 97 TitleURL("Site 4", "https://www.site4.com/"), | |
| 98 }; | |
| 99 // Without any popular suggestions, the result after merge should be the | |
| 100 // personal suggestions. | |
| 101 std::vector<bool> expected_sites_source(kNumSites, true /*personal source*/); | |
| 102 Check(std::vector<TitleURL>(), std::vector<TitleURL>(), personal_sites, | |
| 103 expected_sites_source, personal_sites); | |
| 104 } | |
| 105 | |
| 106 TEST_F(MostVisitedSitesTest, PopularSites) { | |
| 107 std::vector<TitleURL> popular_sites{ | |
| 108 TitleURL("Site 1", "https://www.site1.com/"), | |
| 109 TitleURL("Site 2", "https://www.site2.com/"), | |
| 110 TitleURL("Site 3", "https://www.site3.com/"), | |
| 111 TitleURL("Site 4", "https://www.site4.com/"), | |
| 112 }; | |
| 113 // Without any personal suggestions, the result after merge should be the | |
| 114 // popular suggestions. | |
| 115 std::vector<bool> expected_sites_source(kNumSites, false /*popular source*/); | |
| 116 Check(popular_sites, std::vector<TitleURL>(), std::vector<TitleURL>(), | |
| 117 expected_sites_source, popular_sites); | |
| 118 } | |
| 119 | |
| 120 TEST_F(MostVisitedSitesTest, PersonalPrecedePopularSites) { | |
| 121 std::vector<TitleURL> popular_sites{ | |
| 122 TitleURL("Site 1", "https://www.site1.com/"), | |
| 123 TitleURL("Site 2", "https://www.site2.com/"), | |
| 124 }; | |
| 125 std::vector<TitleURL> personal_sites{ | |
| 126 TitleURL("Site 3", "https://www.site3.com/"), | |
| 127 TitleURL("Site 4", "https://www.site4.com/"), | |
| 128 }; | |
| 129 // Personal suggestions should precede popular suggestions. | |
| 130 std::vector<TitleURL> expected_sites{ | |
| 131 TitleURL("Site 3", "https://www.site3.com/"), | |
| 132 TitleURL("Site 4", "https://www.site4.com/"), | |
| 133 TitleURL("Site 1", "https://www.site1.com/"), | |
| 134 TitleURL("Site 2", "https://www.site2.com/"), | |
| 135 }; | |
| 136 std::vector<bool> expected_sites_source{true, true, false, false}; | |
| 137 Check(popular_sites, std::vector<TitleURL>(), personal_sites, | |
| 138 expected_sites_source, expected_sites); | |
| 139 } | |
| OLD | NEW |