Chromium Code Reviews| 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..2a86e610caaff25e83b22f0bfc474a73735ee868 |
| --- /dev/null |
| +++ b/webkit/fileapi/local_file_writer_unittest.cc |
| @@ -0,0 +1,156 @@ |
| +// 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()); |
| + } |
| + |
| + protected: |
| + FilePath Path(const std::string& name) { |
| + return temp_dir_.path().AppendASCII(name); |
| + } |
| + |
| + int 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(); |
| + if (result <= 0) |
| + return result; |
| + drainable->DidConsume(result); |
| + } |
| + return net::OK; |
| + } |
| + |
| + std::string GetFileContent(const FilePath& path) { |
| + std::string content; |
| + file_util::ReadFileToString(path, &content); |
| + return content; |
| + } |
| + |
| + FilePath CreateFileWithContent(const std::string& name, |
| + const std::string& data) { |
| + FilePath path = Path(name); |
| + file_util::WriteFile(path, data.c_str(), data.size()); |
| + return path; |
| + } |
| + |
| + private: |
|
kinuko
2012/04/23 12:50:29
I think you'll need to run this test in unit_tests
kinaba
2012/04/24 07:35:04
Done. Thanks for the advice!
|
| + ScopedTempDir temp_dir_; |
| +}; |
| + |
| +void NeverCalled(int) { |
| + ADD_FAILURE(); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST_F(LocalFileWriterTest, Write) { |
| + FilePath path = CreateFileWithContent("file_a", ""); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| + EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| + EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); |
| + writer.reset(); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + EXPECT_EQ("foobar", GetFileContent(path)); |
| +} |
|
kinuko
2012/04/23 12:50:29
You may want to run RunAllPending() after resettin
kinaba
2012/04/24 07:35:04
Done.
|
| + |
| +TEST_F(LocalFileWriterTest, WriteMiddle) { |
| + FilePath path = CreateFileWithContent("file_a", "foobar"); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 2, LocalFileWriter::OPEN)); |
| + EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| + writer.reset(); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + EXPECT_EQ("foxxxr", GetFileContent(path)); |
| +} |
| + |
| +TEST_F(LocalFileWriterTest, WriteEnd) { |
| + FilePath path = CreateFileWithContent("file_a", "foobar"); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 6, LocalFileWriter::OPEN)); |
| + EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| + writer.reset(); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + EXPECT_EQ("foobarxxx", GetFileContent(path)); |
| +} |
| + |
| +TEST_F(LocalFileWriterTest, WriteFailForNonexistingFile) { |
| + FilePath path = Path("file_a"); |
| + ASSERT_FALSE(file_util::PathExists(path)); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| + EXPECT_NE(net::OK, WriteStringToWriter(writer.get(), "foo")); |
|
kinuko
2012/04/23 12:50:29
Would be nice to compare the return code with a sp
kinaba
2012/04/24 07:35:04
Done.
|
| + writer.reset(); |
| + EXPECT_FALSE(file_util::PathExists(path)); |
| +} |
| + |
| +TEST_F(LocalFileWriterTest, CancelWhenNoInflightOperation) { |
| + FilePath path = Path("file_a"); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| + EXPECT_EQ(net::ERR_UNEXPECTED, writer->Cancel(base::Bind(&NeverCalled))); |
| +} |
| + |
| +TEST_F(LocalFileWriterTest, CancelWrite) { |
| + FilePath path = CreateFileWithContent("file_a", "foobar"); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| + |
| + scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); |
| + int result = writer->Write(buffer, buffer->size(), base::Bind(&NeverCalled)); |
| + ASSERT_EQ(net::ERR_IO_PENDING, result); |
| + |
| + net::TestCompletionCallback callback; |
| + writer->Cancel(callback.callback()); |
| + int cancel_result = callback.WaitForResult(); |
| + EXPECT_EQ(net::OK, cancel_result); |
|
kinuko
2012/04/23 12:50:29
Could we also test calling Cancel when there's no
kinaba
2012/04/24 07:35:04
I meant CancelWhenNoInflightOperation (Line 115) t
|
| +} |
| + |
| +TEST_F(LocalFileWriterTest, CreateSuccessForNonexistingFile) { |
| + FilePath path = Path("file_a"); |
| + ASSERT_FALSE(file_util::PathExists(path)); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 0, LocalFileWriter::CREATE)); |
| + EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| + writer.reset(); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + EXPECT_EQ("foo", GetFileContent(path)); |
| +} |
| + |
| +TEST_F(LocalFileWriterTest, CreateClearExistingFile) { |
| + FilePath path = CreateFileWithContent("file_a", "foobar"); |
| + scoped_ptr<LocalFileWriter> writer( |
| + new LocalFileWriter(path, 0, LocalFileWriter::CREATE)); |
| + EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| + writer.reset(); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + EXPECT_EQ("xxx", GetFileContent(path)); |
| +} |