| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "content/browser/notifications/platform_notification_context_impl.h" | 13 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 13 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 14 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 15 #include "content/common/service_worker/service_worker_types.h" | 16 #include "content/common/service_worker/service_worker_types.h" |
| 16 #include "content/public/browser/notification_database_data.h" | 17 #include "content/public/browser/notification_database_data.h" |
| 17 #include "content/public/test/test_browser_context.h" | 18 #include "content/public/test/test_browser_context.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 163 |
| 163 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
| 164 | 165 |
| 165 // The read operation should have succeeded, with the right notification. | 166 // The read operation should have succeeded, with the right notification. |
| 166 ASSERT_TRUE(success()); | 167 ASSERT_TRUE(success()); |
| 167 | 168 |
| 168 const NotificationDatabaseData& read_database_data = database_data(); | 169 const NotificationDatabaseData& read_database_data = database_data(); |
| 169 EXPECT_EQ(notification_database_data.origin, read_database_data.origin); | 170 EXPECT_EQ(notification_database_data.origin, read_database_data.origin); |
| 170 } | 171 } |
| 171 | 172 |
| 173 TEST_F(PlatformNotificationContextTest, WriteReadReplacedNotification) { |
| 174 scoped_refptr<PlatformNotificationContextImpl> context = |
| 175 CreatePlatformNotificationContext(); |
| 176 |
| 177 const GURL origin("https://example.com"); |
| 178 const std::string tag = "foo"; |
| 179 |
| 180 NotificationDatabaseData notification_database_data; |
| 181 notification_database_data.service_worker_registration_id = |
| 182 kFakeServiceWorkerRegistrationId; |
| 183 notification_database_data.origin = origin; |
| 184 notification_database_data.notification_data.title = |
| 185 base::ASCIIToUTF16("First"); |
| 186 notification_database_data.notification_data.tag = tag; |
| 187 |
| 188 // Write the first notification with the given |tag|. |
| 189 context->WriteNotificationData( |
| 190 origin, notification_database_data, |
| 191 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
| 192 base::Unretained(this))); |
| 193 |
| 194 base::RunLoop().RunUntilIdle(); |
| 195 |
| 196 int64_t read_notification_id = notification_id(); |
| 197 |
| 198 // The write operation should have succeeded with a notification id. |
| 199 ASSERT_TRUE(success()); |
| 200 EXPECT_GT(read_notification_id, 0); |
| 201 |
| 202 notification_database_data.notification_data.title = |
| 203 base::ASCIIToUTF16("Second"); |
| 204 |
| 205 // Write the second notification with the given |tag|. |
| 206 context->WriteNotificationData( |
| 207 origin, notification_database_data, |
| 208 base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData, |
| 209 base::Unretained(this))); |
| 210 |
| 211 base::RunLoop().RunUntilIdle(); |
| 212 |
| 213 ASSERT_TRUE(success()); |
| 214 ASSERT_NE(notification_id(), read_notification_id); |
| 215 ASSERT_GT(notification_id(), 0); |
| 216 |
| 217 // Reading the notifications should only yield the second, replaced one. |
| 218 std::vector<NotificationDatabaseData> notification_database_datas; |
| 219 context->ReadAllNotificationDataForServiceWorkerRegistration( |
| 220 origin, kFakeServiceWorkerRegistrationId, |
| 221 base::Bind(&PlatformNotificationContextTest::DidReadAllNotificationDatas, |
| 222 base::Unretained(this), ¬ification_database_datas)); |
| 223 |
| 224 base::RunLoop().RunUntilIdle(); |
| 225 |
| 226 // The read operation should have succeeded, with the right notification. |
| 227 ASSERT_TRUE(success()); |
| 228 |
| 229 ASSERT_EQ(1u, notification_database_datas.size()); |
| 230 |
| 231 EXPECT_EQ(tag, notification_database_datas[0].notification_data.tag); |
| 232 EXPECT_EQ(base::ASCIIToUTF16("Second"), |
| 233 notification_database_datas[0].notification_data.title); |
| 234 } |
| 235 |
| 172 TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) { | 236 TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) { |
| 173 scoped_refptr<PlatformNotificationContextImpl> context = | 237 scoped_refptr<PlatformNotificationContextImpl> context = |
| 174 CreatePlatformNotificationContext(); | 238 CreatePlatformNotificationContext(); |
| 175 | 239 |
| 176 context->DeleteNotificationData( | 240 context->DeleteNotificationData( |
| 177 42 /* notification_id */, GURL("https://example.com"), | 241 42 /* notification_id */, GURL("https://example.com"), |
| 178 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData, | 242 base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData, |
| 179 base::Unretained(this))); | 243 base::Unretained(this))); |
| 180 | 244 |
| 181 base::RunLoop().RunUntilIdle(); | 245 base::RunLoop().RunUntilIdle(); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 ASSERT_EQ(10u, notification_database_datas.size()); | 477 ASSERT_EQ(10u, notification_database_datas.size()); |
| 414 | 478 |
| 415 for (int i = 0; i < 10; ++i) { | 479 for (int i = 0; i < 10; ++i) { |
| 416 EXPECT_EQ(origin, notification_database_datas[i].origin); | 480 EXPECT_EQ(origin, notification_database_datas[i].origin); |
| 417 EXPECT_EQ(kFakeServiceWorkerRegistrationId, | 481 EXPECT_EQ(kFakeServiceWorkerRegistrationId, |
| 418 notification_database_datas[i].service_worker_registration_id); | 482 notification_database_datas[i].service_worker_registration_id); |
| 419 } | 483 } |
| 420 } | 484 } |
| 421 | 485 |
| 422 } // namespace content | 486 } // namespace content |
| OLD | NEW |