| 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/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "content/browser/notifications/platform_notification_context_impl.h" | 9 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 12 #include "content/common/service_worker/service_worker_types.h" | 12 #include "content/common/service_worker/service_worker_types.h" |
| 13 #include "content/public/browser/notification_database_data.h" | 13 #include "content/public/browser/notification_database_data.h" |
| 14 #include "content/public/test/test_browser_context.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 // Fake render process id to use in tests requiring one. | 21 // Fake render process id to use in tests requiring one. |
| 21 const int kFakeRenderProcessId = 99; | 22 const int kFakeRenderProcessId = 99; |
| 22 | 23 |
| 23 // Fake Service Worker registration id to use in tests requiring one. | 24 // Fake Service Worker registration id to use in tests requiring one. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 success_ = success; | 80 success_ = success; |
| 80 *store_notification_datas = notification_datas; | 81 *store_notification_datas = notification_datas; |
| 81 } | 82 } |
| 82 | 83 |
| 83 protected: | 84 protected: |
| 84 // Creates a new PlatformNotificationContextImpl instance. When using this | 85 // Creates a new PlatformNotificationContextImpl instance. When using this |
| 85 // method, the underlying database will always be created in memory. The | 86 // method, the underlying database will always be created in memory. The |
| 86 // current message loop proxy will be used as the task runner. | 87 // current message loop proxy will be used as the task runner. |
| 87 PlatformNotificationContextImpl* CreatePlatformNotificationContext() { | 88 PlatformNotificationContextImpl* CreatePlatformNotificationContext() { |
| 88 PlatformNotificationContextImpl* context = | 89 PlatformNotificationContextImpl* context = |
| 89 new PlatformNotificationContextImpl(base::FilePath(), nullptr); | 90 new PlatformNotificationContextImpl(base::FilePath(), |
| 91 &browser_context_, |
| 92 nullptr); |
| 90 context->Initialize(); | 93 context->Initialize(); |
| 91 | 94 |
| 92 OverrideTaskRunnerForTesting(context); | 95 OverrideTaskRunnerForTesting(context); |
| 93 return context; | 96 return context; |
| 94 } | 97 } |
| 95 | 98 |
| 96 // Overrides the task runner in |context| with the current message loop | 99 // Overrides the task runner in |context| with the current message loop |
| 97 // proxy, to reduce the number of threads involved in the tests. | 100 // proxy, to reduce the number of threads involved in the tests. |
| 98 void OverrideTaskRunnerForTesting(PlatformNotificationContextImpl* context) { | 101 void OverrideTaskRunnerForTesting(PlatformNotificationContextImpl* context) { |
| 99 context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); | 102 context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); |
| 100 } | 103 } |
| 101 | 104 |
| 105 // Returns the testing browsing context that can be used for this test. |
| 106 BrowserContext* browser_context() { return &browser_context_; } |
| 107 |
| 102 // Returns whether the last invoked callback finished successfully. | 108 // Returns whether the last invoked callback finished successfully. |
| 103 bool success() const { return success_; } | 109 bool success() const { return success_; } |
| 104 | 110 |
| 105 // Returns the NotificationDatabaseData associated with the last invoked | 111 // Returns the NotificationDatabaseData associated with the last invoked |
| 106 // ReadNotificationData callback. | 112 // ReadNotificationData callback. |
| 107 const NotificationDatabaseData& database_data() const { | 113 const NotificationDatabaseData& database_data() const { |
| 108 return database_data_; | 114 return database_data_; |
| 109 } | 115 } |
| 110 | 116 |
| 111 // Returns the notification id of the notification last written. | 117 // Returns the notification id of the notification last written. |
| 112 int64_t notification_id() const { return notification_id_; } | 118 int64_t notification_id() const { return notification_id_; } |
| 113 | 119 |
| 114 private: | 120 private: |
| 115 TestBrowserThreadBundle thread_bundle_; | 121 TestBrowserThreadBundle thread_bundle_; |
| 122 TestBrowserContext browser_context_; |
| 116 | 123 |
| 117 bool success_; | 124 bool success_; |
| 118 NotificationDatabaseData database_data_; | 125 NotificationDatabaseData database_data_; |
| 119 int64_t notification_id_; | 126 int64_t notification_id_; |
| 120 }; | 127 }; |
| 121 | 128 |
| 122 TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) { | 129 TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) { |
| 123 scoped_refptr<PlatformNotificationContextImpl> context = | 130 scoped_refptr<PlatformNotificationContextImpl> context = |
| 124 CreatePlatformNotificationContext(); | 131 CreatePlatformNotificationContext(); |
| 125 | 132 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 240 |
| 234 TEST_F(PlatformNotificationContextTest, ServiceWorkerUnregistered) { | 241 TEST_F(PlatformNotificationContextTest, ServiceWorkerUnregistered) { |
| 235 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_test_helper( | 242 scoped_ptr<EmbeddedWorkerTestHelper> embedded_worker_test_helper( |
| 236 new EmbeddedWorkerTestHelper(base::FilePath(), kFakeRenderProcessId)); | 243 new EmbeddedWorkerTestHelper(base::FilePath(), kFakeRenderProcessId)); |
| 237 | 244 |
| 238 // Manually create the PlatformNotificationContextImpl so that the Service | 245 // Manually create the PlatformNotificationContextImpl so that the Service |
| 239 // Worker context wrapper can be passed in. | 246 // Worker context wrapper can be passed in. |
| 240 scoped_refptr<PlatformNotificationContextImpl> notification_context( | 247 scoped_refptr<PlatformNotificationContextImpl> notification_context( |
| 241 new PlatformNotificationContextImpl( | 248 new PlatformNotificationContextImpl( |
| 242 base::FilePath(), | 249 base::FilePath(), |
| 250 browser_context(), |
| 243 embedded_worker_test_helper->context_wrapper())); | 251 embedded_worker_test_helper->context_wrapper())); |
| 244 notification_context->Initialize(); | 252 notification_context->Initialize(); |
| 245 | 253 |
| 246 OverrideTaskRunnerForTesting(notification_context.get()); | 254 OverrideTaskRunnerForTesting(notification_context.get()); |
| 247 | 255 |
| 248 GURL origin("https://example.com"); | 256 GURL origin("https://example.com"); |
| 249 GURL script_url("https://example.com/worker.js"); | 257 GURL script_url("https://example.com/worker.js"); |
| 250 | 258 |
| 251 int64_t service_worker_registration_id = kInvalidServiceWorkerRegistrationId; | 259 int64_t service_worker_registration_id = kInvalidServiceWorkerRegistrationId; |
| 252 | 260 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 EXPECT_FALSE(success()); | 344 EXPECT_FALSE(success()); |
| 337 } | 345 } |
| 338 | 346 |
| 339 TEST_F(PlatformNotificationContextTest, DestroyOnDiskDatabase) { | 347 TEST_F(PlatformNotificationContextTest, DestroyOnDiskDatabase) { |
| 340 base::ScopedTempDir database_dir; | 348 base::ScopedTempDir database_dir; |
| 341 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 349 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 342 | 350 |
| 343 // Manually construct the PlatformNotificationContextImpl because this test | 351 // Manually construct the PlatformNotificationContextImpl because this test |
| 344 // requires the database to be created on the filesystem. | 352 // requires the database to be created on the filesystem. |
| 345 scoped_refptr<PlatformNotificationContextImpl> context( | 353 scoped_refptr<PlatformNotificationContextImpl> context( |
| 346 new PlatformNotificationContextImpl(database_dir.path(), nullptr)); | 354 new PlatformNotificationContextImpl(database_dir.path(), |
| 355 browser_context(), |
| 356 nullptr)); |
| 347 | 357 |
| 348 OverrideTaskRunnerForTesting(context.get()); | 358 OverrideTaskRunnerForTesting(context.get()); |
| 349 | 359 |
| 350 // Trigger a read-operation to force creating the database. | 360 // Trigger a read-operation to force creating the database. |
| 351 context->ReadNotificationData( | 361 context->ReadNotificationData( |
| 352 42 /* notification_id */, | 362 42 /* notification_id */, |
| 353 GURL("https://example.com"), | 363 GURL("https://example.com"), |
| 354 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, | 364 base::Bind(&PlatformNotificationContextTest::DidReadNotificationData, |
| 355 base::Unretained(this))); | 365 base::Unretained(this))); |
| 356 | 366 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 ASSERT_EQ(10u, notification_database_datas.size()); | 439 ASSERT_EQ(10u, notification_database_datas.size()); |
| 430 | 440 |
| 431 for (int i = 0; i < 10; ++i) { | 441 for (int i = 0; i < 10; ++i) { |
| 432 EXPECT_EQ(origin, notification_database_datas[i].origin); | 442 EXPECT_EQ(origin, notification_database_datas[i].origin); |
| 433 EXPECT_EQ(kFakeServiceWorkerRegistrationId, | 443 EXPECT_EQ(kFakeServiceWorkerRegistrationId, |
| 434 notification_database_datas[i].service_worker_registration_id); | 444 notification_database_datas[i].service_worker_registration_id); |
| 435 } | 445 } |
| 436 } | 446 } |
| 437 | 447 |
| 438 } // namespace content | 448 } // namespace content |
| OLD | NEW |