Chromium Code Reviews| Index: webkit/chromeos/fileapi/memory_file_util_unittest.cc |
| diff --git a/webkit/chromeos/fileapi/memory_file_util_unittest.cc b/webkit/chromeos/fileapi/memory_file_util_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..475711449c116e0a0031d62404449a7805a5d416 |
| --- /dev/null |
| +++ b/webkit/chromeos/fileapi/memory_file_util_unittest.cc |
| @@ -0,0 +1,810 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop.h" |
| +#include "base/platform_file.h" |
| +#include "base/time.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/chromeos/fileapi/memory_file_util.h" |
| + |
| +namespace { |
| +const FilePath::CharType kRootPath[] = "/mnt/memory"; |
| +const char kTestString[] = "A test string. A test string."; |
| +const char kTestStringLength = arraysize(kTestString) - 1; |
| +} // namespace |
| + |
| +namespace fileapi { |
| + |
| +// This test is actually testing MemoryFileUtil — an async in-memory file |
| +// system, based on FileUtilAsync. |
| +class MemoryFileUtilTest : public testing::Test { |
| + public: |
| + MemoryFileUtilTest() { |
| + } |
| + |
| + void SetUp() { |
| + file_util_.reset(new MemoryFileUtil(FilePath(kRootPath))); |
| + } |
| + |
| + MemoryFileUtil* file_util() { |
| + return file_util_.get(); |
| + } |
| + |
| + enum CallbackType { |
| + CALLBACK_TYPE_ERROR, |
| + CALLBACK_TYPE_STATUS, |
| + CALLBACK_TYPE_GET_FILE_INFO, |
| + CALLBACK_TYPE_OPEN, |
| + CALLBACK_TYPE_READ_WRITE, |
| + CALLBACK_TYPE_READ_DIRECTORY |
| + }; |
| + |
| + struct CallbackStatus { |
| + CallbackType type; |
| + base::PlatformFileError result; |
| + base::PlatformFileInfo file_info; |
| + // This object should be deleted in the test code. |
| + AsyncFileStream* file_stream; |
| + int64 length; |
| + |
| + // Following 4 fields only for ReadDirectory. |
| + FileUtilAsync::FileList entries; |
| + bool completed; |
| + bool called_after_completed; |
| + int called; |
| + }; |
| + |
| + StatusCallback GetStatusCallback(int request_id) { |
| + if (request_id > max_request_id_) { |
|
kinuko
2011/12/14 03:39:22
Please always omit (or use) the curly braces for s
satorux1
2011/12/14 04:43:26
Done.
|
| + max_request_id_ = request_id; |
| + } |
| + return base::Bind(&MemoryFileUtilTest::StatusCallbackImpl, |
| + base::Unretained(this), |
| + request_id); |
| + } |
| + |
| + FileUtilAsync::GetFileInfoCallback GetGetFileInfoCallback( |
| + int request_id) { |
| + if (request_id > max_request_id_) { |
| + max_request_id_ = request_id; |
| + } |
| + return base::Bind(&MemoryFileUtilTest::GetFileInfoCallback, |
| + base::Unretained(this), |
| + request_id); |
| + } |
| + |
| + FileUtilAsync::OpenCallback GetOpenCallback(int request_id) { |
| + if (request_id > max_request_id_) { |
| + max_request_id_ = request_id; |
| + } |
| + return base::Bind(&MemoryFileUtilTest::OpenCallback, |
| + base::Unretained(this), |
| + request_id); |
| + } |
| + |
| + ReadWriteCallback GetReadWriteCallback(int request_id) { |
| + if (request_id > max_request_id_) { |
| + max_request_id_ = request_id; |
| + } |
| + return base::Bind(&MemoryFileUtilTest::ReadWriteCallbackImpl, |
| + base::Unretained(this), |
| + request_id); |
| + } |
| + |
| + FileUtilAsync::ReadDirectoryCallback GetReadDirectoryCallback( |
| + int request_id) { |
| + if (request_id > max_request_id_) { |
| + max_request_id_ = request_id; |
| + } |
| + return base::Bind(&MemoryFileUtilTest::ReadDirectoryCallback, |
| + base::Unretained(this), |
| + request_id); |
| + } |
| + |
| + int CreateEmptyFile(const FilePath& file_path) { |
| + int request_id = GetNextRequestId(); |
| + file_util_->Create(file_path, GetStatusCallback(request_id)); |
| + return request_id; |
| + } |
| + |
| + int CreateNonEmptyFile(const FilePath& file_path, |
| + const char* data, |
| + int length) { |
| + int request_id = GetNextRequestId(); |
| + file_util_->Open( |
| + file_path, |
| + base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| + base::Bind(&MemoryFileUtilTest::WriteToOpenedFile, |
| + base::Unretained(this), |
| + request_id, data, length)); |
| + return request_id; |
| + } |
| + |
| + CallbackType GetStatusType(int request_id) { |
| + if (status_map_.find(request_id) == status_map_.end()) |
| + return CALLBACK_TYPE_ERROR; |
| + return status_map_[request_id].type; |
| + } |
| + |
| + // Return the operation status. |
| + CallbackStatus& GetStatus(int request_id) { |
| + return status_map_[request_id]; |
| + } |
| + |
| + int StatusQueueSize() { |
| + return status_map_.size(); |
| + } |
| + |
| + int GetNextRequestId() { |
| + return ++max_request_id_; |
| + } |
| + |
| + void set_read_directory_buffer_size(int size) { |
| + file_util_->set_read_directory_buffer_size(size); |
| + } |
| + |
| + private: |
| + void StatusCallbackImpl(int request_id, PlatformFileError result) { |
| + CallbackStatus status; |
| + status.type = CALLBACK_TYPE_STATUS; |
| + status.result = result; |
| + status_map_[request_id] = status; |
| + } |
| + |
| + void GetFileInfoCallback(int request_id, |
| + PlatformFileError result, |
| + const base::PlatformFileInfo& file_info) { |
| + CallbackStatus status; |
| + status.type = CALLBACK_TYPE_GET_FILE_INFO; |
| + status.result = result; |
| + status.file_info = file_info; |
| + status_map_[request_id] = status; |
| + } |
| + |
| + void OpenCallback(int request_id, |
| + PlatformFileError result, |
| + AsyncFileStream* file_stream) { |
| + CallbackStatus status; |
| + status.type = CALLBACK_TYPE_OPEN; |
| + status.result = result; |
| + status.file_stream = file_stream; |
| + status_map_[request_id] = status; |
| + } |
| + |
| + void ReadWriteCallbackImpl(int request_id, |
| + PlatformFileError result, |
| + int64 length) { |
| + CallbackStatus status; |
| + status.type = CALLBACK_TYPE_READ_WRITE; |
| + status.result = result; |
| + status.length = length; |
| + status_map_[request_id] = status; |
| + } |
| + |
| + void ReadDirectoryCallback(int request_id, |
| + PlatformFileError result, |
| + const FileUtilAsync::FileList& entries, |
| + bool completed) { |
| + if (status_map_.find(request_id) == status_map_.end()) { |
| + CallbackStatus status; |
| + status.type = CALLBACK_TYPE_READ_DIRECTORY; |
| + status.called_after_completed = false; |
| + status.result = result; |
| + status.completed = completed; |
| + status.called = 1; |
| + status.entries = entries; |
| + status_map_[request_id] = status; |
| + } else { |
| + CallbackStatus& status = status_map_[request_id]; |
| + if (status.completed) |
| + status.called_after_completed = true; |
| + status.result = result; |
| + status.completed = completed; |
| + ++status.called; |
| + status.entries.insert(status.entries.begin(), entries.begin(), |
| + entries.end()); |
| + } |
| + } |
| + |
| + void WriteToOpenedFile(int request_id, |
| + const char* data, |
| + int length, |
| + PlatformFileError result, |
| + AsyncFileStream* stream) { |
| + CallbackStatus status; |
| + status.type = CALLBACK_TYPE_OPEN; |
| + status.result = result; |
| + status_map_[request_id] = status; |
| + stream->Write(data, length, GetReadWriteCallback(GetNextRequestId())); |
| + } |
| + |
| + scoped_ptr<MemoryFileUtil> file_util_; |
| + std::map<int, CallbackStatus> status_map_; |
| + int max_request_id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MemoryFileUtilTest); |
| +}; |
| + |
| +TEST_F(MemoryFileUtilTest, TestCreateGetFileInfo) { |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test.txt"), |
| + GetGetFileInfoCallback(1)); |
| + |
| + // TODO(olege): In case the file system is truely asynchronous, RunAllPending |
| + // is not enough to wait for answer. In that case the thread should be blocked |
| + // until the callback is called. |
|
kinuko
2011/12/14 03:39:22
Is this really a TODO comment or just a note for o
satorux1
2011/12/14 04:43:26
Looks like a note. Removed TODO, and added some mo
|
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(1)); |
| + CallbackStatus status = GetStatus(1); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status.result); |
| + |
| + base::Time start_create = base::Time::Now(); |
| + |
| + file_util()->Create(FilePath("/mnt/memory/test.txt"), |
|
kinuko
2011/12/14 03:39:22
I prefer using (local) const variables if we refer
satorux1
2011/12/14 04:43:26
Let's keep it as is. file_util() is inlined anyway
|
| + GetStatusCallback(2)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(CALLBACK_TYPE_STATUS, GetStatusType(2)); |
| + status = GetStatus(2); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test.txt"), |
| + GetGetFileInfoCallback(3)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + base::Time end_create = base::Time::Now(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(3)); |
| + status = GetStatus(3); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(0, status.file_info.size); |
| + ASSERT_FALSE(status.file_info.is_directory); |
| + ASSERT_FALSE(status.file_info.is_symbolic_link); |
| + ASSERT_GE(status.file_info.last_modified, start_create); |
| + ASSERT_LE(status.file_info.last_modified, end_create); |
| + ASSERT_GE(status.file_info.creation_time, start_create); |
| + ASSERT_LE(status.file_info.creation_time, end_create); |
| +} |
| + |
| +TEST_F(MemoryFileUtilTest, TestReadWrite) { |
| + // Check that the file does not exist. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(1)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(1)); |
| + CallbackStatus status = GetStatus(1); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status.result); |
| + |
| + // Create & open file for writing. |
| + |
| + base::Time start_create = base::Time::Now(); |
| + file_util()->Open(FilePath("/mnt/memory/test1.txt"), |
| + base::PLATFORM_FILE_CREATE_ALWAYS | |
| + base::PLATFORM_FILE_WRITE, |
| + GetOpenCallback(2)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + base::Time end_create = base::Time::Now(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_OPEN, GetStatusType(2)); |
| + status = GetStatus(2); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + |
| + AsyncFileStream* write_file_stream = status.file_stream; |
| + |
| + // Check that file was created and has 0 size. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(3)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(3)); |
| + status = GetStatus(3); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(0, status.file_info.size); |
| + ASSERT_FALSE(status.file_info.is_directory); |
| + ASSERT_FALSE(status.file_info.is_symbolic_link); |
| + ASSERT_GE(status.file_info.last_modified, start_create); |
| + ASSERT_LE(status.file_info.last_modified, end_create); |
| + |
| + // Write 10 bytes to file. |
| + |
| + base::Time start_write = base::Time::Now(); |
| + write_file_stream->Write(kTestString, 10, |
| + GetReadWriteCallback(4)); |
| + MessageLoop::current()->RunAllPending(); |
| + base::Time end_write = base::Time::Now(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_READ_WRITE, GetStatusType(4)); |
| + status = GetStatus(4); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(10, status.length); |
| + |
| + // Check that the file has now size 10 and correct modification time. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(5)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(5)); |
| + status = GetStatus(5); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(10, status.file_info.size); |
| + ASSERT_GE(status.file_info.last_modified, start_write); |
| + ASSERT_LE(status.file_info.last_modified, end_write); |
| + |
| + // Write the rest of the string to file. |
| + |
| + start_write = base::Time::Now(); |
| + write_file_stream->Write(kTestString + 10, |
| + kTestStringLength - 10, |
| + GetReadWriteCallback(6)); |
| + MessageLoop::current()->RunAllPending(); |
| + end_write = base::Time::Now(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_READ_WRITE, GetStatusType(6)); |
| + status = GetStatus(6); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength) - 10, status.length); |
| + |
| + // Check the file size & modification time. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(7)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(7)); |
| + status = GetStatus(7); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), status.file_info.size); |
| + ASSERT_GE(status.file_info.last_modified, start_write); |
| + ASSERT_LE(status.file_info.last_modified, end_write); |
| + |
| + // Open file for reading. |
| + |
| + file_util()->Open(FilePath("/mnt/memory/test1.txt"), |
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| + GetOpenCallback(8)); |
|
kinuko
2011/12/14 03:39:22
This code would make it hard to add other test sni
satorux1
2011/12/14 04:43:26
Done.
|
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_OPEN, GetStatusType(8)); |
| + status = GetStatus(8); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + |
| + scoped_ptr<AsyncFileStream> read_file_stream(status.file_stream); |
| + |
| + // Read the whole file |
| + char buffer[1024]; |
| + read_file_stream->Read(buffer, 1023, GetReadWriteCallback(9)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_READ_WRITE, GetStatusType(9)); |
| + status = GetStatus(9); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), status.length); |
| + |
| + buffer[status.length] = '\0'; |
| + std::string result_string(buffer); |
| + ASSERT_EQ(kTestString, result_string); |
| + |
| + // Check that size & modification time have not changed. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(10)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(10)); |
| + status = GetStatus(10); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), status.file_info.size); |
| + ASSERT_GE(status.file_info.last_modified, start_write); |
| + ASSERT_LE(status.file_info.last_modified, end_write); |
| + |
| + // Open once more for writing. |
| + |
| + file_util()->Open(FilePath("/mnt/memory/test1.txt"), |
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| + GetOpenCallback(11)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_OPEN, GetStatusType(11)); |
| + status = GetStatus(11); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + |
| + scoped_ptr<AsyncFileStream> write_file_stream2(status.file_stream); |
| + |
| + // Check that the size has not changed. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(12)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_GET_FILE_INFO, GetStatusType(12)); |
| + status = GetStatus(12); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), status.file_info.size); |
| + |
| + // Seek beyond the end of file. Should return error. |
| + |
| + write_file_stream2->Seek(1000, GetStatusCallback(13)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(CALLBACK_TYPE_STATUS, GetStatusType(13)); |
| + status = GetStatus(13); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status.result); |
| + |
| + // Try to write to read-only stream. |
| + |
| + read_file_stream->Write(kTestString, |
| + kTestStringLength, |
| + GetReadWriteCallback(14)); |
| + MessageLoop::current()->RunAllPending(); |
| + status = GetStatus(14); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status.result); |
| + |
| + // Write data overlapping with already written. |
| + |
| + |
| + write_file_stream2->Seek(10, GetStatusCallback(15)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(15).result); |
| + |
| + write_file_stream2->Write(kTestString, |
| + kTestStringLength, |
| + GetReadWriteCallback(16)); |
| + MessageLoop::current()->RunAllPending(); |
| + status = GetStatus(16); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), status.length); |
| + |
| + // Check size. |
| + |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/test1.txt"), |
| + GetGetFileInfoCallback(17)); |
| + MessageLoop::current()->RunAllPending(); |
| + status = GetStatus(17); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength) + 10, |
| + status.file_info.size); |
| + |
| + // Read from 10th byte. |
| + read_file_stream->Seek(10, GetStatusCallback(18)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(18).result); |
| + |
| + read_file_stream->Read(buffer, 1023, GetReadWriteCallback(19)); |
| + MessageLoop::current()->RunAllPending(); |
| + status = GetStatus(19); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, status.result); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), status.length); |
| + buffer[status.length] = '\0'; |
| + std::string result_string2(buffer); |
| + ASSERT_EQ(kTestString, result_string2); |
| +} |
| + |
| +// The directory structure we'll be testing on: |
| +// path size |
| +// |
| +// /mnt/memory/a 0 |
| +// /mnt/memory/b/ |
| +// /mnt/memory/b/c kTestStringLength |
| +// /mnt/memory/b/d/ |
| +// /mnt/memory/b/e 0 |
| +// /mnt/memory/b/f kTestStringLength |
| +// /mnt/memory/b/g/ |
| +// /mnt/memory/b/g/h 0 |
| +// /mnt/memory/b/i kTestStringLength |
| +// /mnt/memory/c/ |
| +// /mnt/memory/longer_file_name.txt kTestStringLength |
|
kinuko
2011/12/14 03:39:22
Can we define these test constants somewhere and m
satorux1
2011/12/14 04:43:26
I thought about it but constants may make the code
satorux1
2011/12/14 04:44:40
I meant variable names.
|
| +TEST_F(MemoryFileUtilTest, TestDirectoryOperations) { |
| + // Check the directory is empty. |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/"), |
| + GetReadDirectoryCallback(1)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(CALLBACK_TYPE_READ_DIRECTORY, GetStatusType(1)); |
| + CallbackStatus& status = GetStatus(1); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(1, status.called); |
| + ASSERT_EQ(0u, status.entries.size()); |
| + |
| + // Create /mnt/memory/a, /mnt/memory/b/, /mnt/memory/longer_file_name.txt, |
| + // /mnt/memory/c/ asyncronously (i.e. we do not wait for each operation to |
| + // complete before starting the next one. |
| + |
| + base::Time start_create = base::Time::Now(); |
| + CreateEmptyFile(FilePath("/mnt/memory/a")); |
| + |
| + int request_id1 = GetNextRequestId(); |
| + file_util()->CreateDirectory(FilePath("/mnt/memory/b"), |
| + GetStatusCallback(request_id1)); |
| + |
| + CreateNonEmptyFile(FilePath("/mnt/memory/longer_file_name.txt"), |
| + kTestString, |
| + kTestStringLength); |
| + |
| + int request_id2 = GetNextRequestId(); |
| + file_util()->CreateDirectory(FilePath("/mnt/memory/c"), |
| + GetStatusCallback(request_id2)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + base::Time end_create = base::Time::Now(); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id2).result); |
| + |
| + // ReadDirectory /mnt/memory, /mnt/memory/a (not a dir), /mnt/memory/b/, |
| + // /mnt/memory/d (not found) |
| + |
| + set_read_directory_buffer_size(5); // Should complete in one go. |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory"), |
| + GetReadDirectoryCallback(request_id1)); |
| + request_id2 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/a"), |
| + GetReadDirectoryCallback(request_id2)); |
| + int request_id3 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/b/"), |
| + GetReadDirectoryCallback(request_id3)); |
| + int request_id4 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/d/"), |
| + GetReadDirectoryCallback(request_id4)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + status = GetStatus(request_id1); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(1, status.called); // Because the number of entries < 5. |
| + ASSERT_EQ(4u, status.entries.size()); |
| + |
| + std::set<FilePath::StringType> seen; |
| + |
| + for (FileUtilAsync::FileList::const_iterator it = status.entries.begin(); |
| + it != status.entries.end(); |
| + ++it) { |
| + ASSERT_LE(start_create, it->last_modified_time); |
| + ASSERT_GE(end_create, it->last_modified_time); |
| + |
| + ASSERT_EQ(seen.end(), seen.find(it->name)); |
| + seen.insert(it->name); |
| + |
| + if (it->name == "a") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(0, it->size); |
| + } else if (it->name == "b") { |
| + ASSERT_TRUE(it->is_directory); |
| + } else if (it->name == "c") { |
| + ASSERT_TRUE(it->is_directory); |
| + } else if (it->name == "longer_file_name.txt") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); |
| + } else { |
| + LOG(ERROR) << "Unexpected file: " << it->name; |
| + ASSERT_TRUE(false); |
| + } |
| + } |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, |
| + GetStatus(request_id2).result); |
| + |
| + status = GetStatus(request_id3); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(1, status.called); |
| + ASSERT_EQ(0u, status.entries.size()); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, GetStatus(request_id4).result); |
| + |
| + // Create files & dirs inside b/: |
| + // /mnt/memory/b/c kTestStringLength |
| + // /mnt/memory/b/d/ |
| + // /mnt/memory/b/e 0 |
| + // /mnt/memory/b/f kTestStringLength |
| + // /mnt/memory/b/g/ |
| + // /mnt/memory/b/i kTestStringLength |
| + // |
| + // /mnt/memory/b/g/h 0 |
| + |
| + start_create = base::Time::Now(); |
| + CreateNonEmptyFile(FilePath("/mnt/memory/b/c"), |
| + kTestString, |
| + kTestStringLength); |
| + request_id1 = GetNextRequestId(); |
| + file_util()->CreateDirectory(FilePath("/mnt/memory/b/d"), |
| + GetStatusCallback(request_id1)); |
| + CreateEmptyFile(FilePath("/mnt/memory/b/e")); |
| + CreateNonEmptyFile(FilePath("/mnt/memory/b/f"), |
| + kTestString, |
| + kTestStringLength); |
| + request_id2 = GetNextRequestId(); |
| + file_util()->CreateDirectory(FilePath("/mnt/memory/b/g"), |
| + GetStatusCallback(request_id1)); |
| + CreateNonEmptyFile(FilePath("/mnt/memory/b/i"), |
| + kTestString, |
| + kTestStringLength); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + CreateEmptyFile(FilePath("/mnt/memory/b/g/h")); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + end_create = base::Time::Now(); |
| + |
| + // Read /mnt/memory and check that the number of entries is unchanged. |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory"), |
| + GetReadDirectoryCallback(request_id1)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + status = GetStatus(request_id1); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(1, status.called); // Because the number of entries < 5. |
| + ASSERT_EQ(4u, status.entries.size()); |
| + |
| + // Read /mnt/memory/b |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/b"), |
| + GetReadDirectoryCallback(request_id1)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + status = GetStatus(request_id1); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(2, status.called); // Because the number of entries > 5. |
| + ASSERT_EQ(6u, status.entries.size()); |
| + |
| + seen.clear(); |
| + |
| + for (FileUtilAsync::FileList::const_iterator it = status.entries.begin(); |
| + it != status.entries.end(); |
| + ++it) { |
| + ASSERT_LE(start_create, it->last_modified_time); |
| + ASSERT_GE(end_create, it->last_modified_time); |
| + |
| + ASSERT_EQ(seen.end(), seen.find(it->name)); |
| + seen.insert(it->name); |
| + |
| + // /mnt/memory/b/c kTestStringLength |
| + // /mnt/memory/b/d/ |
| + // /mnt/memory/b/e 0 |
| + // /mnt/memory/b/f kTestStringLength |
| + // /mnt/memory/b/g/ |
| + // /mnt/memory/b/i kTestStringLength |
| + |
| + if (it->name == "c") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); |
| + } else if (it->name == "d") { |
| + ASSERT_TRUE(it->is_directory); |
| + } else if (it->name == "e") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(0, it->size); |
| + } else if (it->name == "f") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); |
| + } else if (it->name == "g") { |
| + ASSERT_TRUE(it->is_directory); |
| + } else if (it->name == "i") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); |
| + } else { |
| + LOG(ERROR) << "Unexpected file: " << it->name; |
| + ASSERT_TRUE(false); |
| + } |
| + } |
| + |
| + // Remove single file: /mnt/memory/b/f |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->Remove(FilePath("/mnt/memory/b/f"), false /* recursive */, |
| + GetStatusCallback(request_id1)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + |
| + // Check the number of files in b/ |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/b"), |
| + GetReadDirectoryCallback(request_id1)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + status = GetStatus(request_id1); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(5u, status.entries.size()); |
| + |
| + // Try remove /mnt/memory/b non-recursively (error) |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->Remove(FilePath("/mnt/memory/b"), false /* recursive */, |
| + GetStatusCallback(request_id1)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, |
| + GetStatus(request_id1).result); |
| + |
| + // Non-recursively remove empty directory. |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->Remove(FilePath("/mnt/memory/b/d"), false, |
| + GetStatusCallback(request_id1)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->GetFileInfo(FilePath("/mnt/memory/b/d"), |
| + GetGetFileInfoCallback(request_id1)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, GetStatus(request_id1).result); |
| + |
| + // Remove /mnt/memory/b recursively. |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->Remove(FilePath("/mnt/memory/b"), true /* recursive */, |
| + GetStatusCallback(request_id1)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + |
| + // ReadDirectory /mnt/memory/b -> not found |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory/b"), |
| + GetReadDirectoryCallback(request_id1)); |
| + MessageLoop::current()->RunAllPending(); |
| + ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, GetStatus(request_id1).result); |
| + |
| + // ReadDirectory /mnt/memory |
| + |
| + request_id1 = GetNextRequestId(); |
| + file_util()->ReadDirectory(FilePath("/mnt/memory"), |
| + GetReadDirectoryCallback(request_id1)); |
| + |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, GetStatus(request_id1).result); |
| + status = GetStatus(request_id1); |
| + ASSERT_TRUE(status.completed); |
| + ASSERT_FALSE(status.called_after_completed); |
| + ASSERT_EQ(3u, status.entries.size()); |
| + |
| + seen.clear(); |
| + |
| + for (FileUtilAsync::FileList::const_iterator it = status.entries.begin(); |
| + it != status.entries.end(); |
| + ++it) { |
| + ASSERT_EQ(seen.end(), seen.find(it->name)); |
| + seen.insert(it->name); |
| + |
| + if (it->name == "a") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(0, it->size); |
| + } else if (it->name == "c") { |
| + ASSERT_TRUE(it->is_directory); |
| + } else if (it->name == "longer_file_name.txt") { |
| + ASSERT_FALSE(it->is_directory); |
| + ASSERT_EQ(static_cast<int64>(kTestStringLength), it->size); |
| + } else { |
| + LOG(ERROR) << "Unexpected file: " << it->name; |
| + ASSERT_TRUE(false); |
| + } |
| + } |
| +} |
| + |
| +} // namespace fileapi |