Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: content/browser/notifications/notification_database_unittest.cc

Issue 2300093002: Make //content responsible for generating notification Ids (Closed)
Patch Set: rebase + comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/notifications/notification_database.h" 5 #include "content/browser/notifications/notification_database.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/guid.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
14 #include "content/public/browser/notification_database_data.h" 15 #include "content/public/browser/notification_database_data.h"
15 #include "content/public/common/platform_notification_data.h" 16 #include "content/public/common/platform_notification_data.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/leveldatabase/src/include/leveldb/db.h" 18 #include "third_party/leveldatabase/src/include/leveldb/db.h"
18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
(...skipping 28 matching lines...) Expand all
49 return new NotificationDatabase(path); 50 return new NotificationDatabase(path);
50 } 51 }
51 52
52 // Creates a new notification for |service_worker_registration_id| belonging 53 // Creates a new notification for |service_worker_registration_id| belonging
53 // to |origin| and writes it to the database. The written notification id 54 // to |origin| and writes it to the database. The written notification id
54 // will be stored in |notification_id|. 55 // will be stored in |notification_id|.
55 void CreateAndWriteNotification(NotificationDatabase* database, 56 void CreateAndWriteNotification(NotificationDatabase* database,
56 const GURL& origin, 57 const GURL& origin,
57 const std::string& tag, 58 const std::string& tag,
58 int64_t service_worker_registration_id, 59 int64_t service_worker_registration_id,
59 int64_t* notification_id) { 60 std::string* notification_id) {
61 DCHECK(notification_id);
62
60 NotificationDatabaseData database_data; 63 NotificationDatabaseData database_data;
64 database_data.notification_id = base::GenerateGUID();
61 database_data.origin = origin; 65 database_data.origin = origin;
62 database_data.service_worker_registration_id = 66 database_data.service_worker_registration_id =
63 service_worker_registration_id; 67 service_worker_registration_id;
64 database_data.notification_data.tag = tag; 68 database_data.notification_data.tag = tag;
65 69
66 ASSERT_EQ(NotificationDatabase::STATUS_OK, 70 ASSERT_EQ(NotificationDatabase::STATUS_OK,
67 database->WriteNotificationData(origin, database_data, 71 database->WriteNotificationData(origin, database_data));
68 notification_id)); 72
73 *notification_id = database_data.notification_id;
69 } 74 }
70 75
71 // Populates |database| with a series of example notifications that differ in 76 // Populates |database| with a series of example notifications that differ in
72 // their origin and Service Worker registration id. 77 // their origin and Service Worker registration id.
73 void PopulateDatabaseWithExampleData(NotificationDatabase* database) { 78 void PopulateDatabaseWithExampleData(NotificationDatabase* database) {
74 int64_t notification_id; 79 std::string notification_id;
75 for (size_t i = 0; i < arraysize(kExampleNotificationData); ++i) { 80 for (size_t i = 0; i < arraysize(kExampleNotificationData); ++i) {
76 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( 81 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
77 database, GURL(kExampleNotificationData[i].origin), 82 database, GURL(kExampleNotificationData[i].origin),
78 kExampleNotificationData[i].tag, 83 kExampleNotificationData[i].tag,
79 kExampleNotificationData[i].service_worker_registration_id, 84 kExampleNotificationData[i].service_worker_registration_id,
80 &notification_id)); 85 &notification_id));
81 } 86 }
82 } 87 }
83 88
84 // Returns if |database| has been opened. 89 // Returns if |database| has been opened.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 TEST_F(NotificationDatabaseTest, NotificationIdIncrements) { 178 TEST_F(NotificationDatabaseTest, NotificationIdIncrements) {
174 base::ScopedTempDir database_dir; 179 base::ScopedTempDir database_dir;
175 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); 180 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
176 181
177 std::unique_ptr<NotificationDatabase> database( 182 std::unique_ptr<NotificationDatabase> database(
178 CreateDatabaseOnFileSystem(database_dir.path())); 183 CreateDatabaseOnFileSystem(database_dir.path()));
179 184
180 ASSERT_EQ(NotificationDatabase::STATUS_OK, 185 ASSERT_EQ(NotificationDatabase::STATUS_OK,
181 database->Open(true /* create_if_missing */)); 186 database->Open(true /* create_if_missing */));
182 187
188 EXPECT_EQ(database->GetNextPersistentNotificationId(), 1);
189 EXPECT_EQ(database->GetNextPersistentNotificationId(), 2);
190 EXPECT_EQ(database->GetNextPersistentNotificationId(), 3);
191
183 GURL origin("https://example.com"); 192 GURL origin("https://example.com");
184 193
185 int64_t notification_id = 0; 194 std::string notification_id;
186
187 // Verify that getting two ids on the same database instance results in
188 // incrementing values. Notification ids will start at 1.
189 ASSERT_NO_FATAL_FAILURE( 195 ASSERT_NO_FATAL_FAILURE(
190 CreateAndWriteNotification(database.get(), origin, "" /* tag */, 196 CreateAndWriteNotification(database.get(), origin, "" /* tag */,
191 0 /* sw_registration_id */, &notification_id)); 197 0 /* sw_registration_id */, &notification_id));
192 EXPECT_EQ(notification_id, 1);
193
194 ASSERT_NO_FATAL_FAILURE(
195 CreateAndWriteNotification(database.get(), origin, "" /* tag */,
196 0 /* sw_registration_id */, &notification_id));
197 EXPECT_EQ(notification_id, 2);
198 198
199 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); 199 database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
200 ASSERT_EQ(NotificationDatabase::STATUS_OK, 200 ASSERT_EQ(NotificationDatabase::STATUS_OK,
201 database->Open(false /* create_if_missing */)); 201 database->Open(false /* create_if_missing */));
202 202
203 // Verify that the next notification id was stored in the database, and 203 // Verify that the next persistent notification id was stored in the database,
204 // continues where we expect it to be, even after closing and opening it. 204 // and continues where we expect it to be, even after closing and opening it.
205 ASSERT_NO_FATAL_FAILURE( 205 EXPECT_EQ(database->GetNextPersistentNotificationId(), 4);
206 CreateAndWriteNotification(database.get(), origin, "" /* tag */,
207 0 /* sw_registration_id */, &notification_id));
208 EXPECT_EQ(notification_id, 3);
209 } 206 }
210 207
211 TEST_F(NotificationDatabaseTest, NotificationIdIncrementsStorage) { 208 TEST_F(NotificationDatabaseTest, NotificationIdIncrementsStorage) {
212 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 209 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
213 ASSERT_EQ(NotificationDatabase::STATUS_OK, 210 ASSERT_EQ(NotificationDatabase::STATUS_OK,
214 database->Open(true /* create_if_missing */)); 211 database->Open(true /* create_if_missing */));
215 212
216 GURL origin("https://example.com"); 213 GURL origin("https://example.com");
217 214
218 NotificationDatabaseData database_data; 215 NotificationDatabaseData database_data, read_database_data;
219 database_data.notification_id = -1; 216 database_data.notification_id = base::GenerateGUID();
220 217
221 int64_t notification_id = 0; 218 ASSERT_EQ(NotificationDatabase::STATUS_OK,
222 ASSERT_EQ( 219 database->WriteNotificationData(origin, database_data));
223 NotificationDatabase::STATUS_OK,
224 database->WriteNotificationData(origin, database_data, &notification_id));
225 220
226 ASSERT_EQ( 221 ASSERT_EQ(NotificationDatabase::STATUS_OK,
227 NotificationDatabase::STATUS_OK, 222 database->ReadNotificationData(database_data.notification_id,
228 database->ReadNotificationData(notification_id, origin, &database_data)); 223 origin, &read_database_data));
229 224
230 EXPECT_EQ(notification_id, database_data.notification_id); 225 EXPECT_EQ(database_data.notification_id, read_database_data.notification_id);
231 } 226 }
232 227
233 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) { 228 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) {
234 base::ScopedTempDir database_dir; 229 base::ScopedTempDir database_dir;
235 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); 230 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
236 231
237 std::unique_ptr<NotificationDatabase> database( 232 std::unique_ptr<NotificationDatabase> database(
238 CreateDatabaseOnFileSystem(database_dir.path())); 233 CreateDatabaseOnFileSystem(database_dir.path()));
239 234
240 ASSERT_EQ(NotificationDatabase::STATUS_OK, 235 ASSERT_EQ(NotificationDatabase::STATUS_OK,
241 database->Open(true /* create_if_missing */)); 236 database->Open(true /* create_if_missing */));
242 237
243 GURL origin("https://example.com"); 238 GURL origin("https://example.com");
244 239
245 NotificationDatabaseData database_data; 240 NotificationDatabaseData database_data;
246 int64_t notification_id = 0; 241 database_data.notification_id = base::GenerateGUID();
247 242
248 ASSERT_EQ( 243 ASSERT_EQ(NotificationDatabase::STATUS_OK,
249 NotificationDatabase::STATUS_OK, 244 database->WriteNotificationData(origin, database_data));
250 database->WriteNotificationData(origin, database_data, &notification_id));
251 EXPECT_EQ(notification_id, 1);
252 245
253 // Deliberately write an invalid value as the next notification id. When 246 // Deliberately write an invalid value as the next notification id. When
254 // re-opening the database, the Open() method should realize that an invalid 247 // re-opening the database, the Open() method should realize that an invalid
255 // value is being read, and mark the database as corrupted. 248 // value is being read, and mark the database as corrupted.
256 ASSERT_NO_FATAL_FAILURE( 249 ASSERT_NO_FATAL_FAILURE(
257 WriteLevelDBKeyValuePair(database.get(), "NEXT_NOTIFICATION_ID", "-42")); 250 WriteLevelDBKeyValuePair(database.get(), "NEXT_NOTIFICATION_ID", "-42"));
258 251
259 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); 252 database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
260 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED, 253 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED,
261 database->Open(false /* create_if_missing */)); 254 database->Open(false /* create_if_missing */));
262 } 255 }
263 256
264 TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) { 257 TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) {
265 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 258 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
266 ASSERT_EQ(NotificationDatabase::STATUS_OK, 259 ASSERT_EQ(NotificationDatabase::STATUS_OK,
267 database->Open(true /* create_if_missing */)); 260 database->Open(true /* create_if_missing */));
268 261
269 NotificationDatabaseData database_data; 262 NotificationDatabaseData database_data;
270 263
271 // Reading the notification data for a notification that does not exist should 264 // Reading the notification data for a notification that does not exist should
272 // return the ERROR_NOT_FOUND status code. 265 // return the ERROR_NOT_FOUND status code.
273 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, 266 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
274 database->ReadNotificationData(9001, GURL("https://chrome.com"), 267 database->ReadNotificationData("my-id", GURL("https://chrome.com"),
275 &database_data)); 268 &database_data));
276 } 269 }
277 270
278 TEST_F(NotificationDatabaseTest, ReadNotificationDataDifferentOrigin) { 271 TEST_F(NotificationDatabaseTest, ReadNotificationDataDifferentOrigin) {
279 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 272 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
280 ASSERT_EQ(NotificationDatabase::STATUS_OK, 273 ASSERT_EQ(NotificationDatabase::STATUS_OK,
281 database->Open(true /* create_if_missing */)); 274 database->Open(true /* create_if_missing */));
282 275
283 int64_t notification_id = 0;
284 GURL origin("https://example.com"); 276 GURL origin("https://example.com");
285 277
286 NotificationDatabaseData database_data, read_database_data; 278 NotificationDatabaseData database_data, read_database_data;
279 database_data.notification_id = base::GenerateGUID();
287 database_data.notification_data.title = base::UTF8ToUTF16("My Notification"); 280 database_data.notification_data.title = base::UTF8ToUTF16("My Notification");
288 281
289 ASSERT_EQ( 282 ASSERT_EQ(NotificationDatabase::STATUS_OK,
290 NotificationDatabase::STATUS_OK, 283 database->WriteNotificationData(origin, database_data));
291 database->WriteNotificationData(origin, database_data, &notification_id));
292 284
293 // Reading the notification from the database when given a different origin 285 // Reading the notification from the database when given a different origin
294 // should return the ERROR_NOT_FOUND status code. 286 // should return the ERROR_NOT_FOUND status code.
295 EXPECT_EQ( 287 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
296 NotificationDatabase::STATUS_ERROR_NOT_FOUND, 288 database->ReadNotificationData(database_data.notification_id,
297 database->ReadNotificationData( 289 GURL("https://chrome.com"),
298 notification_id, GURL("https://chrome.com"), &read_database_data)); 290 &read_database_data));
299 291
300 // However, reading the notification from the database with the same origin 292 // However, reading the notification from the database with the same origin
301 // should return STATUS_OK and the associated notification data. 293 // should return STATUS_OK and the associated notification data.
302 ASSERT_EQ(NotificationDatabase::STATUS_OK, 294 ASSERT_EQ(NotificationDatabase::STATUS_OK,
303 database->ReadNotificationData(notification_id, origin, 295 database->ReadNotificationData(database_data.notification_id,
304 &read_database_data)); 296 origin, &read_database_data));
305 297
306 EXPECT_EQ(database_data.notification_data.title, 298 EXPECT_EQ(database_data.notification_data.title,
307 read_database_data.notification_data.title); 299 read_database_data.notification_data.title);
308 } 300 }
309 301
310 TEST_F(NotificationDatabaseTest, ReadNotificationDataReflection) { 302 TEST_F(NotificationDatabaseTest, ReadNotificationDataReflection) {
311 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 303 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
312 ASSERT_EQ(NotificationDatabase::STATUS_OK, 304 ASSERT_EQ(NotificationDatabase::STATUS_OK,
313 database->Open(true /* create_if_missing */)); 305 database->Open(true /* create_if_missing */));
314 306
315 int64_t notification_id = 0;
316
317 GURL origin("https://example.com"); 307 GURL origin("https://example.com");
318 308
319 PlatformNotificationData notification_data; 309 PlatformNotificationData notification_data;
320 notification_data.title = base::UTF8ToUTF16("My Notification"); 310 notification_data.title = base::UTF8ToUTF16("My Notification");
321 notification_data.direction = 311 notification_data.direction =
322 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT; 312 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT;
323 notification_data.lang = "nl-NL"; 313 notification_data.lang = "nl-NL";
324 notification_data.body = base::UTF8ToUTF16("Hello, world!"); 314 notification_data.body = base::UTF8ToUTF16("Hello, world!");
325 notification_data.tag = "replace id"; 315 notification_data.tag = "replace id";
326 notification_data.icon = GURL("https://example.com/icon.png"); 316 notification_data.icon = GURL("https://example.com/icon.png");
327 notification_data.silent = true; 317 notification_data.silent = true;
328 318
329 NotificationDatabaseData database_data; 319 NotificationDatabaseData database_data;
330 database_data.notification_id = notification_id; 320 database_data.notification_id = base::GenerateGUID();
331 database_data.origin = origin; 321 database_data.origin = origin;
332 database_data.service_worker_registration_id = 42; 322 database_data.service_worker_registration_id = 42;
333 database_data.notification_data = notification_data; 323 database_data.notification_data = notification_data;
334 324
335 // Write the constructed notification to the database, and then immediately 325 // Write the constructed notification to the database, and then immediately
336 // read it back from the database again as well. 326 // read it back from the database again as well.
337 ASSERT_EQ( 327 ASSERT_EQ(NotificationDatabase::STATUS_OK,
338 NotificationDatabase::STATUS_OK, 328 database->WriteNotificationData(origin, database_data));
339 database->WriteNotificationData(origin, database_data, &notification_id));
340 329
341 NotificationDatabaseData read_database_data; 330 NotificationDatabaseData read_database_data;
342 ASSERT_EQ(NotificationDatabase::STATUS_OK, 331 ASSERT_EQ(NotificationDatabase::STATUS_OK,
343 database->ReadNotificationData(notification_id, origin, 332 database->ReadNotificationData(database_data.notification_id,
344 &read_database_data)); 333 origin, &read_database_data));
345 334
346 // Verify that all members retrieved from the database are exactly the same 335 // Verify that all members retrieved from the database are exactly the same
347 // as the ones that were written to it. This tests the serialization behavior. 336 // as the ones that were written to it. This tests the serialization behavior.
348 337
349 EXPECT_EQ(notification_id, read_database_data.notification_id); 338 EXPECT_EQ(database_data.notification_id, read_database_data.notification_id);
350 339
351 EXPECT_EQ(database_data.origin, read_database_data.origin); 340 EXPECT_EQ(database_data.origin, read_database_data.origin);
352 EXPECT_EQ(database_data.service_worker_registration_id, 341 EXPECT_EQ(database_data.service_worker_registration_id,
353 read_database_data.service_worker_registration_id); 342 read_database_data.service_worker_registration_id);
354 343
355 const PlatformNotificationData& read_notification_data = 344 const PlatformNotificationData& read_notification_data =
356 read_database_data.notification_data; 345 read_database_data.notification_data;
357 346
358 EXPECT_EQ(notification_data.title, read_notification_data.title); 347 EXPECT_EQ(notification_data.title, read_notification_data.title);
359 EXPECT_EQ(notification_data.direction, read_notification_data.direction); 348 EXPECT_EQ(notification_data.direction, read_notification_data.direction);
360 EXPECT_EQ(notification_data.lang, read_notification_data.lang); 349 EXPECT_EQ(notification_data.lang, read_notification_data.lang);
361 EXPECT_EQ(notification_data.body, read_notification_data.body); 350 EXPECT_EQ(notification_data.body, read_notification_data.body);
362 EXPECT_EQ(notification_data.tag, read_notification_data.tag); 351 EXPECT_EQ(notification_data.tag, read_notification_data.tag);
363 EXPECT_EQ(notification_data.icon, read_notification_data.icon); 352 EXPECT_EQ(notification_data.icon, read_notification_data.icon);
364 EXPECT_EQ(notification_data.silent, read_notification_data.silent); 353 EXPECT_EQ(notification_data.silent, read_notification_data.silent);
365 } 354 }
366 355
367 TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) { 356 TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) {
368 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 357 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
369 ASSERT_EQ(NotificationDatabase::STATUS_OK, 358 ASSERT_EQ(NotificationDatabase::STATUS_OK,
370 database->Open(true /* create_if_missing */)); 359 database->Open(true /* create_if_missing */));
371 360
372 GURL origin("https://example.com"); 361 GURL origin("https://example.com");
373 int64_t notification_id = 0; 362
363 std::vector<std::string> notification_ids;
364 std::string notification_id;
374 365
375 // Write ten notifications to the database, each with a unique title and 366 // Write ten notifications to the database, each with a unique title and
376 // notification id (it is the responsibility of the user to increment this). 367 // notification id (it is the responsibility of the user to increment this).
377 for (int i = 1; i <= 10; ++i) { 368 for (int i = 1; i <= 10; ++i) {
378 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( 369 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
379 database.get(), origin, "" /* tag */, i /* sw_registration_id */, 370 database.get(), origin, "" /* tag */, i /* sw_registration_id */,
380 &notification_id)); 371 &notification_id));
381 EXPECT_EQ(notification_id, i); 372
373 EXPECT_FALSE(notification_id.empty());
374
375 notification_ids.push_back(notification_id);
382 } 376 }
383 377
384 NotificationDatabaseData database_data; 378 NotificationDatabaseData database_data;
385 379
380 int64_t service_worker_registration_id = 1;
381
386 // Read the ten notifications from the database, and verify that the titles 382 // Read the ten notifications from the database, and verify that the titles
387 // of each of them matches with how they were created. 383 // of each of them matches with how they were created.
388 for (int i = 1; i <= 10; ++i) { 384 for (const std::string& notification_id : notification_ids) {
389 ASSERT_EQ(NotificationDatabase::STATUS_OK, 385 ASSERT_EQ(NotificationDatabase::STATUS_OK,
390 database->ReadNotificationData(i /* notification_id */, origin, 386 database->ReadNotificationData(notification_id, origin,
391 &database_data)); 387 &database_data));
392 388
393 EXPECT_EQ(i, database_data.service_worker_registration_id); 389 EXPECT_EQ(service_worker_registration_id++,
390 database_data.service_worker_registration_id);
394 } 391 }
395 } 392 }
396 393
397 TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) { 394 TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) {
398 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 395 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
399 ASSERT_EQ(NotificationDatabase::STATUS_OK, 396 ASSERT_EQ(NotificationDatabase::STATUS_OK,
400 database->Open(true /* create_if_missing */)); 397 database->Open(true /* create_if_missing */));
401 398
402 // Deleting non-existing notifications is not considered to be a failure. 399 // Deleting non-existing notifications is not considered to be a failure.
403 ASSERT_EQ(NotificationDatabase::STATUS_OK, 400 ASSERT_EQ(
404 database->DeleteNotificationData(9001, GURL("https://chrome.com"))); 401 NotificationDatabase::STATUS_OK,
402 database->DeleteNotificationData("9001", GURL("https://chrome.com")));
405 } 403 }
406 404
407 TEST_F(NotificationDatabaseTest, DeleteNotificationDataSameOrigin) { 405 TEST_F(NotificationDatabaseTest, DeleteNotificationDataSameOrigin) {
408 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 406 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
409 ASSERT_EQ(NotificationDatabase::STATUS_OK, 407 ASSERT_EQ(NotificationDatabase::STATUS_OK,
410 database->Open(true /* create_if_missing */)); 408 database->Open(true /* create_if_missing */));
411 409
412 int64_t notification_id = 0; 410 const std::string notification_id = base::GenerateGUID();
413 411
414 NotificationDatabaseData database_data; 412 NotificationDatabaseData database_data;
413 database_data.notification_id = notification_id;
414
415 GURL origin("https://example.com"); 415 GURL origin("https://example.com");
416 416
417 ASSERT_EQ( 417 ASSERT_EQ(NotificationDatabase::STATUS_OK,
418 NotificationDatabase::STATUS_OK, 418 database->WriteNotificationData(origin, database_data));
419 database->WriteNotificationData(origin, database_data, &notification_id));
420 419
421 // Reading a notification after writing one should succeed. 420 // Reading a notification after writing one should succeed.
422 EXPECT_EQ( 421 EXPECT_EQ(
423 NotificationDatabase::STATUS_OK, 422 NotificationDatabase::STATUS_OK,
424 database->ReadNotificationData(notification_id, origin, &database_data)); 423 database->ReadNotificationData(notification_id, origin, &database_data));
425 424
426 // Delete the notification which was just written to the database, and verify 425 // Delete the notification which was just written to the database, and verify
427 // that reading it again will fail. 426 // that reading it again will fail.
428 EXPECT_EQ(NotificationDatabase::STATUS_OK, 427 EXPECT_EQ(NotificationDatabase::STATUS_OK,
429 database->DeleteNotificationData(notification_id, origin)); 428 database->DeleteNotificationData(notification_id, origin));
430 EXPECT_EQ( 429 EXPECT_EQ(
431 NotificationDatabase::STATUS_ERROR_NOT_FOUND, 430 NotificationDatabase::STATUS_ERROR_NOT_FOUND,
432 database->ReadNotificationData(notification_id, origin, &database_data)); 431 database->ReadNotificationData(notification_id, origin, &database_data));
433 } 432 }
434 433
435 TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) { 434 TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) {
436 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 435 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
437 ASSERT_EQ(NotificationDatabase::STATUS_OK, 436 ASSERT_EQ(NotificationDatabase::STATUS_OK,
438 database->Open(true /* create_if_missing */)); 437 database->Open(true /* create_if_missing */));
439 438
440 int64_t notification_id = 0; 439 const std::string notification_id = base::GenerateGUID();
441 440
442 NotificationDatabaseData database_data; 441 NotificationDatabaseData database_data;
442 database_data.notification_id = notification_id;
443
443 GURL origin("https://example.com"); 444 GURL origin("https://example.com");
444 445
445 ASSERT_EQ( 446 ASSERT_EQ(NotificationDatabase::STATUS_OK,
446 NotificationDatabase::STATUS_OK, 447 database->WriteNotificationData(origin, database_data));
447 database->WriteNotificationData(origin, database_data, &notification_id));
448 448
449 // Attempting to delete the notification with a different origin, but with the 449 // Attempting to delete the notification with a different origin, but with the
450 // same |notification_id|, should not return an error (the notification could 450 // same |notification_id|, should not return an error (the notification could
451 // not be found, but that's not considered a failure). However, it should not 451 // not be found, but that's not considered a failure). However, it should not
452 // remove the notification either. 452 // remove the notification either.
453 EXPECT_EQ(NotificationDatabase::STATUS_OK, 453 EXPECT_EQ(NotificationDatabase::STATUS_OK,
454 database->DeleteNotificationData(notification_id, 454 database->DeleteNotificationData(notification_id,
455 GURL("https://chrome.com"))); 455 GURL("https://chrome.com")));
456 456
457 EXPECT_EQ( 457 EXPECT_EQ(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 521
522 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOrigin) { 522 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOrigin) {
523 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 523 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
524 ASSERT_EQ(NotificationDatabase::STATUS_OK, 524 ASSERT_EQ(NotificationDatabase::STATUS_OK,
525 database->Open(true /* create_if_missing */)); 525 database->Open(true /* create_if_missing */));
526 526
527 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 527 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
528 528
529 GURL origin("https://example.com:443"); 529 GURL origin("https://example.com:443");
530 530
531 std::set<int64_t> deleted_notification_set; 531 std::set<std::string> deleted_notification_ids;
532 ASSERT_EQ(NotificationDatabase::STATUS_OK, 532 ASSERT_EQ(NotificationDatabase::STATUS_OK,
533 database->DeleteAllNotificationDataForOrigin( 533 database->DeleteAllNotificationDataForOrigin(
534 origin, "" /* tag */, &deleted_notification_set)); 534 origin, "" /* tag */, &deleted_notification_ids));
535 535
536 EXPECT_EQ(4u, deleted_notification_set.size()); 536 EXPECT_EQ(4u, deleted_notification_ids.size());
537 537
538 std::vector<NotificationDatabaseData> notifications; 538 std::vector<NotificationDatabaseData> notifications;
539 ASSERT_EQ(NotificationDatabase::STATUS_OK, 539 ASSERT_EQ(NotificationDatabase::STATUS_OK,
540 database->ReadAllNotificationDataForOrigin(origin, &notifications)); 540 database->ReadAllNotificationDataForOrigin(origin, &notifications));
541 541
542 EXPECT_EQ(0u, notifications.size()); 542 EXPECT_EQ(0u, notifications.size());
543 } 543 }
544 544
545 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginWithTag) { 545 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginWithTag) {
546 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 546 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
(...skipping 16 matching lines...) Expand all
563 for (const auto& database_data : notifications) { 563 for (const auto& database_data : notifications) {
564 if (database_data.notification_data.tag == tag) 564 if (database_data.notification_data.tag == tag)
565 ++notifications_with_tag; 565 ++notifications_with_tag;
566 else 566 else
567 ++notifications_without_tag; 567 ++notifications_without_tag;
568 } 568 }
569 569
570 ASSERT_GT(notifications_with_tag, 0u); 570 ASSERT_GT(notifications_with_tag, 0u);
571 ASSERT_GT(notifications_without_tag, 0u); 571 ASSERT_GT(notifications_without_tag, 0u);
572 572
573 std::set<int64_t> deleted_notification_set; 573 std::set<std::string> deleted_notification_ids;
574 ASSERT_EQ(NotificationDatabase::STATUS_OK, 574 ASSERT_EQ(NotificationDatabase::STATUS_OK,
575 database->DeleteAllNotificationDataForOrigin( 575 database->DeleteAllNotificationDataForOrigin(
576 origin, "foo" /* tag */, &deleted_notification_set)); 576 origin, "foo" /* tag */, &deleted_notification_ids));
577 577
578 EXPECT_EQ(notifications_with_tag, deleted_notification_set.size()); 578 EXPECT_EQ(notifications_with_tag, deleted_notification_ids.size());
579 579
580 std::vector<NotificationDatabaseData> updated_notifications; 580 std::vector<NotificationDatabaseData> updated_notifications;
581 ASSERT_EQ(NotificationDatabase::STATUS_OK, 581 ASSERT_EQ(NotificationDatabase::STATUS_OK,
582 database->ReadAllNotificationDataForOrigin(origin, 582 database->ReadAllNotificationDataForOrigin(origin,
583 &updated_notifications)); 583 &updated_notifications));
584 584
585 EXPECT_EQ(notifications_without_tag, updated_notifications.size()); 585 EXPECT_EQ(notifications_without_tag, updated_notifications.size());
586 586
587 size_t updated_notifications_with_tag = 0; 587 size_t updated_notifications_with_tag = 0;
588 size_t updated_notifications_without_tag = 0; 588 size_t updated_notifications_without_tag = 0;
589 589
590 for (const auto& database_data : updated_notifications) { 590 for (const auto& database_data : updated_notifications) {
591 if (database_data.notification_data.tag == tag) 591 if (database_data.notification_data.tag == tag)
592 ++updated_notifications_with_tag; 592 ++updated_notifications_with_tag;
593 else 593 else
594 ++updated_notifications_without_tag; 594 ++updated_notifications_without_tag;
595 } 595 }
596 596
597 EXPECT_EQ(0u, updated_notifications_with_tag); 597 EXPECT_EQ(0u, updated_notifications_with_tag);
598 EXPECT_EQ(notifications_without_tag, updated_notifications_without_tag); 598 EXPECT_EQ(notifications_without_tag, updated_notifications_without_tag);
599 } 599 }
600 600
601 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginEmpty) { 601 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginEmpty) {
602 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 602 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
603 ASSERT_EQ(NotificationDatabase::STATUS_OK, 603 ASSERT_EQ(NotificationDatabase::STATUS_OK,
604 database->Open(true /* create_if_missing */)); 604 database->Open(true /* create_if_missing */));
605 605
606 GURL origin("https://example.com"); 606 GURL origin("https://example.com");
607 607
608 std::set<int64_t> deleted_notification_set; 608 std::set<std::string> deleted_notification_ids;
609 ASSERT_EQ(NotificationDatabase::STATUS_OK, 609 ASSERT_EQ(NotificationDatabase::STATUS_OK,
610 database->DeleteAllNotificationDataForOrigin( 610 database->DeleteAllNotificationDataForOrigin(
611 origin, "" /* tag */, &deleted_notification_set)); 611 origin, "" /* tag */, &deleted_notification_ids));
612 612
613 EXPECT_EQ(0u, deleted_notification_set.size()); 613 EXPECT_EQ(0u, deleted_notification_ids.size());
614 } 614 }
615 615
616 TEST_F(NotificationDatabaseTest, 616 TEST_F(NotificationDatabaseTest,
617 DeleteAllNotificationDataForServiceWorkerRegistration) { 617 DeleteAllNotificationDataForServiceWorkerRegistration) {
618 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 618 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
619 ASSERT_EQ(NotificationDatabase::STATUS_OK, 619 ASSERT_EQ(NotificationDatabase::STATUS_OK,
620 database->Open(true /* create_if_missing */)); 620 database->Open(true /* create_if_missing */));
621 621
622 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 622 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
623 623
624 GURL origin("https://example.com:443"); 624 GURL origin("https://example.com:443");
625 std::set<int64_t> deleted_notification_set; 625 std::set<std::string> deleted_notification_ids;
626 ASSERT_EQ(NotificationDatabase::STATUS_OK, 626 ASSERT_EQ(NotificationDatabase::STATUS_OK,
627 database->DeleteAllNotificationDataForServiceWorkerRegistration( 627 database->DeleteAllNotificationDataForServiceWorkerRegistration(
628 origin, kExampleServiceWorkerRegistrationId, 628 origin, kExampleServiceWorkerRegistrationId,
629 &deleted_notification_set)); 629 &deleted_notification_ids));
630 630
631 EXPECT_EQ(2u, deleted_notification_set.size()); 631 EXPECT_EQ(2u, deleted_notification_ids.size());
632 632
633 std::vector<NotificationDatabaseData> notifications; 633 std::vector<NotificationDatabaseData> notifications;
634 ASSERT_EQ(NotificationDatabase::STATUS_OK, 634 ASSERT_EQ(NotificationDatabase::STATUS_OK,
635 database->ReadAllNotificationDataForServiceWorkerRegistration( 635 database->ReadAllNotificationDataForServiceWorkerRegistration(
636 origin, kExampleServiceWorkerRegistrationId, &notifications)); 636 origin, kExampleServiceWorkerRegistrationId, &notifications));
637 637
638 EXPECT_EQ(0u, notifications.size()); 638 EXPECT_EQ(0u, notifications.size());
639 } 639 }
640 640
641 } // namespace content 641 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698