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" | |
19 #include "content/public/test/test_browser_context.h" | 18 #include "content/public/test/test_browser_context.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 19 #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" | |
23 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "url/gurl.h" | 21 #include "url/gurl.h" |
25 | 22 |
26 namespace content { | 23 namespace content { |
27 | 24 |
28 // Fake Service Worker registration id to use in tests requiring one. | 25 // Fake Service Worker registration id to use in tests requiring one. |
29 const int64_t kFakeServiceWorkerRegistrationId = 42; | 26 const int64_t kFakeServiceWorkerRegistrationId = 42; |
30 | 27 |
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 | |
44 class PlatformNotificationContextTest : public ::testing::Test { | 28 class PlatformNotificationContextTest : public ::testing::Test { |
45 public: | 29 public: |
46 PlatformNotificationContextTest() | 30 PlatformNotificationContextTest() |
47 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), success_(false) {} | 31 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), success_(false) {} |
48 | 32 |
49 // Callback to provide when reading a single notification from the database. | 33 // Callback to provide when reading a single notification from the database. |
50 void DidReadNotificationData(bool success, | 34 void DidReadNotificationData(bool success, |
51 const NotificationDatabaseData& database_data) { | 35 const NotificationDatabaseData& database_data) { |
52 success_ = success; | 36 success_ = success; |
53 database_data_ = database_data; | 37 database_data_ = database_data; |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 ASSERT_TRUE(success()); | 477 ASSERT_TRUE(success()); |
494 ASSERT_EQ(10u, notification_database_datas.size()); | 478 ASSERT_EQ(10u, notification_database_datas.size()); |
495 | 479 |
496 for (int i = 0; i < 10; ++i) { | 480 for (int i = 0; i < 10; ++i) { |
497 EXPECT_EQ(origin, notification_database_datas[i].origin); | 481 EXPECT_EQ(origin, notification_database_datas[i].origin); |
498 EXPECT_EQ(kFakeServiceWorkerRegistrationId, | 482 EXPECT_EQ(kFakeServiceWorkerRegistrationId, |
499 notification_database_datas[i].service_worker_registration_id); | 483 notification_database_datas[i].service_worker_registration_id); |
500 } | 484 } |
501 } | 485 } |
502 | 486 |
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 | |
568 } // namespace content | 487 } // namespace content |
OLD | NEW |