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 namespace { | |
12 | |
13 // Creates a new NotificationDatabase instance in memory. | |
Bernhard Bauer
2015/03/11 17:15:23
Move this into the test fixture, or move the metho
Peter Beverloo
2015/03/11 18:01:26
Moved them to the fixture. I'm having the utility
| |
14 NotificationDatabase* CreateDatabaseInMemory() { | |
15 return new NotificationDatabase(base::FilePath()); | |
16 } | |
17 | |
18 // Creates a new NotificationDatabase instance in |path|. | |
19 NotificationDatabase* CreateDatabaseOnFileSystem( | |
20 const base::FilePath& path) { | |
21 return new NotificationDatabase(path); | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 class NotificationDatabaseTest : public ::testing::Test { | |
27 protected: | |
28 // Returns if |database| exists and has been opened. | |
29 bool IsDatabaseOpen(NotificationDatabase* database) { | |
30 return database && database->IsOpen(); | |
Bernhard Bauer
2015/03/11 17:15:22
What is the null check for here? It looks like in
Peter Beverloo
2015/03/11 18:01:26
Removed it.
| |
31 } | |
32 | |
33 // Returns if |database| exists and is an in-memory only database. | |
34 bool IsInMemoryDatabase(NotificationDatabase* database) { | |
35 return database && database->IsInMemoryDatabase(); | |
36 } | |
37 }; | |
38 | |
39 TEST_F(NotificationDatabaseTest, OpenCloseMemory) { | |
40 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); | |
41 | |
42 // Should return false because the database does not exist in memory. | |
43 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | |
44 database->Open(false /* create_if_missing */)); | |
45 | |
46 // Should return true, indicating that the database could be created. | |
47 EXPECT_EQ(NotificationDatabase::STATUS_OK, | |
48 database->Open(true /* create_if_missing */)); | |
49 | |
50 EXPECT_TRUE(IsDatabaseOpen(database.get())); | |
51 EXPECT_TRUE(IsInMemoryDatabase(database.get())); | |
52 | |
53 // Verify that in-memory databases do not persist when being re-created. | |
54 database.reset(CreateDatabaseInMemory()); | |
55 | |
56 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | |
57 database->Open(false /* create_if_missing */)); | |
58 } | |
59 | |
60 TEST_F(NotificationDatabaseTest, OpenCloseFileSystem) { | |
61 base::ScopedTempDir database_dir; | |
62 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | |
63 | |
64 scoped_ptr<NotificationDatabase> database( | |
65 CreateDatabaseOnFileSystem(database_dir.path())); | |
66 | |
67 // Should return false because the database does not exist on the file system. | |
68 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | |
69 database->Open(false /* create_if_missing */)); | |
70 | |
71 // Should return true, indicating that the database could be created. | |
72 EXPECT_EQ(NotificationDatabase::STATUS_OK, | |
73 database->Open(true /* create_if_missing */)); | |
74 | |
75 EXPECT_TRUE(IsDatabaseOpen(database.get())); | |
76 EXPECT_FALSE(IsInMemoryDatabase(database.get())); | |
77 | |
78 // Close the database, and re-open it without attempting to create it because | |
79 // the files on the file system should still exist as expected. | |
80 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | |
81 EXPECT_EQ(NotificationDatabase::STATUS_OK, | |
82 database->Open(false /* create_if_missing */)); | |
83 } | |
84 | |
85 TEST_F(NotificationDatabaseTest, DestroyDatabase) { | |
86 base::ScopedTempDir database_dir; | |
87 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | |
88 | |
89 scoped_ptr<NotificationDatabase> database( | |
90 CreateDatabaseOnFileSystem(database_dir.path())); | |
91 | |
92 EXPECT_EQ(NotificationDatabase::STATUS_OK, | |
93 database->Open(true /* create_if_missing */)); | |
94 EXPECT_TRUE(IsDatabaseOpen(database.get())); | |
95 | |
96 // Destroy the database. This will immediately close it as well. | |
97 ASSERT_EQ(NotificationDatabase::STATUS_OK, database->Destroy()); | |
98 EXPECT_FALSE(IsDatabaseOpen(database.get())); | |
99 | |
100 // Try to re-open the database (but not re-create it). This should fail as | |
101 // the files associated with the database should have been blown away. | |
102 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | |
103 EXPECT_EQ(NotificationDatabase::STATUS_ERROR_NOT_FOUND, | |
104 database->Open(false /* create_if_missing */)); | |
105 } | |
106 | |
107 } // namespace content | |
OLD | NEW |