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

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

Issue 93873017: IndexedDBFactory now ForceCloses databases. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years 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
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" 7 #include "base/test/test_simple_task_runner.h"
8 #include "base/threading/thread.h" 8 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/indexed_db/indexed_db_connection.h" 10 #include "content/browser/indexed_db/indexed_db_connection.h"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 109 }
110 110
111 // Make sure we wait until the destructor has run. 111 // Make sure we wait until the destructor has run.
112 message_loop_.RunUntilIdle(); 112 message_loop_.RunUntilIdle();
113 113
114 // No data was cleared because of SetForceKeepSessionState. 114 // No data was cleared because of SetForceKeepSessionState.
115 EXPECT_TRUE(base::DirectoryExists(normal_path)); 115 EXPECT_TRUE(base::DirectoryExists(normal_path));
116 EXPECT_TRUE(base::DirectoryExists(session_only_path)); 116 EXPECT_TRUE(base::DirectoryExists(session_only_path));
117 } 117 }
118 118
119 class MockConnection : public IndexedDBConnection { 119 class ForceCloseDBCallbacks : public IndexedDBCallbacks {
120 public: 120 public:
121 explicit MockConnection(bool expect_force_close) 121 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context,
122 : IndexedDBConnection(NULL, NULL), 122 const GURL& origin_url)
jsbell 2013/12/19 22:35:34 Nit: indentation
123 expect_force_close_(expect_force_close), 123 : IndexedDBCallbacks(NULL, 0, 0),
124 force_close_called_(false) {} 124 idb_context_(idb_context),
125 origin_url_(origin_url),
126 connection_(NULL) {}
125 127
126 virtual ~MockConnection() { 128 virtual void OnSuccess() OVERRIDE {}
127 EXPECT_TRUE(force_close_called_ == expect_force_close_); 129 virtual void OnSuccess(const std::vector<base::string16>&) OVERRIDE {}
130 virtual void OnSuccess(scoped_ptr<IndexedDBConnection> connection,
131 const IndexedDBDatabaseMetadata& metadata) OVERRIDE {
132 connection_ = connection.release();
133 idb_context_->ConnectionOpened(origin_url_, connection_);
128 } 134 }
129 135
130 virtual void ForceClose() OVERRIDE { 136 IndexedDBConnection* connection() { return connection_; }
131 ASSERT_TRUE(expect_force_close_); 137 protected:
132 force_close_called_ = true; 138 virtual ~ForceCloseDBCallbacks() {}
133 }
134
135 virtual bool IsConnected() OVERRIDE {
136 return !force_close_called_;
137 }
138 139
139 private: 140 private:
140 bool expect_force_close_; 141 scoped_refptr<IndexedDBContextImpl> idb_context_;
141 bool force_close_called_; 142 GURL origin_url_;
143 IndexedDBConnection* connection_;
144 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks);
142 }; 145 };
143 146
144 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { 147 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) {
145 base::ScopedTempDir temp_dir; 148 base::ScopedTempDir temp_dir;
146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
147 150
148 base::FilePath test_path; 151 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks(
152 new MockIndexedDBDatabaseCallbacks());
153 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks(
154 new MockIndexedDBDatabaseCallbacks());
149 155
150 // Create the scope which will ensure we run the destructor of the context. 156 // Create the scope which will ensure we run the destructor of the context.
151 { 157 {
152 TestBrowserContext browser_context; 158 TestBrowserContext browser_context;
153 159
154 const GURL kTestOrigin("http://test/");
155
156 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 160 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
157 temp_dir.path(), special_storage_policy_, NULL, task_runner_); 161 temp_dir.path(), special_storage_policy_, NULL, task_runner_);
158 162
159 test_path = idb_context->GetFilePathForTesting( 163 const GURL kTestOrigin("http://test/");
164
165 scoped_refptr<ForceCloseDBCallbacks> open_callbacks =
166 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
167
168 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks =
169 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
170
171 IndexedDBFactory* factory = idb_context->GetIDBFactory();
172
173 base::FilePath test_path = idb_context->GetFilePathForTesting(
160 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); 174 webkit_database::GetIdentifierFromOrigin(kTestOrigin));
161 ASSERT_TRUE(base::CreateDirectory(test_path)); 175 ASSERT_TRUE(base::CreateDirectory(test_path));
162 176
163 const bool kExpectForceClose = true; 177 factory->Open(ASCIIToUTF16("opendb"), 0, 0, open_callbacks,
178 open_db_callbacks, kTestOrigin, test_path);
179 factory->Open(ASCIIToUTF16("closeddb"), 0, 0, closed_callbacks,
180 closed_db_callbacks, kTestOrigin, test_path);
164 181
165 MockConnection connection1(kExpectForceClose); 182 closed_callbacks->connection()->Close();
166 idb_context->TaskRunner()->PostTask(
167 FROM_HERE,
168 base::Bind(&IndexedDBContextImpl::ConnectionOpened,
169 idb_context,
170 kTestOrigin,
171 &connection1));
172
173 MockConnection connection2(!kExpectForceClose);
174 idb_context->TaskRunner()->PostTask(
175 FROM_HERE,
176 base::Bind(&IndexedDBContextImpl::ConnectionOpened,
177 idb_context,
178 kTestOrigin,
179 &connection2));
180 idb_context->TaskRunner()->PostTask(
181 FROM_HERE,
182 base::Bind(&IndexedDBContextImpl::ConnectionClosed,
183 idb_context,
184 kTestOrigin,
185 &connection2));
186 183
187 idb_context->TaskRunner()->PostTask( 184 idb_context->TaskRunner()->PostTask(
188 FROM_HERE, 185 FROM_HERE,
189 base::Bind( 186 base::Bind(
190 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); 187 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin));
191 FlushIndexedDBTaskRunner(); 188 FlushIndexedDBTaskRunner();
192 message_loop_.RunUntilIdle(); 189 message_loop_.RunUntilIdle();
193 } 190 }
194 191
195 // Make sure we wait until the destructor has run. 192 // Make sure we wait until the destructor has run.
196 message_loop_.RunUntilIdle(); 193 message_loop_.RunUntilIdle();
197 194
198 EXPECT_FALSE(base::DirectoryExists(test_path)); 195 EXPECT_TRUE(open_db_callbacks->forced_close_called());
196 EXPECT_FALSE(closed_db_callbacks->forced_close_called());
199 } 197 }
200 198
201 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { 199 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) {
202 base::ScopedTempDir temp_dir; 200 base::ScopedTempDir temp_dir;
203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 201 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
204 const GURL kTestOrigin("http://test/"); 202 const GURL kTestOrigin("http://test/");
205 203
206 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 204 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
207 temp_dir.path(), special_storage_policy_, NULL, task_runner_); 205 temp_dir.path(), special_storage_policy_, NULL, task_runner_);
208 206
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); 252 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin));
255 253
256 // Simulate the write failure. 254 // Simulate the write failure.
257 callbacks->connection()->database()->TransactionCommitFailed(); 255 callbacks->connection()->database()->TransactionCommitFailed();
258 256
259 EXPECT_TRUE(db_callbacks->forced_close_called()); 257 EXPECT_TRUE(db_callbacks->forced_close_called());
260 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); 258 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin));
261 } 259 }
262 260
263 } // namespace content 261 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698