OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "content/browser/notifications/platform_notification_context_impl.h" | 9 #include "content/browser/notifications/platform_notification_context_impl.h" |
10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/common/service_worker/service_worker_types.h" | 12 #include "content/common/service_worker/service_worker_types.h" |
13 #include "content/public/browser/notification_database_data.h" | 13 #include "content/public/browser/notification_database_data.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 // Fake render process id to use in tests requiring one. | 20 // Fake render process id to use in tests requiring one. |
21 const int kFakeRenderProcessId = 99; | 21 const int kFakeRenderProcessId = 99; |
22 | 22 |
| 23 // Fake Service Worker registration id to use in tests requiring one. |
| 24 const int64_t kFakeServiceWorkerRegistrationId = 42; |
| 25 |
23 class PlatformNotificationContextTest : public ::testing::Test { | 26 class PlatformNotificationContextTest : public ::testing::Test { |
24 public: | 27 public: |
25 PlatformNotificationContextTest() | 28 PlatformNotificationContextTest() |
26 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 29 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
27 success_(false) {} | 30 success_(false) {} |
28 | 31 |
29 // Callback to provide when reading a single notification from the database. | 32 // Callback to provide when reading a single notification from the database. |
30 void DidReadNotificationData( | 33 void DidReadNotificationData( |
31 bool success, const NotificationDatabaseData& database_data) { | 34 bool success, const NotificationDatabaseData& database_data) { |
32 success_ = success; | 35 success_ = success; |
(...skipping 24 matching lines...) Expand all Loading... |
57 } | 60 } |
58 | 61 |
59 // Callback to provide when unregistering a Service Worker. Will write the | 62 // Callback to provide when unregistering a Service Worker. Will write the |
60 // resulting status code to |store_status|. | 63 // resulting status code to |store_status|. |
61 void DidUnregisterServiceWorker(ServiceWorkerStatusCode* store_status, | 64 void DidUnregisterServiceWorker(ServiceWorkerStatusCode* store_status, |
62 ServiceWorkerStatusCode status) { | 65 ServiceWorkerStatusCode status) { |
63 DCHECK(store_status); | 66 DCHECK(store_status); |
64 *store_status = status; | 67 *store_status = status; |
65 } | 68 } |
66 | 69 |
| 70 // Callback to provide when reading multiple notifications from the database. |
| 71 // Will store the success value in the class member, and write the read |
| 72 // notification datas to |store_notification_datas|. |
| 73 void DidReadAllNotificationDatas( |
| 74 std::vector<NotificationDatabaseData>* store_notification_datas, |
| 75 bool success, |
| 76 const std::vector<NotificationDatabaseData>& notification_datas) { |
| 77 DCHECK(store_notification_datas); |
| 78 |
| 79 success_ = success; |
| 80 *store_notification_datas = notification_datas; |
| 81 } |
| 82 |
67 protected: | 83 protected: |
68 // Creates a new PlatformNotificationContextImpl instance. When using this | 84 // Creates a new PlatformNotificationContextImpl instance. When using this |
69 // method, the underlying database will always be created in memory. The | 85 // method, the underlying database will always be created in memory. The |
70 // current message loop proxy will be used as the task runner. | 86 // current message loop proxy will be used as the task runner. |
71 PlatformNotificationContextImpl* CreatePlatformNotificationContext() { | 87 PlatformNotificationContextImpl* CreatePlatformNotificationContext() { |
72 PlatformNotificationContextImpl* context = | 88 PlatformNotificationContextImpl* context = |
73 new PlatformNotificationContextImpl(base::FilePath(), nullptr); | 89 new PlatformNotificationContextImpl(base::FilePath(), nullptr); |
74 context->Initialize(); | 90 context->Initialize(); |
75 | 91 |
76 OverrideTaskRunnerForTesting(context); | 92 OverrideTaskRunnerForTesting(context); |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 361 |
346 // Blow away the database by faking a Service Worker Context wipe-out. | 362 // Blow away the database by faking a Service Worker Context wipe-out. |
347 context->OnStorageWiped(); | 363 context->OnStorageWiped(); |
348 | 364 |
349 base::RunLoop().RunUntilIdle(); | 365 base::RunLoop().RunUntilIdle(); |
350 | 366 |
351 // The database's directory should be empty at this point. | 367 // The database's directory should be empty at this point. |
352 EXPECT_TRUE(IsDirectoryEmpty(database_dir.path())); | 368 EXPECT_TRUE(IsDirectoryEmpty(database_dir.path())); |
353 } | 369 } |
354 | 370 |
| 371 TEST_F(PlatformNotificationContextTest, ReadAllServiceWorkerDataEmpty) { |
| 372 scoped_refptr<PlatformNotificationContextImpl> context = |
| 373 CreatePlatformNotificationContext(); |
| 374 |
| 375 GURL origin("https://example.com"); |
| 376 |
| 377 std::vector<NotificationDatabaseData> notification_database_datas; |
| 378 context->ReadAllNotificationDataForServiceWorkerRegistration( |
| 379 origin, |
| 380 kFakeServiceWorkerRegistrationId, |
| 381 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas, |
| 382 base::Unretained(this), |
| 383 ¬ification_database_datas)); |
| 384 |
| 385 base::RunLoop().RunUntilIdle(); |
| 386 |
| 387 EXPECT_TRUE(success()); |
| 388 EXPECT_EQ(0u, notification_database_datas.size()); |
| 389 } |
| 390 |
| 391 TEST_F(PlatformNotificationContextTest, ReadAllServiceWorkerDataFilled) { |
| 392 scoped_refptr<PlatformNotificationContextImpl> context = |
| 393 CreatePlatformNotificationContext(); |
| 394 |
| 395 GURL origin("https://example.com"); |
| 396 |
| 397 NotificationDatabaseData notification_database_data; |
| 398 notification_database_data.origin = origin; |
| 399 notification_database_data.service_worker_registration_id = |
| 400 kFakeServiceWorkerRegistrationId; |
| 401 |
| 402 // Insert ten notifications into the database belonging to origin and the |
| 403 // test Service Worker Registration id. |
| 404 for (int i = 0; i < 10; ++i) { |
| 405 context->WriteNotificationData( |
| 406 origin, |
| 407 notification_database_data, |
| 408 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
| 409 base::Unretained(this))); |
| 410 |
| 411 base::RunLoop().RunUntilIdle(); |
| 412 |
| 413 ASSERT_TRUE(success()); |
| 414 } |
| 415 |
| 416 // Now read the notifications from the database again. There should be ten, |
| 417 // all set with the correct origin and Service Worker Registration id. |
| 418 std::vector<NotificationDatabaseData> notification_database_datas; |
| 419 context->ReadAllNotificationDataForServiceWorkerRegistration( |
| 420 origin, |
| 421 kFakeServiceWorkerRegistrationId, |
| 422 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas, |
| 423 base::Unretained(this), |
| 424 ¬ification_database_datas)); |
| 425 |
| 426 base::RunLoop().RunUntilIdle(); |
| 427 |
| 428 ASSERT_TRUE(success()); |
| 429 ASSERT_EQ(10u, notification_database_datas.size()); |
| 430 |
| 431 for (int i = 0; i < 10; ++i) { |
| 432 EXPECT_EQ(origin, notification_database_datas[i].origin); |
| 433 EXPECT_EQ(kFakeServiceWorkerRegistrationId, |
| 434 notification_database_datas[i].service_worker_registration_id); |
| 435 } |
| 436 } |
| 437 |
355 } // namespace content | 438 } // namespace content |
OLD | NEW |