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.
5 #include "webkit/browser/fileapi/local_file_stream_writer.h"
9 #include "base/callback.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/threading/thread.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/test_completion_callback.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class LocalFileStreamWriterTest : public testing::Test {
25 LocalFileStreamWriterTest()
26 : file_thread_("FileUtilProxyTestFileThread") {}
28 virtual void SetUp() OVERRIDE {
29 ASSERT_TRUE(file_thread_.Start());
30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
33 virtual void TearDown() OVERRIDE {
34 // Give another chance for deleted streams to perform Close.
35 base::RunLoop().RunUntilIdle();
37 base::RunLoop().RunUntilIdle();
41 base::FilePath Path(const std::string& name) {
42 return temp_dir_.path().AppendASCII(name);
45 int WriteStringToWriter(LocalFileStreamWriter* writer,
46 const std::string& data) {
47 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data));
48 scoped_refptr<net::DrainableIOBuffer> drainable(
49 new net::DrainableIOBuffer(buffer.get(), buffer->size()));
51 while (drainable->BytesRemaining() > 0) {
52 net::TestCompletionCallback callback;
53 int result = writer->Write(
54 drainable.get(), drainable->BytesRemaining(), callback.callback());
55 if (result == net::ERR_IO_PENDING)
56 result = callback.WaitForResult();
59 drainable->DidConsume(result);
64 std::string GetFileContent(const base::FilePath& path) {
66 base::ReadFileToString(path, &content);
70 base::FilePath CreateFileWithContent(const std::string& name,
71 const std::string& data) {
72 base::FilePath path = Path(name);
73 file_util::WriteFile(path, data.c_str(), data.size());
77 base::MessageLoopProxy* file_task_runner() const {
78 return file_thread_.message_loop_proxy().get();
81 LocalFileStreamWriter* CreateWriter(const base::FilePath& path,
83 return new LocalFileStreamWriter(file_task_runner(), path, offset);
87 base::MessageLoopForIO message_loop_;
88 base::Thread file_thread_;
89 base::ScopedTempDir temp_dir_;
92 void NeverCalled(int unused) {
96 TEST_F(LocalFileStreamWriterTest, Write) {
97 base::FilePath path = CreateFileWithContent("file_a", std::string());
98 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
99 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo"));
100 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar"));
102 base::RunLoop().RunUntilIdle();
103 EXPECT_TRUE(base::PathExists(path));
104 EXPECT_EQ("foobar", GetFileContent(path));
107 TEST_F(LocalFileStreamWriterTest, WriteMiddle) {
108 base::FilePath path = CreateFileWithContent("file_a", "foobar");
109 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 2));
110 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx"));
112 base::RunLoop().RunUntilIdle();
113 EXPECT_TRUE(base::PathExists(path));
114 EXPECT_EQ("foxxxr", GetFileContent(path));
117 TEST_F(LocalFileStreamWriterTest, WriteEnd) {
118 base::FilePath path = CreateFileWithContent("file_a", "foobar");
119 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 6));
120 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx"));
122 base::RunLoop().RunUntilIdle();
123 EXPECT_TRUE(base::PathExists(path));
124 EXPECT_EQ("foobarxxx", GetFileContent(path));
127 TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) {
128 base::FilePath path = Path("file_a");
129 ASSERT_FALSE(base::PathExists(path));
130 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
131 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo"));
133 base::RunLoop().RunUntilIdle();
134 EXPECT_FALSE(base::PathExists(path));
137 TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) {
138 base::FilePath path = Path("file_a");
139 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
140 // Cancel immediately fails when there's no in-flight operation.
141 int cancel_result = writer->Cancel(base::Bind(&NeverCalled));
142 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result);
145 TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) {
146 base::FilePath path = CreateFileWithContent("file_a", std::string());
147 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
148 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo"));
150 // Cancel immediately fails when there's no in-flight operation.
151 int cancel_result = writer->Cancel(base::Bind(&NeverCalled));
152 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result);
155 base::RunLoop().RunUntilIdle();
156 // Write operation is already completed.
157 EXPECT_TRUE(base::PathExists(path));
158 EXPECT_EQ("foo", GetFileContent(path));
161 TEST_F(LocalFileStreamWriterTest, CancelWrite) {
162 base::FilePath path = CreateFileWithContent("file_a", "foobar");
163 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0));
165 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx"));
167 writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled));
168 ASSERT_EQ(net::ERR_IO_PENDING, result);
170 net::TestCompletionCallback callback;
171 writer->Cancel(callback.callback());
172 int cancel_result = callback.WaitForResult();
173 EXPECT_EQ(net::OK, cancel_result);
176 } // namespace fileapi