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

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

Issue 1874893002: Convert //content/browser from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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"
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 void WriteLevelDBKeyValuePair(NotificationDatabase* database, 90 void WriteLevelDBKeyValuePair(NotificationDatabase* database,
91 const std::string& key, 91 const std::string& key,
92 const std::string& value) { 92 const std::string& value) {
93 leveldb::Status status = 93 leveldb::Status status =
94 database->GetDBForTesting()->Put(leveldb::WriteOptions(), key, value); 94 database->GetDBForTesting()->Put(leveldb::WriteOptions(), key, value);
95 ASSERT_TRUE(status.ok()); 95 ASSERT_TRUE(status.ok());
96 } 96 }
97 }; 97 };
98 98
99 TEST_F(NotificationDatabaseTest, OpenCloseMemory) { 99 TEST_F(NotificationDatabaseTest, OpenCloseMemory) {
100 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 100 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
101 101
102 // Should return false because the database does not exist in memory. 102 // Should return false because the database does not exist in memory.
103 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, 103 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
104 database->Open(false /* create_if_missing */)); 104 database->Open(false /* create_if_missing */));
105 105
106 // Should return true, indicating that the database could be created. 106 // Should return true, indicating that the database could be created.
107 EXPECT_EQ(NotificationDatabase::STATUS_OK, 107 EXPECT_EQ(NotificationDatabase::STATUS_OK,
108 database->Open(true /* create_if_missing */)); 108 database->Open(true /* create_if_missing */));
109 109
110 EXPECT_TRUE(IsDatabaseOpen(database.get())); 110 EXPECT_TRUE(IsDatabaseOpen(database.get()));
111 EXPECT_TRUE(IsInMemoryDatabase(database.get())); 111 EXPECT_TRUE(IsInMemoryDatabase(database.get()));
112 112
113 // Verify that in-memory databases do not persist when being re-created. 113 // Verify that in-memory databases do not persist when being re-created.
114 database.reset(CreateDatabaseInMemory()); 114 database.reset(CreateDatabaseInMemory());
115 115
116 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, 116 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
117 database->Open(false /* create_if_missing */)); 117 database->Open(false /* create_if_missing */));
118 } 118 }
119 119
120 TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) { 120 TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) {
121 base::ScopedTempDir database_dir; 121 base::ScopedTempDir database_dir;
122 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); 122 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
123 123
124 scoped_ptr<NotificationDatabase> database( 124 std::unique_ptr<NotificationDatabase> database(
125 CreateDatabaseOnFileSystem(database_dir.path())); 125 CreateDatabaseOnFileSystem(database_dir.path()));
126 126
127 // Should return false because the database does not exist on the file system. 127 // Should return false because the database does not exist on the file system.
128 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, 128 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
129 database->Open(false /* create_if_missing */)); 129 database->Open(false /* create_if_missing */));
130 130
131 // Should return true, indicating that the database could be created. 131 // Should return true, indicating that the database could be created.
132 EXPECT_EQ(NotificationDatabase::STATUS_OK, 132 EXPECT_EQ(NotificationDatabase::STATUS_OK,
133 database->Open(true /* create_if_missing */)); 133 database->Open(true /* create_if_missing */));
134 134
135 EXPECT_TRUE(IsDatabaseOpen(database.get())); 135 EXPECT_TRUE(IsDatabaseOpen(database.get()));
136 EXPECT_FALSE(IsInMemoryDatabase(database.get())); 136 EXPECT_FALSE(IsInMemoryDatabase(database.get()));
137 137
138 // Close the database, and re-open it without attempting to create it because 138 // Close the database, and re-open it without attempting to create it because
139 // the files on the file system should still exist as expected. 139 // the files on the file system should still exist as expected.
140 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); 140 database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
141 EXPECT_EQ(NotificationDatabase::STATUS_OK, 141 EXPECT_EQ(NotificationDatabase::STATUS_OK,
142 database->Open(false /* create_if_missing */)); 142 database->Open(false /* create_if_missing */));
143 } 143 }
144 144
145 TEST_F(NotificationDatabaseTest, DestroyDatabase) { 145 TEST_F(NotificationDatabaseTest, DestroyDatabase) {
146 base::ScopedTempDir database_dir; 146 base::ScopedTempDir database_dir;
147 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); 147 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
148 148
149 scoped_ptr<NotificationDatabase> database( 149 std::unique_ptr<NotificationDatabase> database(
150 CreateDatabaseOnFileSystem(database_dir.path())); 150 CreateDatabaseOnFileSystem(database_dir.path()));
151 151
152 EXPECT_EQ(NotificationDatabase::STATUS_OK, 152 EXPECT_EQ(NotificationDatabase::STATUS_OK,
153 database->Open(true /* create_if_missing */)); 153 database->Open(true /* create_if_missing */));
154 EXPECT_TRUE(IsDatabaseOpen(database.get())); 154 EXPECT_TRUE(IsDatabaseOpen(database.get()));
155 155
156 // Destroy the database. This will immediately close it as well. 156 // Destroy the database. This will immediately close it as well.
157 ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy()); 157 ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy());
158 EXPECT_FALSE(IsDatabaseOpen(database.get())); 158 EXPECT_FALSE(IsDatabaseOpen(database.get()));
159 159
160 // Try to re-open the database (but not re-create it). This should fail as 160 // Try to re-open the database (but not re-create it). This should fail as
161 // the files associated with the database should have been blown away. 161 // the files associated with the database should have been blown away.
162 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); 162 database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
163 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, 163 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
164 database->Open(false /* create_if_missing */)); 164 database->Open(false /* create_if_missing */));
165 } 165 }
166 166
167 TEST_F(NotificationDatabaseTest, NotificationIdIncrements) { 167 TEST_F(NotificationDatabaseTest, NotificationIdIncrements) {
168 base::ScopedTempDir database_dir; 168 base::ScopedTempDir database_dir;
169 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); 169 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
170 170
171 scoped_ptr<NotificationDatabase> database( 171 std::unique_ptr<NotificationDatabase> database(
172 CreateDatabaseOnFileSystem(database_dir.path())); 172 CreateDatabaseOnFileSystem(database_dir.path()));
173 173
174 ASSERT_EQ(NotificationDatabase::STATUS_OK, 174 ASSERT_EQ(NotificationDatabase::STATUS_OK,
175 database->Open(true /* create_if_missing */)); 175 database->Open(true /* create_if_missing */));
176 176
177 GURL origin("https://example.com"); 177 GURL origin("https://example.com");
178 178
179 int64_t notification_id = 0; 179 int64_t notification_id = 0;
180 180
181 // Verify that getting two ids on the same database instance results in 181 // Verify that getting two ids on the same database instance results in
(...skipping 11 matching lines...) Expand all
193 database->Open(false /* create_if_missing */)); 193 database->Open(false /* create_if_missing */));
194 194
195 // Verify that the next notification id was stored in the database, and 195 // Verify that the next notification id was stored in the database, and
196 // continues where we expect it to be, even after closing and opening it. 196 // continues where we expect it to be, even after closing and opening it.
197 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( 197 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
198 database.get(), origin, 0 /* sw_registration_id */, &notification_id)); 198 database.get(), origin, 0 /* sw_registration_id */, &notification_id));
199 EXPECT_EQ(notification_id, 3); 199 EXPECT_EQ(notification_id, 3);
200 } 200 }
201 201
202 TEST_F(NotificationDatabaseTest, NotificationIdIncrementsStorage) { 202 TEST_F(NotificationDatabaseTest, NotificationIdIncrementsStorage) {
203 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 203 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
204 ASSERT_EQ(NotificationDatabase::STATUS_OK, 204 ASSERT_EQ(NotificationDatabase::STATUS_OK,
205 database->Open(true /* create_if_missing */)); 205 database->Open(true /* create_if_missing */));
206 206
207 GURL origin("https://example.com"); 207 GURL origin("https://example.com");
208 208
209 NotificationDatabaseData database_data; 209 NotificationDatabaseData database_data;
210 database_data.notification_id = -1; 210 database_data.notification_id = -1;
211 211
212 int64_t notification_id = 0; 212 int64_t notification_id = 0;
213 ASSERT_EQ( 213 ASSERT_EQ(
214 NotificationDatabase::STATUS_OK, 214 NotificationDatabase::STATUS_OK,
215 database->WriteNotificationData(origin, database_data, &notification_id)); 215 database->WriteNotificationData(origin, database_data, &notification_id));
216 216
217 ASSERT_EQ( 217 ASSERT_EQ(
218 NotificationDatabase::STATUS_OK, 218 NotificationDatabase::STATUS_OK,
219 database->ReadNotificationData(notification_id, origin, &database_data)); 219 database->ReadNotificationData(notification_id, origin, &database_data));
220 220
221 EXPECT_EQ(notification_id, database_data.notification_id); 221 EXPECT_EQ(notification_id, database_data.notification_id);
222 } 222 }
223 223
224 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) { 224 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) {
225 base::ScopedTempDir database_dir; 225 base::ScopedTempDir database_dir;
226 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); 226 ASSERT_TRUE(database_dir.CreateUniqueTempDir());
227 227
228 scoped_ptr<NotificationDatabase> database( 228 std::unique_ptr<NotificationDatabase> database(
229 CreateDatabaseOnFileSystem(database_dir.path())); 229 CreateDatabaseOnFileSystem(database_dir.path()));
230 230
231 ASSERT_EQ(NotificationDatabase::STATUS_OK, 231 ASSERT_EQ(NotificationDatabase::STATUS_OK,
232 database->Open(true /* create_if_missing */)); 232 database->Open(true /* create_if_missing */));
233 233
234 GURL origin("https://example.com"); 234 GURL origin("https://example.com");
235 235
236 NotificationDatabaseData database_data; 236 NotificationDatabaseData database_data;
237 int64_t notification_id = 0; 237 int64_t notification_id = 0;
238 238
239 ASSERT_EQ( 239 ASSERT_EQ(
240 NotificationDatabase::STATUS_OK, 240 NotificationDatabase::STATUS_OK,
241 database->WriteNotificationData(origin, database_data, &notification_id)); 241 database->WriteNotificationData(origin, database_data, &notification_id));
242 EXPECT_EQ(notification_id, 1); 242 EXPECT_EQ(notification_id, 1);
243 243
244 // Deliberately write an invalid value as the next notification id. When 244 // Deliberately write an invalid value as the next notification id. When
245 // re-opening the database, the Open() method should realize that an invalid 245 // re-opening the database, the Open() method should realize that an invalid
246 // value is being read, and mark the database as corrupted. 246 // value is being read, and mark the database as corrupted.
247 ASSERT_NO_FATAL_FAILURE( 247 ASSERT_NO_FATAL_FAILURE(
248 WriteLevelDBKeyValuePair(database.get(), "NEXT_NOTIFICATION_ID", "-42")); 248 WriteLevelDBKeyValuePair(database.get(), "NEXT_NOTIFICATION_ID", "-42"));
249 249
250 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); 250 database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
251 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED, 251 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_CORRUPTED,
252 database->Open(false /* create_if_missing */)); 252 database->Open(false /* create_if_missing */));
253 } 253 }
254 254
255 TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) { 255 TEST_F(NotificationDatabaseTest, ReadInvalidNotificationData) {
256 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 256 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
257 ASSERT_EQ(NotificationDatabase::STATUS_OK, 257 ASSERT_EQ(NotificationDatabase::STATUS_OK,
258 database->Open(true /* create_if_missing */)); 258 database->Open(true /* create_if_missing */));
259 259
260 NotificationDatabaseData database_data; 260 NotificationDatabaseData database_data;
261 261
262 // Reading the notification data for a notification that does not exist should 262 // Reading the notification data for a notification that does not exist should
263 // return the ERROR_NOT_FOUND status code. 263 // return the ERROR_NOT_FOUND status code.
264 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, 264 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND,
265 database->ReadNotificationData(9001, GURL("https://chrome.com"), 265 database->ReadNotificationData(9001, GURL("https://chrome.com"),
266 &database_data)); 266 &database_data));
267 } 267 }
268 268
269 TEST_F(NotificationDatabaseTest, ReadNotificationDataDifferentOrigin) { 269 TEST_F(NotificationDatabaseTest, ReadNotificationDataDifferentOrigin) {
270 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 270 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
271 ASSERT_EQ(NotificationDatabase::STATUS_OK, 271 ASSERT_EQ(NotificationDatabase::STATUS_OK,
272 database->Open(true /* create_if_missing */)); 272 database->Open(true /* create_if_missing */));
273 273
274 int64_t notification_id = 0; 274 int64_t notification_id = 0;
275 GURL origin("https://example.com"); 275 GURL origin("https://example.com");
276 276
277 NotificationDatabaseData database_data, read_database_data; 277 NotificationDatabaseData database_data, read_database_data;
278 database_data.notification_data.title = base::UTF8ToUTF16("My Notification"); 278 database_data.notification_data.title = base::UTF8ToUTF16("My Notification");
279 279
280 ASSERT_EQ( 280 ASSERT_EQ(
(...skipping 11 matching lines...) Expand all
292 // should return STATUS_OK and the associated notification data. 292 // should return STATUS_OK and the associated notification data.
293 ASSERT_EQ(NotificationDatabase::STATUS_OK, 293 ASSERT_EQ(NotificationDatabase::STATUS_OK,
294 database->ReadNotificationData(notification_id, origin, 294 database->ReadNotificationData(notification_id, origin,
295 &read_database_data)); 295 &read_database_data));
296 296
297 EXPECT_EQ(database_data.notification_data.title, 297 EXPECT_EQ(database_data.notification_data.title,
298 read_database_data.notification_data.title); 298 read_database_data.notification_data.title);
299 } 299 }
300 300
301 TEST_F(NotificationDatabaseTest, ReadNotificationDataReflection) { 301 TEST_F(NotificationDatabaseTest, ReadNotificationDataReflection) {
302 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 302 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
303 ASSERT_EQ(NotificationDatabase::STATUS_OK, 303 ASSERT_EQ(NotificationDatabase::STATUS_OK,
304 database->Open(true /* create_if_missing */)); 304 database->Open(true /* create_if_missing */));
305 305
306 int64_t notification_id = 0; 306 int64_t notification_id = 0;
307 307
308 GURL origin("https://example.com"); 308 GURL origin("https://example.com");
309 309
310 PlatformNotificationData notification_data; 310 PlatformNotificationData notification_data;
311 notification_data.title = base::UTF8ToUTF16("My Notification"); 311 notification_data.title = base::UTF8ToUTF16("My Notification");
312 notification_data.direction = 312 notification_data.direction =
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 EXPECT_EQ(notification_data.title, read_notification_data.title); 349 EXPECT_EQ(notification_data.title, read_notification_data.title);
350 EXPECT_EQ(notification_data.direction, read_notification_data.direction); 350 EXPECT_EQ(notification_data.direction, read_notification_data.direction);
351 EXPECT_EQ(notification_data.lang, read_notification_data.lang); 351 EXPECT_EQ(notification_data.lang, read_notification_data.lang);
352 EXPECT_EQ(notification_data.body, read_notification_data.body); 352 EXPECT_EQ(notification_data.body, read_notification_data.body);
353 EXPECT_EQ(notification_data.tag, read_notification_data.tag); 353 EXPECT_EQ(notification_data.tag, read_notification_data.tag);
354 EXPECT_EQ(notification_data.icon, read_notification_data.icon); 354 EXPECT_EQ(notification_data.icon, read_notification_data.icon);
355 EXPECT_EQ(notification_data.silent, read_notification_data.silent); 355 EXPECT_EQ(notification_data.silent, read_notification_data.silent);
356 } 356 }
357 357
358 TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) { 358 TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) {
359 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 359 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
360 ASSERT_EQ(NotificationDatabase::STATUS_OK, 360 ASSERT_EQ(NotificationDatabase::STATUS_OK,
361 database->Open(true /* create_if_missing */)); 361 database->Open(true /* create_if_missing */));
362 362
363 GURL origin("https://example.com"); 363 GURL origin("https://example.com");
364 int64_t notification_id = 0; 364 int64_t notification_id = 0;
365 365
366 // 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
367 // 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).
368 for (int i = 1; i <= 10; ++i) { 368 for (int i = 1; i <= 10; ++i) {
369 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( 369 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
370 database.get(), origin, i /* sw_registration_id */, &notification_id)); 370 database.get(), origin, i /* sw_registration_id */, &notification_id));
371 EXPECT_EQ(notification_id, i); 371 EXPECT_EQ(notification_id, i);
372 } 372 }
373 373
374 NotificationDatabaseData database_data; 374 NotificationDatabaseData database_data;
375 375
376 // Read the ten notifications from the database, and verify that the titles 376 // Read the ten notifications from the database, and verify that the titles
377 // of each of them matches with how they were created. 377 // of each of them matches with how they were created.
378 for (int i = 1; i <= 10; ++i) { 378 for (int i = 1; i <= 10; ++i) {
379 ASSERT_EQ(NotificationDatabase::STATUS_OK, 379 ASSERT_EQ(NotificationDatabase::STATUS_OK,
380 database->ReadNotificationData(i /* notification_id */, origin, 380 database->ReadNotificationData(i /* notification_id */, origin,
381 &database_data)); 381 &database_data));
382 382
383 EXPECT_EQ(i, database_data.service_worker_registration_id); 383 EXPECT_EQ(i, database_data.service_worker_registration_id);
384 } 384 }
385 } 385 }
386 386
387 TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) { 387 TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) {
388 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 388 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
389 ASSERT_EQ(NotificationDatabase::STATUS_OK, 389 ASSERT_EQ(NotificationDatabase::STATUS_OK,
390 database->Open(true /* create_if_missing */)); 390 database->Open(true /* create_if_missing */));
391 391
392 // Deleting non-existing notifications is not considered to be a failure. 392 // Deleting non-existing notifications is not considered to be a failure.
393 ASSERT_EQ(NotificationDatabase::STATUS_OK, 393 ASSERT_EQ(NotificationDatabase::STATUS_OK,
394 database->DeleteNotificationData(9001, GURL("https://chrome.com"))); 394 database->DeleteNotificationData(9001, GURL("https://chrome.com")));
395 } 395 }
396 396
397 TEST_F(NotificationDatabaseTest, DeleteNotificationDataSameOrigin) { 397 TEST_F(NotificationDatabaseTest, DeleteNotificationDataSameOrigin) {
398 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 398 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
399 ASSERT_EQ(NotificationDatabase::STATUS_OK, 399 ASSERT_EQ(NotificationDatabase::STATUS_OK,
400 database->Open(true /* create_if_missing */)); 400 database->Open(true /* create_if_missing */));
401 401
402 int64_t notification_id = 0; 402 int64_t notification_id = 0;
403 403
404 NotificationDatabaseData database_data; 404 NotificationDatabaseData database_data;
405 GURL origin("https://example.com"); 405 GURL origin("https://example.com");
406 406
407 ASSERT_EQ( 407 ASSERT_EQ(
408 NotificationDatabase::STATUS_OK, 408 NotificationDatabase::STATUS_OK,
409 database->WriteNotificationData(origin, database_data, &notification_id)); 409 database->WriteNotificationData(origin, database_data, &notification_id));
410 410
411 // Reading a notification after writing one should succeed. 411 // Reading a notification after writing one should succeed.
412 EXPECT_EQ( 412 EXPECT_EQ(
413 NotificationDatabase::STATUS_OK, 413 NotificationDatabase::STATUS_OK,
414 database->ReadNotificationData(notification_id, origin, &database_data)); 414 database->ReadNotificationData(notification_id, origin, &database_data));
415 415
416 // Delete the notification which was just written to the database, and verify 416 // Delete the notification which was just written to the database, and verify
417 // that reading it again will fail. 417 // that reading it again will fail.
418 EXPECT_EQ(NotificationDatabase::STATUS_OK, 418 EXPECT_EQ(NotificationDatabase::STATUS_OK,
419 database->DeleteNotificationData(notification_id, origin)); 419 database->DeleteNotificationData(notification_id, origin));
420 EXPECT_EQ( 420 EXPECT_EQ(
421 NotificationDatabase::STATUS_ERROR_NOT_FOUND, 421 NotificationDatabase::STATUS_ERROR_NOT_FOUND,
422 database->ReadNotificationData(notification_id, origin, &database_data)); 422 database->ReadNotificationData(notification_id, origin, &database_data));
423 } 423 }
424 424
425 TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) { 425 TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) {
426 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 426 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
427 ASSERT_EQ(NotificationDatabase::STATUS_OK, 427 ASSERT_EQ(NotificationDatabase::STATUS_OK,
428 database->Open(true /* create_if_missing */)); 428 database->Open(true /* create_if_missing */));
429 429
430 int64_t notification_id = 0; 430 int64_t notification_id = 0;
431 431
432 NotificationDatabaseData database_data; 432 NotificationDatabaseData database_data;
433 GURL origin("https://example.com"); 433 GURL origin("https://example.com");
434 434
435 ASSERT_EQ( 435 ASSERT_EQ(
436 NotificationDatabase::STATUS_OK, 436 NotificationDatabase::STATUS_OK,
437 database->WriteNotificationData(origin, database_data, &notification_id)); 437 database->WriteNotificationData(origin, database_data, &notification_id));
438 438
439 // Attempting to delete the notification with a different origin, but with the 439 // Attempting to delete the notification with a different origin, but with the
440 // same |notification_id|, should not return an error (the notification could 440 // same |notification_id|, should not return an error (the notification could
441 // not be found, but that's not considered a failure). However, it should not 441 // not be found, but that's not considered a failure). However, it should not
442 // remove the notification either. 442 // remove the notification either.
443 EXPECT_EQ(NotificationDatabase::STATUS_OK, 443 EXPECT_EQ(NotificationDatabase::STATUS_OK,
444 database->DeleteNotificationData(notification_id, 444 database->DeleteNotificationData(notification_id,
445 GURL("https://chrome.com"))); 445 GURL("https://chrome.com")));
446 446
447 EXPECT_EQ( 447 EXPECT_EQ(
448 NotificationDatabase::STATUS_OK, 448 NotificationDatabase::STATUS_OK,
449 database->ReadNotificationData(notification_id, origin, &database_data)); 449 database->ReadNotificationData(notification_id, origin, &database_data));
450 } 450 }
451 451
452 TEST_F(NotificationDatabaseTest, ReadAllNotificationData) { 452 TEST_F(NotificationDatabaseTest, ReadAllNotificationData) {
453 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 453 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
454 ASSERT_EQ(NotificationDatabase::STATUS_OK, 454 ASSERT_EQ(NotificationDatabase::STATUS_OK,
455 database->Open(true /* create_if_missing */)); 455 database->Open(true /* create_if_missing */));
456 456
457 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 457 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
458 458
459 std::vector<NotificationDatabaseData> notifications; 459 std::vector<NotificationDatabaseData> notifications;
460 ASSERT_EQ(NotificationDatabase::STATUS_OK, 460 ASSERT_EQ(NotificationDatabase::STATUS_OK,
461 database->ReadAllNotificationData(&notifications)); 461 database->ReadAllNotificationData(&notifications));
462 462
463 EXPECT_EQ(arraysize(kExampleNotificationData), notifications.size()); 463 EXPECT_EQ(arraysize(kExampleNotificationData), notifications.size());
464 } 464 }
465 465
466 TEST_F(NotificationDatabaseTest, ReadAllNotificationDataEmpty) { 466 TEST_F(NotificationDatabaseTest, ReadAllNotificationDataEmpty) {
467 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 467 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
468 ASSERT_EQ(NotificationDatabase::STATUS_OK, 468 ASSERT_EQ(NotificationDatabase::STATUS_OK,
469 database->Open(true /* create_if_missing */)); 469 database->Open(true /* create_if_missing */));
470 470
471 std::vector<NotificationDatabaseData> notifications; 471 std::vector<NotificationDatabaseData> notifications;
472 ASSERT_EQ(NotificationDatabase::STATUS_OK, 472 ASSERT_EQ(NotificationDatabase::STATUS_OK,
473 database->ReadAllNotificationData(&notifications)); 473 database->ReadAllNotificationData(&notifications));
474 474
475 EXPECT_EQ(0u, notifications.size()); 475 EXPECT_EQ(0u, notifications.size());
476 } 476 }
477 477
478 TEST_F(NotificationDatabaseTest, ReadAllNotificationDataForOrigin) { 478 TEST_F(NotificationDatabaseTest, ReadAllNotificationDataForOrigin) {
479 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 479 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
480 ASSERT_EQ(NotificationDatabase::STATUS_OK, 480 ASSERT_EQ(NotificationDatabase::STATUS_OK,
481 database->Open(true /* create_if_missing */)); 481 database->Open(true /* create_if_missing */));
482 482
483 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 483 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
484 484
485 GURL origin("https://example.com"); 485 GURL origin("https://example.com");
486 486
487 std::vector<NotificationDatabaseData> notifications; 487 std::vector<NotificationDatabaseData> notifications;
488 ASSERT_EQ(NotificationDatabase::STATUS_OK, 488 ASSERT_EQ(NotificationDatabase::STATUS_OK,
489 database->ReadAllNotificationDataForOrigin(origin, &notifications)); 489 database->ReadAllNotificationDataForOrigin(origin, &notifications));
490 490
491 EXPECT_EQ(4u, notifications.size()); 491 EXPECT_EQ(4u, notifications.size());
492 } 492 }
493 493
494 TEST_F(NotificationDatabaseTest, 494 TEST_F(NotificationDatabaseTest,
495 ReadAllNotificationDataForServiceWorkerRegistration) { 495 ReadAllNotificationDataForServiceWorkerRegistration) {
496 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 496 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
497 ASSERT_EQ(NotificationDatabase::STATUS_OK, 497 ASSERT_EQ(NotificationDatabase::STATUS_OK,
498 database->Open(true /* create_if_missing */)); 498 database->Open(true /* create_if_missing */));
499 499
500 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 500 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
501 501
502 GURL origin("https://example.com:443"); 502 GURL origin("https://example.com:443");
503 503
504 std::vector<NotificationDatabaseData> notifications; 504 std::vector<NotificationDatabaseData> notifications;
505 ASSERT_EQ(NotificationDatabase::STATUS_OK, 505 ASSERT_EQ(NotificationDatabase::STATUS_OK,
506 database->ReadAllNotificationDataForServiceWorkerRegistration( 506 database->ReadAllNotificationDataForServiceWorkerRegistration(
507 origin, kExampleServiceWorkerRegistrationId, &notifications)); 507 origin, kExampleServiceWorkerRegistrationId, &notifications));
508 508
509 EXPECT_EQ(2u, notifications.size()); 509 EXPECT_EQ(2u, notifications.size());
510 } 510 }
511 511
512 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOrigin) { 512 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOrigin) {
513 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 513 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
514 ASSERT_EQ(NotificationDatabase::STATUS_OK, 514 ASSERT_EQ(NotificationDatabase::STATUS_OK,
515 database->Open(true /* create_if_missing */)); 515 database->Open(true /* create_if_missing */));
516 516
517 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 517 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
518 518
519 GURL origin("https://example.com:443"); 519 GURL origin("https://example.com:443");
520 520
521 std::set<int64_t> deleted_notification_set; 521 std::set<int64_t> deleted_notification_set;
522 ASSERT_EQ(NotificationDatabase::STATUS_OK, 522 ASSERT_EQ(NotificationDatabase::STATUS_OK,
523 database->DeleteAllNotificationDataForOrigin( 523 database->DeleteAllNotificationDataForOrigin(
524 origin, &deleted_notification_set)); 524 origin, &deleted_notification_set));
525 525
526 EXPECT_EQ(4u, deleted_notification_set.size()); 526 EXPECT_EQ(4u, deleted_notification_set.size());
527 527
528 std::vector<NotificationDatabaseData> notifications; 528 std::vector<NotificationDatabaseData> notifications;
529 ASSERT_EQ(NotificationDatabase::STATUS_OK, 529 ASSERT_EQ(NotificationDatabase::STATUS_OK,
530 database->ReadAllNotificationDataForOrigin(origin, &notifications)); 530 database->ReadAllNotificationDataForOrigin(origin, &notifications));
531 531
532 EXPECT_EQ(0u, notifications.size()); 532 EXPECT_EQ(0u, notifications.size());
533 } 533 }
534 534
535 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginEmpty) { 535 TEST_F(NotificationDatabaseTest, DeleteAllNotificationDataForOriginEmpty) {
536 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 536 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
537 ASSERT_EQ(NotificationDatabase::STATUS_OK, 537 ASSERT_EQ(NotificationDatabase::STATUS_OK,
538 database->Open(true /* create_if_missing */)); 538 database->Open(true /* create_if_missing */));
539 539
540 GURL origin("https://example.com"); 540 GURL origin("https://example.com");
541 541
542 std::set<int64_t> deleted_notification_set; 542 std::set<int64_t> deleted_notification_set;
543 ASSERT_EQ(NotificationDatabase::STATUS_OK, 543 ASSERT_EQ(NotificationDatabase::STATUS_OK,
544 database->DeleteAllNotificationDataForOrigin( 544 database->DeleteAllNotificationDataForOrigin(
545 origin, &deleted_notification_set)); 545 origin, &deleted_notification_set));
546 546
547 EXPECT_EQ(0u, deleted_notification_set.size()); 547 EXPECT_EQ(0u, deleted_notification_set.size());
548 } 548 }
549 549
550 TEST_F(NotificationDatabaseTest, 550 TEST_F(NotificationDatabaseTest,
551 DeleteAllNotificationDataForServiceWorkerRegistration) { 551 DeleteAllNotificationDataForServiceWorkerRegistration) {
552 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); 552 std::unique_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
553 ASSERT_EQ(NotificationDatabase::STATUS_OK, 553 ASSERT_EQ(NotificationDatabase::STATUS_OK,
554 database->Open(true /* create_if_missing */)); 554 database->Open(true /* create_if_missing */));
555 555
556 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get())); 556 ASSERT_NO_FATAL_FAILURE(PopulateDatabaseWithExampleData(database.get()));
557 557
558 GURL origin("https://example.com:443"); 558 GURL origin("https://example.com:443");
559 std::set<int64_t> deleted_notification_set; 559 std::set<int64_t> deleted_notification_set;
560 ASSERT_EQ(NotificationDatabase::STATUS_OK, 560 ASSERT_EQ(NotificationDatabase::STATUS_OK,
561 database->DeleteAllNotificationDataForServiceWorkerRegistration( 561 database->DeleteAllNotificationDataForServiceWorkerRegistration(
562 origin, kExampleServiceWorkerRegistrationId, 562 origin, kExampleServiceWorkerRegistrationId,
563 &deleted_notification_set)); 563 &deleted_notification_set));
564 564
565 EXPECT_EQ(2u, deleted_notification_set.size()); 565 EXPECT_EQ(2u, deleted_notification_set.size());
566 566
567 std::vector<NotificationDatabaseData> notifications; 567 std::vector<NotificationDatabaseData> notifications;
568 ASSERT_EQ(NotificationDatabase::STATUS_OK, 568 ASSERT_EQ(NotificationDatabase::STATUS_OK,
569 database->ReadAllNotificationDataForServiceWorkerRegistration( 569 database->ReadAllNotificationDataForServiceWorkerRegistration(
570 origin, kExampleServiceWorkerRegistrationId, &notifications)); 570 origin, kExampleServiceWorkerRegistrationId, &notifications));
571 571
572 EXPECT_EQ(0u, notifications.size()); 572 EXPECT_EQ(0u, notifications.size());
573 } 573 }
574 574
575 } // namespace content 575 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/notifications/notification_database.cc ('k') | content/browser/notifications/notification_message_filter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698