Chromium Code Reviews| 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: | |
|
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!
| |
| 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 } | |
|
kinuko
2012/04/23 12:50:29
You may want to run RunAllPending() after resettin
kinaba
2012/04/24 07:35:04
Done.
| |
| 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")); | |
|
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.
| |
| 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 EXPECT_EQ(net::ERR_UNEXPECTED, writer->Cancel(base::Bind(&NeverCalled))); | |
| 120 } | |
| 121 | |
| 122 TEST_F(LocalFileWriterTest, CancelWrite) { | |
| 123 FilePath path = CreateFileWithContent("file_a", "foobar"); | |
| 124 scoped_ptr<LocalFileWriter> writer( | |
| 125 new LocalFileWriter(path, 0, LocalFileWriter::OPEN)); | |
| 126 | |
| 127 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); | |
| 128 int result = writer->Write(buffer, buffer->size(), base::Bind(&NeverCalled)); | |
| 129 ASSERT_EQ(net::ERR_IO_PENDING, result); | |
| 130 | |
| 131 net::TestCompletionCallback callback; | |
| 132 writer->Cancel(callback.callback()); | |
| 133 int cancel_result = callback.WaitForResult(); | |
| 134 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
| |
| 135 } | |
| 136 | |
| 137 TEST_F(LocalFileWriterTest, CreateSuccessForNonexistingFile) { | |
| 138 FilePath path = Path("file_a"); | |
| 139 ASSERT_FALSE(file_util::PathExists(path)); | |
| 140 scoped_ptr<LocalFileWriter> writer( | |
| 141 new LocalFileWriter(path, 0, LocalFileWriter::CREATE)); | |
| 142 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | |
| 143 writer.reset(); | |
| 144 EXPECT_TRUE(file_util::PathExists(path)); | |
| 145 EXPECT_EQ("foo", GetFileContent(path)); | |
| 146 } | |
| 147 | |
| 148 TEST_F(LocalFileWriterTest, CreateClearExistingFile) { | |
| 149 FilePath path = CreateFileWithContent("file_a", "foobar"); | |
| 150 scoped_ptr<LocalFileWriter> writer( | |
| 151 new LocalFileWriter(path, 0, LocalFileWriter::CREATE)); | |
| 152 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | |
| 153 writer.reset(); | |
| 154 EXPECT_TRUE(file_util::PathExists(path)); | |
| 155 EXPECT_EQ("xxx", GetFileContent(path)); | |
| 156 } | |
| OLD | NEW |