Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/notifications/notification_database.h" |
| 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 10 | #include <optional> |
| 11 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 12 | #include "base/files/scoped_temp_dir.h" |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 13 | #include "base/strings/string_number_conversions.h" |
| 14 | #include "base/strings/utf_string_conversions.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 15 | #include "base/test/bind.h" |
Claudio DeSouza | e843ed6 | 2023-04-17 10:58:43 | [diff] [blame] | 16 | #include "base/uuid.h" |
peter | 7b79a19 | 2015-03-17 22:14:05 | [diff] [blame] | 17 | #include "content/public/browser/notification_database_data.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 18 | #include "content/public/test/browser_task_environment.h" |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 20 | #include "third_party/blink/public/common/notifications/notification_resources.h" |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 21 | #include "third_party/blink/public/common/notifications/platform_notification_data.h" |
Gyuyoung Kim | 74e4058a | 2019-09-19 08:06:31 | [diff] [blame] | 22 | #include "third_party/blink/public/mojom/notifications/notification.mojom.h" |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 23 | #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 24 | #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 25 | #include "url/gurl.h" |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 26 | |
| 27 | namespace content { |
| 28 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 29 | const int kExampleServiceWorkerRegistrationId = 42; |
| 30 | |
| 31 | const struct { |
| 32 | const char* origin; |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 33 | const char* tag; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 34 | int64_t service_worker_registration_id; |
| 35 | } kExampleNotificationData[] = { |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 36 | {"https://example.com", "" /* tag */, 0}, |
| 37 | {"https://example.com", "" /* tag */, kExampleServiceWorkerRegistrationId}, |
| 38 | {"https://example.com", "" /* tag */, kExampleServiceWorkerRegistrationId}, |
| 39 | {"https://example.com", "" /* tag */, |
| 40 | kExampleServiceWorkerRegistrationId + 1}, |
| 41 | {"https://chrome.com", "" /* tag */, 0}, |
| 42 | {"https://chrome.com", "" /* tag */, 0}, |
| 43 | {"https://chrome.com", "" /* tag */, kExampleServiceWorkerRegistrationId}, |
| 44 | {"https://chrome.com", "foo" /* tag */, 0}}; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 45 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 46 | class NotificationDatabaseTest : public ::testing::Test { |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 47 | public: |
| 48 | NotificationDatabaseTest() |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 49 | : task_environment_(BrowserTaskEnvironment::IO_MAINLOOP) {} |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 50 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 51 | protected: |
| 52 | // Creates a new NotificationDatabase instance in memory. |
| 53 | NotificationDatabase* CreateDatabaseInMemory() { |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 54 | return new NotificationDatabase(base::FilePath(), callback()); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Creates a new NotificationDatabase instance in |path|. |
peter | 372525c | 2015-11-20 16:07:17 | [diff] [blame] | 58 | NotificationDatabase* CreateDatabaseOnFileSystem(const base::FilePath& path) { |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 59 | return new NotificationDatabase(path, callback()); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 60 | } |
| 61 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 62 | // Creates a new notification for |service_worker_registration_id| belonging |
| 63 | // to |origin| and writes it to the database. The written notification id |
| 64 | // will be stored in |notification_id|. |
| 65 | void CreateAndWriteNotification(NotificationDatabase* database, |
| 66 | const GURL& origin, |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 67 | const std::string& tag, |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 68 | bool is_shown_by_browser, |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 69 | int64_t service_worker_registration_id, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 70 | std::string* notification_id) { |
| 71 | DCHECK(notification_id); |
| 72 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 73 | NotificationDatabaseData database_data; |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 74 | database_data.notification_id = GenerateNotificationId(); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 75 | database_data.origin = origin; |
| 76 | database_data.service_worker_registration_id = |
| 77 | service_worker_registration_id; |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 78 | database_data.notification_data.tag = tag; |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 79 | database_data.is_shown_by_browser = is_shown_by_browser; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 80 | |
| 81 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 82 | database->WriteNotificationData(origin, database_data)); |
| 83 | |
| 84 | *notification_id = database_data.notification_id; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Populates |database| with a series of example notifications that differ in |
| 88 | // their origin and Service Worker registration id. |
| 89 | void PopulateDatabaseWithExampleData(NotificationDatabase* database) { |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 90 | std::string notification_id; |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 91 | for (const auto& notification_data : kExampleNotificationData) { |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 92 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 93 | database, GURL(notification_data.origin), notification_data.tag, |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 94 | false /* is_shown_by_browser */, |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 95 | notification_data.service_worker_registration_id, ¬ification_id)); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 99 | // Returns if |database| has been opened. |
| 100 | bool IsDatabaseOpen(NotificationDatabase* database) { |
| 101 | return database->IsOpen(); |
| 102 | } |
| 103 | |
| 104 | // Returns if |database| is an in-memory only database. |
| 105 | bool IsInMemoryDatabase(NotificationDatabase* database) { |
| 106 | return database->IsInMemoryDatabase(); |
| 107 | } |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 108 | |
| 109 | // Writes a LevelDB key-value pair directly to the LevelDB backing the |
| 110 | // notification database in |database|. |
| 111 | void WriteLevelDBKeyValuePair(NotificationDatabase* database, |
| 112 | const std::string& key, |
| 113 | const std::string& value) { |
| 114 | leveldb::Status status = |
| 115 | database->GetDBForTesting()->Put(leveldb::WriteOptions(), key, value); |
| 116 | ASSERT_TRUE(status.ok()); |
| 117 | } |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 118 | |
| 119 | // Generates a random notification ID. The format of the ID is opaque. |
Claudio DeSouza | e843ed6 | 2023-04-17 10:58:43 | [diff] [blame] | 120 | std::string GenerateNotificationId() { |
| 121 | return base::Uuid::GenerateRandomV4().AsLowercaseString(); |
| 122 | } |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 123 | |
| 124 | NotificationDatabase::UkmCallback callback() { return callback_; } |
| 125 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 126 | BrowserTaskEnvironment task_environment_; // Must be first member. |
Sharon Yang | ea520d8 | 2018-07-25 20:46:53 | [diff] [blame] | 127 | |
| 128 | NotificationDatabase::UkmCallback callback_; |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | TEST_F(NotificationDatabaseTest, OpenCloseMemory) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 132 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 133 | |
| 134 | // Should return false because the database does not exist in memory. |
| 135 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 136 | database->Open(false /* create_if_missing */)); |
| 137 | |
| 138 | // Should return true, indicating that the database could be created. |
| 139 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 140 | database->Open(true /* create_if_missing */)); |
| 141 | |
| 142 | EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 143 | EXPECT_TRUE(IsInMemoryDatabase(database.get())); |
| 144 | |
| 145 | // Verify that in-memory databases do not persist when being re-created. |
| 146 | database.reset(CreateDatabaseInMemory()); |
| 147 | |
| 148 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 149 | database->Open(false /* create_if_missing */)); |
| 150 | } |
| 151 | |
| 152 | TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) { |
| 153 | base::ScopedTempDir database_dir; |
| 154 | ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 155 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 156 | std::unique_ptr<NotificationDatabase> database( |
vabr | 39acefd3 | 2016-09-13 13:38:53 | [diff] [blame] | 157 | CreateDatabaseOnFileSystem(database_dir.GetPath())); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 158 | |
| 159 | // Should return false because the database does not exist on the file system. |
| 160 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 161 | database->Open(false /* create_if_missing */)); |
| 162 | |
| 163 | // Should return true, indicating that the database could be created. |
| 164 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 165 | database->Open(true /* create_if_missing */)); |
| 166 | |
| 167 | EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 168 | EXPECT_FALSE(IsInMemoryDatabase(database.get())); |
| 169 | |
| 170 | // Close the database, and re-open it without attempting to create it because |
| 171 | // the files on the file system should still exist as expected. |
vabr | 39acefd3 | 2016-09-13 13:38:53 | [diff] [blame] | 172 | database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 173 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 174 | database->Open(false /* create_if_missing */)); |
| 175 | } |
| 176 | |
| 177 | TEST_F(NotificationDatabaseTest, DestroyDatabase) { |
| 178 | base::ScopedTempDir database_dir; |
| 179 | ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 180 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 181 | std::unique_ptr<NotificationDatabase> database( |
vabr | 39acefd3 | 2016-09-13 13:38:53 | [diff] [blame] | 182 | CreateDatabaseOnFileSystem(database_dir.GetPath())); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 183 | |
| 184 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 185 | database->Open(true /* create_if_missing */)); |
| 186 | EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 187 | |
| 188 | // Destroy the database. This will immediately close it as well. |
| 189 | ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy()); |
| 190 | EXPECT_FALSE(IsDatabaseOpen(database.get())); |
| 191 | |
| 192 | // Try to re-open the database (but not re-create it). This should fail as |
| 193 | // the files associated with the database should have been blown away. |
vabr | 39acefd3 | 2016-09-13 13:38:53 | [diff] [blame] | 194 | database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 195 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 196 | database->Open(false /* create_if_missing */)); |
| 197 | } |
| 198 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 199 | TEST_F(NotificationDatabaseTest, NotificationIdIncrements) { |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 200 | base::ScopedTempDir database_dir; |
| 201 | ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 202 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 203 | std::unique_ptr<NotificationDatabase> database( |
vabr | 39acefd3 | 2016-09-13 13:38:53 | [diff] [blame] | 204 | CreateDatabaseOnFileSystem(database_dir.GetPath())); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 205 | |
| 206 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 207 | database->Open(true /* create_if_missing */)); |
| 208 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 209 | GURL origin("https://example.com"); |
| 210 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 211 | std::string notification_id; |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 212 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 213 | database.get(), origin, "" /* tag */, false /* is_shown_by_browser */, |
| 214 | 0 /* sw_registration_id */, ¬ification_id)); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 215 | |
vabr | 39acefd3 | 2016-09-13 13:38:53 | [diff] [blame] | 216 | database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath())); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 217 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 218 | database->Open(false /* create_if_missing */)); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 219 | } |
| 220 | |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 221 | TEST_F(NotificationDatabaseTest, NotificationIdIncrementsStorage) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 222 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 223 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 224 | database->Open(true /* create_if_missing */)); |
| 225 | |
| 226 | GURL origin("https://example.com"); |
| 227 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 228 | NotificationDatabaseData database_data, read_database_data; |
| 229 | database_data.notification_id = GenerateNotificationId(); |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 230 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 231 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 232 | database->WriteNotificationData(origin, database_data)); |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 233 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 234 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 235 | database->ReadNotificationData(database_data.notification_id, |
| 236 | origin, &read_database_data)); |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 237 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 238 | EXPECT_EQ(database_data.notification_id, read_database_data.notification_id); |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 239 | } |
| 240 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 241 | TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 242 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 243 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 244 | database->Open(true /* create_if_missing */)); |
| 245 | |
| 246 | NotificationDatabaseData database_data; |
| 247 | |
| 248 | // Reading the notification data for a notification that does not exist should |
| 249 | // return the ERROR_NOT_FOUND status code. |
| 250 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 251 | database->ReadNotificationData("bad-id", GURL("https://chrome.com"), |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 252 | &database_data)); |
| 253 | } |
| 254 | |
| 255 | TEST_F(NotificationDatabaseTest, ReadNotificationDataDifferentOrigin) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 256 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 257 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 258 | database->Open(true /* create_if_missing */)); |
| 259 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 260 | GURL origin("https://example.com"); |
| 261 | |
| 262 | NotificationDatabaseData database_data, read_database_data; |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 263 | database_data.notification_id = GenerateNotificationId(); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 264 | database_data.notification_data.title = u"My Notification"; |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 265 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 266 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 267 | database->WriteNotificationData(origin, database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 268 | |
| 269 | // Reading the notification from the database when given a different origin |
| 270 | // should return the ERROR_NOT_FOUND status code. |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 271 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 272 | database->ReadNotificationData(database_data.notification_id, |
| 273 | GURL("https://chrome.com"), |
| 274 | &read_database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 275 | |
| 276 | // However, reading the notification from the database with the same origin |
| 277 | // should return STATUS_OK and the associated notification data. |
| 278 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 279 | database->ReadNotificationData(database_data.notification_id, |
| 280 | origin, &read_database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 281 | |
| 282 | EXPECT_EQ(database_data.notification_data.title, |
| 283 | read_database_data.notification_data.title); |
| 284 | } |
| 285 | |
| 286 | TEST_F(NotificationDatabaseTest, ReadNotificationDataReflection) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 287 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 288 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 289 | database->Open(true /* create_if_missing */)); |
| 290 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 291 | GURL origin("https://example.com"); |
| 292 | |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 293 | blink::PlatformNotificationData notification_data; |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 294 | notification_data.title = u"My Notification"; |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 295 | notification_data.direction = |
Amos Lim | 3dce63b77 | 2019-02-21 16:56:34 | [diff] [blame] | 296 | blink::mojom::NotificationDirection::RIGHT_TO_LEFT; |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 297 | notification_data.lang = "nl-NL"; |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 298 | notification_data.body = u"Hello, world!"; |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 299 | notification_data.tag = "replace id"; |
| 300 | notification_data.icon = GURL("https://example.com/icon.png"); |
| 301 | notification_data.silent = true; |
| 302 | |
| 303 | NotificationDatabaseData database_data; |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 304 | database_data.notification_id = GenerateNotificationId(); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 305 | database_data.origin = origin; |
| 306 | database_data.service_worker_registration_id = 42; |
| 307 | database_data.notification_data = notification_data; |
| 308 | |
| 309 | // Write the constructed notification to the database, and then immediately |
| 310 | // read it back from the database again as well. |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 311 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 312 | database->WriteNotificationData(origin, database_data)); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 313 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 314 | NotificationDatabaseData read_database_data; |
| 315 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 316 | database->ReadNotificationData(database_data.notification_id, |
| 317 | origin, &read_database_data)); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 318 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 319 | // Verify that all members retrieved from the database are exactly the same |
| 320 | // as the ones that were written to it. This tests the serialization behavior. |
| 321 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 322 | EXPECT_EQ(database_data.notification_id, read_database_data.notification_id); |
peter | d5c5bf15 | 2015-03-20 19:14:40 | [diff] [blame] | 323 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 324 | EXPECT_EQ(database_data.origin, read_database_data.origin); |
| 325 | EXPECT_EQ(database_data.service_worker_registration_id, |
| 326 | read_database_data.service_worker_registration_id); |
| 327 | |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 328 | const blink::PlatformNotificationData& read_notification_data = |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 329 | read_database_data.notification_data; |
| 330 | |
| 331 | EXPECT_EQ(notification_data.title, read_notification_data.title); |
| 332 | EXPECT_EQ(notification_data.direction, read_notification_data.direction); |
| 333 | EXPECT_EQ(notification_data.lang, read_notification_data.lang); |
| 334 | EXPECT_EQ(notification_data.body, read_notification_data.body); |
| 335 | EXPECT_EQ(notification_data.tag, read_notification_data.tag); |
| 336 | EXPECT_EQ(notification_data.icon, read_notification_data.icon); |
| 337 | EXPECT_EQ(notification_data.silent, read_notification_data.silent); |
| 338 | } |
| 339 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 340 | TEST_F(NotificationDatabaseTest, ReadInvalidNotificationResources) { |
| 341 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 342 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 343 | database->Open(true /* create_if_missing */)); |
| 344 | |
| 345 | blink::NotificationResources database_resources; |
| 346 | |
| 347 | // Reading the notification resources for a notification that does not exist |
| 348 | // should return the ERROR_NOT_FOUND status code. |
| 349 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 350 | database->ReadNotificationResources( |
| 351 | "bad-id", GURL("https://chrome.com"), &database_resources)); |
| 352 | } |
| 353 | |
| 354 | TEST_F(NotificationDatabaseTest, ReadNotificationResourcesDifferentOrigin) { |
| 355 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 356 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 357 | database->Open(true /* create_if_missing */)); |
| 358 | |
| 359 | GURL origin("https://example.com"); |
| 360 | |
| 361 | NotificationDatabaseData database_data; |
| 362 | blink::NotificationResources database_resources; |
| 363 | database_data.notification_id = GenerateNotificationId(); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 364 | database_data.notification_data.title = u"My Notification"; |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 365 | database_data.notification_resources = blink::NotificationResources(); |
| 366 | |
| 367 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 368 | database->WriteNotificationData(origin, database_data)); |
| 369 | |
| 370 | // Reading the notification resources from the database when given a different |
| 371 | // origin should return the ERROR_NOT_FOUND status code. |
| 372 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 373 | database->ReadNotificationResources(database_data.notification_id, |
| 374 | GURL("https://chrome.com"), |
| 375 | &database_resources)); |
| 376 | |
| 377 | // However, reading the notification from the database with the same origin |
| 378 | // should return STATUS_OK and the associated notification data. |
| 379 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 380 | database->ReadNotificationResources(database_data.notification_id, |
| 381 | origin, &database_resources)); |
| 382 | } |
| 383 | |
| 384 | TEST_F(NotificationDatabaseTest, ReadNotificationResourcesReflection) { |
| 385 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 386 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 387 | database->Open(true /* create_if_missing */)); |
| 388 | |
| 389 | GURL origin("https://example.com"); |
| 390 | |
| 391 | blink::NotificationResources notification_resources; |
| 392 | NotificationDatabaseData database_data; |
| 393 | database_data.notification_id = GenerateNotificationId(); |
| 394 | database_data.origin = origin; |
| 395 | database_data.service_worker_registration_id = 42; |
| 396 | database_data.notification_resources = notification_resources; |
| 397 | |
| 398 | // Write the constructed notification to the database, and then immediately |
| 399 | // read it back from the database again as well. |
| 400 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 401 | database->WriteNotificationData(origin, database_data)); |
| 402 | |
| 403 | NotificationDatabaseData read_database_data; |
| 404 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 405 | database->ReadNotificationData(database_data.notification_id, |
| 406 | origin, &read_database_data)); |
| 407 | |
| 408 | // Verify that all members retrieved from the database are exactly the same |
| 409 | // as the ones that were written to it. This tests the serialization behavior. |
| 410 | |
| 411 | EXPECT_EQ(database_data.notification_id, read_database_data.notification_id); |
| 412 | |
| 413 | EXPECT_EQ(database_data.origin, read_database_data.origin); |
| 414 | EXPECT_EQ(database_data.service_worker_registration_id, |
| 415 | read_database_data.service_worker_registration_id); |
| 416 | |
| 417 | // We do not populate the resources when reading from the database. |
| 418 | EXPECT_FALSE(read_database_data.notification_resources.has_value()); |
| 419 | |
| 420 | blink::NotificationResources read_notification_resources; |
| 421 | EXPECT_EQ( |
| 422 | NotificationDatabase::STATUS_OK, |
| 423 | database->ReadNotificationResources(database_data.notification_id, origin, |
| 424 | &read_notification_resources)); |
| 425 | } |
| 426 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 427 | TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 428 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 429 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 430 | database->Open(true /* create_if_missing */)); |
| 431 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 432 | GURL origin("https://example.com"); |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 433 | |
| 434 | std::vector<std::string> notification_ids; |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 435 | |
| 436 | // Write ten notifications to the database, each with a unique title and |
| 437 | // notification id (it is the responsibility of the user to increment this). |
| 438 | for (int i = 1; i <= 10; ++i) { |
Peter Kasting | eb8c3ce | 2021-08-20 04:39:35 | [diff] [blame] | 439 | std::string notification_id; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 440 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 441 | database.get(), origin, "" /* tag */, false /* is_shown_by_browser */, |
| 442 | i /* sw_registration_id */, ¬ification_id)); |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 443 | |
| 444 | EXPECT_FALSE(notification_id.empty()); |
| 445 | |
| 446 | notification_ids.push_back(notification_id); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 447 | } |
| 448 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 449 | NotificationDatabaseData database_data; |
| 450 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 451 | int64_t service_worker_registration_id = 1; |
| 452 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 453 | // Read the ten notifications from the database, and verify that the titles |
| 454 | // of each of them matches with how they were created. |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 455 | for (const std::string& notification_id : notification_ids) { |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 456 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 457 | database->ReadNotificationData(notification_id, origin, |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 458 | &database_data)); |
| 459 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 460 | EXPECT_EQ(service_worker_registration_id++, |
| 461 | database_data.service_worker_registration_id); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Sharon Yang | d96cd4b | 2018-05-31 11:43:43 | [diff] [blame] | 465 | TEST_F(NotificationDatabaseTest, ReadNotificationUpdateInteraction) { |
| 466 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 467 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 468 | database->Open(true /* create_if_missing */)); |
| 469 | |
| 470 | GURL origin("https://example.com"); |
| 471 | |
| 472 | NotificationDatabaseData database_data, read_database_data; |
| 473 | database_data.notification_id = GenerateNotificationId(); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 474 | database_data.notification_data.title = u"My Notification"; |
Sharon Yang | d96cd4b | 2018-05-31 11:43:43 | [diff] [blame] | 475 | |
| 476 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 477 | database->WriteNotificationData(origin, database_data)); |
| 478 | |
Sharon Yang | 71bfb2be | 2018-06-04 16:04:40 | [diff] [blame] | 479 | // Check that the time deltas have not yet been set. |
| 480 | EXPECT_EQ(false, |
| 481 | read_database_data.time_until_first_click_millis.has_value()); |
| 482 | EXPECT_EQ(false, read_database_data.time_until_last_click_millis.has_value()); |
| 483 | EXPECT_EQ(false, read_database_data.time_until_close_millis.has_value()); |
| 484 | |
Sharon Yang | d96cd4b | 2018-05-31 11:43:43 | [diff] [blame] | 485 | // Check that when a notification has an interaction, the appropriate field is |
| 486 | // updated on the read. |
| 487 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 488 | database->ReadNotificationDataAndRecordInteraction( |
| 489 | database_data.notification_id, origin, |
| 490 | PlatformNotificationContext::Interaction::CLICKED, |
| 491 | &read_database_data)); |
| 492 | EXPECT_EQ(1, read_database_data.num_clicks); |
| 493 | |
| 494 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 495 | database->ReadNotificationDataAndRecordInteraction( |
| 496 | database_data.notification_id, origin, |
| 497 | PlatformNotificationContext::Interaction::ACTION_BUTTON_CLICKED, |
| 498 | &read_database_data)); |
| 499 | EXPECT_EQ(1, read_database_data.num_action_button_clicks); |
| 500 | |
| 501 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 502 | database->ReadNotificationDataAndRecordInteraction( |
| 503 | database_data.notification_id, origin, |
| 504 | PlatformNotificationContext::Interaction::ACTION_BUTTON_CLICKED, |
| 505 | &read_database_data)); |
| 506 | EXPECT_EQ(2, read_database_data.num_action_button_clicks); |
Sharon Yang | 71bfb2be | 2018-06-04 16:04:40 | [diff] [blame] | 507 | |
| 508 | // Check that the click timestamps are correctly updated. |
| 509 | EXPECT_EQ(true, read_database_data.time_until_first_click_millis.has_value()); |
| 510 | EXPECT_EQ(true, read_database_data.time_until_last_click_millis.has_value()); |
| 511 | |
| 512 | // Check that when a read with a CLOSED interaction occurs, the correct |
| 513 | // field is updated. |
| 514 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 515 | database->ReadNotificationDataAndRecordInteraction( |
| 516 | database_data.notification_id, origin, |
| 517 | PlatformNotificationContext::Interaction::CLOSED, |
| 518 | &read_database_data)); |
| 519 | EXPECT_EQ(true, read_database_data.time_until_close_millis.has_value()); |
Sharon Yang | d96cd4b | 2018-05-31 11:43:43 | [diff] [blame] | 520 | } |
| 521 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 522 | TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 523 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 524 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 525 | database->Open(true /* create_if_missing */)); |
| 526 | |
| 527 | // Deleting non-existing notifications is not considered to be a failure. |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 528 | ASSERT_EQ( |
| 529 | NotificationDatabase::STATUS_OK, |
| 530 | database->DeleteNotificationData("bad-id", GURL("https://chrome.com"))); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | TEST_F(NotificationDatabaseTest, DeleteNotificationDataSameOrigin) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 534 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 535 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 536 | database->Open(true /* create_if_missing */)); |
| 537 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 538 | const std::string notification_id = GenerateNotificationId(); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 539 | |
| 540 | NotificationDatabaseData database_data; |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 541 | database_data.notification_id = notification_id; |
| 542 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 543 | GURL origin("https://example.com"); |
| 544 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 545 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 546 | database->WriteNotificationData(origin, database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 547 | |
| 548 | // Reading a notification after writing one should succeed. |
peter | 372525c | 2015-11-20 16:07:17 | [diff] [blame] | 549 | EXPECT_EQ( |
| 550 | NotificationDatabase::STATUS_OK, |
| 551 | database->ReadNotificationData(notification_id, origin, &database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 552 | |
| 553 | // Delete the notification which was just written to the database, and verify |
| 554 | // that reading it again will fail. |
| 555 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 556 | database->DeleteNotificationData(notification_id, origin)); |
peter | 372525c | 2015-11-20 16:07:17 | [diff] [blame] | 557 | EXPECT_EQ( |
| 558 | NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 559 | database->ReadNotificationData(notification_id, origin, &database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 560 | } |
| 561 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 562 | TEST_F(NotificationDatabaseTest, DeleteNotificationResourcesSameOrigin) { |
| 563 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 564 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 565 | database->Open(true /* create_if_missing */)); |
| 566 | |
| 567 | const std::string notification_id = GenerateNotificationId(); |
| 568 | |
| 569 | blink::NotificationResources notification_resources; |
| 570 | NotificationDatabaseData database_data; |
| 571 | database_data.notification_id = notification_id; |
| 572 | database_data.notification_resources = notification_resources; |
| 573 | |
| 574 | GURL origin("https://example.com"); |
| 575 | |
| 576 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 577 | database->WriteNotificationData(origin, database_data)); |
| 578 | |
| 579 | // Reading notification resources after writing should succeed. |
| 580 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 581 | database->ReadNotificationResources(notification_id, origin, |
| 582 | ¬ification_resources)); |
| 583 | |
| 584 | // Delete the notification which was just written to the database, and verify |
| 585 | // that reading the resources again will fail. |
| 586 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 587 | database->DeleteNotificationData(notification_id, origin)); |
| 588 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 589 | database->ReadNotificationResources(notification_id, origin, |
| 590 | ¬ification_resources)); |
| 591 | } |
| 592 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 593 | TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 594 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 595 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 596 | database->Open(true /* create_if_missing */)); |
| 597 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 598 | const std::string notification_id = GenerateNotificationId(); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 599 | |
| 600 | NotificationDatabaseData database_data; |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 601 | database_data.notification_id = notification_id; |
| 602 | |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 603 | GURL origin("https://example.com"); |
| 604 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 605 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 606 | database->WriteNotificationData(origin, database_data)); |
peter | 513eb55 | 2015-03-16 18:55:04 | [diff] [blame] | 607 | |
| 608 | // Attempting to delete the notification with a different origin, but with the |
| 609 | // same |notification_id|, should not return an error (the notification could |
| 610 | // not be found, but that's not considered a failure). However, it should not |
| 611 | // remove the notification either. |
| 612 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 613 | database->DeleteNotificationData(notification_id, |
| 614 | GURL("https://chrome.com"))); |
| 615 | |
peter | 372525c | 2015-11-20 16:07:17 | [diff] [blame] | 616 | EXPECT_EQ( |
| 617 | NotificationDatabase::STATUS_OK, |
| 618 | database->ReadNotificationData(notification_id, origin, &database_data)); |
peter | 05e061b | 2015-03-13 13:16:06 | [diff] [blame] | 619 | } |
| 620 | |
Richard Knoll | acd400ea | 2019-05-31 11:53:07 | [diff] [blame] | 621 | TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationResources) { |
| 622 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 623 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 624 | database->Open(true /* create_if_missing */)); |
| 625 | |
| 626 | // Deleting non-existing resources is not considered to be a failure. |
| 627 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 628 | database->DeleteNotificationResources("bad-id", |
| 629 | GURL("https://chrome.com"))); |
| 630 | } |
| 631 | |
| 632 | TEST_F(NotificationDatabaseTest, DeleteNotificationResources) { |
| 633 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 634 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 635 | database->Open(true /* create_if_missing */)); |
| 636 | |
| 637 | const std::string notification_id = GenerateNotificationId(); |
| 638 | |
| 639 | blink::NotificationResources notification_resources; |
| 640 | NotificationDatabaseData database_data; |
| 641 | database_data.notification_id = notification_id; |
| 642 | database_data.notification_resources = notification_resources; |
| 643 | |
| 644 | GURL origin("https://example.com"); |
| 645 | |
| 646 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 647 | database->WriteNotificationData(origin, database_data)); |
| 648 | |
| 649 | // Reading notification resources after writing should succeed. |
| 650 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 651 | database->ReadNotificationResources(notification_id, origin, |
| 652 | ¬ification_resources)); |
| 653 | |
| 654 | // Delete the notification resources for the notification which was just |
| 655 | // written to the database, and verify that reading them again will fail. |
| 656 | EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 657 | database->DeleteNotificationResources(notification_id, origin)); |
| 658 | EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 659 | database->ReadNotificationResources(notification_id, origin, |
| 660 | ¬ification_resources)); |
| 661 | } |
| 662 | |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 663 | TEST_F(NotificationDatabaseTest, |
| 664 | ForEachNotificationDataForServiceWorkerRegistration) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 665 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 666 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 667 | database->Open(true /* create_if_missing */)); |
| 668 | |
| 669 | ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); |
| 670 | |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 671 | GURL origin("https://example.com:443"); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 672 | |
| 673 | std::vector<NotificationDatabaseData> notifications; |
| 674 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 675 | database->ForEachNotificationDataForServiceWorkerRegistration( |
| 676 | origin, kExampleServiceWorkerRegistrationId, |
| 677 | base::BindLambdaForTesting( |
| 678 | [¬ifications](const NotificationDatabaseData& data) { |
| 679 | notifications.push_back(data); |
| 680 | }))); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 681 | |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 682 | EXPECT_EQ(2u, notifications.size()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | TEST_F(NotificationDatabaseTest, ReadAllNotificationDataForOrigin) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 686 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 687 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 688 | database->Open(true /* create_if_missing */)); |
| 689 | |
| 690 | ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); |
| 691 | |
| 692 | GURL origin("https://example.com"); |
| 693 | |
| 694 | std::vector<NotificationDatabaseData> notifications; |
| 695 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 696 | database->ReadAllNotificationDataForOrigin(origin, ¬ifications)); |
| 697 | |
| 698 | EXPECT_EQ(4u, notifications.size()); |
| 699 | } |
| 700 | |
| 701 | TEST_F(NotificationDatabaseTest, |
| 702 | ReadAllNotificationDataForServiceWorkerRegistration) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 703 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 704 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 705 | database->Open(true /* create_if_missing */)); |
| 706 | |
| 707 | ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); |
| 708 | |
| 709 | GURL origin("https://example.com:443"); |
| 710 | |
| 711 | std::vector<NotificationDatabaseData> notifications; |
| 712 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 713 | database->ReadAllNotificationDataForServiceWorkerRegistration( |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 714 | origin, kExampleServiceWorkerRegistrationId, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 715 | std::nullopt /* is_shown_by_browser */, ¬ifications)); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 716 | |
| 717 | EXPECT_EQ(2u, notifications.size()); |
| 718 | } |
| 719 | |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 720 | TEST_F(NotificationDatabaseTest, |
| 721 | ReadAllNotificationDataForServiceWorkerRegistrationShownByBrowser) { |
| 722 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 723 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 724 | database->Open(true /* create_if_missing */)); |
| 725 | GURL origin("https://example.com:443"); |
| 726 | std::string kTag = "tag"; |
| 727 | |
| 728 | std::string non_browser_notification_id; |
| 729 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 730 | database.get(), origin, kTag, false /* is_shown_by_browser */, |
| 731 | kExampleServiceWorkerRegistrationId, &non_browser_notification_id)); |
| 732 | |
| 733 | std::string browser_notification_id; |
| 734 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 735 | database.get(), origin, kTag, true /* is_shown_by_browser */, |
| 736 | kExampleServiceWorkerRegistrationId, &browser_notification_id)); |
| 737 | |
| 738 | { |
| 739 | // Expect to be able to read notification shown by the browser. |
| 740 | std::vector<NotificationDatabaseData> notifications; |
| 741 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 742 | database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 743 | origin, kExampleServiceWorkerRegistrationId, |
| 744 | true /* is_shown_by_browser */, ¬ifications)); |
| 745 | ASSERT_EQ(1u, notifications.size()); |
| 746 | EXPECT_EQ(browser_notification_id, notifications[0].notification_id); |
| 747 | } |
| 748 | |
| 749 | { |
| 750 | // Expect to be able to read notification not shown by the browser. |
| 751 | std::vector<NotificationDatabaseData> notifications; |
| 752 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 753 | database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 754 | origin, kExampleServiceWorkerRegistrationId, |
| 755 | false /* is_shown_by_browser */, ¬ifications)); |
| 756 | ASSERT_EQ(1u, notifications.size()); |
| 757 | EXPECT_EQ(non_browser_notification_id, notifications[0].notification_id); |
| 758 | } |
| 759 | |
| 760 | { |
| 761 | // Expect to be able to read notification not shown by anyone. |
| 762 | std::vector<NotificationDatabaseData> notifications; |
| 763 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 764 | database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 765 | origin, kExampleServiceWorkerRegistrationId, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 766 | std::nullopt /* is_shown_by_browser */, ¬ifications)); |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 767 | ASSERT_EQ(2u, notifications.size()); |
| 768 | } |
| 769 | } |
| 770 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 771 | TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOrigin) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 772 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 773 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 774 | database->Open(true /* create_if_missing */)); |
| 775 | |
| 776 | ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); |
| 777 | |
| 778 | GURL origin("https://example.com:443"); |
| 779 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 780 | std::set<std::string> deleted_notification_ids; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 781 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 782 | database->DeleteAllNotificationDataForOrigin( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 783 | origin, "" /* tag */, std::nullopt /* is_shown_by_browser */, |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 784 | &deleted_notification_ids)); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 785 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 786 | EXPECT_EQ(4u, deleted_notification_ids.size()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 787 | |
| 788 | std::vector<NotificationDatabaseData> notifications; |
| 789 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 790 | database->ReadAllNotificationDataForOrigin(origin, ¬ifications)); |
| 791 | |
| 792 | EXPECT_EQ(0u, notifications.size()); |
| 793 | } |
| 794 | |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 795 | TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginWithTag) { |
| 796 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 797 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 798 | database->Open(true /* create_if_missing */)); |
| 799 | |
| 800 | ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); |
| 801 | |
| 802 | GURL origin("https://chrome.com"); |
| 803 | |
| 804 | std::vector<NotificationDatabaseData> notifications; |
| 805 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 806 | database->ReadAllNotificationDataForOrigin(origin, ¬ifications)); |
| 807 | |
| 808 | const std::string& tag = "foo"; |
| 809 | |
| 810 | size_t notifications_with_tag = 0; |
| 811 | size_t notifications_without_tag = 0; |
| 812 | |
| 813 | for (const auto& database_data : notifications) { |
| 814 | if (database_data.notification_data.tag == tag) |
| 815 | ++notifications_with_tag; |
| 816 | else |
| 817 | ++notifications_without_tag; |
| 818 | } |
| 819 | |
| 820 | ASSERT_GT(notifications_with_tag, 0u); |
| 821 | ASSERT_GT(notifications_without_tag, 0u); |
| 822 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 823 | std::set<std::string> deleted_notification_ids; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 824 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 825 | database->DeleteAllNotificationDataForOrigin( |
| 826 | origin, "foo" /* tag */, std::nullopt /* is_shown_by_browser */, |
| 827 | &deleted_notification_ids)); |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 828 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 829 | EXPECT_EQ(notifications_with_tag, deleted_notification_ids.size()); |
peter | 3ce3c46 | 2016-08-09 16:23:58 | [diff] [blame] | 830 | |
| 831 | std::vector<NotificationDatabaseData> updated_notifications; |
| 832 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 833 | database->ReadAllNotificationDataForOrigin(origin, |
| 834 | &updated_notifications)); |
| 835 | |
| 836 | EXPECT_EQ(notifications_without_tag, updated_notifications.size()); |
| 837 | |
| 838 | size_t updated_notifications_with_tag = 0; |
| 839 | size_t updated_notifications_without_tag = 0; |
| 840 | |
| 841 | for (const auto& database_data : updated_notifications) { |
| 842 | if (database_data.notification_data.tag == tag) |
| 843 | ++updated_notifications_with_tag; |
| 844 | else |
| 845 | ++updated_notifications_without_tag; |
| 846 | } |
| 847 | |
| 848 | EXPECT_EQ(0u, updated_notifications_with_tag); |
| 849 | EXPECT_EQ(notifications_without_tag, updated_notifications_without_tag); |
| 850 | } |
| 851 | |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 852 | TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginEmpty) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 853 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 854 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 855 | database->Open(true /* create_if_missing */)); |
| 856 | |
| 857 | GURL origin("https://example.com"); |
| 858 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 859 | std::set<std::string> deleted_notification_ids; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 860 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 861 | database->DeleteAllNotificationDataForOrigin( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 862 | origin, "" /* tag */, std::nullopt /* is_shown_by_browser */, |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 863 | &deleted_notification_ids)); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 864 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 865 | EXPECT_EQ(0u, deleted_notification_ids.size()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | TEST_F(NotificationDatabaseTest, |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 869 | DeleteAllNotificationDataForOriginShownByBrowser) { |
| 870 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 871 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 872 | database->Open(true /* create_if_missing */)); |
| 873 | GURL origin("https://example.com:443"); |
| 874 | std::string kTag = "tag"; |
| 875 | |
| 876 | std::string non_browser_notification_id; |
| 877 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 878 | database.get(), origin, kTag, false /* is_shown_by_browser */, |
| 879 | kExampleServiceWorkerRegistrationId, &non_browser_notification_id)); |
| 880 | |
| 881 | std::string browser_notification_id; |
| 882 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 883 | database.get(), origin, kTag, true /* is_shown_by_browser */, |
| 884 | kExampleServiceWorkerRegistrationId, &browser_notification_id)); |
| 885 | |
| 886 | { |
| 887 | // Expect two notifications in the database for |origin|. |
| 888 | std::vector<NotificationDatabaseData> notifications; |
| 889 | ASSERT_EQ( |
| 890 | NotificationDatabase::STATUS_OK, |
| 891 | database->ReadAllNotificationDataForOrigin(origin, ¬ifications)); |
| 892 | EXPECT_EQ(2u, notifications.size()); |
| 893 | } |
| 894 | |
| 895 | { |
| 896 | // Expect to be able to delete only notifications from the browser. |
| 897 | std::set<std::string> deleted_notification_ids; |
| 898 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 899 | database->DeleteAllNotificationDataForOrigin( |
| 900 | origin, kTag, true /* is_shown_by_browser */, |
| 901 | &deleted_notification_ids)); |
| 902 | EXPECT_EQ(1u, deleted_notification_ids.size()); |
| 903 | EXPECT_EQ(1u, deleted_notification_ids.count(browser_notification_id)); |
| 904 | } |
| 905 | |
| 906 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 907 | database.get(), origin, kTag, true /* is_shown_by_browser */, |
| 908 | kExampleServiceWorkerRegistrationId, &browser_notification_id)); |
| 909 | |
| 910 | { |
| 911 | // Expect to be able to delete only notifications not from the browser. |
| 912 | std::set<std::string> deleted_notification_ids; |
| 913 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 914 | database->DeleteAllNotificationDataForOrigin( |
| 915 | origin, kTag, false /* is_shown_by_browser */, |
| 916 | &deleted_notification_ids)); |
| 917 | EXPECT_EQ(1u, deleted_notification_ids.size()); |
| 918 | EXPECT_EQ(1u, deleted_notification_ids.count(non_browser_notification_id)); |
| 919 | } |
| 920 | |
| 921 | ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 922 | database.get(), origin, kTag, false /* is_shown_by_browser */, |
| 923 | kExampleServiceWorkerRegistrationId, &non_browser_notification_id)); |
| 924 | |
| 925 | { |
| 926 | // Expect to be able to delete notifications from the browser or not. |
| 927 | std::set<std::string> deleted_notification_ids; |
| 928 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 929 | database->DeleteAllNotificationDataForOrigin( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 930 | origin, kTag, std::nullopt /* is_shown_by_browser */, |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 931 | &deleted_notification_ids)); |
| 932 | EXPECT_EQ(2u, deleted_notification_ids.size()); |
| 933 | EXPECT_EQ(1u, deleted_notification_ids.count(browser_notification_id)); |
| 934 | EXPECT_EQ(1u, deleted_notification_ids.count(non_browser_notification_id)); |
| 935 | } |
| 936 | |
| 937 | // Expect no remaining notifications. |
| 938 | std::vector<NotificationDatabaseData> notifications; |
| 939 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 940 | database->ReadAllNotificationDataForOrigin(origin, ¬ifications)); |
| 941 | EXPECT_EQ(0u, notifications.size()); |
| 942 | } |
| 943 | |
| 944 | TEST_F(NotificationDatabaseTest, |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 945 | DeleteAllNotificationDataForServiceWorkerRegistration) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 946 | std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 947 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 948 | database->Open(true /* create_if_missing */)); |
| 949 | |
| 950 | ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); |
| 951 | |
| 952 | GURL origin("https://example.com:443"); |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 953 | std::set<std::string> deleted_notification_ids; |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 954 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 955 | database->DeleteAllNotificationDataForServiceWorkerRegistration( |
peter | 372525c | 2015-11-20 16:07:17 | [diff] [blame] | 956 | origin, kExampleServiceWorkerRegistrationId, |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 957 | &deleted_notification_ids)); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 958 | |
peter | c45944c3 | 2016-09-13 14:35:59 | [diff] [blame] | 959 | EXPECT_EQ(2u, deleted_notification_ids.size()); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 960 | |
| 961 | std::vector<NotificationDatabaseData> notifications; |
| 962 | ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 963 | database->ReadAllNotificationDataForServiceWorkerRegistration( |
Richard Knoll | 5dcde886 | 2021-07-16 14:38:56 | [diff] [blame] | 964 | origin, kExampleServiceWorkerRegistrationId, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 965 | std::nullopt /* is_shown_by_browser */, ¬ifications)); |
peter | adb2fe2 | 2015-03-19 18:49:28 | [diff] [blame] | 966 | |
| 967 | EXPECT_EQ(0u, notifications.size()); |
| 968 | } |
| 969 | |
peter | 5bb2e145 | 2015-03-12 15:31:16 | [diff] [blame] | 970 | } // namespace content |