| 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 |
| 17 class FileSystemDirectoryDatabaseTest : public testing::Test { | 18 class FileSystemDirectoryDatabaseTest : public testing::Test { |
| 18 public: | 19 public: |
| 19 typedef FileSystemDirectoryDatabase::FileId FileId; | 20 typedef FileSystemDirectoryDatabase::FileId FileId; |
| 20 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; | 21 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; |
| 21 | 22 |
| 22 FileSystemDirectoryDatabaseTest() { | 23 FileSystemDirectoryDatabaseTest() { |
| 23 EXPECT_TRUE(base_.CreateUniqueTempDir()); | 24 EXPECT_TRUE(base_.CreateUniqueTempDir()); |
| 24 InitDatabase(); | 25 InitDatabase(); |
| 25 } | 26 } |
| 26 | 27 |
| 27 FileSystemDirectoryDatabase* db() { | 28 FileSystemDirectoryDatabase* db() { |
| 28 return db_.get(); | 29 return db_.get(); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void InitDatabase() { | 32 void InitDatabase() { |
| 33 CloseDatabase(); |
| 34 db_.reset(new FileSystemDirectoryDatabase(db_path())); |
| 35 } |
| 36 |
| 37 void CloseDatabase() { |
| 32 db_.reset(); | 38 db_.reset(); |
| 33 FilePath path = base_.path().AppendASCII("db"); | |
| 34 db_.reset(new FileSystemDirectoryDatabase(path)); | |
| 35 } | 39 } |
| 36 | 40 |
| 37 bool AddFileInfo(FileId parent_id, const FilePath::StringType& name) { | 41 bool AddFileInfo(FileId parent_id, const FilePath::StringType& name) { |
| 38 FileId file_id; | 42 FileId file_id; |
| 39 FileInfo info; | 43 FileInfo info; |
| 40 info.parent_id = parent_id; | 44 info.parent_id = parent_id; |
| 41 info.name = name; | 45 info.name = name; |
| 42 return db_->AddFileInfo(info, &file_id); | 46 return db_->AddFileInfo(info, &file_id); |
| 43 } | 47 } |
| 44 | 48 |
| 49 FilePath db_path() const { |
| 50 return base_.path().AppendASCII("db"); |
| 51 } |
| 52 |
| 45 protected: | 53 protected: |
| 46 // Common temp base for nondestructive uses. | 54 // Common temp base for nondestructive uses. |
| 47 ScopedTempDir base_; | 55 ScopedTempDir base_; |
| 48 scoped_ptr<FileSystemDirectoryDatabase> db_; | 56 scoped_ptr<FileSystemDirectoryDatabase> db_; |
| 49 | 57 |
| 50 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabaseTest); | 58 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabaseTest); |
| 51 }; | 59 }; |
| 52 | 60 |
| 53 TEST_F(FileSystemDirectoryDatabaseTest, TestMissingFileGetInfo) { | 61 TEST_F(FileSystemDirectoryDatabaseTest, TestMissingFileGetInfo) { |
| 54 FileId file_id = 888; | 62 FileId file_id = 888; |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 InitDatabase(); | 418 InitDatabase(); |
| 411 EXPECT_TRUE(db()->GetNextInteger(&next)); | 419 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 412 EXPECT_EQ(2, next); | 420 EXPECT_EQ(2, next); |
| 413 EXPECT_TRUE(db()->GetNextInteger(&next)); | 421 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 414 EXPECT_EQ(3, next); | 422 EXPECT_EQ(3, next); |
| 415 InitDatabase(); | 423 InitDatabase(); |
| 416 EXPECT_TRUE(db()->GetNextInteger(&next)); | 424 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 417 EXPECT_EQ(4, next); | 425 EXPECT_EQ(4, next); |
| 418 } | 426 } |
| 419 | 427 |
| 428 TEST_F(FileSystemDirectoryDatabaseTest, TestDatabaseRecovery) { |
| 429 int64 next = -1; |
| 430 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 431 EXPECT_EQ(0, next); |
| 432 InitDatabase(); |
| 433 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 434 EXPECT_EQ(1, next); |
| 435 CloseDatabase(); |
| 436 |
| 437 bool created = false; |
| 438 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; |
| 439 base::PlatformFile file = base::CreatePlatformFile( |
| 440 db_path().AppendASCII("CURRENT"), |
| 441 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 442 &created, &error); |
| 443 EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
| 444 EXPECT_TRUE(created); |
| 445 EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 446 |
| 447 InitDatabase(); |
| 448 EXPECT_TRUE(db()->GetNextInteger(&next)); |
| 449 EXPECT_EQ(0, next); |
| 450 } |
| 451 |
| 420 } // namespace fileapi | 452 } // namespace fileapi |
| OLD | NEW |