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
3 // found in the LICENSE file.
5 #include "content/browser/notifications/platform_notification_context.h"
8 #include "base/run_loop.h"
9 #include "content/browser/notifications/notification_database_data.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 class PlatformNotificationContextTest
: public ::testing::Test
{
18 PlatformNotificationContextTest()
19 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
22 // Callback to provide when reading a single notification from the database.
23 void DidReadNotificationData(
24 bool success
, const NotificationDatabaseData
& database_data
) {
26 database_data_
= database_data
;
29 // Callback to provide when writing a notification to the database.
30 void DidWriteNotificationData(bool success
, int64_t notification_id
) {
32 notification_id_
= notification_id
;
36 // Creates a new PlatformNotificationContext instance. When using this method,
37 // the underlying database will always be created in memory. The current
38 // message loop proxy will be used as the task runner.
39 PlatformNotificationContext
* CreatePlatformNotificationContext() {
40 PlatformNotificationContext
* context
=
41 new PlatformNotificationContext(base::FilePath());
43 context
->SetTaskRunnerForTesting(base::MessageLoopProxy::current());
47 // Returns whether the last invoked callback finished successfully.
48 bool success() const { return success_
; }
50 // Returns the NotificationDatabaseData associated with the last invoked
51 // ReadNotificationData callback.
52 const NotificationDatabaseData
& database_data() const {
53 return database_data_
;
56 // Returns the notification id of the notification last written.
57 int64_t notification_id() const { return notification_id_
; }
60 TestBrowserThreadBundle thread_bundle_
;
63 NotificationDatabaseData database_data_
;
64 int64_t notification_id_
;
67 TEST_F(PlatformNotificationContextTest
, ReadNonExistentNotification
) {
68 scoped_refptr
<PlatformNotificationContext
> context
=
69 CreatePlatformNotificationContext();
71 context
->ReadNotificationData(
72 42 /* notification_id */,
73 GURL("https://example.com"),
74 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
75 base::Unretained(this)));
77 base::RunLoop().RunUntilIdle();
79 // The read operation should have failed, as it does not exist.
80 ASSERT_FALSE(success());
83 TEST_F(PlatformNotificationContextTest
, WriteReadNotification
) {
84 scoped_refptr
<PlatformNotificationContext
> context
=
85 CreatePlatformNotificationContext();
87 GURL
origin("https://example.com");
88 NotificationDatabaseData notification_database_data
;
89 notification_database_data
.origin
= origin
;
91 context
->WriteNotificationData(
93 notification_database_data
,
94 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData
,
95 base::Unretained(this)));
97 base::RunLoop().RunUntilIdle();
99 // The write operation should have succeeded with a notification id.
100 ASSERT_TRUE(success());
101 EXPECT_GT(notification_id(), 0);
103 context
->ReadNotificationData(
106 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
107 base::Unretained(this)));
109 base::RunLoop().RunUntilIdle();
111 // The read operation should have succeeded, with the right notification.
112 ASSERT_TRUE(success());
114 const NotificationDatabaseData
& read_database_data
= database_data();
115 EXPECT_EQ(notification_database_data
.origin
, read_database_data
.origin
);
118 } // namespace content