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/browser/fileapi/local_file_stream_writer.h" | |
6 | |
7 #include <string> | |
8 | |
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" | |
20 | |
21 namespace fileapi { | |
22 | |
23 class LocalFileStreamWriterTest : public testing::Test { | |
24 public: | |
25 LocalFileStreamWriterTest() | |
26 : file_thread_("FileUtilProxyTestFileThread") {} | |
27 | |
28 virtual void SetUp() OVERRIDE { | |
29 ASSERT_TRUE(file_thread_.Start()); | |
30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
31 } | |
32 | |
33 virtual void TearDown() OVERRIDE { | |
34 // Give another chance for deleted streams to perform Close. | |
35 base::RunLoop().RunUntilIdle(); | |
36 file_thread_.Stop(); | |
37 base::RunLoop().RunUntilIdle(); | |
38 } | |
39 | |
40 protected: | |
41 base::FilePath Path(const std::string& name) { | |
42 return temp_dir_.path().AppendASCII(name); | |
43 } | |
44 | |
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())); | |
50 | |
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(); | |
57 if (result <= 0) | |
58 return result; | |
59 drainable->DidConsume(result); | |
60 } | |
61 return net::OK; | |
62 } | |
63 | |
64 std::string GetFileContent(const base::FilePath& path) { | |
65 std::string content; | |
66 base::ReadFileToString(path, &content); | |
67 return content; | |
68 } | |
69 | |
70 base::FilePath CreateFileWithContent(const std::string& name, | |
71 const std::string& data) { | |
72 base::FilePath path = Path(name); | |
73 base::WriteFile(path, data.c_str(), data.size()); | |
74 return path; | |
75 } | |
76 | |
77 base::MessageLoopProxy* file_task_runner() const { | |
78 return file_thread_.message_loop_proxy().get(); | |
79 } | |
80 | |
81 LocalFileStreamWriter* CreateWriter(const base::FilePath& path, | |
82 int64 offset) { | |
83 return new LocalFileStreamWriter(file_task_runner(), path, offset, | |
84 FileStreamWriter::OPEN_EXISTING_FILE); | |
85 } | |
86 | |
87 private: | |
88 base::MessageLoopForIO message_loop_; | |
89 base::Thread file_thread_; | |
90 base::ScopedTempDir temp_dir_; | |
91 }; | |
92 | |
93 void NeverCalled(int unused) { | |
94 ADD_FAILURE(); | |
95 } | |
96 | |
97 TEST_F(LocalFileStreamWriterTest, Write) { | |
98 base::FilePath path = CreateFileWithContent("file_a", std::string()); | |
99 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
100 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | |
101 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); | |
102 writer.reset(); | |
103 base::RunLoop().RunUntilIdle(); | |
104 EXPECT_TRUE(base::PathExists(path)); | |
105 EXPECT_EQ("foobar", GetFileContent(path)); | |
106 } | |
107 | |
108 TEST_F(LocalFileStreamWriterTest, WriteMiddle) { | |
109 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | |
110 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 2)); | |
111 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | |
112 writer.reset(); | |
113 base::RunLoop().RunUntilIdle(); | |
114 EXPECT_TRUE(base::PathExists(path)); | |
115 EXPECT_EQ("foxxxr", GetFileContent(path)); | |
116 } | |
117 | |
118 TEST_F(LocalFileStreamWriterTest, WriteEnd) { | |
119 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | |
120 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 6)); | |
121 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | |
122 writer.reset(); | |
123 base::RunLoop().RunUntilIdle(); | |
124 EXPECT_TRUE(base::PathExists(path)); | |
125 EXPECT_EQ("foobarxxx", GetFileContent(path)); | |
126 } | |
127 | |
128 TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) { | |
129 base::FilePath path = Path("file_a"); | |
130 ASSERT_FALSE(base::PathExists(path)); | |
131 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
132 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); | |
133 writer.reset(); | |
134 base::RunLoop().RunUntilIdle(); | |
135 EXPECT_FALSE(base::PathExists(path)); | |
136 } | |
137 | |
138 TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) { | |
139 base::FilePath path = Path("file_a"); | |
140 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
141 // Cancel immediately fails when there's no in-flight operation. | |
142 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); | |
143 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | |
144 } | |
145 | |
146 TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) { | |
147 base::FilePath path = CreateFileWithContent("file_a", std::string()); | |
148 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
149 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | |
150 | |
151 // Cancel immediately fails when there's no in-flight operation. | |
152 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); | |
153 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | |
154 | |
155 writer.reset(); | |
156 base::RunLoop().RunUntilIdle(); | |
157 // Write operation is already completed. | |
158 EXPECT_TRUE(base::PathExists(path)); | |
159 EXPECT_EQ("foo", GetFileContent(path)); | |
160 } | |
161 | |
162 TEST_F(LocalFileStreamWriterTest, CancelWrite) { | |
163 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | |
164 scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); | |
165 | |
166 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); | |
167 int result = | |
168 writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled)); | |
169 ASSERT_EQ(net::ERR_IO_PENDING, result); | |
170 | |
171 net::TestCompletionCallback callback; | |
172 writer->Cancel(callback.callback()); | |
173 int cancel_result = callback.WaitForResult(); | |
174 EXPECT_EQ(net::OK, cancel_result); | |
175 } | |
176 | |
177 } // namespace fileapi | |
OLD | NEW |