Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: components/offline_pages/offline_page_model_query_unittest.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: more rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "base/time/time.h"
6 #include "components/offline_pages/client_namespace_constants.h"
7 #include "components/offline_pages/client_policy_controller.h"
8 #include "components/offline_pages/offline_page_client_policy.h"
9 #include "components/offline_pages/offline_page_item.h"
10 #include "components/offline_pages/offline_page_model_query.h"
11
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace offline_pages {
15
16 using Requirement = OfflinePageModelQueryBuilder::Requirement;
17
18 namespace {
19
20 const ClientId kClientId1 = {kDefaultNamespace, "id1"};
21 const GURL kUrl1 = GURL("https://ktestitem1.com");
22 const OfflinePageItem kTestItem1(kUrl1, 1, kClientId1, base::FilePath(), 1);
23
24 const ClientId kClientId2 = {kDefaultNamespace, "id2"};
25 const GURL kUrl2 = GURL("https://ktestitem2.com");
26 const OfflinePageItem kTestItem2(kUrl2, 2, kClientId2, base::FilePath(), 2);
27
28 const char kTestNamespace[] = "test_namespace";
29 } // namespace
30
31 class OfflinePageModelQueryTest : public testing::Test {
32 public:
33 OfflinePageModelQueryTest();
34 ~OfflinePageModelQueryTest() override;
35
36 protected:
37 ClientPolicyController policy_;
38 OfflinePageModelQueryBuilder builder_;
39
40 const OfflinePageItem expired_page() {
41 OfflinePageItem expiredTestItem(GURL("https://ktestitem1.com"), 3,
42 kClientId1, base::FilePath(), 3);
43 expiredTestItem.expiration_time = base::Time::Now();
44
45 return expiredTestItem;
46 }
47
48 const OfflinePageItem download_page() {
49 return OfflinePageItem(GURL("https://download.com"), 4,
50 {kDownloadNamespace, "id1"}, base::FilePath(), 4);
51 }
52
53 const OfflinePageItem original_tab_page() {
54 return OfflinePageItem(GURL("https://download.com"), 5,
55 {kLastNNamespace, "id1"}, base::FilePath(), 5);
56 }
57
58 const OfflinePageItem test_namespace_page() {
59 return OfflinePageItem(GURL("https://download.com"), 6,
60 {kTestNamespace, "id1"}, base::FilePath(), 6);
61 }
62
63 const OfflinePageItem recent_page() {
64 return OfflinePageItem(GURL("https://download.com"), 7,
65 {kLastNNamespace, "id1"}, base::FilePath(), 7);
66 }
67 };
68
69 OfflinePageModelQueryTest::OfflinePageModelQueryTest() {
70 policy_.AddPolicyForTest(
71 kTestNamespace,
72 OfflinePageClientPolicyBuilder(kTestNamespace,
73 LifetimePolicy::LifetimeType::TEMPORARY,
74 kUnlimitedPages, kUnlimitedPages)
75 .SetIsOnlyShownInOriginalTab(true));
76 }
77
78 OfflinePageModelQueryTest::~OfflinePageModelQueryTest() {}
79
80 TEST_F(OfflinePageModelQueryTest, DefaultValues) {
81 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
82
83 EXPECT_NE(nullptr, query.get());
84 EXPECT_FALSE(query->GetAllowExpired());
85 EXPECT_EQ(Requirement::UNSET, query->GetRestrictedToOfflineIds().first);
86 EXPECT_FALSE(query->GetRestrictedToNamespaces().first);
87
88 EXPECT_TRUE(query->Matches(kTestItem1));
89 EXPECT_TRUE(query->Matches(kTestItem2));
90 EXPECT_FALSE(query->Matches(expired_page()));
91 }
92
93 TEST_F(OfflinePageModelQueryTest, OfflinePageIdsSet_Exclude) {
94 std::vector<int64_t> ids = {1, 4, 9, 16};
95 builder_.SetOfflinePageIds(Requirement::EXCLUDE_MATCHING, ids);
96
97 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
98 std::pair<Requirement, std::set<int64_t>> offline_id_restriction =
99 query->GetRestrictedToOfflineIds();
100 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, offline_id_restriction.first);
101
102 ASSERT_EQ(ids.size(), offline_id_restriction.second.size());
103 for (auto id : ids) {
104 EXPECT_EQ(1U, offline_id_restriction.second.count(id))
105 << "Did not find " << id << "in query restrictions.";
106 }
107
108 EXPECT_FALSE(query->Matches(kTestItem1));
109 EXPECT_TRUE(query->Matches(kTestItem2));
110 }
111
112 TEST_F(OfflinePageModelQueryTest, OfflinePageIdsSet) {
113 std::vector<int64_t> ids = {1, 4, 9, 16};
114 builder_.SetOfflinePageIds(Requirement::INCLUDE_MATCHING, ids);
115
116 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
117 std::pair<Requirement, std::set<int64_t>> offline_id_restriction =
118 query->GetRestrictedToOfflineIds();
119 EXPECT_EQ(Requirement::INCLUDE_MATCHING, offline_id_restriction.first);
120
121 ASSERT_EQ(ids.size(), offline_id_restriction.second.size());
122 for (auto id : ids) {
123 EXPECT_EQ(1U, offline_id_restriction.second.count(id))
124 << "Did not find " << id << "in query restrictions.";
125 }
126
127 EXPECT_TRUE(query->Matches(kTestItem1));
128 EXPECT_FALSE(query->Matches(kTestItem2));
129 }
130
131 TEST_F(OfflinePageModelQueryTest, OfflinePageIdsReplace) {
132 std::vector<int64_t> ids = {1, 4, 9, 16};
133 std::vector<int64_t> ids2 = {1, 2, 3, 4};
134
135 builder_.SetOfflinePageIds(Requirement::INCLUDE_MATCHING, ids);
136 builder_.SetOfflinePageIds(Requirement::INCLUDE_MATCHING, ids2);
137
138 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
139 std::pair<Requirement, std::set<int64_t>> offline_id_restriction =
140 query->GetRestrictedToOfflineIds();
141 EXPECT_EQ(Requirement::INCLUDE_MATCHING, offline_id_restriction.first);
142
143 ASSERT_EQ(ids2.size(), offline_id_restriction.second.size());
144 for (auto id : ids2) {
145 EXPECT_EQ(1U, offline_id_restriction.second.count(id))
146 << "Did not find " << id << "in query restrictions.";
147 }
148
149 EXPECT_TRUE(query->Matches(kTestItem1));
150 EXPECT_TRUE(query->Matches(kTestItem2));
151 }
152
153 TEST_F(OfflinePageModelQueryTest, ClientIdsSet) {
154 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}};
155 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids);
156
157 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
158
159 auto restriction = query->GetRestrictedToClientIds();
160 const Requirement& requirement = restriction.first;
161 const std::set<ClientId>& ids_out = restriction.second;
162
163 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement);
164
165 ASSERT_EQ(ids.size(), ids_out.size());
166 for (auto id : ids) {
167 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "."
168 << id.id << "in query restrictions.";
169 }
170
171 EXPECT_TRUE(query->Matches(kTestItem2));
172 EXPECT_FALSE(query->Matches(kTestItem1));
173 }
174
175 TEST_F(OfflinePageModelQueryTest, ClientIdsSet_Exclude) {
176 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}};
177 builder_.SetClientIds(Requirement::EXCLUDE_MATCHING, ids);
178
179 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
180
181 auto restriction = query->GetRestrictedToClientIds();
182 const Requirement& requirement = restriction.first;
183 const std::set<ClientId>& ids_out = restriction.second;
184
185 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, requirement);
186
187 ASSERT_EQ(ids.size(), ids_out.size());
188 for (auto id : ids) {
189 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "."
190 << id.id << "in query restrictions.";
191 }
192
193 EXPECT_TRUE(query->Matches(kTestItem1));
194 EXPECT_FALSE(query->Matches(kTestItem2));
195 }
196
197 TEST_F(OfflinePageModelQueryTest, ClientIdsReplace) {
198 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}};
199 std::vector<ClientId> ids2 = {kClientId1, {"invalid", "client id"}};
200
201 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids);
202 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids2);
203
204 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
205
206 auto restriction = query->GetRestrictedToClientIds();
207 const Requirement& requirement = restriction.first;
208 const std::set<ClientId>& ids_out = restriction.second;
209
210 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement);
211
212 ASSERT_EQ(ids2.size(), ids_out.size());
213 for (auto id : ids2) {
214 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "."
215 << id.id << "in query restrictions.";
216 }
217
218 EXPECT_TRUE(query->Matches(kTestItem1));
219 EXPECT_FALSE(query->Matches(kTestItem2));
220 }
221
222 TEST_F(OfflinePageModelQueryTest, UrlsSet) {
223 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")};
224 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls);
225
226 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
227
228 auto restriction = query->GetRestrictedToUrls();
229 const Requirement& requirement = restriction.first;
230 const std::set<GURL>& urls_out = restriction.second;
231
232 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement);
233
234 ASSERT_EQ(urls.size(), urls_out.size());
235 for (auto url : urls) {
236 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url
237 << "in query restrictions.";
238 }
239
240 EXPECT_TRUE(query->Matches(kTestItem1));
241 EXPECT_FALSE(query->Matches(kTestItem2));
242 }
243
244 TEST_F(OfflinePageModelQueryTest, UrlsSet_Exclude) {
245 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")};
246 builder_.SetUrls(Requirement::EXCLUDE_MATCHING, urls);
247
248 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
249
250 auto restriction = query->GetRestrictedToUrls();
251 const Requirement& requirement = restriction.first;
252 const std::set<GURL>& urls_out = restriction.second;
253
254 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, requirement);
255
256 ASSERT_EQ(urls.size(), urls_out.size());
257 for (auto url : urls) {
258 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url
259 << "in query restrictions.";
260 }
261
262 EXPECT_FALSE(query->Matches(kTestItem1));
263 EXPECT_TRUE(query->Matches(kTestItem2));
264 }
265
266 TEST_F(OfflinePageModelQueryTest, UrlsReplace) {
267 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")};
268 std::vector<GURL> urls2 = {kUrl2, GURL("https://abc.def")};
269
270 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls);
271 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls2);
272
273 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
274
275 auto restriction = query->GetRestrictedToUrls();
276 const Requirement& requirement = restriction.first;
277 const std::set<GURL>& urls_out = restriction.second;
278
279 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement);
280
281 ASSERT_EQ(urls2.size(), urls_out.size());
282 for (auto url : urls2) {
283 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url
284 << "in query restrictions.";
285 }
286
287 EXPECT_FALSE(query->Matches(kTestItem1));
288 EXPECT_TRUE(query->Matches(kTestItem2));
289 }
290
291 TEST_F(OfflinePageModelQueryTest, AllowExpired) {
292 std::unique_ptr<OfflinePageModelQuery> query =
293 builder_.AllowExpiredPages(true).Build(&policy_);
294
295 EXPECT_NE(nullptr, query.get());
296 EXPECT_TRUE(query->GetAllowExpired());
297
298 EXPECT_TRUE(query->Matches(kTestItem1));
299 EXPECT_TRUE(query->Matches(kTestItem2));
300 EXPECT_TRUE(query->Matches(expired_page()));
301 }
302
303 TEST_F(OfflinePageModelQueryTest, RequireSupportedByDownload_Only) {
304 builder_.RequireSupportedByDownload(Requirement::INCLUDE_MATCHING);
305 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
306
307 auto restriction = query->GetRestrictedToNamespaces();
308 std::set<std::string> namespaces_allowed = restriction.second;
309 bool restricted_to_namespaces = restriction.first;
310 EXPECT_TRUE(restricted_to_namespaces);
311
312 for (const std::string& name_space : namespaces_allowed) {
313 EXPECT_TRUE(policy_.IsSupportedByDownload(name_space)) << "Namespace: "
314 << name_space;
315 }
316 EXPECT_FALSE(query->Matches(kTestItem1));
317 EXPECT_TRUE(query->Matches(download_page()));
318 }
319
320 TEST_F(OfflinePageModelQueryTest, RequireSupportedByDownload_Except) {
321 builder_.RequireSupportedByDownload(Requirement::EXCLUDE_MATCHING);
322 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
323
324 auto restriction = query->GetRestrictedToNamespaces();
325 std::set<std::string> namespaces_allowed = restriction.second;
326 bool restricted_to_namespaces = restriction.first;
327 EXPECT_TRUE(restricted_to_namespaces);
328
329 for (const std::string& name_space : namespaces_allowed) {
330 EXPECT_FALSE(policy_.IsSupportedByDownload(name_space)) << "Namespace: "
331 << name_space;
332 }
333
334 EXPECT_TRUE(query->Matches(kTestItem1));
335 EXPECT_FALSE(query->Matches(download_page()));
336 }
337
338 TEST_F(OfflinePageModelQueryTest, RequireShownAsRecentlyVisitedSite_Only) {
339 builder_.RequireShownAsRecentlyVisitedSite(Requirement::INCLUDE_MATCHING);
340 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
341
342 auto restriction = query->GetRestrictedToNamespaces();
343 std::set<std::string> namespaces_allowed = restriction.second;
344 bool restricted_to_namespaces = restriction.first;
345 EXPECT_TRUE(restricted_to_namespaces);
346
347 for (const std::string& name_space : namespaces_allowed) {
348 EXPECT_TRUE(policy_.IsShownAsRecentlyVisitedSite(name_space))
349 << "Namespace: " << name_space;
350 }
351 EXPECT_FALSE(query->Matches(kTestItem1));
352 EXPECT_TRUE(query->Matches(recent_page()));
353 }
354
355 TEST_F(OfflinePageModelQueryTest, RequireShownAsRecentlyVisitedSite_Except) {
356 builder_.RequireShownAsRecentlyVisitedSite(Requirement::EXCLUDE_MATCHING);
357 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
358
359 auto restriction = query->GetRestrictedToNamespaces();
360 std::set<std::string> namespaces_allowed = restriction.second;
361 bool restricted_to_namespaces = restriction.first;
362 EXPECT_TRUE(restricted_to_namespaces);
363
364 for (const std::string& name_space : namespaces_allowed) {
365 EXPECT_FALSE(policy_.IsShownAsRecentlyVisitedSite(name_space))
366 << "Namespace: " << name_space;
367 }
368 EXPECT_TRUE(query->Matches(kTestItem1));
369 EXPECT_FALSE(query->Matches(recent_page()));
370 }
371
372 TEST_F(OfflinePageModelQueryTest, RequireRestrictedToOriginalTab_Only) {
373 builder_.RequireRestrictedToOriginalTab(Requirement::INCLUDE_MATCHING);
374 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
375
376 auto restriction = query->GetRestrictedToNamespaces();
377 std::set<std::string> namespaces_allowed = restriction.second;
378 bool restricted_to_namespaces = restriction.first;
379 EXPECT_TRUE(restricted_to_namespaces);
380
381 for (const std::string& name_space : namespaces_allowed) {
382 EXPECT_TRUE(policy_.IsRestrictedToOriginalTab(name_space)) << "Namespace: "
383 << name_space;
384 }
385 EXPECT_FALSE(query->Matches(kTestItem1));
386 EXPECT_TRUE(query->Matches(original_tab_page()));
387 }
388
389 TEST_F(OfflinePageModelQueryTest, RequireRestrictedToOriginalTab_Except) {
390 builder_.RequireRestrictedToOriginalTab(Requirement::EXCLUDE_MATCHING);
391 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
392
393 auto restriction = query->GetRestrictedToNamespaces();
394 std::set<std::string> namespaces_allowed = restriction.second;
395 bool restricted_to_namespaces = restriction.first;
396 EXPECT_TRUE(restricted_to_namespaces);
397
398 for (const std::string& name_space : namespaces_allowed) {
399 EXPECT_FALSE(policy_.IsRestrictedToOriginalTab(name_space)) << "Namespace: "
400 << name_space;
401 }
402 EXPECT_TRUE(query->Matches(kTestItem1));
403 EXPECT_FALSE(query->Matches(original_tab_page()));
404 }
405
406 TEST_F(OfflinePageModelQueryTest, IntersectNamespaces) {
407 // This should exclude last N, but include |kTestNamespace|.
408 builder_.RequireRestrictedToOriginalTab(Requirement::INCLUDE_MATCHING)
409 .RequireShownAsRecentlyVisitedSite(Requirement::EXCLUDE_MATCHING);
410 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_);
411
412 auto restriction = query->GetRestrictedToNamespaces();
413 std::set<std::string> namespaces_allowed = restriction.second;
414 bool restricted_to_namespaces = restriction.first;
415 EXPECT_TRUE(restricted_to_namespaces);
416
417 EXPECT_TRUE(namespaces_allowed.count(kTestNamespace) == 1);
418 EXPECT_FALSE(query->Matches(recent_page()));
419 }
420
421 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698