Index: content/browser/notifications/platform_notification_context_unittest.cc |
diff --git a/content/browser/notifications/platform_notification_context_unittest.cc b/content/browser/notifications/platform_notification_context_unittest.cc |
index c42b3c5bebd0a1c38049037934616bb60c109a2f..fde53f4fd822e7b13ff746e94f6d89f989ea9272 100644 |
--- a/content/browser/notifications/platform_notification_context_unittest.cc |
+++ b/content/browser/notifications/platform_notification_context_unittest.cc |
@@ -5,6 +5,9 @@ |
#include "base/bind.h" |
#include "base/run_loop.h" |
#include "content/browser/notifications/platform_notification_context_impl.h" |
+#include "content/browser/service_worker/embedded_worker_test_helper.h" |
+#include "content/browser/service_worker/service_worker_context_wrapper.h" |
+#include "content/common/service_worker/service_worker_types.h" |
#include "content/public/browser/notification_database_data.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -12,6 +15,9 @@ |
namespace content { |
+// Fake render process id to use in tests requiring one. |
+const int kFakeRenderProcessId = 99; |
+ |
class PlatformNotificationContextTest : public ::testing::Test { |
public: |
PlatformNotificationContextTest() |
@@ -36,18 +42,45 @@ class PlatformNotificationContextTest : public ::testing::Test { |
success_ = success; |
} |
+ // Callback to provide when registering a Service Worker with a Service |
+ // Worker Context. Will write the registration id to |store_registration_id|. |
+ void DidRegisterServiceWorker(int64_t* store_registration_id, |
+ ServiceWorkerStatusCode status, |
+ const std::string& status_message, |
+ int64_t service_worker_registration_id) { |
+ DCHECK(store_registration_id); |
+ EXPECT_EQ(SERVICE_WORKER_OK, status); |
+ |
+ *store_registration_id = service_worker_registration_id; |
+ } |
+ |
+ // Callback to provide when unregistering a Service Worker. Will write the |
+ // resulting status code to |store_status|. |
+ void DidUnregisterServiceWorker(ServiceWorkerStatusCode* store_status, |
+ ServiceWorkerStatusCode status) { |
+ DCHECK(store_status); |
+ *store_status = status; |
+ } |
+ |
protected: |
// Creates a new PlatformNotificationContextImpl instance. When using this |
// method, the underlying database will always be created in memory. The |
// current message loop proxy will be used as the task runner. |
PlatformNotificationContextImpl* CreatePlatformNotificationContext() { |
PlatformNotificationContextImpl* context = |
- new PlatformNotificationContextImpl(base::FilePath()); |
+ new PlatformNotificationContextImpl(base::FilePath(), nullptr); |
+ context->Initialize(); |
- context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); |
+ OverrideTaskRunnerForTesting(context); |
return context; |
} |
+ // Overrides the task runner in |context| with the current message loop |
+ // proxy, to reduce the number of threads involved in the tests. |
+ void OverrideTaskRunnerForTesting(PlatformNotificationContextImpl* context) { |
+ context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); |
+ } |
+ |
// Returns whether the last invoked callback finished successfully. |
bool success() const { return success_; } |
@@ -180,4 +213,72 @@ TEST_F(PlatformNotificationContextTest, DeleteNotification) { |
EXPECT_FALSE(success()); |
} |
+TEST_F(PlatformNotificationContextTest, ServiceWorkerUnregistered) { |
+ scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_test_helper( |
+ new EmbeddedWorkerTestHelper(base::FilePath(), kFakeRenderProcessId)); |
+ |
+ // Manually create the PlatformNotificationContextImpl so that the Service |
+ // Worker context wrapper can be passed in. |
+ scoped_refptr<PlatformNotificationContextImpl> notification_context( |
+ new PlatformNotificationContextImpl( |
+ base::FilePath(), |
+ embedded_worker_test_helper->context_wrapper())); |
+ notification_context->Initialize(); |
+ |
+ OverrideTaskRunnerForTesting(notification_context.get()); |
+ |
+ GURL origin("https://example.com"); |
+ GURL script_url("https://example.com/worker.js"); |
+ |
+ int64_t service_worker_registration_id = kInvalidServiceWorkerRegistrationId; |
+ |
+ // Register a Service Worker to get a valid registration id. |
+ embedded_worker_test_helper->context()->RegisterServiceWorker( |
+ origin, |
+ script_url, |
+ nullptr /* provider_host */, |
+ base::Bind(&PlatformNotificationContextTest::DidRegisterServiceWorker, |
+ base::Unretained(this), &service_worker_registration_id)); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ ASSERT_NE(service_worker_registration_id, |
+ kInvalidServiceWorkerRegistrationId); |
+ |
+ NotificationDatabaseData notification_database_data; |
+ |
+ // Create a notification for that Service Worker registration. |
+ notification_context->WriteNotificationData( |
+ origin, |
+ notification_database_data, |
+ base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
+ base::Unretained(this))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ ASSERT_TRUE(success()); |
+ EXPECT_GT(notification_id(), 0); |
+ |
+ ServiceWorkerStatusCode unregister_status; |
+ |
+ // Now drop the Service Worker registration which owns that notification. |
+ embedded_worker_test_helper->context()->UnregisterServiceWorker( |
+ origin, |
+ base::Bind(&PlatformNotificationContextTest::DidUnregisterServiceWorker, |
+ base::Unretained(this), &unregister_status)); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ ASSERT_EQ(SERVICE_WORKER_OK, unregister_status); |
+ |
+ // And verify that the associated notification has indeed been dropped. |
+ notification_context->ReadNotificationData( |
+ notification_id(), |
+ origin, |
+ base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
+ base::Unretained(this))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ EXPECT_FALSE(success()); |
+} |
+ |
} // namespace content |