blob: 1ad14984f4367ac75357eb0a9a66017b9ab44dea [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2015 The Chromium Authors
peter5bb2e1452015-03-12 15:31:162// 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
avib7348942015-12-25 20:57:107#include <stddef.h>
8#include <stdint.h>
9
Arthur Sonzognic686e8f2024-01-11 08:36:3710#include <optional>
11
peter5bb2e1452015-03-12 15:31:1612#include "base/files/scoped_temp_dir.h"
peter513eb552015-03-16 18:55:0413#include "base/strings/string_number_conversions.h"
14#include "base/strings/utf_string_conversions.h"
Guido Urdanetaef4e91942020-11-09 15:06:2415#include "base/test/bind.h"
Claudio DeSouzae843ed62023-04-17 10:58:4316#include "base/uuid.h"
peter7b79a192015-03-17 22:14:0517#include "content/public/browser/notification_database_data.h"
Gabriel Charettec7108742019-08-23 03:31:4018#include "content/public/test/browser_task_environment.h"
peter5bb2e1452015-03-12 15:31:1619#include "testing/gtest/include/gtest/gtest.h"
Richard Knoll2e136ef2019-03-07 09:45:1120#include "third_party/blink/public/common/notifications/notification_resources.h"
Han Leon96d6b6e8c22018-09-06 06:21:0621#include "third_party/blink/public/common/notifications/platform_notification_data.h"
Gyuyoung Kim74e4058a2019-09-19 08:06:3122#include "third_party/blink/public/mojom/notifications/notification.mojom.h"
peter05e061b2015-03-13 13:16:0623#include "third_party/leveldatabase/src/include/leveldb/db.h"
24#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
peter513eb552015-03-16 18:55:0425#include "url/gurl.h"
peter5bb2e1452015-03-12 15:31:1626
27namespace content {
28
peteradb2fe22015-03-19 18:49:2829const int kExampleServiceWorkerRegistrationId = 42;
30
31const struct {
32 const char* origin;
peter3ce3c462016-08-09 16:23:5833 const char* tag;
peteradb2fe22015-03-19 18:49:2834 int64_t service_worker_registration_id;
35} kExampleNotificationData[] = {
peter3ce3c462016-08-09 16:23:5836 {"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}};
peteradb2fe22015-03-19 18:49:2845
peter5bb2e1452015-03-12 15:31:1646class NotificationDatabaseTest : public ::testing::Test {
Sharon Yangea520d82018-07-25 20:46:5347 public:
48 NotificationDatabaseTest()
Gabriel Charette798fde72019-08-20 22:24:0449 : task_environment_(BrowserTaskEnvironment::IO_MAINLOOP) {}
Sharon Yangea520d82018-07-25 20:46:5350
peter5bb2e1452015-03-12 15:31:1651 protected:
52 // Creates a new NotificationDatabase instance in memory.
53 NotificationDatabase* CreateDatabaseInMemory() {
Sharon Yangea520d82018-07-25 20:46:5354 return new NotificationDatabase(base::FilePath(), callback());
peter5bb2e1452015-03-12 15:31:1655 }
56
57 // Creates a new NotificationDatabase instance in |path|.
peter372525c2015-11-20 16:07:1758 NotificationDatabase* CreateDatabaseOnFileSystem(const base::FilePath& path) {
Sharon Yangea520d82018-07-25 20:46:5359 return new NotificationDatabase(path, callback());
peter5bb2e1452015-03-12 15:31:1660 }
61
peteradb2fe22015-03-19 18:49:2862 // 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,
peter3ce3c462016-08-09 16:23:5867 const std::string& tag,
Richard Knoll5dcde8862021-07-16 14:38:5668 bool is_shown_by_browser,
peteradb2fe22015-03-19 18:49:2869 int64_t service_worker_registration_id,
peterc45944c32016-09-13 14:35:5970 std::string* notification_id) {
71 DCHECK(notification_id);
72
peteradb2fe22015-03-19 18:49:2873 NotificationDatabaseData database_data;
peterc45944c32016-09-13 14:35:5974 database_data.notification_id = GenerateNotificationId();
peteradb2fe22015-03-19 18:49:2875 database_data.origin = origin;
76 database_data.service_worker_registration_id =
77 service_worker_registration_id;
peter3ce3c462016-08-09 16:23:5878 database_data.notification_data.tag = tag;
Richard Knoll5dcde8862021-07-16 14:38:5679 database_data.is_shown_by_browser = is_shown_by_browser;
peteradb2fe22015-03-19 18:49:2880
81 ASSERT_EQ(NotificationDatabase::STATUS_OK,
peterc45944c32016-09-13 14:35:5982 database->WriteNotificationData(origin, database_data));
83
84 *notification_id = database_data.notification_id;
peteradb2fe22015-03-19 18:49:2885 }
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) {
peterc45944c32016-09-13 14:35:5990 std::string notification_id;
Richard Knoll25d3a8d2020-07-26 03:26:4391 for (const auto& notification_data : kExampleNotificationData) {
peteradb2fe22015-03-19 18:49:2892 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
Richard Knoll25d3a8d2020-07-26 03:26:4393 database, GURL(notification_data.origin), notification_data.tag,
Richard Knoll5dcde8862021-07-16 14:38:5694 false /* is_shown_by_browser */,
Richard Knoll25d3a8d2020-07-26 03:26:4395 notification_data.service_worker_registration_id, &notification_id));
peteradb2fe22015-03-19 18:49:2896 }
97 }
98
peter5bb2e1452015-03-12 15:31:1699 // 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 }
peter05e061b2015-03-13 13:16:06108
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 }
peterc45944c32016-09-13 14:35:59118
119 // Generates a random notification ID. The format of the ID is opaque.
Claudio DeSouzae843ed62023-04-17 10:58:43120 std::string GenerateNotificationId() {
121 return base::Uuid::GenerateRandomV4().AsLowercaseString();
122 }
Sharon Yangea520d82018-07-25 20:46:53123
124 NotificationDatabase::UkmCallback callback() { return callback_; }
125
Gabriel Charette798fde72019-08-20 22:24:04126 BrowserTaskEnvironment task_environment_; // Must be first member.
Sharon Yangea520d82018-07-25 20:46:53127
128 NotificationDatabase::UkmCallback callback_;
peter5bb2e1452015-03-12 15:31:16129};
130
131TEST_F(NotificationDatabaseTest, OpenCloseMemory) {
dcheng59716272016-04-09 05:19:08132 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter5bb2e1452015-03-12 15:31:16133
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
152TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) {
153 base::ScopedTempDir database_dir;
154 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
155
dcheng59716272016-04-09 05:19:08156 std::unique_ptr<NotificationDatabase> database(
vabr39acefd32016-09-13 13:38:53157 CreateDatabaseOnFileSystem(database_dir.GetPath()));
peter5bb2e1452015-03-12 15:31:16158
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.
vabr39acefd32016-09-13 13:38:53172 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath()));
peter5bb2e1452015-03-12 15:31:16173 EXPECT_EQ(NotificationDatabase::STATUS_OK,
174 database->Open(false /* create_if_missing */));
175}
176
177TEST_F(NotificationDatabaseTest, DestroyDatabase) {
178 base::ScopedTempDir database_dir;
179 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
180
dcheng59716272016-04-09 05:19:08181 std::unique_ptr<NotificationDatabase> database(
vabr39acefd32016-09-13 13:38:53182 CreateDatabaseOnFileSystem(database_dir.GetPath()));
peter5bb2e1452015-03-12 15:31:16183
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.
vabr39acefd32016-09-13 13:38:53194 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath()));
peter5bb2e1452015-03-12 15:31:16195 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
196 database->Open(false /* create_if_missing */));
197}
198
peter513eb552015-03-16 18:55:04199TEST_F(NotificationDatabaseTest, NotificationIdIncrements) {
peter05e061b2015-03-13 13:16:06200 base::ScopedTempDir database_dir;
201 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
202
dcheng59716272016-04-09 05:19:08203 std::unique_ptr<NotificationDatabase> database(
vabr39acefd32016-09-13 13:38:53204 CreateDatabaseOnFileSystem(database_dir.GetPath()));
peter05e061b2015-03-13 13:16:06205
206 ASSERT_EQ(NotificationDatabase::STATUS_OK,
207 database->Open(true /* create_if_missing */));
208
peter513eb552015-03-16 18:55:04209 GURL origin("https://example.com");
210
peterc45944c32016-09-13 14:35:59211 std::string notification_id;
Richard Knoll5dcde8862021-07-16 14:38:56212 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
213 database.get(), origin, "" /* tag */, false /* is_shown_by_browser */,
214 0 /* sw_registration_id */, &notification_id));
peter05e061b2015-03-13 13:16:06215
vabr39acefd32016-09-13 13:38:53216 database.reset(CreateDatabaseOnFileSystem(database_dir.GetPath()));
peter05e061b2015-03-13 13:16:06217 ASSERT_EQ(NotificationDatabase::STATUS_OK,
218 database->Open(false /* create_if_missing */));
peter05e061b2015-03-13 13:16:06219}
220
peterd5c5bf152015-03-20 19:14:40221TEST_F(NotificationDatabaseTest, NotificationIdIncrementsStorage) {
dcheng59716272016-04-09 05:19:08222 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peterd5c5bf152015-03-20 19:14:40223 ASSERT_EQ(NotificationDatabase::STATUS_OK,
224 database->Open(true /* create_if_missing */));
225
226 GURL origin("https://example.com");
227
peterc45944c32016-09-13 14:35:59228 NotificationDatabaseData database_data, read_database_data;
229 database_data.notification_id = GenerateNotificationId();
peterd5c5bf152015-03-20 19:14:40230
peterc45944c32016-09-13 14:35:59231 ASSERT_EQ(NotificationDatabase::STATUS_OK,
232 database->WriteNotificationData(origin, database_data));
peterd5c5bf152015-03-20 19:14:40233
peterc45944c32016-09-13 14:35:59234 ASSERT_EQ(NotificationDatabase::STATUS_OK,
235 database->ReadNotificationData(database_data.notification_id,
236 origin, &read_database_data));
peterd5c5bf152015-03-20 19:14:40237
peterc45944c32016-09-13 14:35:59238 EXPECT_EQ(database_data.notification_id, read_database_data.notification_id);
peterd5c5bf152015-03-20 19:14:40239}
240
peter513eb552015-03-16 18:55:04241TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) {
dcheng59716272016-04-09 05:19:08242 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter513eb552015-03-16 18:55:04243 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,
peterc45944c32016-09-13 14:35:59251 database->ReadNotificationData("bad-id", GURL("https://chrome.com"),
peter513eb552015-03-16 18:55:04252 &database_data));
253}
254
255TEST_F(NotificationDatabaseTest, ReadNotificationDataDifferentOrigin) {
dcheng59716272016-04-09 05:19:08256 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter513eb552015-03-16 18:55:04257 ASSERT_EQ(NotificationDatabase::STATUS_OK,
258 database->Open(true /* create_if_missing */));
259
peter513eb552015-03-16 18:55:04260 GURL origin("https://example.com");
261
262 NotificationDatabaseData database_data, read_database_data;
peterc45944c32016-09-13 14:35:59263 database_data.notification_id = GenerateNotificationId();
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24264 database_data.notification_data.title = u"My Notification";
peter513eb552015-03-16 18:55:04265
peterc45944c32016-09-13 14:35:59266 ASSERT_EQ(NotificationDatabase::STATUS_OK,
267 database->WriteNotificationData(origin, database_data));
peter513eb552015-03-16 18:55:04268
269 // Reading the notification from the database when given a different origin
270 // should return the ERROR_NOT_FOUND status code.
peterc45944c32016-09-13 14:35:59271 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
272 database->ReadNotificationData(database_data.notification_id,
273 GURL("https://chrome.com"),
274 &read_database_data));
peter513eb552015-03-16 18:55:04275
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,
peterc45944c32016-09-13 14:35:59279 database->ReadNotificationData(database_data.notification_id,
280 origin, &read_database_data));
peter513eb552015-03-16 18:55:04281
282 EXPECT_EQ(database_data.notification_data.title,
283 read_database_data.notification_data.title);
284}
285
286TEST_F(NotificationDatabaseTest, ReadNotificationDataReflection) {
dcheng59716272016-04-09 05:19:08287 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter05e061b2015-03-13 13:16:06288 ASSERT_EQ(NotificationDatabase::STATUS_OK,
289 database->Open(true /* create_if_missing */));
290
peter513eb552015-03-16 18:55:04291 GURL origin("https://example.com");
292
Han Leon96d6b6e8c22018-09-06 06:21:06293 blink::PlatformNotificationData notification_data;
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24294 notification_data.title = u"My Notification";
peter513eb552015-03-16 18:55:04295 notification_data.direction =
Amos Lim3dce63b772019-02-21 16:56:34296 blink::mojom::NotificationDirection::RIGHT_TO_LEFT;
peter513eb552015-03-16 18:55:04297 notification_data.lang = "nl-NL";
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24298 notification_data.body = u"Hello, world!";
peter513eb552015-03-16 18:55:04299 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;
peterc45944c32016-09-13 14:35:59304 database_data.notification_id = GenerateNotificationId();
peter513eb552015-03-16 18:55:04305 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.
peterc45944c32016-09-13 14:35:59311 ASSERT_EQ(NotificationDatabase::STATUS_OK,
312 database->WriteNotificationData(origin, database_data));
peter05e061b2015-03-13 13:16:06313
peter513eb552015-03-16 18:55:04314 NotificationDatabaseData read_database_data;
315 ASSERT_EQ(NotificationDatabase::STATUS_OK,
peterc45944c32016-09-13 14:35:59316 database->ReadNotificationData(database_data.notification_id,
317 origin, &read_database_data));
peter05e061b2015-03-13 13:16:06318
peter513eb552015-03-16 18:55:04319 // 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
peterc45944c32016-09-13 14:35:59322 EXPECT_EQ(database_data.notification_id, read_database_data.notification_id);
peterd5c5bf152015-03-20 19:14:40323
peter513eb552015-03-16 18:55:04324 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 Leon96d6b6e8c22018-09-06 06:21:06328 const blink::PlatformNotificationData& read_notification_data =
peter513eb552015-03-16 18:55:04329 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 Knoll2e136ef2019-03-07 09:45:11340TEST_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
354TEST_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örrie2c470ea2021-03-22 22:26:24364 database_data.notification_data.title = u"My Notification";
Richard Knoll2e136ef2019-03-07 09:45:11365 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
384TEST_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
peter513eb552015-03-16 18:55:04427TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) {
dcheng59716272016-04-09 05:19:08428 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter513eb552015-03-16 18:55:04429 ASSERT_EQ(NotificationDatabase::STATUS_OK,
430 database->Open(true /* create_if_missing */));
431
peter513eb552015-03-16 18:55:04432 GURL origin("https://example.com");
peterc45944c32016-09-13 14:35:59433
434 std::vector<std::string> notification_ids;
peter513eb552015-03-16 18:55:04435
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 Kastingeb8c3ce2021-08-20 04:39:35439 std::string notification_id;
peteradb2fe22015-03-19 18:49:28440 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
Richard Knoll5dcde8862021-07-16 14:38:56441 database.get(), origin, "" /* tag */, false /* is_shown_by_browser */,
442 i /* sw_registration_id */, &notification_id));
peterc45944c32016-09-13 14:35:59443
444 EXPECT_FALSE(notification_id.empty());
445
446 notification_ids.push_back(notification_id);
peter513eb552015-03-16 18:55:04447 }
448
peteradb2fe22015-03-19 18:49:28449 NotificationDatabaseData database_data;
450
peterc45944c32016-09-13 14:35:59451 int64_t service_worker_registration_id = 1;
452
peter513eb552015-03-16 18:55:04453 // Read the ten notifications from the database, and verify that the titles
454 // of each of them matches with how they were created.
peterc45944c32016-09-13 14:35:59455 for (const std::string& notification_id : notification_ids) {
peter513eb552015-03-16 18:55:04456 ASSERT_EQ(NotificationDatabase::STATUS_OK,
peterc45944c32016-09-13 14:35:59457 database->ReadNotificationData(notification_id, origin,
peter513eb552015-03-16 18:55:04458 &database_data));
459
peterc45944c32016-09-13 14:35:59460 EXPECT_EQ(service_worker_registration_id++,
461 database_data.service_worker_registration_id);
peter513eb552015-03-16 18:55:04462 }
463}
464
Sharon Yangd96cd4b2018-05-31 11:43:43465TEST_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örrie2c470ea2021-03-22 22:26:24474 database_data.notification_data.title = u"My Notification";
Sharon Yangd96cd4b2018-05-31 11:43:43475
476 ASSERT_EQ(NotificationDatabase::STATUS_OK,
477 database->WriteNotificationData(origin, database_data));
478
Sharon Yang71bfb2be2018-06-04 16:04:40479 // 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 Yangd96cd4b2018-05-31 11:43:43485 // 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 Yang71bfb2be2018-06-04 16:04:40507
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 Yangd96cd4b2018-05-31 11:43:43520}
521
peter513eb552015-03-16 18:55:04522TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) {
dcheng59716272016-04-09 05:19:08523 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter513eb552015-03-16 18:55:04524 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.
peterc45944c32016-09-13 14:35:59528 ASSERT_EQ(
529 NotificationDatabase::STATUS_OK,
530 database->DeleteNotificationData("bad-id", GURL("https://chrome.com")));
peter513eb552015-03-16 18:55:04531}
532
533TEST_F(NotificationDatabaseTest, DeleteNotificationDataSameOrigin) {
dcheng59716272016-04-09 05:19:08534 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter513eb552015-03-16 18:55:04535 ASSERT_EQ(NotificationDatabase::STATUS_OK,
536 database->Open(true /* create_if_missing */));
537
peterc45944c32016-09-13 14:35:59538 const std::string notification_id = GenerateNotificationId();
peter513eb552015-03-16 18:55:04539
540 NotificationDatabaseData database_data;
peterc45944c32016-09-13 14:35:59541 database_data.notification_id = notification_id;
542
peter513eb552015-03-16 18:55:04543 GURL origin("https://example.com");
544
peterc45944c32016-09-13 14:35:59545 ASSERT_EQ(NotificationDatabase::STATUS_OK,
546 database->WriteNotificationData(origin, database_data));
peter513eb552015-03-16 18:55:04547
548 // Reading a notification after writing one should succeed.
peter372525c2015-11-20 16:07:17549 EXPECT_EQ(
550 NotificationDatabase::STATUS_OK,
551 database->ReadNotificationData(notification_id, origin, &database_data));
peter513eb552015-03-16 18:55:04552
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));
peter372525c2015-11-20 16:07:17557 EXPECT_EQ(
558 NotificationDatabase::STATUS_ERROR_NOT_FOUND,
559 database->ReadNotificationData(notification_id, origin, &database_data));
peter513eb552015-03-16 18:55:04560}
561
Richard Knoll2e136ef2019-03-07 09:45:11562TEST_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 &notification_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 &notification_resources));
591}
592
peter513eb552015-03-16 18:55:04593TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) {
dcheng59716272016-04-09 05:19:08594 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peter513eb552015-03-16 18:55:04595 ASSERT_EQ(NotificationDatabase::STATUS_OK,
596 database->Open(true /* create_if_missing */));
597
peterc45944c32016-09-13 14:35:59598 const std::string notification_id = GenerateNotificationId();
peter513eb552015-03-16 18:55:04599
600 NotificationDatabaseData database_data;
peterc45944c32016-09-13 14:35:59601 database_data.notification_id = notification_id;
602
peter513eb552015-03-16 18:55:04603 GURL origin("https://example.com");
604
peterc45944c32016-09-13 14:35:59605 ASSERT_EQ(NotificationDatabase::STATUS_OK,
606 database->WriteNotificationData(origin, database_data));
peter513eb552015-03-16 18:55:04607
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
peter372525c2015-11-20 16:07:17616 EXPECT_EQ(
617 NotificationDatabase::STATUS_OK,
618 database->ReadNotificationData(notification_id, origin, &database_data));
peter05e061b2015-03-13 13:16:06619}
620
Richard Knollacd400ea2019-05-31 11:53:07621TEST_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
632TEST_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 &notification_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 &notification_resources));
661}
662
Richard Knoll25d3a8d2020-07-26 03:26:43663TEST_F(NotificationDatabaseTest,
664 ForEachNotificationDataForServiceWorkerRegistration) {
dcheng59716272016-04-09 05:19:08665 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peteradb2fe22015-03-19 18:49:28666 ASSERT_EQ(NotificationDatabase::STATUS_OK,
667 database->Open(true /* create_if_missing */));
668
669 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
670
Richard Knoll25d3a8d2020-07-26 03:26:43671 GURL origin("https://example.com:443");
peteradb2fe22015-03-19 18:49:28672
673 std::vector<NotificationDatabaseData> notifications;
674 ASSERT_EQ(NotificationDatabase::STATUS_OK,
Richard Knoll25d3a8d2020-07-26 03:26:43675 database->ForEachNotificationDataForServiceWorkerRegistration(
676 origin, kExampleServiceWorkerRegistrationId,
677 base::BindLambdaForTesting(
678 [&notifications](const NotificationDatabaseData& data) {
679 notifications.push_back(data);
680 })));
peteradb2fe22015-03-19 18:49:28681
Richard Knoll25d3a8d2020-07-26 03:26:43682 EXPECT_EQ(2u, notifications.size());
peteradb2fe22015-03-19 18:49:28683}
684
685TEST_F(NotificationDatabaseTest, ReadAllNotificationDataForOrigin) {
dcheng59716272016-04-09 05:19:08686 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peteradb2fe22015-03-19 18:49:28687 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, &notifications));
697
698 EXPECT_EQ(4u, notifications.size());
699}
700
701TEST_F(NotificationDatabaseTest,
702 ReadAllNotificationDataForServiceWorkerRegistration) {
dcheng59716272016-04-09 05:19:08703 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peteradb2fe22015-03-19 18:49:28704 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 Knoll5dcde8862021-07-16 14:38:56714 origin, kExampleServiceWorkerRegistrationId,
Arthur Sonzognic686e8f2024-01-11 08:36:37715 std::nullopt /* is_shown_by_browser */, &notifications));
peteradb2fe22015-03-19 18:49:28716
717 EXPECT_EQ(2u, notifications.size());
718}
719
Richard Knoll5dcde8862021-07-16 14:38:56720TEST_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 */, &notifications));
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 */, &notifications));
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 Sonzognic686e8f2024-01-11 08:36:37766 std::nullopt /* is_shown_by_browser */, &notifications));
Richard Knoll5dcde8862021-07-16 14:38:56767 ASSERT_EQ(2u, notifications.size());
768 }
769}
770
peteradb2fe22015-03-19 18:49:28771TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOrigin) {
dcheng59716272016-04-09 05:19:08772 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peteradb2fe22015-03-19 18:49:28773 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
peterc45944c32016-09-13 14:35:59780 std::set<std::string> deleted_notification_ids;
peteradb2fe22015-03-19 18:49:28781 ASSERT_EQ(NotificationDatabase::STATUS_OK,
782 database->DeleteAllNotificationDataForOrigin(
Arthur Sonzognic686e8f2024-01-11 08:36:37783 origin, "" /* tag */, std::nullopt /* is_shown_by_browser */,
Richard Knoll5dcde8862021-07-16 14:38:56784 &deleted_notification_ids));
peteradb2fe22015-03-19 18:49:28785
peterc45944c32016-09-13 14:35:59786 EXPECT_EQ(4u, deleted_notification_ids.size());
peteradb2fe22015-03-19 18:49:28787
788 std::vector<NotificationDatabaseData> notifications;
789 ASSERT_EQ(NotificationDatabase::STATUS_OK,
790 database->ReadAllNotificationDataForOrigin(origin, &notifications));
791
792 EXPECT_EQ(0u, notifications.size());
793}
794
peter3ce3c462016-08-09 16:23:58795TEST_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, &notifications));
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
peterc45944c32016-09-13 14:35:59823 std::set<std::string> deleted_notification_ids;
Arthur Sonzognic686e8f2024-01-11 08:36:37824 ASSERT_EQ(NotificationDatabase::STATUS_OK,
825 database->DeleteAllNotificationDataForOrigin(
826 origin, "foo" /* tag */, std::nullopt /* is_shown_by_browser */,
827 &deleted_notification_ids));
peter3ce3c462016-08-09 16:23:58828
peterc45944c32016-09-13 14:35:59829 EXPECT_EQ(notifications_with_tag, deleted_notification_ids.size());
peter3ce3c462016-08-09 16:23:58830
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
peteradb2fe22015-03-19 18:49:28852TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginEmpty) {
dcheng59716272016-04-09 05:19:08853 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peteradb2fe22015-03-19 18:49:28854 ASSERT_EQ(NotificationDatabase::STATUS_OK,
855 database->Open(true /* create_if_missing */));
856
857 GURL origin("https://example.com");
858
peterc45944c32016-09-13 14:35:59859 std::set<std::string> deleted_notification_ids;
peteradb2fe22015-03-19 18:49:28860 ASSERT_EQ(NotificationDatabase::STATUS_OK,
861 database->DeleteAllNotificationDataForOrigin(
Arthur Sonzognic686e8f2024-01-11 08:36:37862 origin, "" /* tag */, std::nullopt /* is_shown_by_browser */,
Richard Knoll5dcde8862021-07-16 14:38:56863 &deleted_notification_ids));
peteradb2fe22015-03-19 18:49:28864
peterc45944c32016-09-13 14:35:59865 EXPECT_EQ(0u, deleted_notification_ids.size());
peteradb2fe22015-03-19 18:49:28866}
867
868TEST_F(NotificationDatabaseTest,
Richard Knoll5dcde8862021-07-16 14:38:56869 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, &notifications));
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 Sonzognic686e8f2024-01-11 08:36:37930 origin, kTag, std::nullopt /* is_shown_by_browser */,
Richard Knoll5dcde8862021-07-16 14:38:56931 &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, &notifications));
941 EXPECT_EQ(0u, notifications.size());
942}
943
944TEST_F(NotificationDatabaseTest,
peteradb2fe22015-03-19 18:49:28945 DeleteAllNotificationDataForServiceWorkerRegistration) {
dcheng59716272016-04-09 05:19:08946 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
peteradb2fe22015-03-19 18:49:28947 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");
peterc45944c32016-09-13 14:35:59953 std::set<std::string> deleted_notification_ids;
peteradb2fe22015-03-19 18:49:28954 ASSERT_EQ(NotificationDatabase::STATUS_OK,
955 database->DeleteAllNotificationDataForServiceWorkerRegistration(
peter372525c2015-11-20 16:07:17956 origin, kExampleServiceWorkerRegistrationId,
peterc45944c32016-09-13 14:35:59957 &deleted_notification_ids));
peteradb2fe22015-03-19 18:49:28958
peterc45944c32016-09-13 14:35:59959 EXPECT_EQ(2u, deleted_notification_ids.size());
peteradb2fe22015-03-19 18:49:28960
961 std::vector<NotificationDatabaseData> notifications;
962 ASSERT_EQ(NotificationDatabase::STATUS_OK,
963 database->ReadAllNotificationDataForServiceWorkerRegistration(
Richard Knoll5dcde8862021-07-16 14:38:56964 origin, kExampleServiceWorkerRegistrationId,
Arthur Sonzognic686e8f2024-01-11 08:36:37965 std::nullopt /* is_shown_by_browser */, &notifications));
peteradb2fe22015-03-19 18:49:28966
967 EXPECT_EQ(0u, notifications.size());
968}
969
peter5bb2e1452015-03-12 15:31:16970} // namespace content