OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/browser/fileapi/local_file_stream_writer.h" | 5 #include "webkit/browser/fileapi/local_file_stream_writer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 } // namespace | 93 } // namespace |
94 | 94 |
95 TEST_F(LocalFileStreamWriterTest, Write) { | 95 TEST_F(LocalFileStreamWriterTest, Write) { |
96 base::FilePath path = CreateFileWithContent("file_a", std::string()); | 96 base::FilePath path = CreateFileWithContent("file_a", std::string()); |
97 scoped_ptr<LocalFileStreamWriter> writer( | 97 scoped_ptr<LocalFileStreamWriter> writer( |
98 new LocalFileStreamWriter(file_task_runner(), path, 0)); | 98 new LocalFileStreamWriter(file_task_runner(), path, 0)); |
99 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | 99 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
100 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); | 100 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); |
101 writer.reset(); | 101 writer.reset(); |
102 base::MessageLoop::current()->RunUntilIdle(); | 102 base::MessageLoop::current()->RunUntilIdle(); |
103 EXPECT_TRUE(file_util::PathExists(path)); | 103 EXPECT_TRUE(base::PathExists(path)); |
104 EXPECT_EQ("foobar", GetFileContent(path)); | 104 EXPECT_EQ("foobar", GetFileContent(path)); |
105 } | 105 } |
106 | 106 |
107 TEST_F(LocalFileStreamWriterTest, WriteMiddle) { | 107 TEST_F(LocalFileStreamWriterTest, WriteMiddle) { |
108 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | 108 base::FilePath path = CreateFileWithContent("file_a", "foobar"); |
109 scoped_ptr<LocalFileStreamWriter> writer( | 109 scoped_ptr<LocalFileStreamWriter> writer( |
110 new LocalFileStreamWriter(file_task_runner(), path, 2)); | 110 new LocalFileStreamWriter(file_task_runner(), path, 2)); |
111 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | 111 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
112 writer.reset(); | 112 writer.reset(); |
113 base::MessageLoop::current()->RunUntilIdle(); | 113 base::MessageLoop::current()->RunUntilIdle(); |
114 EXPECT_TRUE(file_util::PathExists(path)); | 114 EXPECT_TRUE(base::PathExists(path)); |
115 EXPECT_EQ("foxxxr", GetFileContent(path)); | 115 EXPECT_EQ("foxxxr", GetFileContent(path)); |
116 } | 116 } |
117 | 117 |
118 TEST_F(LocalFileStreamWriterTest, WriteEnd) { | 118 TEST_F(LocalFileStreamWriterTest, WriteEnd) { |
119 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | 119 base::FilePath path = CreateFileWithContent("file_a", "foobar"); |
120 scoped_ptr<LocalFileStreamWriter> writer( | 120 scoped_ptr<LocalFileStreamWriter> writer( |
121 new LocalFileStreamWriter(file_task_runner(), path, 6)); | 121 new LocalFileStreamWriter(file_task_runner(), path, 6)); |
122 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); | 122 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); |
123 writer.reset(); | 123 writer.reset(); |
124 base::MessageLoop::current()->RunUntilIdle(); | 124 base::MessageLoop::current()->RunUntilIdle(); |
125 EXPECT_TRUE(file_util::PathExists(path)); | 125 EXPECT_TRUE(base::PathExists(path)); |
126 EXPECT_EQ("foobarxxx", GetFileContent(path)); | 126 EXPECT_EQ("foobarxxx", GetFileContent(path)); |
127 } | 127 } |
128 | 128 |
129 TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) { | 129 TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) { |
130 base::FilePath path = Path("file_a"); | 130 base::FilePath path = Path("file_a"); |
131 ASSERT_FALSE(file_util::PathExists(path)); | 131 ASSERT_FALSE(base::PathExists(path)); |
132 scoped_ptr<LocalFileStreamWriter> writer( | 132 scoped_ptr<LocalFileStreamWriter> writer( |
133 new LocalFileStreamWriter(file_task_runner(), path, 0)); | 133 new LocalFileStreamWriter(file_task_runner(), path, 0)); |
134 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); | 134 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); |
135 writer.reset(); | 135 writer.reset(); |
136 base::MessageLoop::current()->RunUntilIdle(); | 136 base::MessageLoop::current()->RunUntilIdle(); |
137 EXPECT_FALSE(file_util::PathExists(path)); | 137 EXPECT_FALSE(base::PathExists(path)); |
138 } | 138 } |
139 | 139 |
140 TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) { | 140 TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) { |
141 base::FilePath path = Path("file_a"); | 141 base::FilePath path = Path("file_a"); |
142 scoped_ptr<LocalFileStreamWriter> writer( | 142 scoped_ptr<LocalFileStreamWriter> writer( |
143 new LocalFileStreamWriter(file_task_runner(), path, 0)); | 143 new LocalFileStreamWriter(file_task_runner(), path, 0)); |
144 // Cancel immediately fails when there's no in-flight operation. | 144 // Cancel immediately fails when there's no in-flight operation. |
145 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); | 145 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); |
146 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | 146 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
147 } | 147 } |
148 | 148 |
149 TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) { | 149 TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) { |
150 base::FilePath path = CreateFileWithContent("file_a", std::string()); | 150 base::FilePath path = CreateFileWithContent("file_a", std::string()); |
151 scoped_ptr<LocalFileStreamWriter> writer( | 151 scoped_ptr<LocalFileStreamWriter> writer( |
152 new LocalFileStreamWriter(file_task_runner(), path, 0)); | 152 new LocalFileStreamWriter(file_task_runner(), path, 0)); |
153 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); | 153 EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); |
154 | 154 |
155 // Cancel immediately fails when there's no in-flight operation. | 155 // Cancel immediately fails when there's no in-flight operation. |
156 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); | 156 int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); |
157 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | 157 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
158 | 158 |
159 writer.reset(); | 159 writer.reset(); |
160 base::MessageLoop::current()->RunUntilIdle(); | 160 base::MessageLoop::current()->RunUntilIdle(); |
161 // Write operation is already completed. | 161 // Write operation is already completed. |
162 EXPECT_TRUE(file_util::PathExists(path)); | 162 EXPECT_TRUE(base::PathExists(path)); |
163 EXPECT_EQ("foo", GetFileContent(path)); | 163 EXPECT_EQ("foo", GetFileContent(path)); |
164 } | 164 } |
165 | 165 |
166 TEST_F(LocalFileStreamWriterTest, CancelWrite) { | 166 TEST_F(LocalFileStreamWriterTest, CancelWrite) { |
167 base::FilePath path = CreateFileWithContent("file_a", "foobar"); | 167 base::FilePath path = CreateFileWithContent("file_a", "foobar"); |
168 scoped_ptr<LocalFileStreamWriter> writer( | 168 scoped_ptr<LocalFileStreamWriter> writer( |
169 new LocalFileStreamWriter(file_task_runner(), path, 0)); | 169 new LocalFileStreamWriter(file_task_runner(), path, 0)); |
170 | 170 |
171 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); | 171 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); |
172 int result = | 172 int result = |
173 writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled)); | 173 writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled)); |
174 ASSERT_EQ(net::ERR_IO_PENDING, result); | 174 ASSERT_EQ(net::ERR_IO_PENDING, result); |
175 | 175 |
176 net::TestCompletionCallback callback; | 176 net::TestCompletionCallback callback; |
177 writer->Cancel(callback.callback()); | 177 writer->Cancel(callback.callback()); |
178 int cancel_result = callback.WaitForResult(); | 178 int cancel_result = callback.WaitForResult(); |
179 EXPECT_EQ(net::OK, cancel_result); | 179 EXPECT_EQ(net::OK, cancel_result); |
180 } | 180 } |
OLD | NEW |