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

Unified Diff: content/browser/notifications/notification_database_unittest.cc

Issue 1019073002: The notification database should be able to read or delete multiple items. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/notifications/notification_database_unittest.cc
diff --git a/content/browser/notifications/notification_database_unittest.cc b/content/browser/notifications/notification_database_unittest.cc
index 2f6f207a91c45740aa6555ca70582e4b5c3d60b0..ef39d39caeaee4479777be42d2e53e55855b0194 100644
--- a/content/browser/notifications/notification_database_unittest.cc
+++ b/content/browser/notifications/notification_database_unittest.cc
@@ -29,6 +29,24 @@ class NotificationDatabaseTest : public ::testing::Test {
return new NotificationDatabase(path);
}
+ // Creates a new notification for |service_worker_registration_id| belonging
+ // to |origin| and writes it to the database. The written notification id
+ // will be stored in |notification_id|.
+ void CreateAndWriteNotification(NotificationDatabase* database,
+ const GURL& origin,
+ int64_t service_worker_registration_id,
+ int64_t* notification_id) {
+ NotificationDatabaseData database_data;
+ database_data.origin = origin;
+ database_data.service_worker_registration_id =
+ service_worker_registration_id;
+
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->WriteNotificationData(origin,
+ database_data,
+ notification_id));
+ }
+
// Returns if |database| has been opened.
bool IsDatabaseOpen(NotificationDatabase* database) {
return database->IsOpen();
@@ -130,21 +148,16 @@ TEST_F(NotificationDatabaseTest, NotificationIdIncrements) {
GURL origin("https://example.com");
- NotificationDatabaseData database_data;
int64_t notification_id = 0;
// Verify that getting two ids on the same database instance results in
// incrementing values. Notification ids will start at 1.
- ASSERT_EQ(NotificationDatabase::STATUS_OK,
- database->WriteNotificationData(origin,
- database_data,
- &notification_id));
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(), origin, 0 /* sw_registration_id */, &notification_id));
EXPECT_EQ(notification_id, 1);
- ASSERT_EQ(NotificationDatabase::STATUS_OK,
- database->WriteNotificationData(origin,
- database_data,
- &notification_id));
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(), origin, 0 /* sw_registration_id */, &notification_id));
EXPECT_EQ(notification_id, 2);
database.reset(CreateDatabaseOnFileSystem(database_dir.path()));
@@ -153,10 +166,8 @@ TEST_F(NotificationDatabaseTest, NotificationIdIncrements) {
// Verify that the next notification id was stored in the database, and
// continues where we expect it to be, even after closing and opening it.
- ASSERT_EQ(NotificationDatabase::STATUS_OK,
- database->WriteNotificationData(origin,
- database_data,
- &notification_id));
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(), origin, 0 /* sw_registration_id */, &notification_id));
EXPECT_EQ(notification_id, 3);
}
@@ -305,23 +316,19 @@ TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) {
ASSERT_EQ(NotificationDatabase::STATUS_OK,
database->Open(true /* create_if_missing */));
- NotificationDatabaseData database_data;
GURL origin("https://example.com");
+ int64_t notification_id = 0;
// Write ten notifications to the database, each with a unique title and
// notification id (it is the responsibility of the user to increment this).
for (int i = 1; i <= 10; ++i) {
- database_data.notification_id = i;
- database_data.notification_data.title = base::IntToString16(i);
-
- int64_t notification_id = 0;
- ASSERT_EQ(NotificationDatabase::STATUS_OK,
- database->WriteNotificationData(origin,
- database_data,
- &notification_id));
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(), origin, i /* sw_registration_id */, &notification_id));
EXPECT_EQ(notification_id, i);
}
+ NotificationDatabaseData database_data;
+
// Read the ten notifications from the database, and verify that the titles
// of each of them matches with how they were created.
for (int i = 1; i <= 10; ++i) {
@@ -330,7 +337,7 @@ TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) {
origin,
&database_data));
- EXPECT_EQ(base::IntToString16(i), database_data.notification_data.title);
+ EXPECT_EQ(i, database_data.service_worker_registration_id);
}
}
@@ -405,4 +412,147 @@ TEST_F(NotificationDatabaseTest, DeleteNotificationDataDifferentOrigin) {
&database_data));
}
+TEST_F(NotificationDatabaseTest, ReadAllServiceWorkerNotificationData) {
+ scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->Open(true /* create_if_missing */));
+
+ GURL origin("https://example.com");
+
+ int64_t service_worker_registration_id = 42;
+ int64_t notification_id = 0;
+
+ // Write ten notifications belonging to |origin| to the database.
+ for (int i = 0; i < 10; ++i) {
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(),
+ origin,
+ service_worker_registration_id,
+ &notification_id));
+ }
+
+ // Write one notification not belonging to |origin| to the database, but with
+ // the same Service Worker registration id.
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(),
+ GURL("https://foobar.com"),
+ service_worker_registration_id,
+ &notification_id));
+
+ // Write one notification belonging to |origin| to the database, but with a
+ // different Service Worker registration id.
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(),
+ origin,
+ service_worker_registration_id + 1,
+ &notification_id));
+
+ // Read all notifications from the database which belong to |origin| and
+ // |service_worker_registration_id|. There should be ten.
+ std::vector<NotificationDatabaseData> notifications;
+
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->ReadAllNotificationDataForServiceWorkerRegistration(
+ origin,
+ service_worker_registration_id,
+ &notifications));
+
+ EXPECT_EQ(10u, notifications.size());
+}
+
+TEST_F(NotificationDatabaseTest, ReadAllNotificationDataAfterDelete) {
+ scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->Open(true /* create_if_missing */));
+
+ GURL origin("https://example.com");
+
+ int64_t service_worker_registration_id = 42;
+ int64_t notification_id = 0;
+
+ // Write ten notifications belonging to |origin| to the database.
+ for (int i = 0; i < 10; ++i) {
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(),
+ origin,
+ service_worker_registration_id,
+ &notification_id));
+ }
+
+ // Remove the most recent notification from the database.
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->DeleteNotificationData(notification_id, origin));
+
+ // Make sure that only nine notifications remain.
+ std::vector<NotificationDatabaseData> notifications;
+
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->ReadAllNotificationDataForServiceWorkerRegistration(
+ origin,
+ service_worker_registration_id,
+ &notifications));
+
+ EXPECT_EQ(9u, notifications.size());
+}
+
+TEST_F(NotificationDatabaseTest, DeleteAllServiceWorkerNotificationData) {
+ scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory());
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->Open(true /* create_if_missing */));
+
+ GURL origin("https://example.com");
+
+ int64_t service_worker_registration_id = 42;
+ int64_t notification_id = 0;
+
+ // Write ten notifications belonging to |origin| to the database.
+ for (int i = 0; i < 10; ++i) {
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(),
+ origin,
+ service_worker_registration_id,
+ &notification_id));
+ }
+
+ // Write one notification to the database with a different Service Worker
+ // registration id, but still the same origin.
+ ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification(
+ database.get(),
+ origin,
+ service_worker_registration_id + 1,
+ &notification_id));
+
+ std::set<int64_t> deleted_notification_ids;
+
+ // Delete all the notifications from the database which used to belong to the
+ // origin and Service Worker registration id. Ten should be deleted.
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->DeleteAllNotificationDataForServiceWorkerRegistration(
+ origin,
+ service_worker_registration_id,
+ &deleted_notification_ids));
+
+ EXPECT_EQ(10u, deleted_notification_ids.size());
+
+ std::vector<NotificationDatabaseData> notifications;
+
+ // Make sure that all the notifications which are said to be deleted actually
+ // have been removed from the database.
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->ReadAllNotificationDataForServiceWorkerRegistration(
+ origin,
+ service_worker_registration_id,
+ &notifications));
+
+ EXPECT_EQ(0u, notifications.size());
+
+ NotificationDatabaseData database_data;
+
+ // Make sure that the non-associated notification still exists.
+ ASSERT_EQ(NotificationDatabase::STATUS_OK,
+ database->ReadNotificationData(notification_id,
+ origin,
+ &database_data));
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698