OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/fileapi/local_file_writer.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/test_completion_callback.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 using fileapi::LocalFileWriter; |
| 21 |
| 22 class LocalFileWriterTest : public testing::Test { |
| 23 public: |
| 24 virtual void SetUp() OVERRIDE { |
| 25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 26 } |
| 27 |
| 28 protected: |
| 29 FilePath Path(const std::string& name) { |
| 30 return temp_dir_.path().AppendASCII(name); |
| 31 } |
| 32 |
| 33 int WriteStringToWriter(LocalFileWriter* writer, const std::string& data) { |
| 34 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); |
| 35 scoped_refptr<net::DrainableIOBuffer> drainable( |
| 36 new net::DrainableIOBuffer(buffer, buffer->size())); |
| 37 |
| 38 while (drainable->BytesRemaining() > 0) { |
| 39 net::TestCompletionCallback callback; |
| 40 int result = writer->Write(drainable, drainable->BytesRemaining(), |
| 41 callback.callback()); |
| 42 if (result == net::ERR_IO_PENDING) |
| 43 result = callback.WaitForResult(); |
| 44 if (result <= 0) |
| 45 return result; |
| 46 drainable->DidConsume(result); |
| 47 } |
| 48 return net::OK; |
| 49 } |
| 50 |
| 51 std::string GetFileContent(const FilePath& path) { |
| 52 std::string content; |
| 53 file_util::ReadFileToString(path, &content); |
| 54 return content; |
| 55 } |
| 56 |
| 57 FilePath CreateFileWithContent(const std::string& name, |
| 58 const std::string& data) { |
| 59 FilePath path = Path(name); |
| 60 file_util::WriteFile(path, data.c_str(), data.size()); |
| 61 return path; |
| 62 } |
| 63 |
| 64 private: |
| 65 ScopedTempDir temp_dir_; |
| 66 }; |
| 67 |
| 68 void NeverCalled(int) { |
| 69 ADD_FAILURE(); |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 TEST_F(LocalFileWriterTest, Write) { |
| 75 FilePath path = CreateFileWithContent("file_a", ""); |
| 76 scoped_ptr<LocalFileWriter> writer( |
| 77 new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| 78 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 79 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); |
| 80 writer.reset(); |
| 81 EXPECT_TRUE(file_util::PathExists(path)); |
| 82 EXPECT_EQ("foobar", GetFileContent(path)); |
| 83 } |
| 84 |
| 85 TEST_F(LocalFileWriterTest, WriteMiddle) { |
| 86 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 87 scoped_ptr<LocalFileWriter> writer( |
| 88 new LocalFileWriter(path, 2, LocalFileWriter::OPEN)); |
| 89 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 90 writer.reset(); |
| 91 EXPECT_TRUE(file_util::PathExists(path)); |
| 92 EXPECT_EQ("foxxxr", GetFileContent(path)); |
| 93 } |
| 94 |
| 95 TEST_F(LocalFileWriterTest, WriteEnd) { |
| 96 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 97 scoped_ptr<LocalFileWriter> writer( |
| 98 new LocalFileWriter(path, 6, LocalFileWriter::OPEN)); |
| 99 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 100 writer.reset(); |
| 101 EXPECT_TRUE(file_util::PathExists(path)); |
| 102 EXPECT_EQ("foobarxxx", GetFileContent(path)); |
| 103 } |
| 104 |
| 105 TEST_F(LocalFileWriterTest, WriteFailForNonexistingFile) { |
| 106 FilePath path = Path("file_a"); |
| 107 ASSERT_FALSE(file_util::PathExists(path)); |
| 108 scoped_ptr<LocalFileWriter> writer( |
| 109 new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| 110 EXPECT_NE(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 111 writer.reset(); |
| 112 EXPECT_FALSE(file_util::PathExists(path)); |
| 113 } |
| 114 |
| 115 TEST_F(LocalFileWriterTest, CancelWhenNoInflightOperation) { |
| 116 FilePath path = Path("file_a"); |
| 117 scoped_ptr<LocalFileWriter> writer( |
| 118 new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| 119 net::TestCompletionCallback callback; |
| 120 writer->Cancel(callback.callback()); |
| 121 EXPECT_EQ(net::ERR_FAILED, callback.WaitForResult()); |
| 122 } |
| 123 |
| 124 TEST_F(LocalFileWriterTest, CancelWrite) { |
| 125 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 126 scoped_ptr<LocalFileWriter> writer( |
| 127 new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); |
| 128 |
| 129 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); |
| 130 int result = writer->Write(buffer, buffer->size(), base::Bind(&NeverCalled)); |
| 131 ASSERT_EQ(net::ERR_IO_PENDING, result); |
| 132 |
| 133 net::TestCompletionCallback callback; |
| 134 writer->Cancel(callback.callback()); |
| 135 int cancel_result = callback.WaitForResult(); |
| 136 EXPECT_EQ(net::OK, cancel_result); |
| 137 } |
| 138 |
| 139 TEST_F(LocalFileWriterTest, CreateSuccessForNonexistingFile) { |
| 140 FilePath path = Path("file_a"); |
| 141 ASSERT_FALSE(file_util::PathExists(path)); |
| 142 scoped_ptr<LocalFileWriter> writer( |
| 143 new LocalFileWriter(path, 0, LocalFileWriter::CREATE)); |
| 144 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 145 writer.reset(); |
| 146 EXPECT_TRUE(file_util::PathExists(path)); |
| 147 EXPECT_EQ("foo", GetFileContent(path)); |
| 148 } |
| 149 |
| 150 TEST_F(LocalFileWriterTest, CreateClearExistingFile) { |
| 151 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 152 scoped_ptr<LocalFileWriter> writer( |
| 153 new LocalFileWriter(path, 0, LocalFileWriter::CREATE)); |
| 154 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 155 writer.reset(); |
| 156 EXPECT_TRUE(file_util::PathExists(path)); |
| 157 EXPECT_EQ("xxx", GetFileContent(path)); |
| 158 } |
OLD | NEW |