OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/test/thread_test_helper.h" |
| 8 #include "base/threading/thread.h" |
7 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
8 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 10 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
9 #include "content/browser/indexed_db/webidbdatabase_impl.h" | 11 #include "content/browser/indexed_db/webidbdatabase_impl.h" |
10 #include "content/public/browser/storage_partition.h" | 12 #include "content/public/browser/storage_partition.h" |
11 #include "content/public/common/url_constants.h" | 13 #include "content/public/common/url_constants.h" |
12 #include "content/public/test/test_browser_context.h" | 14 #include "content/public/test/test_browser_context.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "webkit/browser/quota/mock_special_storage_policy.h" | 16 #include "webkit/browser/quota/mock_special_storage_policy.h" |
15 #include "webkit/browser/quota/quota_manager.h" | 17 #include "webkit/browser/quota/quota_manager.h" |
16 #include "webkit/browser/quota/special_storage_policy.h" | 18 #include "webkit/browser/quota/special_storage_policy.h" |
17 #include "webkit/common/database/database_identifier.h" | 19 #include "webkit/common/database/database_identifier.h" |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 | 22 |
21 class IndexedDBTest : public testing::Test { | 23 class IndexedDBTest : public testing::Test { |
22 public: | 24 public: |
| 25 const GURL kNormalOrigin; |
| 26 const GURL kSessionOnlyOrigin; |
| 27 |
23 IndexedDBTest() | 28 IndexedDBTest() |
24 : message_loop_(base::MessageLoop::TYPE_IO), | 29 : kNormalOrigin("http://normal/"), |
25 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), | 30 kSessionOnlyOrigin("http://session-only/"), |
| 31 message_loop_(base::MessageLoop::TYPE_IO), |
| 32 idb_thread_(new base::Thread("IndexedDBTest")), |
| 33 special_storage_policy_(new quota::MockSpecialStoragePolicy), |
26 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), | 34 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), |
27 io_thread_(BrowserThread::IO, &message_loop_) {} | 35 io_thread_(BrowserThread::IO, &message_loop_) { |
| 36 special_storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
| 37 idb_thread_->Start(); |
| 38 } |
| 39 |
| 40 void FlushIndexedDBTaskRunner() { |
| 41 scoped_refptr<base::ThreadTestHelper> helper( |
| 42 new base::ThreadTestHelper(idb_thread_->message_loop_proxy())); |
| 43 ASSERT_TRUE(helper->Run()); |
| 44 } |
28 | 45 |
29 protected: | 46 protected: |
30 base::MessageLoop message_loop_; | 47 base::MessageLoop message_loop_; |
| 48 scoped_ptr<base::Thread> idb_thread_; |
| 49 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy_; |
31 | 50 |
32 private: | 51 private: |
33 BrowserThreadImpl webkit_thread_; | |
34 BrowserThreadImpl file_thread_; | 52 BrowserThreadImpl file_thread_; |
35 BrowserThreadImpl io_thread_; | 53 BrowserThreadImpl io_thread_; |
36 }; | 54 }; |
37 | 55 |
38 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { | 56 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { |
39 base::ScopedTempDir temp_dir; | 57 base::ScopedTempDir temp_dir; |
40 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 58 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
41 | 59 |
42 base::FilePath normal_path; | 60 base::FilePath normal_path; |
43 base::FilePath session_only_path; | 61 base::FilePath session_only_path; |
44 | 62 |
45 // Create the scope which will ensure we run the destructor of the webkit | 63 // Create the scope which will ensure we run the destructor of the context |
46 // context which should trigger the clean up. | 64 // which should trigger the clean up. |
47 { | 65 { |
48 TestBrowserContext browser_context; | 66 scoped_refptr<IndexedDBContextImpl> idb_context = |
49 | 67 new IndexedDBContextImpl(temp_dir.path(), |
50 const GURL kNormalOrigin("http://normal/"); | 68 special_storage_policy_, |
51 const GURL kSessionOnlyOrigin("http://session-only/"); | 69 NULL, |
52 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 70 idb_thread_->message_loop()); |
53 new quota::MockSpecialStoragePolicy; | |
54 special_storage_policy->AddSessionOnly(kSessionOnlyOrigin); | |
55 | |
56 // Create some indexedDB paths. | |
57 // With the levelDB backend, these are directories. | |
58 IndexedDBContextImpl* idb_context = static_cast<IndexedDBContextImpl*>( | |
59 BrowserContext::GetDefaultStoragePartition(&browser_context) | |
60 ->GetIndexedDBContext()); | |
61 | |
62 // Override the storage policy with our own. | |
63 idb_context->special_storage_policy_ = special_storage_policy; | |
64 idb_context->set_data_path_for_testing(temp_dir.path()); | |
65 | 71 |
66 normal_path = idb_context->GetFilePathForTesting( | 72 normal_path = idb_context->GetFilePathForTesting( |
67 webkit_database::GetIdentifierFromOrigin(kNormalOrigin)); | 73 webkit_database::GetIdentifierFromOrigin(kNormalOrigin)); |
68 session_only_path = idb_context->GetFilePathForTesting( | 74 session_only_path = idb_context->GetFilePathForTesting( |
69 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin)); | 75 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin)); |
70 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); | 76 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); |
71 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); | 77 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); |
| 78 FlushIndexedDBTaskRunner(); |
72 message_loop_.RunUntilIdle(); | 79 message_loop_.RunUntilIdle(); |
73 } | 80 } |
74 | 81 |
| 82 FlushIndexedDBTaskRunner(); |
75 message_loop_.RunUntilIdle(); | 83 message_loop_.RunUntilIdle(); |
76 | 84 |
77 EXPECT_TRUE(file_util::DirectoryExists(normal_path)); | 85 EXPECT_TRUE(file_util::DirectoryExists(normal_path)); |
78 EXPECT_FALSE(file_util::DirectoryExists(session_only_path)); | 86 EXPECT_FALSE(file_util::DirectoryExists(session_only_path)); |
79 } | 87 } |
80 | 88 |
81 TEST_F(IndexedDBTest, SetForceKeepSessionState) { | 89 TEST_F(IndexedDBTest, SetForceKeepSessionState) { |
82 base::ScopedTempDir temp_dir; | 90 base::ScopedTempDir temp_dir; |
83 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 91 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
84 | 92 |
85 base::FilePath normal_path; | 93 base::FilePath normal_path; |
86 base::FilePath session_only_path; | 94 base::FilePath session_only_path; |
87 | 95 |
88 // Create the scope which will ensure we run the destructor of the webkit | 96 // Create the scope which will ensure we run the destructor of the context. |
89 // context. | |
90 { | 97 { |
91 TestBrowserContext browser_context; | |
92 | |
93 const GURL kNormalOrigin("http://normal/"); | |
94 const GURL kSessionOnlyOrigin("http://session-only/"); | |
95 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | |
96 new quota::MockSpecialStoragePolicy; | |
97 special_storage_policy->AddSessionOnly(kSessionOnlyOrigin); | |
98 | |
99 // Create some indexedDB paths. | 98 // Create some indexedDB paths. |
100 // With the levelDB backend, these are directories. | 99 // With the levelDB backend, these are directories. |
101 IndexedDBContextImpl* idb_context = static_cast<IndexedDBContextImpl*>( | 100 scoped_refptr<IndexedDBContextImpl> idb_context = |
102 BrowserContext::GetDefaultStoragePartition(&browser_context) | 101 new IndexedDBContextImpl(temp_dir.path(), |
103 ->GetIndexedDBContext()); | 102 special_storage_policy_, |
104 | 103 NULL, |
105 // Override the storage policy with our own. | 104 idb_thread_->message_loop()); |
106 idb_context->special_storage_policy_ = special_storage_policy; | |
107 idb_context->set_data_path_for_testing(temp_dir.path()); | |
108 | 105 |
109 // Save session state. This should bypass the destruction-time deletion. | 106 // Save session state. This should bypass the destruction-time deletion. |
110 idb_context->SetForceKeepSessionState(); | 107 idb_context->SetForceKeepSessionState(); |
111 | 108 |
112 normal_path = idb_context->GetFilePathForTesting( | 109 normal_path = idb_context->GetFilePathForTesting( |
113 webkit_database::GetIdentifierFromOrigin(kNormalOrigin)); | 110 webkit_database::GetIdentifierFromOrigin(kNormalOrigin)); |
114 session_only_path = idb_context->GetFilePathForTesting( | 111 session_only_path = idb_context->GetFilePathForTesting( |
115 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin)); | 112 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin)); |
116 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); | 113 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); |
117 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); | 114 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); |
(...skipping 28 matching lines...) Expand all Loading... |
146 bool expect_force_close_; | 143 bool expect_force_close_; |
147 bool force_close_called_; | 144 bool force_close_called_; |
148 }; | 145 }; |
149 | 146 |
150 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { | 147 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { |
151 base::ScopedTempDir temp_dir; | 148 base::ScopedTempDir temp_dir; |
152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
153 | 150 |
154 base::FilePath test_path; | 151 base::FilePath test_path; |
155 | 152 |
156 // Create the scope which will ensure we run the destructor of the webkit | 153 // Create the scope which will ensure we run the destructor of the context. |
157 // context. | |
158 { | 154 { |
159 TestBrowserContext browser_context; | 155 TestBrowserContext browser_context; |
160 | 156 |
161 const GURL kTestOrigin("http://test/"); | 157 const GURL kTestOrigin("http://test/"); |
162 | 158 |
163 IndexedDBContextImpl* idb_context = static_cast<IndexedDBContextImpl*>( | 159 scoped_refptr<IndexedDBContextImpl> idb_context = |
164 BrowserContext::GetDefaultStoragePartition(&browser_context) | 160 new IndexedDBContextImpl(temp_dir.path(), |
165 ->GetIndexedDBContext()); | 161 special_storage_policy_, |
166 | 162 NULL, |
167 idb_context->quota_manager_proxy_ = NULL; | 163 idb_thread_->message_loop()); |
168 idb_context->set_data_path_for_testing(temp_dir.path()); | |
169 | 164 |
170 test_path = idb_context->GetFilePathForTesting( | 165 test_path = idb_context->GetFilePathForTesting( |
171 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); | 166 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); |
172 ASSERT_TRUE(file_util::CreateDirectory(test_path)); | 167 ASSERT_TRUE(file_util::CreateDirectory(test_path)); |
173 | 168 |
174 const bool kExpectForceClose = true; | 169 const bool kExpectForceClose = true; |
175 | 170 |
176 MockWebIDBDatabase connection1(kExpectForceClose); | 171 MockWebIDBDatabase connection1(kExpectForceClose); |
177 idb_context->ConnectionOpened(kTestOrigin, &connection1); | 172 idb_context->TaskRunner()->PostTask( |
| 173 FROM_HERE, |
| 174 base::Bind(&IndexedDBContextImpl::ConnectionOpened, |
| 175 idb_context, |
| 176 kTestOrigin, |
| 177 &connection1)); |
178 | 178 |
179 MockWebIDBDatabase connection2(!kExpectForceClose); | 179 MockWebIDBDatabase connection2(!kExpectForceClose); |
180 idb_context->ConnectionOpened(kTestOrigin, &connection2); | 180 idb_context->TaskRunner()->PostTask( |
181 idb_context->ConnectionClosed(kTestOrigin, &connection2); | 181 FROM_HERE, |
| 182 base::Bind(&IndexedDBContextImpl::ConnectionOpened, |
| 183 idb_context, |
| 184 kTestOrigin, |
| 185 &connection2)); |
| 186 idb_context->TaskRunner()->PostTask( |
| 187 FROM_HERE, |
| 188 base::Bind(&IndexedDBContextImpl::ConnectionClosed, |
| 189 idb_context, |
| 190 kTestOrigin, |
| 191 &connection2)); |
182 | 192 |
183 idb_context->DeleteForOrigin(kTestOrigin); | 193 idb_context->TaskRunner()->PostTask( |
184 | 194 FROM_HERE, |
| 195 base::Bind(&IndexedDBContextImpl::DeleteForOrigin, |
| 196 idb_context, |
| 197 kTestOrigin)); |
| 198 scoped_refptr<base::ThreadTestHelper> helper( |
| 199 new base::ThreadTestHelper(idb_context->MessageLoopProxy())); |
| 200 EXPECT_TRUE(helper->Run()); |
185 message_loop_.RunUntilIdle(); | 201 message_loop_.RunUntilIdle(); |
186 } | 202 } |
187 | 203 |
188 // Make sure we wait until the destructor has run. | 204 // Make sure we wait until the destructor has run. |
189 message_loop_.RunUntilIdle(); | 205 message_loop_.RunUntilIdle(); |
190 | 206 |
191 EXPECT_FALSE(file_util::DirectoryExists(test_path)); | 207 EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
192 } | 208 } |
193 | 209 |
194 } // namespace content | 210 } // namespace content |
OLD | NEW |