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

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: Removed {} and ran git cl format. 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)
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_);
132 force_close_called_ = true;
133 }
134 137
135 virtual bool IsConnected() OVERRIDE { 138 protected:
136 return !force_close_called_; 139 virtual ~ForceCloseDBCallbacks() {}
137 }
138 140
139 private: 141 private:
140 bool expect_force_close_; 142 scoped_refptr<IndexedDBContextImpl> idb_context_;
141 bool force_close_called_; 143 GURL origin_url_;
144 IndexedDBConnection* connection_;
145 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks);
142 }; 146 };
143 147
144 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { 148 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) {
145 base::ScopedTempDir temp_dir; 149 base::ScopedTempDir temp_dir;
146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
147 151
148 base::FilePath test_path; 152 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks(
153 new MockIndexedDBDatabaseCallbacks());
154 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks(
155 new MockIndexedDBDatabaseCallbacks());
149 156
150 // Create the scope which will ensure we run the destructor of the context. 157 // Create the scope which will ensure we run the destructor of the context.
151 { 158 {
152 TestBrowserContext browser_context; 159 TestBrowserContext browser_context;
153 160
154 const GURL kTestOrigin("http://test/");
155
156 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 161 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
157 temp_dir.path(), special_storage_policy_, NULL, task_runner_); 162 temp_dir.path(), special_storage_policy_, NULL, task_runner_);
158 163
159 test_path = idb_context->GetFilePathForTesting( 164 const GURL kTestOrigin("http://test/");
165
166 scoped_refptr<ForceCloseDBCallbacks> open_callbacks =
167 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
168
169 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks =
170 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
171
172 IndexedDBFactory* factory = idb_context->GetIDBFactory();
173
174 base::FilePath test_path = idb_context->GetFilePathForTesting(
160 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); 175 webkit_database::GetIdentifierFromOrigin(kTestOrigin));
161 ASSERT_TRUE(base::CreateDirectory(test_path)); 176 ASSERT_TRUE(base::CreateDirectory(test_path));
162 177
163 const bool kExpectForceClose = true; 178 factory->Open(ASCIIToUTF16("opendb"),
179 0,
180 0,
181 open_callbacks,
182 open_db_callbacks,
183 kTestOrigin,
184 test_path);
185 factory->Open(ASCIIToUTF16("closeddb"),
186 0,
187 0,
188 closed_callbacks,
189 closed_db_callbacks,
190 kTestOrigin,
191 test_path);
164 192
165 MockConnection connection1(kExpectForceClose); 193 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 194
187 idb_context->TaskRunner()->PostTask( 195 idb_context->TaskRunner()->PostTask(
188 FROM_HERE, 196 FROM_HERE,
189 base::Bind( 197 base::Bind(
190 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); 198 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin));
191 FlushIndexedDBTaskRunner(); 199 FlushIndexedDBTaskRunner();
192 message_loop_.RunUntilIdle(); 200 message_loop_.RunUntilIdle();
193 } 201 }
194 202
195 // Make sure we wait until the destructor has run. 203 // Make sure we wait until the destructor has run.
196 message_loop_.RunUntilIdle(); 204 message_loop_.RunUntilIdle();
197 205
198 EXPECT_FALSE(base::DirectoryExists(test_path)); 206 EXPECT_TRUE(open_db_callbacks->forced_close_called());
207 EXPECT_FALSE(closed_db_callbacks->forced_close_called());
199 } 208 }
200 209
201 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { 210 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) {
202 base::ScopedTempDir temp_dir; 211 base::ScopedTempDir temp_dir;
203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 212 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
204 const GURL kTestOrigin("http://test/"); 213 const GURL kTestOrigin("http://test/");
205 214
206 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 215 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
207 temp_dir.path(), special_storage_policy_, NULL, task_runner_); 216 temp_dir.path(), special_storage_policy_, NULL, task_runner_);
208 217
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); 263 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin));
255 264
256 // Simulate the write failure. 265 // Simulate the write failure.
257 callbacks->connection()->database()->TransactionCommitFailed(); 266 callbacks->connection()->database()->TransactionCommitFailed();
258 267
259 EXPECT_TRUE(db_callbacks->forced_close_called()); 268 EXPECT_TRUE(db_callbacks->forced_close_called());
260 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); 269 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin));
261 } 270 }
262 271
263 } // namespace content 272 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698