Index: webkit/fileapi/local_file_writer_unittest.cc |
diff --git a/webkit/fileapi/local_file_writer_unittest.cc b/webkit/fileapi/local_file_writer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fcd1de028abbe6306b2d086a1cfeca2b7825427e |
--- /dev/null |
+++ b/webkit/fileapi/local_file_writer_unittest.cc |
@@ -0,0 +1,97 @@ |
+// Copyright (c) 2012 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 "webkit/fileapi/local_file_writer.h" |
+ |
+#include <string> |
+ |
+#include "base/callback.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/scoped_temp_dir.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "net/base/io_buffer.h" |
+#include "net/base/test_completion_callback.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+using fileapi::LocalFileWriter; |
+ |
+class LocalFileWriterTest : public testing::Test { |
+ public: |
+ virtual void SetUp() OVERRIDE { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ } |
kinuko
2012/04/20 11:26:37
Everything below this could be protected methods.
kinaba
2012/04/23 08:56:41
Done.
|
+ FilePath Path(const std::string& name) { |
+ return temp_dir_.path().AppendASCII(name); |
+ } |
+ void WriteStringToWriter(LocalFileWriter* writer, const std::string& data) { |
+ scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); |
+ scoped_refptr<net::DrainableIOBuffer> drainable( |
+ new net::DrainableIOBuffer(buffer, buffer->size())); |
+ |
+ while (drainable->BytesRemaining() > 0) { |
+ net::TestCompletionCallback callback; |
+ int result = writer->Write(drainable, drainable->BytesRemaining(), |
+ callback.callback()); |
+ if (result == net::ERR_IO_PENDING) |
+ result = callback.WaitForResult(); |
+ EXPECT_GE(result, 0); |
+ drainable->DidConsume(result); |
+ } |
+ } |
+ void SeekWriter(LocalFileWriter* writer, int64 offset) { |
+ net::TestInt64CompletionCallback callback; |
+ int result = writer->Seek(offset, callback.callback()); |
+ EXPECT_EQ(net::ERR_IO_PENDING, result); |
+ int64 after_seek = callback.WaitForResult(); |
+ EXPECT_EQ(after_seek, offset); |
+ } |
+ std::string FileContent(const FilePath& path) { |
kinuko
2012/04/20 11:26:37
naming-nit: GetFileContent
kinaba
2012/04/23 08:56:41
Done.
|
+ std::string content; |
+ file_util::ReadFileToString(path, &content); |
+ return content; |
+ } |
+ FilePath CreateEmptyFile(const std::string& name, const std::string& data) { |
+ FilePath path = Path(name); |
+ file_util::WriteFile(path, data.c_str(), data.size()); |
kinuko
2012/04/20 11:26:37
'Empty'? :)
kinaba
2012/04/23 08:56:41
Surely not!
|
+ return path; |
+ } |
+ |
+ private: |
+ ScopedTempDir temp_dir_; |
+}; |
+ |
+} // namespace |
+ |
+TEST_F(LocalFileWriterTest, Write) { |
+ FilePath path = CreateEmptyFile("file_a", ""); |
+ scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path)); |
+ WriteStringToWriter(writer.get(), "foo"); |
+ WriteStringToWriter(writer.get(), "bar"); |
+ writer.reset(); |
+ EXPECT_TRUE(file_util::PathExists(path)); |
+ EXPECT_EQ("foobar", FileContent(path)); |
+} |
+ |
+TEST_F(LocalFileWriterTest, Seek) { |
+ FilePath path = CreateEmptyFile("file_a", "foobar"); |
+ scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path)); |
+ SeekWriter(writer.get(), 2); |
+ WriteStringToWriter(writer.get(), "xxx"); |
+ writer.reset(); |
+ EXPECT_TRUE(file_util::PathExists(path)); |
+ EXPECT_EQ("foxxxr", FileContent(path)); |
+} |
+ |
+TEST_F(LocalFileWriterTest, SeekEnd) { |
+ FilePath path = CreateEmptyFile("file_a", "foobar"); |
+ scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path)); |
+ SeekWriter(writer.get(), 6); |
+ WriteStringToWriter(writer.get(), "buz"); |
+ writer.reset(); |
+ EXPECT_TRUE(file_util::PathExists(path)); |
+ EXPECT_EQ("foobarbuz", FileContent(path)); |
+} |