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 // NOTE: These tests are run as part of "unit_tests" (in chrome/test) rather |
| 6 // than as part of test_shell_tests because they rely on being able to |
| 7 // instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses TYPE_UI, |
| 8 // which net::TestCompletionCallback doesn't allow. |
| 9 |
| 10 #include "webkit/fileapi/local_file_writer.h" |
| 11 |
| 12 #include <string> |
| 13 |
| 14 #include "base/callback.h" |
| 15 #include "base/file_util.h" |
| 16 #include "base/logging.h" |
| 17 #include "base/message_loop.h" |
| 18 #include "base/scoped_temp_dir.h" |
| 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "net/base/io_buffer.h" |
| 21 #include "net/base/test_completion_callback.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 using fileapi::LocalFileWriter; |
| 27 |
| 28 class LocalFileWriterTest : public testing::Test { |
| 29 public: |
| 30 LocalFileWriterTest() : message_loop_(MessageLoop::TYPE_IO) {} |
| 31 |
| 32 virtual void SetUp() OVERRIDE { |
| 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 34 } |
| 35 |
| 36 protected: |
| 37 FilePath Path(const std::string& name) { |
| 38 return temp_dir_.path().AppendASCII(name); |
| 39 } |
| 40 |
| 41 int WriteStringToWriter(LocalFileWriter* writer, const std::string& data) { |
| 42 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); |
| 43 scoped_refptr<net::DrainableIOBuffer> drainable( |
| 44 new net::DrainableIOBuffer(buffer, buffer->size())); |
| 45 |
| 46 while (drainable->BytesRemaining() > 0) { |
| 47 net::TestCompletionCallback callback; |
| 48 int result = writer->Write(drainable, drainable->BytesRemaining(), |
| 49 callback.callback()); |
| 50 if (result == net::ERR_IO_PENDING) |
| 51 result = callback.WaitForResult(); |
| 52 if (result <= 0) |
| 53 return result; |
| 54 drainable->DidConsume(result); |
| 55 } |
| 56 return net::OK; |
| 57 } |
| 58 |
| 59 std::string GetFileContent(const FilePath& path) { |
| 60 std::string content; |
| 61 file_util::ReadFileToString(path, &content); |
| 62 return content; |
| 63 } |
| 64 |
| 65 FilePath CreateFileWithContent(const std::string& name, |
| 66 const std::string& data) { |
| 67 FilePath path = Path(name); |
| 68 file_util::WriteFile(path, data.c_str(), data.size()); |
| 69 return path; |
| 70 } |
| 71 |
| 72 private: |
| 73 MessageLoop message_loop_; |
| 74 ScopedTempDir temp_dir_; |
| 75 }; |
| 76 |
| 77 void NeverCalled(int) { |
| 78 ADD_FAILURE(); |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
| 83 TEST_F(LocalFileWriterTest, Write) { |
| 84 FilePath path = CreateFileWithContent("file_a", ""); |
| 85 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 0)); |
| 86 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 87 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); |
| 88 writer.reset(); |
| 89 MessageLoop::current()->RunAllPending(); |
| 90 EXPECT_TRUE(file_util::PathExists(path)); |
| 91 EXPECT_EQ("foobar", GetFileContent(path)); |
| 92 } |
| 93 |
| 94 TEST_F(LocalFileWriterTest, WriteMiddle) { |
| 95 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 96 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 2)); |
| 97 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 98 writer.reset(); |
| 99 MessageLoop::current()->RunAllPending(); |
| 100 EXPECT_TRUE(file_util::PathExists(path)); |
| 101 EXPECT_EQ("foxxxr", GetFileContent(path)); |
| 102 } |
| 103 |
| 104 TEST_F(LocalFileWriterTest, WriteEnd) { |
| 105 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 106 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 6)); |
| 107 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
| 108 writer.reset(); |
| 109 MessageLoop::current()->RunAllPending(); |
| 110 EXPECT_TRUE(file_util::PathExists(path)); |
| 111 EXPECT_EQ("foobarxxx", GetFileContent(path)); |
| 112 } |
| 113 |
| 114 TEST_F(LocalFileWriterTest, WriteFailForNonexistingFile) { |
| 115 FilePath path = Path("file_a"); |
| 116 ASSERT_FALSE(file_util::PathExists(path)); |
| 117 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 0)); |
| 118 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); |
| 119 writer.reset(); |
| 120 MessageLoop::current()->RunAllPending(); |
| 121 EXPECT_FALSE(file_util::PathExists(path)); |
| 122 } |
| 123 |
| 124 TEST_F(LocalFileWriterTest, CancelBeforeOperation) { |
| 125 FilePath path = Path("file_a"); |
| 126 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 0)); |
| 127 // Cancel immediately fails when there's no in-flight operation. |
| 128 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); |
| 129 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
| 130 } |
| 131 |
| 132 TEST_F(LocalFileWriterTest, CancelAfterFinishedOperation) { |
| 133 FilePath path = CreateFileWithContent("file_a", ""); |
| 134 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 0)); |
| 135 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
| 136 |
| 137 // Cancel immediately fails when there's no in-flight operation. |
| 138 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); |
| 139 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
| 140 |
| 141 writer.reset(); |
| 142 MessageLoop::current()->RunAllPending(); |
| 143 // Write operation is already completed. |
| 144 EXPECT_TRUE(file_util::PathExists(path)); |
| 145 EXPECT_EQ("foo", GetFileContent(path)); |
| 146 } |
| 147 |
| 148 TEST_F(LocalFileWriterTest, CancelWrite) { |
| 149 FilePath path = CreateFileWithContent("file_a", "foobar"); |
| 150 scoped_ptr<LocalFileWriter> writer(new LocalFileWriter(path, 0)); |
| 151 |
| 152 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); |
| 153 int result = writer->Write(buffer, buffer->size(), base::Bind(&NeverCalled)); |
| 154 ASSERT_EQ(net::ERR_IO_PENDING, result); |
| 155 |
| 156 net::TestCompletionCallback callback; |
| 157 writer->Cancel(callback.callback()); |
| 158 int cancel_result = callback.WaitForResult(); |
| 159 EXPECT_EQ(net::OK, cancel_result); |
| 160 } |
OLD | NEW |