OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/fileapi/file_system_directory_database.h" | 5 #include "webkit/fileapi/file_system_directory_database.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
| 9 #include "base/platform_file.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
11 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace fileapi { | 16 namespace fileapi { |
16 | 17 |
| 18 namespace { |
| 19 const FilePath::CharType kDirectoryDatabaseName[] = FILE_PATH_LITERAL("Paths"); |
| 20 } |
| 21 |
17 class FileSystemDirectoryDatabaseTest : public testing::Test { | 22 class FileSystemDirectoryDatabaseTest : public testing::Test { |
18 public: | 23 public: |
19 typedef FileSystemDirectoryDatabase::FileId FileId; | 24 typedef FileSystemDirectoryDatabase::FileId FileId; |
20 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; | 25 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; |
21 | 26 |
22 FileSystemDirectoryDatabaseTest() { | 27 FileSystemDirectoryDatabaseTest() { |
23 EXPECT_TRUE(base_.CreateUniqueTempDir()); | 28 EXPECT_TRUE(base_.CreateUniqueTempDir()); |
24 InitDatabase(); | 29 InitDatabase(); |
25 } | 30 } |
26 | 31 |
27 FileSystemDirectoryDatabase* db() { | 32 FileSystemDirectoryDatabase* db() { |
28 return db_.get(); | 33 return db_.get(); |
29 } | 34 } |
30 | 35 |
31 void InitDatabase() { | 36 void InitDatabase() { |
| 37 CloseDatabase(); |
| 38 db_.reset(new FileSystemDirectoryDatabase(sandbox_path())); |
| 39 } |
| 40 |
| 41 void CloseDatabase() { |
32 db_.reset(); | 42 db_.reset(); |
33 FilePath path = base_.path().AppendASCII("db"); | |
34 db_.reset(new FileSystemDirectoryDatabase(path)); | |
35 } | 43 } |
36 | 44 |
37 bool AddFileInfo(FileId parent_id, const FilePath::StringType& name) { | 45 bool AddFileInfo(FileId parent_id, const FilePath::StringType& name) { |
38 FileId file_id; | 46 FileId file_id; |
39 FileInfo info; | 47 FileInfo info; |
40 info.parent_id = parent_id; | 48 info.parent_id = parent_id; |
41 info.name = name; | 49 info.name = name; |
42 return db_->AddFileInfo(info, &file_id); | 50 return db_->AddFileInfo(info, &file_id); |
43 } | 51 } |
44 | 52 |
| 53 FilePath sandbox_path() const { |
| 54 return base_.path(); |
| 55 } |
| 56 |
45 protected: | 57 protected: |
46 // Common temp base for nondestructive uses. | 58 // Common temp base for nondestructive uses. |
47 ScopedTempDir base_; | 59 ScopedTempDir base_; |
48 scoped_ptr<FileSystemDirectoryDatabase> db_; | 60 scoped_ptr<FileSystemDirectoryDatabase> db_; |
49 | 61 |
50 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabaseTest); | 62 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabaseTest); |
51 }; | 63 }; |
52 | 64 |
53 TEST_F(FileSystemDirectoryDatabaseTest, TestMissingFileGetInfo) { | 65 TEST_F(FileSystemDirectoryDatabaseTest, TestMissingFileGetInfo) { |
54 FileId file_id = 888; | 66 FileId file_id = 888; |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 InitDatabase(); | 422 InitDatabase(); |
411 EXPECT_TRUE(db()->GetNextInteger(&next)); | 423 EXPECT_TRUE(db()->GetNextInteger(&next)); |
412 EXPECT_EQ(2, next); | 424 EXPECT_EQ(2, next); |
413 EXPECT_TRUE(db()->GetNextInteger(&next)); | 425 EXPECT_TRUE(db()->GetNextInteger(&next)); |
414 EXPECT_EQ(3, next); | 426 EXPECT_EQ(3, next); |
415 InitDatabase(); | 427 InitDatabase(); |
416 EXPECT_TRUE(db()->GetNextInteger(&next)); | 428 EXPECT_TRUE(db()->GetNextInteger(&next)); |
417 EXPECT_EQ(4, next); | 429 EXPECT_EQ(4, next); |
418 } | 430 } |
419 | 431 |
| 432 TEST_F(FileSystemDirectoryDatabaseTest, TestDatabaseRecovery) { |
| 433 int64 next = -1; |
| 434 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 435 EXPECT_EQ(0, next); |
| 436 InitDatabase(); |
| 437 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 438 EXPECT_EQ(1, next); |
| 439 CloseDatabase(); |
| 440 |
| 441 bool created = false; |
| 442 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; |
| 443 base::PlatformFile file = base::CreatePlatformFile( |
| 444 sandbox_path().Append(kDirectoryDatabaseName).AppendASCII("CURRENT"), |
| 445 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 446 &created, &error); |
| 447 EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
| 448 EXPECT_TRUE(created); |
| 449 EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 450 |
| 451 InitDatabase(); |
| 452 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 453 EXPECT_EQ(0, next); |
| 454 } |
| 455 |
420 } // namespace fileapi | 456 } // namespace fileapi |
OLD | NEW |