1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/content_suggestions_provider.h"
#include "components/ntp_snippets/mock_content_suggestions_provider_observer.h"
#include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/offline_page_item.h"
#include "components/offline_pages/core/stub_offline_page_model.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ntp_snippets::test::CaptureDismissedSuggestions;
using ntp_snippets::test::FakeOfflinePageModel;
using offline_pages::ClientId;
using offline_pages::MultipleOfflinePageItemCallback;
using offline_pages::OfflinePageItem;
using offline_pages::StubOfflinePageModel;
using testing::_;
using testing::IsEmpty;
using testing::Mock;
using testing::Property;
using testing::SizeIs;
namespace ntp_snippets {
namespace {
OfflinePageItem CreateDummyRecentTab(int id) {
return test::CreateDummyOfflinePageItem(id, offline_pages::kLastNNamespace);
}
std::vector<OfflinePageItem> CreateDummyRecentTabs(
const std::vector<int>& ids) {
std::vector<OfflinePageItem> result;
for (int id : ids) {
result.push_back(CreateDummyRecentTab(id));
}
return result;
}
OfflinePageItem CreateDummyRecentTab(int id, base::Time time) {
OfflinePageItem item = CreateDummyRecentTab(id);
item.creation_time = time;
item.last_access_time = time;
return item;
}
} // namespace
class RecentTabSuggestionsProviderTest : public testing::Test {
public:
RecentTabSuggestionsProviderTest()
: pref_service_(new TestingPrefServiceSimple()) {
RecentTabSuggestionsProvider::RegisterProfilePrefs(
pref_service()->registry());
provider_.reset(
new RecentTabSuggestionsProvider(&observer_, &model_, pref_service()));
}
Category recent_tabs_category() {
return Category::FromKnownCategory(KnownCategories::RECENT_TABS);
}
ContentSuggestion::ID GetDummySuggestionId(int id) {
return ContentSuggestion::ID(recent_tabs_category(), base::IntToString(id));
}
void AddOfflinePageToModel(const OfflinePageItem& item) {
model_.mutable_items()->push_back(item);
provider_->OfflinePageAdded(&model_, item);
}
void FireOfflinePageDeleted(const OfflinePageItem& item) {
auto iter = std::remove(model_.mutable_items()->begin(),
model_.mutable_items()->end(), item);
auto end = model_.mutable_items()->end();
model_.mutable_items()->erase(iter, end);
provider_->OfflinePageDeleted(item.offline_id, item.client_id);
}
std::set<std::string> ReadDismissedIDsFromPrefs() {
return provider_->ReadDismissedIDsFromPrefs();
}
RecentTabSuggestionsProvider* provider() { return provider_.get(); }
FakeOfflinePageModel* model() { return &model_; }
MockContentSuggestionsProviderObserver* observer() { return &observer_; }
TestingPrefServiceSimple* pref_service() { return pref_service_.get(); }
private:
FakeOfflinePageModel model_;
MockContentSuggestionsProviderObserver observer_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
// Last so that the dependencies are deleted after the provider.
std::unique_ptr<RecentTabSuggestionsProvider> provider_;
DISALLOW_COPY_AND_ASSIGN(RecentTabSuggestionsProviderTest);
};
TEST_F(RecentTabSuggestionsProviderTest, ShouldConvertToSuggestions) {
auto recent_tabs_list = CreateDummyRecentTabs({1, 2});
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(2);
for (OfflinePageItem& recent_tab : recent_tabs_list)
AddOfflinePageToModel(recent_tab);
EXPECT_CALL(
*observer(),
OnNewSuggestions(_, recent_tabs_category(),
UnorderedElementsAre(
Property(&ContentSuggestion::url,
GURL("http://dummy.com/1")),
Property(&ContentSuggestion::url,
GURL("http://dummy.com/2")),
Property(&ContentSuggestion::url,
GURL("http://dummy.com/3")))));
AddOfflinePageToModel(CreateDummyRecentTab(3));
}
TEST_F(RecentTabSuggestionsProviderTest, ShouldSortByCreationTime) {
base::Time now = base::Time::Now();
base::Time yesterday = now - base::TimeDelta::FromDays(1);
base::Time tomorrow = now + base::TimeDelta::FromDays(1);
std::vector<OfflinePageItem> offline_pages = {
CreateDummyRecentTab(1, now), CreateDummyRecentTab(2, yesterday),
CreateDummyRecentTab(3, tomorrow)};
EXPECT_CALL(
*observer(),
OnNewSuggestions(_, recent_tabs_category(),
ElementsAre(Property(&ContentSuggestion::url,
GURL("http://dummy.com/1")))));
AddOfflinePageToModel(CreateDummyRecentTab(1, now));
EXPECT_CALL(
*observer(),
OnNewSuggestions(
_, recent_tabs_category(),
ElementsAre(
Property(&ContentSuggestion::url, GURL("http://dummy.com/1")),
Property(&ContentSuggestion::url, GURL("http://dummy.com/2")))));
AddOfflinePageToModel(CreateDummyRecentTab(2, yesterday));
offline_pages[1].last_access_time =
offline_pages[0].last_access_time + base::TimeDelta::FromHours(1);
EXPECT_CALL(
*observer(),
OnNewSuggestions(
_, recent_tabs_category(),
ElementsAre(Property(&ContentSuggestion::url,
GURL("http://dummy.com/3")),
Property(&ContentSuggestion::url,
GURL("http://dummy.com/1")),
Property(&ContentSuggestion::url,
GURL("http://dummy.com/2")))));
AddOfflinePageToModel(CreateDummyRecentTab(3, tomorrow));
}
TEST_F(RecentTabSuggestionsProviderTest, ShouldDeliverCorrectCategoryInfo) {
EXPECT_FALSE(
provider()->GetCategoryInfo(recent_tabs_category()).has_more_action());
EXPECT_FALSE(
provider()->GetCategoryInfo(recent_tabs_category()).has_reload_action());
EXPECT_FALSE(provider()
->GetCategoryInfo(recent_tabs_category())
.has_view_all_action());
}
// TODO(vitaliii): Break this test into multiple tests. Currently if it fails,
// it takes long time to find which part of it actually fails.
TEST_F(RecentTabSuggestionsProviderTest, ShouldDismiss) {
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(3);
auto recent_tabs_list = CreateDummyRecentTabs({1, 2, 3});
for (OfflinePageItem& recent_tab : recent_tabs_list) {
AddOfflinePageToModel(recent_tab);
}
// Dismiss 2 and 3.
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
provider()->DismissSuggestion(GetDummySuggestionId(2));
provider()->DismissSuggestion(GetDummySuggestionId(3));
Mock::VerifyAndClearExpectations(observer());
// They should disappear from the reported suggestions.
EXPECT_CALL(
*observer(),
OnNewSuggestions(
_, recent_tabs_category(),
UnorderedElementsAre(
Property(&ContentSuggestion::url, GURL("http://dummy.com/1")),
Property(&ContentSuggestion::url, GURL("http://dummy.com/4")))));
AddOfflinePageToModel(CreateDummyRecentTab(4));
Mock::VerifyAndClearExpectations(observer());
// And appear in the dismissed suggestions.
std::vector<ContentSuggestion> dismissed_suggestions;
provider()->GetDismissedSuggestionsForDebugging(
recent_tabs_category(),
base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions));
EXPECT_THAT(
dismissed_suggestions,
UnorderedElementsAre(Property(&ContentSuggestion::url,
GURL("http://dummy.com/2")),
Property(&ContentSuggestion::url,
GURL("http://dummy.com/3"))));
// Clear dismissed suggestions.
provider()->ClearDismissedSuggestionsForDebugging(recent_tabs_category());
// They should be gone from the dismissed suggestions.
dismissed_suggestions.clear();
provider()->GetDismissedSuggestionsForDebugging(
recent_tabs_category(),
base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions));
EXPECT_THAT(dismissed_suggestions, IsEmpty());
// And appear in the reported suggestions for the category again.
EXPECT_CALL(*observer(),
OnNewSuggestions(_, recent_tabs_category(), SizeIs(5)));
AddOfflinePageToModel(CreateDummyRecentTab(5));
Mock::VerifyAndClearExpectations(observer());
}
TEST_F(RecentTabSuggestionsProviderTest,
ShouldInvalidateWhenOfflinePageDeleted) {
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(3);
std::vector<OfflinePageItem> offline_pages = CreateDummyRecentTabs({1, 2, 3});
for (OfflinePageItem& recent_tab : offline_pages)
AddOfflinePageToModel(recent_tab);
// Invalidation of suggestion 2 should be forwarded.
EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, GetDummySuggestionId(2)));
FireOfflinePageDeleted(offline_pages[1]);
}
TEST_F(RecentTabSuggestionsProviderTest, ShouldClearDismissedOnInvalidate) {
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(3);
std::vector<OfflinePageItem> offline_pages = CreateDummyRecentTabs({1, 2, 3});
for (OfflinePageItem& recent_tab : offline_pages)
AddOfflinePageToModel(recent_tab);
EXPECT_THAT(ReadDismissedIDsFromPrefs(), IsEmpty());
provider()->DismissSuggestion(GetDummySuggestionId(2));
EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(1));
FireOfflinePageDeleted(offline_pages[1]);
EXPECT_THAT(ReadDismissedIDsFromPrefs(), IsEmpty());
}
TEST_F(RecentTabSuggestionsProviderTest, ShouldClearDismissedOnFetch) {
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(3);
std::vector<OfflinePageItem> offline_pages = CreateDummyRecentTabs({1, 2, 3});
for (OfflinePageItem& recent_tab : offline_pages)
AddOfflinePageToModel(recent_tab);
provider()->DismissSuggestion(GetDummySuggestionId(2));
provider()->DismissSuggestion(GetDummySuggestionId(3));
EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(2));
FireOfflinePageDeleted(offline_pages[0]);
FireOfflinePageDeleted(offline_pages[2]);
EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(1));
FireOfflinePageDeleted(offline_pages[1]);
EXPECT_THAT(ReadDismissedIDsFromPrefs(), IsEmpty());
}
TEST_F(RecentTabSuggestionsProviderTest, ShouldNotShowSameUrlMutlipleTimes) {
base::Time now = base::Time::Now();
base::Time yesterday = now - base::TimeDelta::FromDays(1);
base::Time tomorrow = now + base::TimeDelta::FromDays(1);
std::vector<OfflinePageItem> offline_pages = {
CreateDummyRecentTab(1, yesterday), CreateDummyRecentTab(2, now),
CreateDummyRecentTab(3, tomorrow)};
// We leave IDs different, but make the URLs the same.
offline_pages[2].url = offline_pages[0].url;
AddOfflinePageToModel(offline_pages[0]);
AddOfflinePageToModel(offline_pages[1]);
Mock::VerifyAndClearExpectations(observer());
EXPECT_CALL(*observer(),
OnNewSuggestions(
_, recent_tabs_category(),
UnorderedElementsAre(
Property(&ContentSuggestion::publish_date, now),
Property(&ContentSuggestion::publish_date, tomorrow))));
AddOfflinePageToModel(offline_pages[2]);
}
TEST_F(RecentTabSuggestionsProviderTest,
ShouldNotFetchIfAddedOfflinePageIsNotRecentTab) {
// The provider is not notified about the first recent tab yet.
model()->mutable_items()->push_back(CreateDummyRecentTab(1));
// It should not fetch when not a recent tab is added, thus, it should not
// report the first recent tab (which it is not aware about).
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
AddOfflinePageToModel(ntp_snippets::test::CreateDummyOfflinePageItem(
2, offline_pages::kDefaultNamespace));
}
TEST_F(RecentTabSuggestionsProviderTest,
ShouldFetchIfAddedOfflinePageIsRecentTab) {
// The provider is not notified about the first recent tab yet.
model()->mutable_items()->push_back(CreateDummyRecentTab(1));
// However, it must return the first recent tab (i.e. manually fetch it) even
// when notified about a different recent tab.
EXPECT_CALL(
*observer(),
OnNewSuggestions(
_, recent_tabs_category(),
UnorderedElementsAre(
Property(&ContentSuggestion::url, GURL("http://dummy.com/1")),
Property(&ContentSuggestion::url, GURL("http://dummy.com/2")))));
AddOfflinePageToModel(CreateDummyRecentTab(2));
}
} // namespace ntp_snippets
|