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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "content/browser/notifications/platform_notification_context_impl.h" | 7 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 10 #include "content/common/service_worker/service_worker_types.h" |
8 #include "content/public/browser/notification_database_data.h" | 11 #include "content/public/browser/notification_database_data.h" |
9 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 14 #include "url/gurl.h" |
12 | 15 |
13 namespace content { | 16 namespace content { |
14 | 17 |
| 18 // Fake render process id to use in tests requiring one. |
| 19 const int kFakeRenderProcessId = 99; |
| 20 |
15 class PlatformNotificationContextTest : public ::testing::Test { | 21 class PlatformNotificationContextTest : public ::testing::Test { |
16 public: | 22 public: |
17 PlatformNotificationContextTest() | 23 PlatformNotificationContextTest() |
18 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
19 success_(false) {} | 25 success_(false) {} |
20 | 26 |
21 // Callback to provide when reading a single notification from the database. | 27 // Callback to provide when reading a single notification from the database. |
22 void DidReadNotificationData( | 28 void DidReadNotificationData( |
23 bool success, const NotificationDatabaseData& database_data) { | 29 bool success, const NotificationDatabaseData& database_data) { |
24 success_ = success; | 30 success_ = success; |
25 database_data_ = database_data; | 31 database_data_ = database_data; |
26 } | 32 } |
27 | 33 |
28 // Callback to provide when writing a notification to the database. | 34 // Callback to provide when writing a notification to the database. |
29 void DidWriteNotificationData(bool success, int64_t notification_id) { | 35 void DidWriteNotificationData(bool success, int64_t notification_id) { |
30 success_ = success; | 36 success_ = success; |
31 notification_id_ = notification_id; | 37 notification_id_ = notification_id; |
32 } | 38 } |
33 | 39 |
34 // Callback to provide when deleting notification data from the database. | 40 // Callback to provide when deleting notification data from the database. |
35 void DidDeleteNotificationData(bool success) { | 41 void DidDeleteNotificationData(bool success) { |
36 success_ = success; | 42 success_ = success; |
37 } | 43 } |
38 | 44 |
| 45 // Callback to provide when registering a Service Worker with a Service |
| 46 // Worker Context. Will write the registration id to |store_registration_id|. |
| 47 void DidRegisterServiceWorker(int64_t* store_registration_id, |
| 48 ServiceWorkerStatusCode status, |
| 49 const std::string& status_message, |
| 50 int64_t service_worker_registration_id) { |
| 51 DCHECK(store_registration_id); |
| 52 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 53 |
| 54 *store_registration_id = service_worker_registration_id; |
| 55 } |
| 56 |
| 57 // Callback to provide when unregistering a Service Worker. Will write the |
| 58 // resulting status code to |store_status|. |
| 59 void DidUnregisterServiceWorker(ServiceWorkerStatusCode* store_status, |
| 60 ServiceWorkerStatusCode status) { |
| 61 DCHECK(store_status); |
| 62 *store_status = status; |
| 63 } |
| 64 |
39 protected: | 65 protected: |
40 // Creates a new PlatformNotificationContextImpl instance. When using this | 66 // Creates a new PlatformNotificationContextImpl instance. When using this |
41 // method, the underlying database will always be created in memory. The | 67 // method, the underlying database will always be created in memory. The |
42 // current message loop proxy will be used as the task runner. | 68 // current message loop proxy will be used as the task runner. |
43 PlatformNotificationContextImpl* CreatePlatformNotificationContext() { | 69 PlatformNotificationContextImpl* CreatePlatformNotificationContext() { |
44 PlatformNotificationContextImpl* context = | 70 PlatformNotificationContextImpl* context = |
45 new PlatformNotificationContextImpl(base::FilePath()); | 71 new PlatformNotificationContextImpl(base::FilePath(), nullptr); |
| 72 context->Initialize(); |
46 | 73 |
| 74 OverrideTaskRunnerForTesting(context); |
| 75 return context; |
| 76 } |
| 77 |
| 78 // Overrides the task runner in |context| with the current message loop |
| 79 // proxy, to reduce the number of threads involved in the tests. |
| 80 void OverrideTaskRunnerForTesting(PlatformNotificationContextImpl* context) { |
47 context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); | 81 context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); |
48 return context; | |
49 } | 82 } |
50 | 83 |
51 // Returns whether the last invoked callback finished successfully. | 84 // Returns whether the last invoked callback finished successfully. |
52 bool success() const { return success_; } | 85 bool success() const { return success_; } |
53 | 86 |
54 // Returns the NotificationDatabaseData associated with the last invoked | 87 // Returns the NotificationDatabaseData associated with the last invoked |
55 // ReadNotificationData callback. | 88 // ReadNotificationData callback. |
56 const NotificationDatabaseData& database_data() const { | 89 const NotificationDatabaseData& database_data() const { |
57 return database_data_; | 90 return database_data_; |
58 } | 91 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, | 206 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
174 base::Unretained(this))); | 207 base::Unretained(this))); |
175 | 208 |
176 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
177 | 210 |
178 // The notification was removed, so we shouldn't be able to read it from | 211 // The notification was removed, so we shouldn't be able to read it from |
179 // the database anymore. | 212 // the database anymore. |
180 EXPECT_FALSE(success()); | 213 EXPECT_FALSE(success()); |
181 } | 214 } |
182 | 215 |
| 216 TEST_F(PlatformNotificationContextTest, ServiceWorkerUnregistered) { |
| 217 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_test_helper( |
| 218 new EmbeddedWorkerTestHelper(base::FilePath(), kFakeRenderProcessId)); |
| 219 |
| 220 // Manually create the PlatformNotificationContextImpl so that the Service |
| 221 // Worker context wrapper can be passed in. |
| 222 scoped_refptr<PlatformNotificationContextImpl> notification_context( |
| 223 new PlatformNotificationContextImpl( |
| 224 base::FilePath(), |
| 225 embedded_worker_test_helper->context_wrapper())); |
| 226 notification_context->Initialize(); |
| 227 |
| 228 OverrideTaskRunnerForTesting(notification_context.get()); |
| 229 |
| 230 GURL origin("https://example.com"); |
| 231 GURL script_url("https://example.com/worker.js"); |
| 232 |
| 233 int64_t service_worker_registration_id = kInvalidServiceWorkerRegistrationId; |
| 234 |
| 235 // Register a Service Worker to get a valid registration id. |
| 236 embedded_worker_test_helper->context()->RegisterServiceWorker( |
| 237 origin, |
| 238 script_url, |
| 239 nullptr /* provider_host */, |
| 240 base::Bind(&PlatformNotificationContextTest::DidRegisterServiceWorker, |
| 241 base::Unretained(this), &service_worker_registration_id)); |
| 242 |
| 243 base::RunLoop().RunUntilIdle(); |
| 244 ASSERT_NE(service_worker_registration_id, |
| 245 kInvalidServiceWorkerRegistrationId); |
| 246 |
| 247 NotificationDatabaseData notification_database_data; |
| 248 |
| 249 // Create a notification for that Service Worker registration. |
| 250 notification_context->WriteNotificationData( |
| 251 origin, |
| 252 notification_database_data, |
| 253 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
| 254 base::Unretained(this))); |
| 255 |
| 256 base::RunLoop().RunUntilIdle(); |
| 257 |
| 258 ASSERT_TRUE(success()); |
| 259 EXPECT_GT(notification_id(), 0); |
| 260 |
| 261 ServiceWorkerStatusCode unregister_status; |
| 262 |
| 263 // Now drop the Service Worker registration which owns that notification. |
| 264 embedded_worker_test_helper->context()->UnregisterServiceWorker( |
| 265 origin, |
| 266 base::Bind(&PlatformNotificationContextTest::DidUnregisterServiceWorker, |
| 267 base::Unretained(this), &unregister_status)); |
| 268 |
| 269 base::RunLoop().RunUntilIdle(); |
| 270 ASSERT_EQ(SERVICE_WORKER_OK, unregister_status); |
| 271 |
| 272 // And verify that the associated notification has indeed been dropped. |
| 273 notification_context->ReadNotificationData( |
| 274 notification_id(), |
| 275 origin, |
| 276 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
| 277 base::Unretained(this))); |
| 278 |
| 279 base::RunLoop().RunUntilIdle(); |
| 280 |
| 281 EXPECT_FALSE(success()); |
| 282 } |
| 283 |
183 } // namespace content | 284 } // namespace content |
OLD | NEW |