OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/notifications/notification_database.h" |
| 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 class NotificationDatabaseTest : public ::testing::Test { |
| 13 protected: |
| 14 // Creates a new NotificationDatabase instance in memory. |
| 15 NotificationDatabase* CreateDatabaseInMemory() { |
| 16 return new NotificationDatabase(base::FilePath()); |
| 17 } |
| 18 |
| 19 // Creates a new NotificationDatabase instance in |path|. |
| 20 NotificationDatabase* CreateDatabaseOnFileSystem( |
| 21 const base::FilePath& path) { |
| 22 return new NotificationDatabase(path); |
| 23 } |
| 24 |
| 25 // Returns if |database| has been opened. |
| 26 bool IsDatabaseOpen(NotificationDatabase* database) { |
| 27 return database->IsOpen(); |
| 28 } |
| 29 |
| 30 // Returns if |database| is an in-memory only database. |
| 31 bool IsInMemoryDatabase(NotificationDatabase* database) { |
| 32 return database->IsInMemoryDatabase(); |
| 33 } |
| 34 }; |
| 35 |
| 36 TEST_F(NotificationDatabaseTest, OpenCloseMemory) { |
| 37 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 38 |
| 39 // Should return false because the database does not exist in memory. |
| 40 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 41 database->Open(false /* create_if_missing */)); |
| 42 |
| 43 // Should return true, indicating that the database could be created. |
| 44 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 45 database->Open(true /* create_if_missing */)); |
| 46 |
| 47 EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 48 EXPECT_TRUE(IsInMemoryDatabase(database.get())); |
| 49 |
| 50 // Verify that in-memory databases do not persist when being re-created. |
| 51 database.reset(CreateDatabaseInMemory()); |
| 52 |
| 53 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 54 database->Open(false /* create_if_missing */)); |
| 55 } |
| 56 |
| 57 TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) { |
| 58 base::ScopedTempDir database_dir; |
| 59 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 60 |
| 61 scoped_ptr<NotificationDatabase> database( |
| 62 CreateDatabaseOnFileSystem(database_dir.path())); |
| 63 |
| 64 // Should return false because the database does not exist on the file system. |
| 65 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 66 database->Open(false /* create_if_missing */)); |
| 67 |
| 68 // Should return true, indicating that the database could be created. |
| 69 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 70 database->Open(true /* create_if_missing */)); |
| 71 |
| 72 EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 73 EXPECT_FALSE(IsInMemoryDatabase(database.get())); |
| 74 |
| 75 // Close the database, and re-open it without attempting to create it because |
| 76 // the files on the file system should still exist as expected. |
| 77 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); |
| 78 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 79 database->Open(false /* create_if_missing */)); |
| 80 } |
| 81 |
| 82 TEST_F(NotificationDatabaseTest, DestroyDatabase) { |
| 83 base::ScopedTempDir database_dir; |
| 84 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 85 |
| 86 scoped_ptr<NotificationDatabase> database( |
| 87 CreateDatabaseOnFileSystem(database_dir.path())); |
| 88 |
| 89 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 90 database->Open(true /* create_if_missing */)); |
| 91 EXPECT_TRUE(IsDatabaseOpen(database.get())); |
| 92 |
| 93 // Destroy the database. This will immediately close it as well. |
| 94 ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy()); |
| 95 EXPECT_FALSE(IsDatabaseOpen(database.get())); |
| 96 |
| 97 // Try to re-open the database (but not re-create it). This should fail as |
| 98 // the files associated with the database should have been blown away. |
| 99 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); |
| 100 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, |
| 101 database->Open(false /* create_if_missing */)); |
| 102 } |
| 103 |
| 104 } // namespace content |
OLD | NEW |