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

Side by Side Diff: content/browser/in_process_webkit/indexed_db_browsertest.cc

Issue 11196029: Test for destroying leveldb directory when we detect a schema downgrade (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix initializer Created 8 years, 1 month 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
« no previous file with comments | « no previous file | content/test/data/indexeddb/open_bad_db.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop.h"
10 #include "base/process_util.h" 11 #include "base/process_util.h"
11 #include "base/test/thread_test_helper.h" 12 #include "base/test/thread_test_helper.h"
12 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
13 #include "content/browser/in_process_webkit/indexed_db_context_impl.h" 14 #include "content/browser/in_process_webkit/indexed_db_context_impl.h"
14 #include "content/browser/web_contents/web_contents_impl.h" 15 #include "content/browser/web_contents/web_contents_impl.h"
15 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h" 18 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/storage_partition.h" 19 #include "content/public/browser/storage_partition.h"
19 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 const char* expected_string) { 62 const char* expected_string) {
62 GURL url = GetTestUrl("indexeddb", filename); 63 GURL url = GetTestUrl("indexeddb", filename);
63 if (hash) 64 if (hash)
64 url = GURL(url.spec() + hash); 65 url = GURL(url.spec() + hash);
65 66
66 string16 expected_title16(ASCIIToUTF16(expected_string)); 67 string16 expected_title16(ASCIIToUTF16(expected_string));
67 TitleWatcher title_watcher(shell->web_contents(), expected_title16); 68 TitleWatcher title_watcher(shell->web_contents(), expected_title16);
68 NavigateToURL(shell, url); 69 NavigateToURL(shell, url);
69 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); 70 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
70 } 71 }
72
73 scoped_refptr<IndexedDBContext> GetContext() {
74 StoragePartition* partition =
75 BrowserContext::GetDefaultStoragePartition(
76 shell()->web_contents()->GetBrowserContext());
77 return partition->GetIndexedDBContext();
78 };
71 }; 79 };
72 80
73 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTest) { 81 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTest) {
74 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html")); 82 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"));
75 } 83 }
76 84
77 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTestIncognito) { 85 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTestIncognito) {
78 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"), 86 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"),
79 true /* incognito */); 87 true /* incognito */);
80 } 88 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 virtual void SetUpCommandLine(CommandLine* command_line) { 193 virtual void SetUpCommandLine(CommandLine* command_line) {
186 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); 194 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
187 } 195 }
188 }; 196 };
189 197
190 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed, 198 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed,
191 DatabaseCallbacksTest) { 199 DatabaseCallbacksTest) {
192 SimpleTest(GetTestUrl("indexeddb", "database_callbacks_first.html")); 200 SimpleTest(GetTestUrl("indexeddb", "database_callbacks_first.html"));
193 } 201 }
194 202
195 class IndexedDBBrowserTestWithVersion0Schema : public IndexedDBBrowserTest { 203 static void CopyLevelDBToProfile(Shell* shell,
204 scoped_refptr<IndexedDBContext> context,
205 const std::string& test_directory) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
207 FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
208 FilePath test_data_dir =
209 GetTestFilePath("indexeddb", test_directory.c_str()).Append(leveldb_dir);
210 IndexedDBContextImpl* context_impl =
211 static_cast<IndexedDBContextImpl*>(context.get());
212 FilePath dest = context_impl->data_path().Append(leveldb_dir);
213 // If we don't create the destination directory first, the contents of the
214 // leveldb directory are copied directly into profile/IndexedDB instead of
215 // profile/IndexedDB/file__0.xxx/
216 ASSERT_TRUE(file_util::CreateDirectory(dest));
217 const bool kRecursive = true;
218 ASSERT_TRUE(file_util::CopyDirectory(test_data_dir,
219 context_impl->data_path(),
220 kRecursive));
221 }
222
223 class IndexedDBBrowserTestWithPreexistingLevelDB : public IndexedDBBrowserTest {
196 public: 224 public:
225 IndexedDBBrowserTestWithPreexistingLevelDB() : disk_usage_(-1) { }
226
197 virtual void SetUpOnMainThread() { 227 virtual void SetUpOnMainThread() {
198 scoped_refptr<IndexedDBContext> context = 228 scoped_refptr<IndexedDBContext> context = GetContext();
199 BrowserContext::GetDefaultStoragePartition(
200 shell()->web_contents()->GetBrowserContext())->
201 GetIndexedDBContext();
202 BrowserThread::PostTask( 229 BrowserThread::PostTask(
203 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, 230 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
204 base::Bind( 231 base::Bind(&CopyLevelDBToProfile, shell(), context,
205 &IndexedDBBrowserTestWithVersion0Schema::CopyLevelDBToProfile, 232 EnclosingLevelDBDir()));
206 shell(),
207 context));
208 scoped_refptr<base::ThreadTestHelper> helper( 233 scoped_refptr<base::ThreadTestHelper> helper(
209 new base::ThreadTestHelper(BrowserThread::GetMessageLoopProxyForThread( 234 new base::ThreadTestHelper(BrowserThread::GetMessageLoopProxyForThread(
210 BrowserThread::WEBKIT_DEPRECATED))); 235 BrowserThread::WEBKIT_DEPRECATED)));
211 ASSERT_TRUE(helper->Run()); 236 ASSERT_TRUE(helper->Run());
212 } 237 }
213 static void CopyLevelDBToProfile(Shell* shell, 238
214 scoped_refptr<IndexedDBContext> context) { 239 virtual std::string EnclosingLevelDBDir() = 0;
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); 240
216 FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb")); 241 protected:
217 FilePath test_data_dir = 242 virtual int64 RequestDiskUsage() {
218 GetTestFilePath("indexeddb", "migration_from_0").Append(leveldb_dir); 243 BrowserThread::PostTaskAndReplyWithResult(
219 IndexedDBContextImpl* context_impl = 244 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE,
220 static_cast<IndexedDBContextImpl*>(context.get()); 245 base::Bind(&IndexedDBContext::GetOriginDiskUsage, GetContext(),
221 FilePath dest = context_impl->data_path().Append(leveldb_dir); 246 GURL("file:///")), base::Bind(
222 // If we don't create the destination directory first, the contents of the 247 &IndexedDBBrowserTestWithPreexistingLevelDB::DidGetDiskUsage,
223 // leveldb directory are copied directly into profile/IndexedDB instead of 248 this));
224 // profile/IndexedDB/file__0.xxx/ 249 scoped_refptr<base::ThreadTestHelper> helper(
225 ASSERT_TRUE(file_util::CreateDirectory(dest)); 250 new base::ThreadTestHelper(BrowserThread::GetMessageLoopProxyForThread(
226 const bool kRecursive = true; 251 BrowserThread::WEBKIT_DEPRECATED)));
227 ASSERT_TRUE(file_util::CopyDirectory(test_data_dir, 252 EXPECT_TRUE(helper->Run());
228 context_impl->data_path(), 253 // Wait for DidGetDiskUsage to be called.
229 kRecursive)); 254 MessageLoop::current()->RunUntilIdle();
255 return disk_usage_;
256 }
257 private:
258 virtual void DidGetDiskUsage(int64 bytes) {
259 EXPECT_GT(bytes, 0);
260 disk_usage_ = bytes;
261 }
262
263 int64 disk_usage_;
264 };
265
266 class IndexedDBBrowserTestWithVersion0Schema : public
267 IndexedDBBrowserTestWithPreexistingLevelDB {
268 virtual std::string EnclosingLevelDBDir() {
269 return "migration_from_0";
230 } 270 }
231 }; 271 };
232 272
233 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion0Schema, MigrationTest) { 273 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion0Schema, MigrationTest) {
234 SimpleTest(GetTestUrl("indexeddb", "migration_test.html")); 274 SimpleTest(GetTestUrl("indexeddb", "migration_test.html"));
235 } 275 }
236 276
277 class IndexedDBBrowserTestWithVersion123456Schema : public
278 IndexedDBBrowserTestWithPreexistingLevelDB {
279 virtual std::string EnclosingLevelDBDir() {
280 return "schema_version_123456";
281 }
282 };
283
284 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion123456Schema,
285 DestroyTest) {
286 int64 original_size = RequestDiskUsage();
287 EXPECT_GT(original_size, 0);
288 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
289 int64 new_size = RequestDiskUsage();
290 EXPECT_NE(original_size, new_size);
291 }
292
293 class IndexedDBBrowserTestWithCorruptLevelDB : public
294 IndexedDBBrowserTestWithPreexistingLevelDB {
295 virtual std::string EnclosingLevelDBDir() {
296 return "corrupt_leveldb";
297 }
298 };
299
300 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithCorruptLevelDB,
301 DestroyTest) {
302 int64 original_size = RequestDiskUsage();
303 EXPECT_GT(original_size, 0);
304 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
305 int64 new_size = RequestDiskUsage();
306 EXPECT_NE(original_size, new_size);
307 }
308
237 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, LevelDBLogFileTest) { 309 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, LevelDBLogFileTest) {
238 // Any page that opens an IndexedDB will work here. 310 // Any page that opens an IndexedDB will work here.
239 SimpleTest(GetTestUrl("indexeddb", "database_test.html")); 311 SimpleTest(GetTestUrl("indexeddb", "database_test.html"));
240 scoped_refptr<IndexedDBContext> context = 312 scoped_refptr<IndexedDBContext> context =
241 BrowserContext::GetDefaultStoragePartition( 313 BrowserContext::GetDefaultStoragePartition(
242 shell()->web_contents()->GetBrowserContext())-> 314 shell()->web_contents()->GetBrowserContext())->
243 GetIndexedDBContext(); 315 GetIndexedDBContext();
244 IndexedDBContextImpl* context_impl = 316 IndexedDBContextImpl* context_impl =
245 static_cast<IndexedDBContextImpl*>(context.get()); 317 static_cast<IndexedDBContextImpl*>(context.get());
246 FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb")); 318 FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 TitleWatcher title_watcher(new_shell->web_contents(), expected_title16); 372 TitleWatcher title_watcher(new_shell->web_contents(), expected_title16);
301 373
302 base::KillProcess( 374 base::KillProcess(
303 shell()->web_contents()->GetRenderProcessHost()->GetHandle(), 0, true); 375 shell()->web_contents()->GetRenderProcessHost()->GetHandle(), 0, true);
304 shell()->Close(); 376 shell()->Close();
305 377
306 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); 378 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
307 } 379 }
308 380
309 } // namespace content 381 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/test/data/indexeddb/open_bad_db.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698