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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "content/browser/notifications/platform_notification_context_impl.h" | 13 #include "content/browser/notifications/platform_notification_context_impl.h" |
14 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 14 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
16 #include "content/common/service_worker/service_worker_types.h" | 16 #include "content/common/service_worker/service_worker_types.h" |
17 #include "content/public/browser/notification_database_data.h" | 17 #include "content/public/browser/notification_database_data.h" |
| 18 #include "content/public/common/notification_resources.h" |
18 #include "content/public/test/test_browser_context.h" | 19 #include "content/public/test/test_browser_context.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "content/test/mock_platform_notification_service.h" |
| 22 #include "content/test/test_content_browser_client.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "url/gurl.h" | 24 #include "url/gurl.h" |
22 | 25 |
23 namespace content { | 26 namespace content { |
24 | 27 |
25 // Fake Service Worker registration id to use in tests requiring one. | 28 // Fake Service Worker registration id to use in tests requiring one. |
26 const int64_t kFakeServiceWorkerRegistrationId = 42; | 29 const int64_t kFakeServiceWorkerRegistrationId = 42; |
27 | 30 |
| 31 class NotificationBrowserClient : public TestContentBrowserClient { |
| 32 public: |
| 33 NotificationBrowserClient() |
| 34 : platform_notification_service_(new MockPlatformNotificationService()) {} |
| 35 |
| 36 PlatformNotificationService* GetPlatformNotificationService() override { |
| 37 return platform_notification_service_.get(); |
| 38 } |
| 39 |
| 40 private: |
| 41 std::unique_ptr<PlatformNotificationService> platform_notification_service_; |
| 42 }; |
| 43 |
28 class PlatformNotificationContextTest : public ::testing::Test { | 44 class PlatformNotificationContextTest : public ::testing::Test { |
29 public: | 45 public: |
30 PlatformNotificationContextTest() | 46 PlatformNotificationContextTest() |
31 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), success_(false) {} | 47 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), success_(false) {} |
32 | 48 |
33 // Callback to provide when reading a single notification from the database. | 49 // Callback to provide when reading a single notification from the database. |
34 void DidReadNotificationData(bool success, | 50 void DidReadNotificationData(bool success, |
35 const NotificationDatabaseData& database_data) { | 51 const NotificationDatabaseData& database_data) { |
36 success_ = success; | 52 success_ = success; |
37 database_data_ = database_data; | 53 database_data_ = database_data; |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 ASSERT_TRUE(success()); | 493 ASSERT_TRUE(success()); |
478 ASSERT_EQ(10u, notification_database_datas.size()); | 494 ASSERT_EQ(10u, notification_database_datas.size()); |
479 | 495 |
480 for (int i = 0; i < 10; ++i) { | 496 for (int i = 0; i < 10; ++i) { |
481 EXPECT_EQ(origin, notification_database_datas[i].origin); | 497 EXPECT_EQ(origin, notification_database_datas[i].origin); |
482 EXPECT_EQ(kFakeServiceWorkerRegistrationId, | 498 EXPECT_EQ(kFakeServiceWorkerRegistrationId, |
483 notification_database_datas[i].service_worker_registration_id); | 499 notification_database_datas[i].service_worker_registration_id); |
484 } | 500 } |
485 } | 501 } |
486 | 502 |
| 503 TEST_F(PlatformNotificationContextTest, SynchronizeNotifications) { |
| 504 NotificationBrowserClient notification_browser_client; |
| 505 SetBrowserClientForTesting(¬ification_browser_client); |
| 506 |
| 507 scoped_refptr<PlatformNotificationContextImpl> context = |
| 508 CreatePlatformNotificationContext(); |
| 509 |
| 510 GURL origin("https://example.com"); |
| 511 NotificationDatabaseData notification_database_data; |
| 512 notification_database_data.service_worker_registration_id = |
| 513 kFakeServiceWorkerRegistrationId; |
| 514 PlatformNotificationData notification_data; |
| 515 content::NotificationResources notification_resources; |
| 516 |
| 517 context->WriteNotificationData( |
| 518 origin, notification_database_data, |
| 519 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
| 520 base::Unretained(this))); |
| 521 |
| 522 base::RunLoop().RunUntilIdle(); |
| 523 ASSERT_TRUE(success()); |
| 524 EXPECT_FALSE(notification_id().empty()); |
| 525 |
| 526 PlatformNotificationService* service = |
| 527 notification_browser_client.GetPlatformNotificationService(); |
| 528 |
| 529 service->DisplayPersistentNotification(browser_context(), notification_id(), |
| 530 origin, origin, notification_data, |
| 531 notification_resources); |
| 532 |
| 533 std::vector<NotificationDatabaseData> notification_database_datas; |
| 534 context->ReadAllNotificationDataForServiceWorkerRegistration( |
| 535 origin, kFakeServiceWorkerRegistrationId, |
| 536 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas, |
| 537 base::Unretained(this), ¬ification_database_datas)); |
| 538 |
| 539 base::RunLoop().RunUntilIdle(); |
| 540 |
| 541 ASSERT_TRUE(success()); |
| 542 ASSERT_EQ(1u, notification_database_datas.size()); |
| 543 |
| 544 // Delete the notification from the display service without removing it from |
| 545 // the database. It should automatically synchronize on the next read. |
| 546 service->ClosePersistentNotification(browser_context(), notification_id()); |
| 547 context->ReadAllNotificationDataForServiceWorkerRegistration( |
| 548 origin, kFakeServiceWorkerRegistrationId, |
| 549 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas, |
| 550 base::Unretained(this), ¬ification_database_datas)); |
| 551 base::RunLoop().RunUntilIdle(); |
| 552 |
| 553 ASSERT_TRUE(success()); |
| 554 ASSERT_EQ(0u, notification_database_datas.size()); |
| 555 |
| 556 context->ReadNotificationData( |
| 557 notification_id(), origin, |
| 558 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
| 559 base::Unretained(this))); |
| 560 |
| 561 base::RunLoop().RunUntilIdle(); |
| 562 |
| 563 // The notification was removed, so we shouldn't be able to read it from |
| 564 // the database anymore. |
| 565 EXPECT_FALSE(success()); |
| 566 } |
| 567 |
487 } // namespace content | 568 } // namespace content |
OLD | NEW |