Chromium Code Reviews| Index: webkit/fileapi/file_system_file_stream_reader_unittest.cc |
| diff --git a/webkit/blob/local_file_stream_reader_unittest.cc b/webkit/fileapi/file_system_file_stream_reader_unittest.cc |
| similarity index 42% |
| copy from webkit/blob/local_file_stream_reader_unittest.cc |
| copy to webkit/fileapi/file_system_file_stream_reader_unittest.cc |
| index 4087037ba80ec5df25315b1637f6115c56dc28e1..7710908592622d9780c4c5141b920f88e5e0c69d 100644 |
| --- a/webkit/blob/local_file_stream_reader_unittest.cc |
| +++ b/webkit/fileapi/file_system_file_stream_reader_unittest.cc |
| @@ -2,31 +2,38 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "webkit/blob/local_file_stream_reader.h" |
| +#include "webkit/fileapi/file_system_file_stream_reader.h" |
| #include <string> |
| -#include "base/file_path.h" |
| -#include "base/file_util.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop.h" |
| #include "base/platform_file.h" |
| #include "base/scoped_temp_dir.h" |
| -#include "base/threading/thread.h" |
| #include "net/base/io_buffer.h" |
| #include "net/base/net_errors.h" |
| #include "net/base/test_completion_callback.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/fileapi/file_system_context.h" |
| +#include "webkit/fileapi/file_system_file_util.h" |
| +#include "webkit/fileapi/file_system_operation_context.h" |
| +#include "webkit/fileapi/file_system_task_runners.h" |
| +#include "webkit/fileapi/mock_file_system_options.h" |
| +#include "webkit/fileapi/sandbox_mount_point_provider.h" |
| +#include "webkit/quota/mock_special_storage_policy.h" |
| -namespace webkit_blob { |
| +namespace fileapi { |
| namespace { |
| +const char kURLOrigin[] = "http://remote/"; |
| +const char kTestFileName[] = "test.dat"; |
| const char kTestData[] = "0123456789"; |
| const int kTestDataSize = arraysize(kTestData) - 1; |
| -void ReadFromReader(LocalFileStreamReader* reader, |
| - std::string* data, size_t size, |
| +void ReadFromReader(FileSystemFileStreamReader* reader, |
| + std::string* data, |
| + size_t size, |
| int* result) { |
| ASSERT_TRUE(reader != NULL); |
| ASSERT_TRUE(result != NULL); |
| @@ -49,83 +56,108 @@ void ReadFromReader(LocalFileStreamReader* reader, |
| } |
| void NeverCalled(int) { ADD_FAILURE(); } |
| -void EmptyCallback() {} |
| - |
| -void QuitLoop() { |
| - MessageLoop::current()->Quit(); |
| -} |
| } // namespace |
| -class LocalFileStreamReaderTest : public testing::Test { |
| +class FileSystemFileStreamReaderTest : public testing::Test { |
| public: |
| - LocalFileStreamReaderTest() |
| - : message_loop_(MessageLoop::TYPE_IO), |
| - file_thread_("FileUtilProxyTestFileThread") {} |
| + FileSystemFileStreamReaderTest() |
| + : message_loop_(MessageLoop::TYPE_IO) {} |
| virtual void SetUp() OVERRIDE { |
| - ASSERT_TRUE(file_thread_.Start()); |
| - ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + |
| + file_system_context_ = |
| + new FileSystemContext( |
| + FileSystemTaskRunners::CreateMockTaskRunners(), |
| + new quota::MockSpecialStoragePolicy, |
| + NULL, |
| + temp_dir_.path(), |
| + CreateDisallowFileAccessOptions()); |
| + |
| + file_system_context_->sandbox_provider()->ValidateFileSystemRoot( |
| + GURL(kURLOrigin), kFileSystemTypeTemporary, true, // create |
| + base::Bind(&OnValidateFileSystem)); |
| + MessageLoop::current()->RunAllPending(); |
| - file_util::WriteFile(test_path(), kTestData, kTestDataSize); |
| - base::PlatformFileInfo info; |
| - ASSERT_TRUE(file_util::GetFileInfo(test_path(), &info)); |
| - test_file_modification_time_ = info.last_modified; |
| + WriteFile(kTestFileName, kTestData, kTestDataSize, |
| + &test_file_modification_time_); |
| } |
| virtual void TearDown() OVERRIDE { |
| - // Give another chance for deleted streams to perform Close. |
| MessageLoop::current()->RunAllPending(); |
|
kinuko
2012/10/15 07:24:07
Before submitting can you make sure FileStream::Cl
hashimoto
2012/10/15 08:05:04
Confirmed it locally and will try with bots.
Since
|
| - file_thread_.Stop(); |
| } |
| protected: |
| - LocalFileStreamReader* CreateFileReader( |
| - const FilePath& path, |
| + FileSystemFileStreamReader* CreateFileReader( |
| + const std::string& file_name, |
| int64 initial_offset, |
| const base::Time& expected_modification_time) { |
| - return new LocalFileStreamReader( |
| - file_task_runner(), |
| - path, |
| - initial_offset, |
| - expected_modification_time); |
| + return new FileSystemFileStreamReader(file_system_context_, |
| + GetFileSystemURL(file_name), |
| + initial_offset, |
| + expected_modification_time); |
| } |
| - void TouchTestFile() { |
| - base::Time new_modified_time = |
| - test_file_modification_time() - base::TimeDelta::FromSeconds(1); |
| - ASSERT_TRUE(file_util::TouchFile(test_path(), |
| - test_file_modification_time(), |
| - new_modified_time)); |
| + base::Time test_file_modification_time() const { |
| + return test_file_modification_time_; |
| } |
| - base::MessageLoopProxy* file_task_runner() const { |
| - return file_thread_.message_loop_proxy().get(); |
| + void WriteFile(const std::string& file_name, |
| + const char* buf, |
| + int buf_size, |
| + base::Time* modification_time) { |
| + FileSystemFileUtil* file_util = file_system_context_-> |
| + sandbox_provider()->GetFileUtil(kFileSystemTypeTemporary); |
| + FileSystemURL url = GetFileSystemURL(file_name); |
| + |
| + FileSystemOperationContext context(file_system_context_); |
| + context.set_allowed_bytes_growth(1024); |
| + |
| + base::PlatformFile handle = base::kInvalidPlatformFileValue; |
| + bool created = false; |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->CreateOrOpen( |
| + &context, |
| + url, |
| + base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| + &handle, |
| + &created)); |
| + EXPECT_TRUE(created); |
| + ASSERT_NE(base::kInvalidPlatformFileValue, handle); |
| + ASSERT_EQ(buf_size, |
| + base::WritePlatformFile(handle, 0 /* offset */, buf, buf_size)); |
| + base::ClosePlatformFile(handle); |
| + |
| + base::PlatformFileInfo file_info; |
| + FilePath platform_path; |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, |
| + file_util->GetFileInfo(&context, url, &file_info, |
| + &platform_path)); |
| + if (modification_time) |
| + *modification_time = file_info.last_modified; |
| } |
| - FilePath test_dir() const { return dir_.path(); } |
| - FilePath test_path() const { return dir_.path().AppendASCII("test"); } |
| - base::Time test_file_modification_time() const { |
| - return test_file_modification_time_; |
| + private: |
| + static void OnValidateFileSystem(base::PlatformFileError result) { |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, result); |
| } |
| - void EnsureFileTaskFinished() { |
| - file_task_runner()->PostTaskAndReply( |
| - FROM_HERE, base::Bind(&EmptyCallback), base::Bind(&QuitLoop)); |
| - MessageLoop::current()->Run(); |
| + FileSystemURL GetFileSystemURL(const std::string& file_name) { |
| + return FileSystemURL(GURL(kURLOrigin), |
| + kFileSystemTypeTemporary, |
| + FilePath().AppendASCII(file_name)); |
| } |
| - private: |
| MessageLoop message_loop_; |
| - base::Thread file_thread_; |
| - ScopedTempDir dir_; |
| + ScopedTempDir temp_dir_; |
| + scoped_refptr<FileSystemContext> file_system_context_; |
| base::Time test_file_modification_time_; |
| }; |
| -TEST_F(LocalFileStreamReaderTest, NonExistent) { |
| - FilePath nonexistent_path = test_dir().AppendASCII("nonexistent"); |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(nonexistent_path, 0, base::Time())); |
| +TEST_F(FileSystemFileStreamReaderTest, NonExistent) { |
| + const char kFileName[] = "nonexistent"; |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kFileName, 0, base::Time())); |
| int result = 0; |
| std::string data; |
| ReadFromReader(reader.get(), &data, 10, &result); |
| @@ -133,19 +165,12 @@ TEST_F(LocalFileStreamReaderTest, NonExistent) { |
| ASSERT_EQ(0U, data.size()); |
| } |
| -TEST_F(LocalFileStreamReaderTest, Empty) { |
| - FilePath empty_path = test_dir().AppendASCII("empty"); |
| - base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| - base::PlatformFile file = base::CreatePlatformFile( |
| - empty_path, |
| - base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| - NULL, &error); |
| - ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| - ASSERT_NE(base::kInvalidPlatformFileValue, file); |
| - base::ClosePlatformFile(file); |
| - |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(empty_path, 0, base::Time())); |
| +TEST_F(FileSystemFileStreamReaderTest, Empty) { |
| + const char kFileName[] = "empty"; |
| + WriteFile(kFileName, NULL, 0, NULL); |
| + |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kFileName, 0, base::Time())); |
| int result = 0; |
| std::string data; |
| ReadFromReader(reader.get(), &data, 10, &result); |
| @@ -159,9 +184,9 @@ TEST_F(LocalFileStreamReaderTest, Empty) { |
| ASSERT_EQ(0, result); |
| } |
| -TEST_F(LocalFileStreamReaderTest, GetLengthNormal) { |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 0, test_file_modification_time())); |
| +TEST_F(FileSystemFileStreamReaderTest, GetLengthNormal) { |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 0, test_file_modification_time())); |
| net::TestInt64CompletionCallback callback; |
| int result = reader->GetLength(callback.callback()); |
| if (result == net::ERR_IO_PENDING) |
| @@ -169,13 +194,13 @@ TEST_F(LocalFileStreamReaderTest, GetLengthNormal) { |
| ASSERT_EQ(kTestDataSize, result); |
| } |
| -TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { |
| - // Touch file so that the file's modification time becomes different |
| - // from what we expect. |
| - TouchTestFile(); |
| +TEST_F(FileSystemFileStreamReaderTest, GetLengthAfterModified) { |
| + // Pass a fake expected modifictaion time so that the expectation fails. |
| + base::Time fake_expected_modification_time = |
| + test_file_modification_time() - base::TimeDelta::FromSeconds(10); |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 0, test_file_modification_time())); |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 0, fake_expected_modification_time)); |
| net::TestInt64CompletionCallback callback; |
| int result = reader->GetLength(callback.callback()); |
| if (result == net::ERR_IO_PENDING) |
| @@ -183,16 +208,16 @@ TEST_F(LocalFileStreamReaderTest, GetLengthAfterModified) { |
| ASSERT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
| // With NULL expected modification time this should work. |
| - reader.reset(CreateFileReader(test_path(), 0, base::Time())); |
| + reader.reset(CreateFileReader(kTestFileName, 0, base::Time())); |
| result = reader->GetLength(callback.callback()); |
| if (result == net::ERR_IO_PENDING) |
| result = callback.WaitForResult(); |
| ASSERT_EQ(kTestDataSize, result); |
| } |
| -TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) { |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 3, base::Time())); |
| +TEST_F(FileSystemFileStreamReaderTest, GetLengthWithOffset) { |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 3, base::Time())); |
| net::TestInt64CompletionCallback callback; |
| int result = reader->GetLength(callback.callback()); |
| if (result == net::ERR_IO_PENDING) |
| @@ -201,9 +226,9 @@ TEST_F(LocalFileStreamReaderTest, GetLengthWithOffset) { |
| ASSERT_EQ(kTestDataSize, result); |
| } |
| -TEST_F(LocalFileStreamReaderTest, ReadNormal) { |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 0, test_file_modification_time())); |
| +TEST_F(FileSystemFileStreamReaderTest, ReadNormal) { |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 0, test_file_modification_time())); |
| int result = 0; |
| std::string data; |
| ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| @@ -211,13 +236,13 @@ TEST_F(LocalFileStreamReaderTest, ReadNormal) { |
| ASSERT_EQ(kTestData, data); |
| } |
| -TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { |
| - // Touch file so that the file's modification time becomes different |
| - // from what we expect. |
| - TouchTestFile(); |
| +TEST_F(FileSystemFileStreamReaderTest, ReadAfterModified) { |
| + // Pass a fake expected modifictaion time so that the expectation fails. |
| + base::Time fake_expected_modification_time = |
| + test_file_modification_time() - base::TimeDelta::FromSeconds(10); |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 0, test_file_modification_time())); |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 0, fake_expected_modification_time)); |
| int result = 0; |
| std::string data; |
| ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| @@ -226,15 +251,15 @@ TEST_F(LocalFileStreamReaderTest, ReadAfterModified) { |
| // With NULL expected modification time this should work. |
| data.clear(); |
| - reader.reset(CreateFileReader(test_path(), 0, base::Time())); |
| + reader.reset(CreateFileReader(kTestFileName, 0, base::Time())); |
| ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| ASSERT_EQ(net::OK, result); |
| ASSERT_EQ(kTestData, data); |
| } |
| -TEST_F(LocalFileStreamReaderTest, ReadWithOffset) { |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 3, base::Time())); |
| +TEST_F(FileSystemFileStreamReaderTest, ReadWithOffset) { |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 3, base::Time())); |
| int result = 0; |
| std::string data; |
| ReadFromReader(reader.get(), &data, kTestDataSize, &result); |
| @@ -242,9 +267,9 @@ TEST_F(LocalFileStreamReaderTest, ReadWithOffset) { |
| ASSERT_EQ(&kTestData[3], data); |
| } |
| -TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) { |
| - scoped_ptr<LocalFileStreamReader> reader( |
| - CreateFileReader(test_path(), 0, base::Time())); |
| +TEST_F(FileSystemFileStreamReaderTest, DeleteWithUnfinishedRead) { |
| + scoped_ptr<FileSystemFileStreamReader> reader( |
| + CreateFileReader(kTestFileName, 0, base::Time())); |
| net::TestCompletionCallback callback; |
| scoped_refptr<net::IOBufferWithSize> buf( |
| @@ -255,7 +280,6 @@ TEST_F(LocalFileStreamReaderTest, DeleteWithUnfinishedRead) { |
| // Delete immediately. |
| // Should not crash; nor should NeverCalled be callback. |
| reader.reset(); |
| - EnsureFileTaskFinished(); |
| } |
| -} // namespace webkit_blob |
| +} // namespace fileapi |