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
ash / quick_insert / model / quick_insert_link_suggester_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/quick_insert/model/quick_insert_link_suggester.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/favicon/core/test/mock_favicon_service.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/history/core/test/history_service_test_util.h"
namespace ash {
namespace {
using ::testing::Field;
class TestFaviconService : public favicon::MockFaviconService {
public:
TestFaviconService() = default;
TestFaviconService(const TestFaviconService&) = delete;
TestFaviconService& operator=(const TestFaviconService&) = delete;
~TestFaviconService() override = default;
// favicon::FaviconService:
base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL(
const GURL& page_url,
favicon_base::FaviconImageCallback callback,
base::CancelableTaskTracker* tracker) override {
page_url_ = page_url;
std::move(callback).Run(favicon_base::FaviconImageResult());
return {};
}
GURL page_url_;
};
class QuickInsertLinkSuggesterTest : public testing::Test {
public:
QuickInsertLinkSuggesterTest() {
CHECK(history_dir_.CreateUniqueTempDir());
history_service_ =
history::CreateHistoryService(history_dir_.GetPath(), true);
}
history::HistoryService* GetHistoryService() {
return history_service_.get();
}
private:
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir history_dir_;
std::unique_ptr<history::HistoryService> history_service_;
};
TEST_F(QuickInsertLinkSuggesterTest, GetSuggestedLinkResultsReturnsLinks) {
const base::Time now = base::Time::Now();
auto* history_service = GetHistoryService();
history_service->AddPageWithDetails(
GURL("http://a.com"), /*title=*/u"", /*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now - base::Seconds(1),
/*hidden=*/false, history::SOURCE_BROWSED);
history_service->AddPageWithDetails(
GURL("http://b.com"), /*title=*/u"", /*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now,
/*hidden=*/false, history::SOURCE_BROWSED);
history::BlockUntilHistoryProcessesPendingRequests(history_service);
TestFaviconService favicon_service;
QuickInsertLinkSuggester link_suggester;
base::test::TestFuture<std::vector<ash::QuickInsertSearchResult>> future;
link_suggester.GetSuggestedLinks(history_service, &favicon_service, 100u,
future.GetRepeatingCallback());
EXPECT_THAT(
future.Get(),
ElementsAre(VariantWith<ash::QuickInsertBrowsingHistoryResult>(
Field("url", &ash::QuickInsertBrowsingHistoryResult::url,
GURL("http://b.com"))),
VariantWith<ash::QuickInsertBrowsingHistoryResult>(
Field("url", &ash::QuickInsertBrowsingHistoryResult::url,
GURL("http://a.com")))));
EXPECT_EQ(favicon_service.page_url_, GURL("http://a.com"));
}
TEST_F(QuickInsertLinkSuggesterTest,
GetSuggestedLinkResultsAreTruncatedToMostRecent) {
const base::Time now = base::Time::Now();
auto* history_service = GetHistoryService();
history_service->AddPageWithDetails(
GURL("http://a.com"), /*title=*/u"", /*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now - base::Seconds(1),
/*hidden=*/false, history::SOURCE_BROWSED);
history_service->AddPageWithDetails(
GURL("http://b.com"), /*title=*/u"", /*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now,
/*hidden=*/false, history::SOURCE_BROWSED);
history::BlockUntilHistoryProcessesPendingRequests(history_service);
TestFaviconService favicon_service;
QuickInsertLinkSuggester link_suggester;
base::test::TestFuture<std::vector<ash::QuickInsertSearchResult>> future;
link_suggester.GetSuggestedLinks(history_service, &favicon_service, 1u,
future.GetRepeatingCallback());
EXPECT_THAT(future.Get(),
ElementsAre(VariantWith<ash::QuickInsertBrowsingHistoryResult>(
Field("url", &ash::QuickInsertBrowsingHistoryResult::url,
GURL("http://b.com")))));
EXPECT_EQ(favicon_service.page_url_, GURL("http://b.com"));
}
TEST_F(QuickInsertLinkSuggesterTest,
GetSuggestedLinkResultsFiltersOutPersonalizedLinks) {
const base::Time now = base::Time::Now();
auto* history_service = GetHistoryService();
history_service->AddPageWithDetails(
GURL("https://mail.google.com/mail/u/0/#inbox/aaa"), /*title=*/u"",
/*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now - base::Seconds(1),
/*hidden=*/false, history::SOURCE_BROWSED);
history_service->AddPageWithDetails(
GURL("https://mail.google.com/chat/u/0/#chat/aaa"), /*title=*/u"",
/*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now,
/*hidden=*/false, history::SOURCE_BROWSED);
history_service->AddPageWithDetails(
GURL("https://mail.google.com"), /*title=*/u"", /*visit_count=*/1,
/*typed_count=*/1,
/*last_visit=*/now,
/*hidden=*/false, history::SOURCE_BROWSED);
history::BlockUntilHistoryProcessesPendingRequests(history_service);
QuickInsertLinkSuggester link_suggester;
base::test::TestFuture<std::vector<ash::QuickInsertSearchResult>> future;
link_suggester.GetSuggestedLinks(history_service,
/*favicon_service=*/nullptr, 100u,
future.GetRepeatingCallback());
EXPECT_THAT(future.Get(),
ElementsAre(VariantWith<ash::QuickInsertBrowsingHistoryResult>(
Field("url", &ash::QuickInsertBrowsingHistoryResult::url,
GURL("https://mail.google.com")))));
}
} // namespace
} // namespace ash