| 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 download_page() { | |
| 41 return OfflinePageItem(GURL("https://download.com"), 4, | |
| 42 {kDownloadNamespace, "id1"}, base::FilePath(), 4); | |
| 43 } | |
| 44 | |
| 45 const OfflinePageItem original_tab_page() { | |
| 46 return OfflinePageItem(GURL("https://download.com"), 5, | |
| 47 {kLastNNamespace, "id1"}, base::FilePath(), 5); | |
| 48 } | |
| 49 | |
| 50 const OfflinePageItem test_namespace_page() { | |
| 51 return OfflinePageItem(GURL("https://download.com"), 6, | |
| 52 {kTestNamespace, "id1"}, base::FilePath(), 6); | |
| 53 } | |
| 54 | |
| 55 const OfflinePageItem recent_page() { | |
| 56 return OfflinePageItem(GURL("https://download.com"), 7, | |
| 57 {kLastNNamespace, "id1"}, base::FilePath(), 7); | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 OfflinePageModelQueryTest::OfflinePageModelQueryTest() { | |
| 62 policy_.AddPolicyForTest( | |
| 63 kTestNamespace, | |
| 64 OfflinePageClientPolicyBuilder(kTestNamespace, | |
| 65 LifetimePolicy::LifetimeType::TEMPORARY, | |
| 66 kUnlimitedPages, kUnlimitedPages) | |
| 67 .SetIsOnlyShownInOriginalTab(true)); | |
| 68 } | |
| 69 | |
| 70 OfflinePageModelQueryTest::~OfflinePageModelQueryTest() {} | |
| 71 | |
| 72 TEST_F(OfflinePageModelQueryTest, DefaultValues) { | |
| 73 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 74 | |
| 75 EXPECT_NE(nullptr, query.get()); | |
| 76 EXPECT_EQ(Requirement::UNSET, query->GetRestrictedToOfflineIds().first); | |
| 77 EXPECT_FALSE(query->GetRestrictedToNamespaces().first); | |
| 78 | |
| 79 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 80 EXPECT_TRUE(query->Matches(kTestItem2)); | |
| 81 } | |
| 82 | |
| 83 TEST_F(OfflinePageModelQueryTest, OfflinePageIdsSet_Exclude) { | |
| 84 std::vector<int64_t> ids = {1, 4, 9, 16}; | |
| 85 builder_.SetOfflinePageIds(Requirement::EXCLUDE_MATCHING, ids); | |
| 86 | |
| 87 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 88 std::pair<Requirement, std::set<int64_t>> offline_id_restriction = | |
| 89 query->GetRestrictedToOfflineIds(); | |
| 90 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, offline_id_restriction.first); | |
| 91 | |
| 92 ASSERT_EQ(ids.size(), offline_id_restriction.second.size()); | |
| 93 for (auto id : ids) { | |
| 94 EXPECT_EQ(1U, offline_id_restriction.second.count(id)) | |
| 95 << "Did not find " << id << "in query restrictions."; | |
| 96 } | |
| 97 | |
| 98 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 99 EXPECT_TRUE(query->Matches(kTestItem2)); | |
| 100 } | |
| 101 | |
| 102 TEST_F(OfflinePageModelQueryTest, OfflinePageIdsSet) { | |
| 103 std::vector<int64_t> ids = {1, 4, 9, 16}; | |
| 104 builder_.SetOfflinePageIds(Requirement::INCLUDE_MATCHING, ids); | |
| 105 | |
| 106 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 107 std::pair<Requirement, std::set<int64_t>> offline_id_restriction = | |
| 108 query->GetRestrictedToOfflineIds(); | |
| 109 EXPECT_EQ(Requirement::INCLUDE_MATCHING, offline_id_restriction.first); | |
| 110 | |
| 111 ASSERT_EQ(ids.size(), offline_id_restriction.second.size()); | |
| 112 for (auto id : ids) { | |
| 113 EXPECT_EQ(1U, offline_id_restriction.second.count(id)) | |
| 114 << "Did not find " << id << "in query restrictions."; | |
| 115 } | |
| 116 | |
| 117 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 118 EXPECT_FALSE(query->Matches(kTestItem2)); | |
| 119 } | |
| 120 | |
| 121 TEST_F(OfflinePageModelQueryTest, OfflinePageIdsReplace) { | |
| 122 std::vector<int64_t> ids = {1, 4, 9, 16}; | |
| 123 std::vector<int64_t> ids2 = {1, 2, 3, 4}; | |
| 124 | |
| 125 builder_.SetOfflinePageIds(Requirement::INCLUDE_MATCHING, ids); | |
| 126 builder_.SetOfflinePageIds(Requirement::INCLUDE_MATCHING, ids2); | |
| 127 | |
| 128 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 129 std::pair<Requirement, std::set<int64_t>> offline_id_restriction = | |
| 130 query->GetRestrictedToOfflineIds(); | |
| 131 EXPECT_EQ(Requirement::INCLUDE_MATCHING, offline_id_restriction.first); | |
| 132 | |
| 133 ASSERT_EQ(ids2.size(), offline_id_restriction.second.size()); | |
| 134 for (auto id : ids2) { | |
| 135 EXPECT_EQ(1U, offline_id_restriction.second.count(id)) | |
| 136 << "Did not find " << id << "in query restrictions."; | |
| 137 } | |
| 138 | |
| 139 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 140 EXPECT_TRUE(query->Matches(kTestItem2)); | |
| 141 } | |
| 142 | |
| 143 TEST_F(OfflinePageModelQueryTest, ClientIdsSet) { | |
| 144 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}}; | |
| 145 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids); | |
| 146 | |
| 147 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 148 | |
| 149 auto restriction = query->GetRestrictedToClientIds(); | |
| 150 const Requirement& requirement = restriction.first; | |
| 151 const std::set<ClientId>& ids_out = restriction.second; | |
| 152 | |
| 153 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement); | |
| 154 | |
| 155 ASSERT_EQ(ids.size(), ids_out.size()); | |
| 156 for (auto id : ids) { | |
| 157 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "." | |
| 158 << id.id << "in query restrictions."; | |
| 159 } | |
| 160 | |
| 161 EXPECT_TRUE(query->Matches(kTestItem2)); | |
| 162 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 163 } | |
| 164 | |
| 165 TEST_F(OfflinePageModelQueryTest, ClientIdsSet_Exclude) { | |
| 166 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}}; | |
| 167 builder_.SetClientIds(Requirement::EXCLUDE_MATCHING, ids); | |
| 168 | |
| 169 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 170 | |
| 171 auto restriction = query->GetRestrictedToClientIds(); | |
| 172 const Requirement& requirement = restriction.first; | |
| 173 const std::set<ClientId>& ids_out = restriction.second; | |
| 174 | |
| 175 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, requirement); | |
| 176 | |
| 177 ASSERT_EQ(ids.size(), ids_out.size()); | |
| 178 for (auto id : ids) { | |
| 179 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "." | |
| 180 << id.id << "in query restrictions."; | |
| 181 } | |
| 182 | |
| 183 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 184 EXPECT_FALSE(query->Matches(kTestItem2)); | |
| 185 } | |
| 186 | |
| 187 TEST_F(OfflinePageModelQueryTest, ClientIdsReplace) { | |
| 188 std::vector<ClientId> ids = {kClientId2, {"invalid", "client id"}}; | |
| 189 std::vector<ClientId> ids2 = {kClientId1, {"invalid", "client id"}}; | |
| 190 | |
| 191 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids); | |
| 192 builder_.SetClientIds(Requirement::INCLUDE_MATCHING, ids2); | |
| 193 | |
| 194 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 195 | |
| 196 auto restriction = query->GetRestrictedToClientIds(); | |
| 197 const Requirement& requirement = restriction.first; | |
| 198 const std::set<ClientId>& ids_out = restriction.second; | |
| 199 | |
| 200 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement); | |
| 201 | |
| 202 ASSERT_EQ(ids2.size(), ids_out.size()); | |
| 203 for (auto id : ids2) { | |
| 204 EXPECT_EQ(1U, ids_out.count(id)) << "Did not find " << id.name_space << "." | |
| 205 << id.id << "in query restrictions."; | |
| 206 } | |
| 207 | |
| 208 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 209 EXPECT_FALSE(query->Matches(kTestItem2)); | |
| 210 } | |
| 211 | |
| 212 TEST_F(OfflinePageModelQueryTest, UrlsSet) { | |
| 213 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")}; | |
| 214 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls); | |
| 215 | |
| 216 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 217 | |
| 218 auto restriction = query->GetRestrictedToUrls(); | |
| 219 const Requirement& requirement = restriction.first; | |
| 220 const std::set<GURL>& urls_out = restriction.second; | |
| 221 | |
| 222 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement); | |
| 223 | |
| 224 ASSERT_EQ(urls.size(), urls_out.size()); | |
| 225 for (auto url : urls) { | |
| 226 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url | |
| 227 << "in query restrictions."; | |
| 228 } | |
| 229 | |
| 230 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 231 EXPECT_FALSE(query->Matches(kTestItem2)); | |
| 232 } | |
| 233 | |
| 234 TEST_F(OfflinePageModelQueryTest, UrlsSet_Exclude) { | |
| 235 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")}; | |
| 236 builder_.SetUrls(Requirement::EXCLUDE_MATCHING, urls); | |
| 237 | |
| 238 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 239 | |
| 240 auto restriction = query->GetRestrictedToUrls(); | |
| 241 const Requirement& requirement = restriction.first; | |
| 242 const std::set<GURL>& urls_out = restriction.second; | |
| 243 | |
| 244 EXPECT_EQ(Requirement::EXCLUDE_MATCHING, requirement); | |
| 245 | |
| 246 ASSERT_EQ(urls.size(), urls_out.size()); | |
| 247 for (auto url : urls) { | |
| 248 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url | |
| 249 << "in query restrictions."; | |
| 250 } | |
| 251 | |
| 252 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 253 EXPECT_TRUE(query->Matches(kTestItem2)); | |
| 254 } | |
| 255 | |
| 256 TEST_F(OfflinePageModelQueryTest, UrlsReplace) { | |
| 257 std::vector<GURL> urls = {kUrl1, GURL("https://abc.def")}; | |
| 258 std::vector<GURL> urls2 = {kUrl2, GURL("https://abc.def")}; | |
| 259 | |
| 260 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls); | |
| 261 builder_.SetUrls(Requirement::INCLUDE_MATCHING, urls2); | |
| 262 | |
| 263 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 264 | |
| 265 auto restriction = query->GetRestrictedToUrls(); | |
| 266 const Requirement& requirement = restriction.first; | |
| 267 const std::set<GURL>& urls_out = restriction.second; | |
| 268 | |
| 269 EXPECT_EQ(Requirement::INCLUDE_MATCHING, requirement); | |
| 270 | |
| 271 ASSERT_EQ(urls2.size(), urls_out.size()); | |
| 272 for (auto url : urls2) { | |
| 273 EXPECT_EQ(1U, urls_out.count(url)) << "Did not find " << url | |
| 274 << "in query restrictions."; | |
| 275 } | |
| 276 | |
| 277 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 278 EXPECT_TRUE(query->Matches(kTestItem2)); | |
| 279 } | |
| 280 | |
| 281 TEST_F(OfflinePageModelQueryTest, RequireSupportedByDownload_Only) { | |
| 282 builder_.RequireSupportedByDownload(Requirement::INCLUDE_MATCHING); | |
| 283 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 284 | |
| 285 auto restriction = query->GetRestrictedToNamespaces(); | |
| 286 std::set<std::string> namespaces_allowed = restriction.second; | |
| 287 bool restricted_to_namespaces = restriction.first; | |
| 288 EXPECT_TRUE(restricted_to_namespaces); | |
| 289 | |
| 290 for (const std::string& name_space : namespaces_allowed) { | |
| 291 EXPECT_TRUE(policy_.IsSupportedByDownload(name_space)) << "Namespace: " | |
| 292 << name_space; | |
| 293 } | |
| 294 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 295 EXPECT_TRUE(query->Matches(download_page())); | |
| 296 } | |
| 297 | |
| 298 TEST_F(OfflinePageModelQueryTest, RequireSupportedByDownload_Except) { | |
| 299 builder_.RequireSupportedByDownload(Requirement::EXCLUDE_MATCHING); | |
| 300 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 301 | |
| 302 auto restriction = query->GetRestrictedToNamespaces(); | |
| 303 std::set<std::string> namespaces_allowed = restriction.second; | |
| 304 bool restricted_to_namespaces = restriction.first; | |
| 305 EXPECT_TRUE(restricted_to_namespaces); | |
| 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 auto restriction = query->GetRestrictedToNamespaces(); | |
| 321 std::set<std::string> namespaces_allowed = restriction.second; | |
| 322 bool restricted_to_namespaces = restriction.first; | |
| 323 EXPECT_TRUE(restricted_to_namespaces); | |
| 324 | |
| 325 for (const std::string& name_space : namespaces_allowed) { | |
| 326 EXPECT_TRUE(policy_.IsShownAsRecentlyVisitedSite(name_space)) | |
| 327 << "Namespace: " << name_space; | |
| 328 } | |
| 329 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 330 EXPECT_TRUE(query->Matches(recent_page())); | |
| 331 } | |
| 332 | |
| 333 TEST_F(OfflinePageModelQueryTest, RequireShownAsRecentlyVisitedSite_Except) { | |
| 334 builder_.RequireShownAsRecentlyVisitedSite(Requirement::EXCLUDE_MATCHING); | |
| 335 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 336 | |
| 337 auto restriction = query->GetRestrictedToNamespaces(); | |
| 338 std::set<std::string> namespaces_allowed = restriction.second; | |
| 339 bool restricted_to_namespaces = restriction.first; | |
| 340 EXPECT_TRUE(restricted_to_namespaces); | |
| 341 | |
| 342 for (const std::string& name_space : namespaces_allowed) { | |
| 343 EXPECT_FALSE(policy_.IsShownAsRecentlyVisitedSite(name_space)) | |
| 344 << "Namespace: " << name_space; | |
| 345 } | |
| 346 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 347 EXPECT_FALSE(query->Matches(recent_page())); | |
| 348 } | |
| 349 | |
| 350 TEST_F(OfflinePageModelQueryTest, RequireRestrictedToOriginalTab_Only) { | |
| 351 builder_.RequireRestrictedToOriginalTab(Requirement::INCLUDE_MATCHING); | |
| 352 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 353 | |
| 354 auto restriction = query->GetRestrictedToNamespaces(); | |
| 355 std::set<std::string> namespaces_allowed = restriction.second; | |
| 356 bool restricted_to_namespaces = restriction.first; | |
| 357 EXPECT_TRUE(restricted_to_namespaces); | |
| 358 | |
| 359 for (const std::string& name_space : namespaces_allowed) { | |
| 360 EXPECT_TRUE(policy_.IsRestrictedToOriginalTab(name_space)) << "Namespace: " | |
| 361 << name_space; | |
| 362 } | |
| 363 EXPECT_FALSE(query->Matches(kTestItem1)); | |
| 364 EXPECT_TRUE(query->Matches(original_tab_page())); | |
| 365 } | |
| 366 | |
| 367 TEST_F(OfflinePageModelQueryTest, RequireRestrictedToOriginalTab_Except) { | |
| 368 builder_.RequireRestrictedToOriginalTab(Requirement::EXCLUDE_MATCHING); | |
| 369 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 370 | |
| 371 auto restriction = query->GetRestrictedToNamespaces(); | |
| 372 std::set<std::string> namespaces_allowed = restriction.second; | |
| 373 bool restricted_to_namespaces = restriction.first; | |
| 374 EXPECT_TRUE(restricted_to_namespaces); | |
| 375 | |
| 376 for (const std::string& name_space : namespaces_allowed) { | |
| 377 EXPECT_FALSE(policy_.IsRestrictedToOriginalTab(name_space)) << "Namespace: " | |
| 378 << name_space; | |
| 379 } | |
| 380 EXPECT_TRUE(query->Matches(kTestItem1)); | |
| 381 EXPECT_FALSE(query->Matches(original_tab_page())); | |
| 382 } | |
| 383 | |
| 384 TEST_F(OfflinePageModelQueryTest, IntersectNamespaces) { | |
| 385 // This should exclude last N, but include |kTestNamespace|. | |
| 386 builder_.RequireRestrictedToOriginalTab(Requirement::INCLUDE_MATCHING) | |
| 387 .RequireShownAsRecentlyVisitedSite(Requirement::EXCLUDE_MATCHING); | |
| 388 std::unique_ptr<OfflinePageModelQuery> query = builder_.Build(&policy_); | |
| 389 | |
| 390 auto restriction = query->GetRestrictedToNamespaces(); | |
| 391 std::set<std::string> namespaces_allowed = restriction.second; | |
| 392 bool restricted_to_namespaces = restriction.first; | |
| 393 EXPECT_TRUE(restricted_to_namespaces); | |
| 394 | |
| 395 EXPECT_TRUE(namespaces_allowed.count(kTestNamespace) == 1); | |
| 396 EXPECT_FALSE(query->Matches(recent_page())); | |
| 397 } | |
| 398 | |
| 399 } // namespace offline_pages | |
| OLD | NEW |