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.
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "content/browser/notifications/platform_notification_context_impl.h"
10 #include "content/browser/service_worker/embedded_worker_test_helper.h"
11 #include "content/browser/service_worker/service_worker_context_wrapper.h"
12 #include "content/common/service_worker/service_worker_types.h"
13 #include "content/public/browser/notification_database_data.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 // Fake render process id to use in tests requiring one.
21 const int kFakeRenderProcessId
= 99;
23 class PlatformNotificationContextTest
: public ::testing::Test
{
25 PlatformNotificationContextTest()
26 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
29 // Callback to provide when reading a single notification from the database.
30 void DidReadNotificationData(
31 bool success
, const NotificationDatabaseData
& database_data
) {
33 database_data_
= database_data
;
36 // Callback to provide when writing a notification to the database.
37 void DidWriteNotificationData(bool success
, int64_t notification_id
) {
39 notification_id_
= notification_id
;
42 // Callback to provide when deleting notification data from the database.
43 void DidDeleteNotificationData(bool success
) {
47 // Callback to provide when registering a Service Worker with a Service
48 // Worker Context. Will write the registration id to |store_registration_id|.
49 void DidRegisterServiceWorker(int64_t* store_registration_id
,
50 ServiceWorkerStatusCode status
,
51 const std::string
& status_message
,
52 int64_t service_worker_registration_id
) {
53 DCHECK(store_registration_id
);
54 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
56 *store_registration_id
= service_worker_registration_id
;
59 // Callback to provide when unregistering a Service Worker. Will write the
60 // resulting status code to |store_status|.
61 void DidUnregisterServiceWorker(ServiceWorkerStatusCode
* store_status
,
62 ServiceWorkerStatusCode status
) {
64 *store_status
= status
;
68 // Creates a new PlatformNotificationContextImpl instance. When using this
69 // method, the underlying database will always be created in memory. The
70 // current message loop proxy will be used as the task runner.
71 PlatformNotificationContextImpl
* CreatePlatformNotificationContext() {
72 PlatformNotificationContextImpl
* context
=
73 new PlatformNotificationContextImpl(base::FilePath(), nullptr);
74 context
->Initialize();
76 OverrideTaskRunnerForTesting(context
);
80 // Overrides the task runner in |context| with the current message loop
81 // proxy, to reduce the number of threads involved in the tests.
82 void OverrideTaskRunnerForTesting(PlatformNotificationContextImpl
* context
) {
83 context
->SetTaskRunnerForTesting(base::MessageLoopProxy::current());
86 // Returns whether the last invoked callback finished successfully.
87 bool success() const { return success_
; }
89 // Returns the NotificationDatabaseData associated with the last invoked
90 // ReadNotificationData callback.
91 const NotificationDatabaseData
& database_data() const {
92 return database_data_
;
95 // Returns the notification id of the notification last written.
96 int64_t notification_id() const { return notification_id_
; }
99 TestBrowserThreadBundle thread_bundle_
;
102 NotificationDatabaseData database_data_
;
103 int64_t notification_id_
;
106 TEST_F(PlatformNotificationContextTest
, ReadNonExistentNotification
) {
107 scoped_refptr
<PlatformNotificationContextImpl
> context
=
108 CreatePlatformNotificationContext();
110 context
->ReadNotificationData(
111 42 /* notification_id */,
112 GURL("https://example.com"),
113 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
114 base::Unretained(this)));
116 base::RunLoop().RunUntilIdle();
118 // The read operation should have failed, as it does not exist.
119 ASSERT_FALSE(success());
122 TEST_F(PlatformNotificationContextTest
, WriteReadNotification
) {
123 scoped_refptr
<PlatformNotificationContextImpl
> context
=
124 CreatePlatformNotificationContext();
126 GURL
origin("https://example.com");
127 NotificationDatabaseData notification_database_data
;
128 notification_database_data
.origin
= origin
;
130 context
->WriteNotificationData(
132 notification_database_data
,
133 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData
,
134 base::Unretained(this)));
136 base::RunLoop().RunUntilIdle();
138 // The write operation should have succeeded with a notification id.
139 ASSERT_TRUE(success());
140 EXPECT_GT(notification_id(), 0);
142 context
->ReadNotificationData(
145 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
146 base::Unretained(this)));
148 base::RunLoop().RunUntilIdle();
150 // The read operation should have succeeded, with the right notification.
151 ASSERT_TRUE(success());
153 const NotificationDatabaseData
& read_database_data
= database_data();
154 EXPECT_EQ(notification_database_data
.origin
, read_database_data
.origin
);
157 TEST_F(PlatformNotificationContextTest
, DeleteInvalidNotification
) {
158 scoped_refptr
<PlatformNotificationContextImpl
> context
=
159 CreatePlatformNotificationContext();
161 context
->DeleteNotificationData(
162 42 /* notification_id */,
163 GURL("https://example.com"),
164 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData
,
165 base::Unretained(this)));
167 base::RunLoop().RunUntilIdle();
169 // The notification may not have existed, but since the goal of deleting data
170 // is to make sure that it's gone, the goal has been satisfied. As such,
171 // deleting a non-existent notification is considered to be a success.
172 EXPECT_TRUE(success());
175 TEST_F(PlatformNotificationContextTest
, DeleteNotification
) {
176 scoped_refptr
<PlatformNotificationContextImpl
> context
=
177 CreatePlatformNotificationContext();
179 GURL
origin("https://example.com");
180 NotificationDatabaseData notification_database_data
;
182 context
->WriteNotificationData(
184 notification_database_data
,
185 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData
,
186 base::Unretained(this)));
188 base::RunLoop().RunUntilIdle();
190 // The write operation should have succeeded with a notification id.
191 ASSERT_TRUE(success());
192 EXPECT_GT(notification_id(), 0);
194 context
->DeleteNotificationData(
197 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData
,
198 base::Unretained(this)));
200 base::RunLoop().RunUntilIdle();
202 // The notification existed, so it should have been removed successfully.
203 ASSERT_TRUE(success());
205 context
->ReadNotificationData(
208 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
209 base::Unretained(this)));
211 base::RunLoop().RunUntilIdle();
213 // The notification was removed, so we shouldn't be able to read it from
214 // the database anymore.
215 EXPECT_FALSE(success());
218 TEST_F(PlatformNotificationContextTest
, ServiceWorkerUnregistered
) {
219 scoped_ptr
<EmbeddedWorkerTestHelper
> embedded_worker_test_helper(
220 new EmbeddedWorkerTestHelper(base::FilePath(), kFakeRenderProcessId
));
222 // Manually create the PlatformNotificationContextImpl so that the Service
223 // Worker context wrapper can be passed in.
224 scoped_refptr
<PlatformNotificationContextImpl
> notification_context(
225 new PlatformNotificationContextImpl(
227 embedded_worker_test_helper
->context_wrapper()));
228 notification_context
->Initialize();
230 OverrideTaskRunnerForTesting(notification_context
.get());
232 GURL
origin("https://example.com");
233 GURL
script_url("https://example.com/worker.js");
235 int64_t service_worker_registration_id
= kInvalidServiceWorkerRegistrationId
;
237 // Register a Service Worker to get a valid registration id.
238 embedded_worker_test_helper
->context()->RegisterServiceWorker(
241 nullptr /* provider_host */,
242 base::Bind(&PlatformNotificationContextTest::DidRegisterServiceWorker
,
243 base::Unretained(this), &service_worker_registration_id
));
245 base::RunLoop().RunUntilIdle();
246 ASSERT_NE(service_worker_registration_id
,
247 kInvalidServiceWorkerRegistrationId
);
249 NotificationDatabaseData notification_database_data
;
251 // Create a notification for that Service Worker registration.
252 notification_context
->WriteNotificationData(
254 notification_database_data
,
255 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData
,
256 base::Unretained(this)));
258 base::RunLoop().RunUntilIdle();
260 ASSERT_TRUE(success());
261 EXPECT_GT(notification_id(), 0);
263 ServiceWorkerStatusCode unregister_status
;
265 // Now drop the Service Worker registration which owns that notification.
266 embedded_worker_test_helper
->context()->UnregisterServiceWorker(
268 base::Bind(&PlatformNotificationContextTest::DidUnregisterServiceWorker
,
269 base::Unretained(this), &unregister_status
));
271 base::RunLoop().RunUntilIdle();
272 ASSERT_EQ(SERVICE_WORKER_OK
, unregister_status
);
274 // And verify that the associated notification has indeed been dropped.
275 notification_context
->ReadNotificationData(
278 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
279 base::Unretained(this)));
281 base::RunLoop().RunUntilIdle();
283 EXPECT_FALSE(success());
286 TEST_F(PlatformNotificationContextTest
, DestroyDatabaseOnStorageWiped
) {
287 scoped_refptr
<PlatformNotificationContextImpl
> context
=
288 CreatePlatformNotificationContext();
290 GURL
origin("https://example.com");
291 NotificationDatabaseData notification_database_data
;
293 context
->WriteNotificationData(
295 notification_database_data
,
296 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData
,
297 base::Unretained(this)));
299 base::RunLoop().RunUntilIdle();
301 // The write operation should have succeeded with a notification id.
302 ASSERT_TRUE(success());
303 EXPECT_GT(notification_id(), 0);
305 // Call the OnStorageWiped override from the ServiceWorkerContextObserver,
306 // which indicates that the database should go away entirely.
307 context
->OnStorageWiped();
309 // Verify that reading notification data fails because the data does not
310 // exist anymore. Deliberately omit RunUntilIdle(), since this is unlikely to
311 // be the case when OnStorageWiped gets called in production.
312 context
->ReadNotificationData(
315 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
316 base::Unretained(this)));
318 base::RunLoop().RunUntilIdle();
320 EXPECT_FALSE(success());
323 TEST_F(PlatformNotificationContextTest
, DestroyOnDiskDatabase
) {
324 base::ScopedTempDir database_dir
;
325 ASSERT_TRUE(database_dir
.CreateUniqueTempDir());
327 // Manually construct the PlatformNotificationContextImpl because this test
328 // requires the database to be created on the filesystem.
329 scoped_refptr
<PlatformNotificationContextImpl
> context(
330 new PlatformNotificationContextImpl(database_dir
.path(), nullptr));
332 OverrideTaskRunnerForTesting(context
.get());
334 // Trigger a read-operation to force creating the database.
335 context
->ReadNotificationData(
336 42 /* notification_id */,
337 GURL("https://example.com"),
338 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData
,
339 base::Unretained(this)));
341 base::RunLoop().RunUntilIdle();
343 EXPECT_FALSE(IsDirectoryEmpty(database_dir
.path()));
344 EXPECT_FALSE(success());
346 // Blow away the database by faking a Service Worker Context wipe-out.
347 context
->OnStorageWiped();
349 base::RunLoop().RunUntilIdle();
351 // The database's directory should be empty at this point.
352 EXPECT_TRUE(IsDirectoryEmpty(database_dir
.path()));
355 } // namespace content