OLD | NEW |
(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(nullptr)); |
| 86 EXPECT_FALSE(query->GetRestrictedToNamespaces(nullptr)); |
| 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::set<int64_t> ids_out; |
| 99 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, |
| 100 query->GetRestrictedToOfflineIds(&ids_out)); |
| 101 |
| 102 ASSERT_EQ(ids.size(), ids_out.size()); |
| 103 for (auto id : ids) { |
| 104 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id |
| 105 << "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::set<int64_t> ids_out; |
| 118 EXPECT_EQ(Requirement::INCLUDE_MATCHING, |
| 119 query->GetRestrictedToOfflineIds(&ids_out)); |
| 120 |
| 121 ASSERT_EQ(ids.size(), ids_out.size()); |
| 122 for (auto id : ids) { |
| 123 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id |
| 124 << "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::set<int64_t> ids_out; |
| 140 EXPECT_EQ(Requirement::INCLUDE_MATCHING, |
| 141 query->GetRestrictedToOfflineIds(&ids_out)); |
| 142 |
| 143 ASSERT_EQ(ids2.size(), ids_out.size()); |
| 144 for (auto id : ids2) { |
| 145 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id |
| 146 << "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 std::set<ClientId> ids_out; |
| 159 EXPECT_EQ(Requirement::INCLUDE_MATCHING, |
| 160 query->GetRestrictedToClientIds(&ids_out)); |
| 161 |
| 162 ASSERT_EQ(ids.size(), ids_out.size()); |
| 163 for (auto id : ids) { |
| 164 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "." |
| 165 << id.id << "in query restrictions."; |
| 166 } |
| 167 |
| 168 EXPECT_TRUE(query->Matches(kTestItem2)); |
| 169 EXPECT_FALSE(query->Matches(kTestItem1)); |
| 170 } |
| 171 |
| 172 TEST_F(OfflinePageModelQueryTest, ClientIdsSet_Exclude) { |
| 173 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}}; |
| 174 builder_.SetClientIds(Requirement::EXCLUDE_MATCHING, ids); |
| 175 |
| 176 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 177 std::set<ClientId> ids_out; |
| 178 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, |
| 179 query->GetRestrictedToClientIds(&ids_out)); |
| 180 |
| 181 ASSERT_EQ(ids.size(), ids_out.size()); |
| 182 for (auto id : ids) { |
| 183 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "." |
| 184 << id.id << "in query restrictions."; |
| 185 } |
| 186 |
| 187 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 188 EXPECT_FALSE(query->Matches(kTestItem2)); |
| 189 } |
| 190 |
| 191 TEST_F(OfflinePageModelQueryTest, ClientIdsReplace) { |
| 192 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}}; |
| 193 std::vector<ClientId> ids2 = {kClientId1, {"invalid", "client id"}}; |
| 194 |
| 195 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids); |
| 196 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids2); |
| 197 |
| 198 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 199 std::set<ClientId> ids_out; |
| 200 EXPECT_EQ(Requirement::INCLUDE_MATCHING, |
| 201 query->GetRestrictedToClientIds(&ids_out)); |
| 202 |
| 203 ASSERT_EQ(ids2.size(), ids_out.size()); |
| 204 for (auto id : ids2) { |
| 205 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "." |
| 206 << id.id << "in query restrictions."; |
| 207 } |
| 208 |
| 209 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 210 EXPECT_FALSE(query->Matches(kTestItem2)); |
| 211 } |
| 212 |
| 213 TEST_F(OfflinePageModelQueryTest, UrlsSet) { |
| 214 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")}; |
| 215 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls); |
| 216 |
| 217 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 218 std::set<GURL> urls_out; |
| 219 EXPECT_EQ(Requirement::INCLUDE_MATCHING, |
| 220 query->GetRestrictedToUrls(&urls_out)); |
| 221 |
| 222 ASSERT_EQ(urls.size(), urls_out.size()); |
| 223 for (auto url : urls) { |
| 224 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url |
| 225 << "in query restrictions."; |
| 226 } |
| 227 |
| 228 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 229 EXPECT_FALSE(query->Matches(kTestItem2)); |
| 230 } |
| 231 |
| 232 TEST_F(OfflinePageModelQueryTest, UrlsSet_Exclude) { |
| 233 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")}; |
| 234 builder_.SetUrls(Requirement::EXCLUDE_MATCHING, urls); |
| 235 |
| 236 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 237 std::set<GURL> urls_out; |
| 238 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, |
| 239 query->GetRestrictedToUrls(&urls_out)); |
| 240 |
| 241 ASSERT_EQ(urls.size(), urls_out.size()); |
| 242 for (auto url : urls) { |
| 243 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url |
| 244 << "in query restrictions."; |
| 245 } |
| 246 |
| 247 EXPECT_FALSE(query->Matches(kTestItem1)); |
| 248 EXPECT_TRUE(query->Matches(kTestItem2)); |
| 249 } |
| 250 |
| 251 TEST_F(OfflinePageModelQueryTest, UrlsReplace) { |
| 252 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")}; |
| 253 std::vector<GURL> urls2 = {kUrl2, GURL("https://abc.def")}; |
| 254 |
| 255 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls); |
| 256 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls2); |
| 257 |
| 258 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 259 std::set<GURL> urls_out; |
| 260 EXPECT_EQ(Requirement::INCLUDE_MATCHING, |
| 261 query->GetRestrictedToUrls(&urls_out)); |
| 262 |
| 263 ASSERT_EQ(urls2.size(), urls_out.size()); |
| 264 for (auto url : urls2) { |
| 265 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url |
| 266 << "in query restrictions."; |
| 267 } |
| 268 |
| 269 EXPECT_FALSE(query->Matches(kTestItem1)); |
| 270 EXPECT_TRUE(query->Matches(kTestItem2)); |
| 271 } |
| 272 |
| 273 TEST_F(OfflinePageModelQueryTest, AllowExpired) { |
| 274 std::unique_ptr<OfflinePageModelQuery> query = |
| 275 builder_.AllowExpiredPages(true).Build(&policy_); |
| 276 |
| 277 EXPECT_NE(nullptr, query.get()); |
| 278 EXPECT_TRUE(query->GetAllowExpired()); |
| 279 |
| 280 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 281 EXPECT_TRUE(query->Matches(kTestItem2)); |
| 282 EXPECT_TRUE(query->Matches(expired_page())); |
| 283 } |
| 284 |
| 285 TEST_F(OfflinePageModelQueryTest, RequireSupportedByDownload_Only) { |
| 286 builder_.RequireSupportedByDownload(Requirement::INCLUDE_MATCHING); |
| 287 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 288 |
| 289 std::set<std::string> namespaces_allowed; |
| 290 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 291 |
| 292 for (const std::string& name_space : namespaces_allowed) { |
| 293 EXPECT_TRUE(policy_.IsSupportedByDownload(name_space)) << "Namespace: " |
| 294 << name_space; |
| 295 } |
| 296 EXPECT_FALSE(query->Matches(kTestItem1)); |
| 297 EXPECT_TRUE(query->Matches(download_page())); |
| 298 } |
| 299 |
| 300 TEST_F(OfflinePageModelQueryTest, RequireSupportedByDownload_Except) { |
| 301 builder_.RequireSupportedByDownload(Requirement::EXCLUDE_MATCHING); |
| 302 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 303 |
| 304 std::set<std::string> namespaces_allowed; |
| 305 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 306 |
| 307 for (const std::string& name_space : namespaces_allowed) { |
| 308 EXPECT_FALSE(policy_.IsSupportedByDownload(name_space)) << "Namespace: " |
| 309 << name_space; |
| 310 } |
| 311 |
| 312 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 313 EXPECT_FALSE(query->Matches(download_page())); |
| 314 } |
| 315 |
| 316 TEST_F(OfflinePageModelQueryTest, RequireShownAsRecentlyVisitedSite_Only) { |
| 317 builder_.RequireShownAsRecentlyVisitedSite(Requirement::INCLUDE_MATCHING); |
| 318 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 319 |
| 320 std::set<std::string> namespaces_allowed; |
| 321 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 322 |
| 323 for (const std::string& name_space : namespaces_allowed) { |
| 324 EXPECT_TRUE(policy_.IsShownAsRecentlyVisitedSite(name_space)) |
| 325 << "Namespace: " << name_space; |
| 326 } |
| 327 EXPECT_FALSE(query->Matches(kTestItem1)); |
| 328 EXPECT_TRUE(query->Matches(recent_page())); |
| 329 } |
| 330 |
| 331 TEST_F(OfflinePageModelQueryTest, RequireShownAsRecentlyVisitedSite_Except) { |
| 332 builder_.RequireShownAsRecentlyVisitedSite(Requirement::EXCLUDE_MATCHING); |
| 333 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 334 |
| 335 std::set<std::string> namespaces_allowed; |
| 336 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 337 |
| 338 for (const std::string& name_space : namespaces_allowed) { |
| 339 EXPECT_FALSE(policy_.IsShownAsRecentlyVisitedSite(name_space)) |
| 340 << "Namespace: " << name_space; |
| 341 } |
| 342 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 343 EXPECT_FALSE(query->Matches(recent_page())); |
| 344 } |
| 345 |
| 346 TEST_F(OfflinePageModelQueryTest, RequireRestrictedToOriginalTab_Only) { |
| 347 builder_.RequireRestrictedToOriginalTab(Requirement::INCLUDE_MATCHING); |
| 348 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 349 |
| 350 std::set<std::string> namespaces_allowed; |
| 351 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 352 |
| 353 for (const std::string& name_space : namespaces_allowed) { |
| 354 EXPECT_TRUE(policy_.IsRestrictedToOriginalTab(name_space)) << "Namespace: " |
| 355 << name_space; |
| 356 } |
| 357 EXPECT_FALSE(query->Matches(kTestItem1)); |
| 358 EXPECT_TRUE(query->Matches(original_tab_page())); |
| 359 } |
| 360 |
| 361 TEST_F(OfflinePageModelQueryTest, RequireRestrictedToOriginalTab_Except) { |
| 362 builder_.RequireRestrictedToOriginalTab(Requirement::EXCLUDE_MATCHING); |
| 363 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 364 |
| 365 std::set<std::string> namespaces_allowed; |
| 366 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 367 |
| 368 for (const std::string& name_space : namespaces_allowed) { |
| 369 EXPECT_FALSE(policy_.IsRestrictedToOriginalTab(name_space)) << "Namespace: " |
| 370 << name_space; |
| 371 } |
| 372 EXPECT_TRUE(query->Matches(kTestItem1)); |
| 373 EXPECT_FALSE(query->Matches(original_tab_page())); |
| 374 } |
| 375 |
| 376 TEST_F(OfflinePageModelQueryTest, IntersectNamespaces) { |
| 377 // This should exclude last N, but include |kTestNamespace|. |
| 378 builder_.RequireRestrictedToOriginalTab(Requirement::INCLUDE_MATCHING) |
| 379 .RequireShownAsRecentlyVisitedSite(Requirement::EXCLUDE_MATCHING); |
| 380 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); |
| 381 |
| 382 std::set<std::string> namespaces_allowed; |
| 383 EXPECT_TRUE(query->GetRestrictedToNamespaces(&namespaces_allowed)); |
| 384 |
| 385 EXPECT_TRUE(namespaces_allowed.count(kTestNamespace) == 1); |
| 386 EXPECT_FALSE(query->Matches(recent_page())); |
| 387 } |
| 388 |
| 389 } // namespace offline_pages |
OLD | NEW |