Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(765)

Side by Side Diff: components/filesystem/directory_impl_unittest.cc

Issue 1646673002: mojo filesystem: Simplify full file reading/writing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Take 2 at trying to fix compile. The release version is complaining about duplicate -1s and this is… Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/filesystem/directory_impl.cc ('k') | components/filesystem/file_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "components/filesystem/files_test_base.h" 12 #include "components/filesystem/files_test_base.h"
13 #include "mojo/common/common_type_converters.h"
13 #include "mojo/util/capture_util.h" 14 #include "mojo/util/capture_util.h"
14 15
15 using mojo::Capture; 16 using mojo::Capture;
16 17
17 namespace filesystem { 18 namespace filesystem {
18 namespace { 19 namespace {
19 20
20 using DirectoryImplTest = FilesTestBase; 21 using DirectoryImplTest = FilesTestBase;
21 22
22 TEST_F(DirectoryImplTest, Read) { 23 TEST_F(DirectoryImplTest, Read) {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // Attempt to open that directory as a file. This must fail! 147 // Attempt to open that directory as a file. This must fail!
147 FilePtr file; 148 FilePtr file;
148 error = FileError::FAILED; 149 error = FileError::FAILED;
149 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen, 150 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen,
150 Capture(&error)); 151 Capture(&error));
151 ASSERT_TRUE(directory.WaitForIncomingResponse()); 152 ASSERT_TRUE(directory.WaitForIncomingResponse());
152 EXPECT_EQ(FileError::NOT_A_FILE, error); 153 EXPECT_EQ(FileError::NOT_A_FILE, error);
153 } 154 }
154 } 155 }
155 156
157 TEST_F(DirectoryImplTest, WriteFileReadFile) {
158 DirectoryPtr directory;
159 GetTemporaryRoot(&directory);
160 FileError error;
161
162 std::string data("one two three");
163 {
164 directory->WriteFile("data", mojo::Array<uint8_t>::From(data),
165 Capture(&error));
166 ASSERT_TRUE(directory.WaitForIncomingResponse());
167 EXPECT_EQ(FileError::OK, error);
168 }
169
170 {
171 mojo::Array<uint8_t> file_contents;
172 directory->ReadEntireFile("data", Capture(&error, &file_contents));
173 ASSERT_TRUE(directory.WaitForIncomingResponse());
174 EXPECT_EQ(FileError::OK, error);
175
176 EXPECT_EQ(data, file_contents.To<std::string>());
177 }
178 }
179
180 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) {
181 DirectoryPtr directory;
182 GetTemporaryRoot(&directory);
183 FileError error;
184
185 {
186 mojo::Array<uint8_t> file_contents;
187 directory->ReadEntireFile("doesnt_exist", Capture(&error, &file_contents));
188 ASSERT_TRUE(directory.WaitForIncomingResponse());
189 EXPECT_EQ(FileError::NOT_FOUND, error);
190 }
191 }
192
193 TEST_F(DirectoryImplTest, CantReadEntireFileOnADirectory) {
194 DirectoryPtr directory;
195 GetTemporaryRoot(&directory);
196 FileError error;
197
198 // Create a directory
199 {
200 DirectoryPtr my_file_directory;
201 error = FileError::FAILED;
202 directory->OpenDirectory(
203 "my_dir", GetProxy(&my_file_directory),
204 kFlagRead | kFlagWrite | kFlagCreate,
205 Capture(&error));
206 ASSERT_TRUE(directory.WaitForIncomingResponse());
207 EXPECT_EQ(FileError::OK, error);
208 }
209
210 // Try to read it as a file
211 {
212 mojo::Array<uint8_t> file_contents;
213 directory->ReadEntireFile("my_dir", Capture(&error, &file_contents));
214 ASSERT_TRUE(directory.WaitForIncomingResponse());
215 EXPECT_EQ(FileError::NOT_A_FILE, error);
216 }
217 }
218
219 TEST_F(DirectoryImplTest, CantWriteFileOnADirectory) {
220 DirectoryPtr directory;
221 GetTemporaryRoot(&directory);
222 FileError error;
223
224 // Create a directory
225 {
226 DirectoryPtr my_file_directory;
227 error = FileError::FAILED;
228 directory->OpenDirectory(
229 "my_dir", GetProxy(&my_file_directory),
230 kFlagRead | kFlagWrite | kFlagCreate,
231 Capture(&error));
232 ASSERT_TRUE(directory.WaitForIncomingResponse());
233 EXPECT_EQ(FileError::OK, error);
234 }
235
236 {
237 std::string data("one two three");
238 directory->WriteFile("my_dir", mojo::Array<uint8_t>::From(data),
239 Capture(&error));
240 ASSERT_TRUE(directory.WaitForIncomingResponse());
241 EXPECT_EQ(FileError::NOT_A_FILE, error);
242 }
243 }
156 244
157 // TODO(vtl): Test delete flags. 245 // TODO(vtl): Test delete flags.
158 246
159 } // namespace 247 } // namespace
160 } // namespace filesystem 248 } // namespace filesystem
OLDNEW
« no previous file with comments | « components/filesystem/directory_impl.cc ('k') | components/filesystem/file_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698