Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: content/browser/notifications/platform_notification_context_unittest.cc

Issue 2672313004: Test the platform notification context synchronize operation (Closed)
Patch Set: - Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(&notification_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 notification_resources.action_icons.resize(notification_data.actions.size());
Peter Beverloo 2017/02/08 14:05:11 Why is this line necessary? Neither |notification_
Miguel Garcia 2017/02/08 17:03:41 it's not needed.
517
518 context->WriteNotificationData(
519 origin, notification_database_data,
520 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
521 base::Unretained(this)));
522
523 base::RunLoop().RunUntilIdle();
524 ASSERT_TRUE(success());
525 EXPECT_FALSE(notification_id().empty());
526
527 PlatformNotificationService* service =
528 notification_browser_client.GetPlatformNotificationService();
529
530 service->DisplayPersistentNotification(browser_context(), notification_id(),
531 origin, origin, notification_data,
532 notification_resources);
533
534 std::vector<NotificationDatabaseData> notification_database_datas;
535 context->ReadAllNotificationDataForServiceWorkerRegistration(
536 origin, kFakeServiceWorkerRegistrationId,
537 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas,
538 base::Unretained(this), &notification_database_datas));
539
540 base::RunLoop().RunUntilIdle();
541
542 ASSERT_TRUE(success());
543 ASSERT_EQ(1u, notification_database_datas.size());
544
545 // Delete the notification from the display service without removing it from
546 // the database. It should automatically synchronize on the next read.
547 service->ClosePersistentNotification(browser_context(), notification_id());
548 context->ReadAllNotificationDataForServiceWorkerRegistration(
549 origin, kFakeServiceWorkerRegistrationId,
550 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas,
551 base::Unretained(this), &notification_database_datas));
552 base::RunLoop().RunUntilIdle();
553
554 ASSERT_TRUE(success());
555 ASSERT_EQ(0u, notification_database_datas.size());
556
557 context->ReadNotificationData(
558 notification_id(), origin,
559 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData,
560 base::Unretained(this)));
561
562 base::RunLoop().RunUntilIdle();
563
564 // The notification was removed, so we shouldn't be able to read it from
565 // the database anymore.
566 EXPECT_FALSE(success());
567 }
568
487 } // namespace content 569 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698