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

Side by Side Diff: content/browser/indexed_db/indexed_db_unittest.cc

Issue 17518004: Move IndexedDB from WEBKIT_DEPRECATED to dedicated thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
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/test_simple_task_runner.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 task_runner_(new base::TestSimpleTaskRunner),
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 }
28 38
29 protected: 39 protected:
40 void FlushIndexedDBTaskRunner() {
41 task_runner_->RunUntilIdle();
42 }
43
30 base::MessageLoop message_loop_; 44 base::MessageLoop message_loop_;
45 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
46 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy_;
31 47
32 private: 48 private:
33 BrowserThreadImpl webkit_thread_;
34 BrowserThreadImpl file_thread_; 49 BrowserThreadImpl file_thread_;
35 BrowserThreadImpl io_thread_; 50 BrowserThreadImpl io_thread_;
36 }; 51 };
37 52
38 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { 53 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) {
39 base::ScopedTempDir temp_dir; 54 base::ScopedTempDir temp_dir;
40 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 55 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
41 56
42 base::FilePath normal_path; 57 base::FilePath normal_path;
43 base::FilePath session_only_path; 58 base::FilePath session_only_path;
44 59
45 // Create the scope which will ensure we run the destructor of the webkit 60 // Create the scope which will ensure we run the destructor of the context
46 // context which should trigger the clean up. 61 // which should trigger the clean up.
47 { 62 {
48 TestBrowserContext browser_context; 63 scoped_refptr<IndexedDBContextImpl> idb_context =
49 64 new IndexedDBContextImpl(temp_dir.path(),
50 const GURL kNormalOrigin("http://normal/"); 65 special_storage_policy_,
51 const GURL kSessionOnlyOrigin("http://session-only/"); 66 NULL,
52 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = 67 task_runner_);
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 68
66 normal_path = idb_context->GetFilePathForTesting( 69 normal_path = idb_context->GetFilePathForTesting(
67 webkit_database::GetIdentifierFromOrigin(kNormalOrigin)); 70 webkit_database::GetIdentifierFromOrigin(kNormalOrigin));
68 session_only_path = idb_context->GetFilePathForTesting( 71 session_only_path = idb_context->GetFilePathForTesting(
69 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin)); 72 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin));
70 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); 73 ASSERT_TRUE(file_util::CreateDirectory(normal_path));
71 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); 74 ASSERT_TRUE(file_util::CreateDirectory(session_only_path));
75 FlushIndexedDBTaskRunner();
72 message_loop_.RunUntilIdle(); 76 message_loop_.RunUntilIdle();
73 } 77 }
74 78
79 FlushIndexedDBTaskRunner();
75 message_loop_.RunUntilIdle(); 80 message_loop_.RunUntilIdle();
76 81
77 EXPECT_TRUE(file_util::DirectoryExists(normal_path)); 82 EXPECT_TRUE(file_util::DirectoryExists(normal_path));
78 EXPECT_FALSE(file_util::DirectoryExists(session_only_path)); 83 EXPECT_FALSE(file_util::DirectoryExists(session_only_path));
79 } 84 }
80 85
81 TEST_F(IndexedDBTest, SetForceKeepSessionState) { 86 TEST_F(IndexedDBTest, SetForceKeepSessionState) {
82 base::ScopedTempDir temp_dir; 87 base::ScopedTempDir temp_dir;
83 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 88 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
84 89
85 base::FilePath normal_path; 90 base::FilePath normal_path;
86 base::FilePath session_only_path; 91 base::FilePath session_only_path;
87 92
88 // Create the scope which will ensure we run the destructor of the webkit 93 // Create the scope which will ensure we run the destructor of the context.
89 // context.
90 { 94 {
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. 95 // Create some indexedDB paths.
100 // With the levelDB backend, these are directories. 96 // With the levelDB backend, these are directories.
101 IndexedDBContextImpl* idb_context = static_cast<IndexedDBContextImpl*>( 97 scoped_refptr<IndexedDBContextImpl> idb_context =
102 BrowserContext::GetDefaultStoragePartition(&browser_context) 98 new IndexedDBContextImpl(temp_dir.path(),
103 ->GetIndexedDBContext()); 99 special_storage_policy_,
104 100 NULL,
105 // Override the storage policy with our own. 101 task_runner_);
106 idb_context->special_storage_policy_ = special_storage_policy;
107 idb_context->set_data_path_for_testing(temp_dir.path());
108 102
109 // Save session state. This should bypass the destruction-time deletion. 103 // Save session state. This should bypass the destruction-time deletion.
110 idb_context->SetForceKeepSessionState(); 104 idb_context->SetForceKeepSessionState();
111 105
112 normal_path = idb_context->GetFilePathForTesting( 106 normal_path = idb_context->GetFilePathForTesting(
113 webkit_database::GetIdentifierFromOrigin(kNormalOrigin)); 107 webkit_database::GetIdentifierFromOrigin(kNormalOrigin));
114 session_only_path = idb_context->GetFilePathForTesting( 108 session_only_path = idb_context->GetFilePathForTesting(
115 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin)); 109 webkit_database::GetIdentifierFromOrigin(kSessionOnlyOrigin));
116 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); 110 ASSERT_TRUE(file_util::CreateDirectory(normal_path));
117 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); 111 ASSERT_TRUE(file_util::CreateDirectory(session_only_path));
(...skipping 28 matching lines...) Expand all
146 bool expect_force_close_; 140 bool expect_force_close_;
147 bool force_close_called_; 141 bool force_close_called_;
148 }; 142 };
149 143
150 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { 144 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) {
151 base::ScopedTempDir temp_dir; 145 base::ScopedTempDir temp_dir;
152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
153 147
154 base::FilePath test_path; 148 base::FilePath test_path;
155 149
156 // Create the scope which will ensure we run the destructor of the webkit 150 // Create the scope which will ensure we run the destructor of the context.
157 // context.
158 { 151 {
159 TestBrowserContext browser_context; 152 TestBrowserContext browser_context;
160 153
161 const GURL kTestOrigin("http://test/"); 154 const GURL kTestOrigin("http://test/");
162 155
163 IndexedDBContextImpl* idb_context = static_cast<IndexedDBContextImpl*>( 156 scoped_refptr<IndexedDBContextImpl> idb_context =
164 BrowserContext::GetDefaultStoragePartition(&browser_context) 157 new IndexedDBContextImpl(temp_dir.path(),
165 ->GetIndexedDBContext()); 158 special_storage_policy_,
166 159 NULL,
167 idb_context->quota_manager_proxy_ = NULL; 160 task_runner_);
168 idb_context->set_data_path_for_testing(temp_dir.path());
169 161
170 test_path = idb_context->GetFilePathForTesting( 162 test_path = idb_context->GetFilePathForTesting(
171 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); 163 webkit_database::GetIdentifierFromOrigin(kTestOrigin));
172 ASSERT_TRUE(file_util::CreateDirectory(test_path)); 164 ASSERT_TRUE(file_util::CreateDirectory(test_path));
173 165
174 const bool kExpectForceClose = true; 166 const bool kExpectForceClose = true;
175 167
176 MockWebIDBDatabase connection1(kExpectForceClose); 168 MockWebIDBDatabase connection1(kExpectForceClose);
177 idb_context->ConnectionOpened(kTestOrigin, &connection1); 169 idb_context->TaskRunner()->PostTask(
170 FROM_HERE,
171 base::Bind(&IndexedDBContextImpl::ConnectionOpened,
172 idb_context,
173 kTestOrigin,
174 &connection1));
178 175
179 MockWebIDBDatabase connection2(!kExpectForceClose); 176 MockWebIDBDatabase connection2(!kExpectForceClose);
180 idb_context->ConnectionOpened(kTestOrigin, &connection2); 177 idb_context->TaskRunner()->PostTask(
181 idb_context->ConnectionClosed(kTestOrigin, &connection2); 178 FROM_HERE,
179 base::Bind(&IndexedDBContextImpl::ConnectionOpened,
180 idb_context,
181 kTestOrigin,
182 &connection2));
183 idb_context->TaskRunner()->PostTask(
184 FROM_HERE,
185 base::Bind(&IndexedDBContextImpl::ConnectionClosed,
186 idb_context,
187 kTestOrigin,
188 &connection2));
182 189
183 idb_context->DeleteForOrigin(kTestOrigin); 190 idb_context->TaskRunner()->PostTask(
184 191 FROM_HERE,
192 base::Bind(&IndexedDBContextImpl::DeleteForOrigin,
193 idb_context,
194 kTestOrigin));
195 FlushIndexedDBTaskRunner();
185 message_loop_.RunUntilIdle(); 196 message_loop_.RunUntilIdle();
186 } 197 }
187 198
188 // Make sure we wait until the destructor has run. 199 // Make sure we wait until the destructor has run.
189 message_loop_.RunUntilIdle(); 200 message_loop_.RunUntilIdle();
190 201
191 EXPECT_FALSE(file_util::DirectoryExists(test_path)); 202 EXPECT_FALSE(file_util::DirectoryExists(test_path));
192 } 203 }
193 204
194 } // namespace content 205 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698