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

Side by Side Diff: base/files/file_proxy_unittest.cc

Issue 2103333006: Remove calls to deprecated MessageLoop methods in base. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 | « base/files/file_path_watcher_unittest.cc ('k') | base/files/file_util_proxy_unittest.cc » ('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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/files/file_proxy.h" 5 #include "base/files/file_proxy.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/files/file.h" 13 #include "base/files/file.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h" 15 #include "base/files/scoped_temp_dir.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
18 #include "base/run_loop.h"
18 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
19 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
20 #include "build/build_config.h" 21 #include "build/build_config.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 namespace base { 24 namespace base {
24 25
25 class FileProxyTest : public testing::Test { 26 class FileProxyTest : public testing::Test {
26 public: 27 public:
27 FileProxyTest() 28 FileProxyTest()
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 error_ = error; 74 error_ = error;
74 bytes_written_ = bytes_written; 75 bytes_written_ = bytes_written;
75 MessageLoop::current()->QuitWhenIdle(); 76 MessageLoop::current()->QuitWhenIdle();
76 } 77 }
77 78
78 protected: 79 protected:
79 void CreateProxy(uint32_t flags, FileProxy* proxy) { 80 void CreateProxy(uint32_t flags, FileProxy* proxy) {
80 proxy->CreateOrOpen( 81 proxy->CreateOrOpen(
81 test_path(), flags, 82 test_path(), flags,
82 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); 83 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
83 MessageLoop::current()->Run(); 84 RunLoop().Run();
84 EXPECT_TRUE(proxy->IsValid()); 85 EXPECT_TRUE(proxy->IsValid());
85 } 86 }
86 87
87 TaskRunner* file_task_runner() const { 88 TaskRunner* file_task_runner() const {
88 return file_thread_.task_runner().get(); 89 return file_thread_.task_runner().get();
89 } 90 }
90 const FilePath& test_dir_path() const { return dir_.path(); } 91 const FilePath& test_dir_path() const { return dir_.path(); }
91 const FilePath test_path() const { return dir_.path().AppendASCII("test"); } 92 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
92 93
93 ScopedTempDir dir_; 94 ScopedTempDir dir_;
94 MessageLoopForIO message_loop_; 95 MessageLoopForIO message_loop_;
95 Thread file_thread_; 96 Thread file_thread_;
96 97
97 File::Error error_; 98 File::Error error_;
98 FilePath path_; 99 FilePath path_;
99 File::Info file_info_; 100 File::Info file_info_;
100 std::vector<char> buffer_; 101 std::vector<char> buffer_;
101 int bytes_written_; 102 int bytes_written_;
102 WeakPtrFactory<FileProxyTest> weak_factory_; 103 WeakPtrFactory<FileProxyTest> weak_factory_;
103 }; 104 };
104 105
105 TEST_F(FileProxyTest, CreateOrOpen_Create) { 106 TEST_F(FileProxyTest, CreateOrOpen_Create) {
106 FileProxy proxy(file_task_runner()); 107 FileProxy proxy(file_task_runner());
107 proxy.CreateOrOpen( 108 proxy.CreateOrOpen(
108 test_path(), 109 test_path(),
109 File::FLAG_CREATE | File::FLAG_READ, 110 File::FLAG_CREATE | File::FLAG_READ,
110 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); 111 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
111 MessageLoop::current()->Run(); 112 RunLoop().Run();
112 113
113 EXPECT_EQ(File::FILE_OK, error_); 114 EXPECT_EQ(File::FILE_OK, error_);
114 EXPECT_TRUE(proxy.IsValid()); 115 EXPECT_TRUE(proxy.IsValid());
115 EXPECT_TRUE(proxy.created()); 116 EXPECT_TRUE(proxy.created());
116 EXPECT_TRUE(PathExists(test_path())); 117 EXPECT_TRUE(PathExists(test_path()));
117 } 118 }
118 119
119 TEST_F(FileProxyTest, CreateOrOpen_Open) { 120 TEST_F(FileProxyTest, CreateOrOpen_Open) {
120 // Creates a file. 121 // Creates a file.
121 base::WriteFile(test_path(), NULL, 0); 122 base::WriteFile(test_path(), NULL, 0);
122 ASSERT_TRUE(PathExists(test_path())); 123 ASSERT_TRUE(PathExists(test_path()));
123 124
124 // Opens the created file. 125 // Opens the created file.
125 FileProxy proxy(file_task_runner()); 126 FileProxy proxy(file_task_runner());
126 proxy.CreateOrOpen( 127 proxy.CreateOrOpen(
127 test_path(), 128 test_path(),
128 File::FLAG_OPEN | File::FLAG_READ, 129 File::FLAG_OPEN | File::FLAG_READ,
129 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); 130 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
130 MessageLoop::current()->Run(); 131 RunLoop().Run();
131 132
132 EXPECT_EQ(File::FILE_OK, error_); 133 EXPECT_EQ(File::FILE_OK, error_);
133 EXPECT_TRUE(proxy.IsValid()); 134 EXPECT_TRUE(proxy.IsValid());
134 EXPECT_FALSE(proxy.created()); 135 EXPECT_FALSE(proxy.created());
135 } 136 }
136 137
137 TEST_F(FileProxyTest, CreateOrOpen_OpenNonExistent) { 138 TEST_F(FileProxyTest, CreateOrOpen_OpenNonExistent) {
138 FileProxy proxy(file_task_runner()); 139 FileProxy proxy(file_task_runner());
139 proxy.CreateOrOpen( 140 proxy.CreateOrOpen(
140 test_path(), 141 test_path(),
141 File::FLAG_OPEN | File::FLAG_READ, 142 File::FLAG_OPEN | File::FLAG_READ,
142 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); 143 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
143 MessageLoop::current()->Run(); 144 RunLoop().Run();
144 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_); 145 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_);
145 EXPECT_FALSE(proxy.IsValid()); 146 EXPECT_FALSE(proxy.IsValid());
146 EXPECT_FALSE(proxy.created()); 147 EXPECT_FALSE(proxy.created());
147 EXPECT_FALSE(PathExists(test_path())); 148 EXPECT_FALSE(PathExists(test_path()));
148 } 149 }
149 150
150 TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) { 151 TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) {
151 bool prev = ThreadRestrictions::SetIOAllowed(false); 152 bool prev = ThreadRestrictions::SetIOAllowed(false);
152 { 153 {
153 FileProxy proxy(file_task_runner()); 154 FileProxy proxy(file_task_runner());
154 proxy.CreateOrOpen( 155 proxy.CreateOrOpen(
155 test_path(), 156 test_path(),
156 File::FLAG_CREATE | File::FLAG_READ, 157 File::FLAG_CREATE | File::FLAG_READ,
157 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); 158 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
158 } 159 }
159 MessageLoop::current()->Run(); 160 RunLoop().Run();
160 ThreadRestrictions::SetIOAllowed(prev); 161 ThreadRestrictions::SetIOAllowed(prev);
161 162
162 EXPECT_TRUE(PathExists(test_path())); 163 EXPECT_TRUE(PathExists(test_path()));
163 } 164 }
164 165
165 TEST_F(FileProxyTest, Close) { 166 TEST_F(FileProxyTest, Close) {
166 // Creates a file. 167 // Creates a file.
167 FileProxy proxy(file_task_runner()); 168 FileProxy proxy(file_task_runner());
168 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy); 169 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy);
169 170
170 #if defined(OS_WIN) 171 #if defined(OS_WIN)
171 // This fails on Windows if the file is not closed. 172 // This fails on Windows if the file is not closed.
172 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new"))); 173 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new")));
173 #endif 174 #endif
174 175
175 proxy.Close(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 176 proxy.Close(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
176 MessageLoop::current()->Run(); 177 RunLoop().Run();
177 EXPECT_EQ(File::FILE_OK, error_); 178 EXPECT_EQ(File::FILE_OK, error_);
178 EXPECT_FALSE(proxy.IsValid()); 179 EXPECT_FALSE(proxy.IsValid());
179 180
180 // Now it should pass on all platforms. 181 // Now it should pass on all platforms.
181 EXPECT_TRUE(base::Move(test_path(), test_dir_path().AppendASCII("new"))); 182 EXPECT_TRUE(base::Move(test_path(), test_dir_path().AppendASCII("new")));
182 } 183 }
183 184
184 TEST_F(FileProxyTest, CreateTemporary) { 185 TEST_F(FileProxyTest, CreateTemporary) {
185 { 186 {
186 FileProxy proxy(file_task_runner()); 187 FileProxy proxy(file_task_runner());
187 proxy.CreateTemporary( 188 proxy.CreateTemporary(
188 0 /* additional_file_flags */, 189 0 /* additional_file_flags */,
189 Bind(&FileProxyTest::DidCreateTemporary, weak_factory_.GetWeakPtr())); 190 Bind(&FileProxyTest::DidCreateTemporary, weak_factory_.GetWeakPtr()));
190 MessageLoop::current()->Run(); 191 RunLoop().Run();
191 192
192 EXPECT_TRUE(proxy.IsValid()); 193 EXPECT_TRUE(proxy.IsValid());
193 EXPECT_EQ(File::FILE_OK, error_); 194 EXPECT_EQ(File::FILE_OK, error_);
194 EXPECT_TRUE(PathExists(path_)); 195 EXPECT_TRUE(PathExists(path_));
195 196
196 // The file should be writable. 197 // The file should be writable.
197 proxy.Write(0, "test", 4, 198 proxy.Write(0, "test", 4,
198 Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr())); 199 Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
199 MessageLoop::current()->Run(); 200 RunLoop().Run();
200 EXPECT_EQ(File::FILE_OK, error_); 201 EXPECT_EQ(File::FILE_OK, error_);
201 EXPECT_EQ(4, bytes_written_); 202 EXPECT_EQ(4, bytes_written_);
202 } 203 }
203 204
204 // Make sure the written data can be read from the returned path. 205 // Make sure the written data can be read from the returned path.
205 std::string data; 206 std::string data;
206 EXPECT_TRUE(ReadFileToString(path_, &data)); 207 EXPECT_TRUE(ReadFileToString(path_, &data));
207 EXPECT_EQ("test", data); 208 EXPECT_EQ("test", data);
208 209
209 // Make sure we can & do delete the created file to prevent leaks on the bots. 210 // Make sure we can & do delete the created file to prevent leaks on the bots.
(...skipping 18 matching lines...) Expand all
228 // Setup. 229 // Setup.
229 ASSERT_EQ(4, base::WriteFile(test_path(), "test", 4)); 230 ASSERT_EQ(4, base::WriteFile(test_path(), "test", 4));
230 File::Info expected_info; 231 File::Info expected_info;
231 GetFileInfo(test_path(), &expected_info); 232 GetFileInfo(test_path(), &expected_info);
232 233
233 // Run. 234 // Run.
234 FileProxy proxy(file_task_runner()); 235 FileProxy proxy(file_task_runner());
235 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy); 236 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
236 proxy.GetInfo( 237 proxy.GetInfo(
237 Bind(&FileProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); 238 Bind(&FileProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
238 MessageLoop::current()->Run(); 239 RunLoop().Run();
239 240
240 // Verify. 241 // Verify.
241 EXPECT_EQ(File::FILE_OK, error_); 242 EXPECT_EQ(File::FILE_OK, error_);
242 EXPECT_EQ(expected_info.size, file_info_.size); 243 EXPECT_EQ(expected_info.size, file_info_.size);
243 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory); 244 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
244 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link); 245 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
245 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified); 246 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
246 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time); 247 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
247 } 248 }
248 249
249 TEST_F(FileProxyTest, Read) { 250 TEST_F(FileProxyTest, Read) {
250 // Setup. 251 // Setup.
251 const char expected_data[] = "bleh"; 252 const char expected_data[] = "bleh";
252 int expected_bytes = arraysize(expected_data); 253 int expected_bytes = arraysize(expected_data);
253 ASSERT_EQ(expected_bytes, 254 ASSERT_EQ(expected_bytes,
254 base::WriteFile(test_path(), expected_data, expected_bytes)); 255 base::WriteFile(test_path(), expected_data, expected_bytes));
255 256
256 // Run. 257 // Run.
257 FileProxy proxy(file_task_runner()); 258 FileProxy proxy(file_task_runner());
258 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy); 259 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
259 260
260 proxy.Read(0, 128, Bind(&FileProxyTest::DidRead, weak_factory_.GetWeakPtr())); 261 proxy.Read(0, 128, Bind(&FileProxyTest::DidRead, weak_factory_.GetWeakPtr()));
261 MessageLoop::current()->Run(); 262 RunLoop().Run();
262 263
263 // Verify. 264 // Verify.
264 EXPECT_EQ(File::FILE_OK, error_); 265 EXPECT_EQ(File::FILE_OK, error_);
265 EXPECT_EQ(expected_bytes, static_cast<int>(buffer_.size())); 266 EXPECT_EQ(expected_bytes, static_cast<int>(buffer_.size()));
266 for (size_t i = 0; i < buffer_.size(); ++i) { 267 for (size_t i = 0; i < buffer_.size(); ++i) {
267 EXPECT_EQ(expected_data[i], buffer_[i]); 268 EXPECT_EQ(expected_data[i], buffer_[i]);
268 } 269 }
269 } 270 }
270 271
271 TEST_F(FileProxyTest, WriteAndFlush) { 272 TEST_F(FileProxyTest, WriteAndFlush) {
272 FileProxy proxy(file_task_runner()); 273 FileProxy proxy(file_task_runner());
273 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy); 274 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy);
274 275
275 const char data[] = "foo!"; 276 const char data[] = "foo!";
276 int data_bytes = arraysize(data); 277 int data_bytes = arraysize(data);
277 proxy.Write(0, data, data_bytes, 278 proxy.Write(0, data, data_bytes,
278 Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr())); 279 Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
279 MessageLoop::current()->Run(); 280 RunLoop().Run();
280 EXPECT_EQ(File::FILE_OK, error_); 281 EXPECT_EQ(File::FILE_OK, error_);
281 EXPECT_EQ(data_bytes, bytes_written_); 282 EXPECT_EQ(data_bytes, bytes_written_);
282 283
283 // Flush the written data. (So that the following read should always 284 // Flush the written data. (So that the following read should always
284 // succeed. On some platforms it may work with or without this flush.) 285 // succeed. On some platforms it may work with or without this flush.)
285 proxy.Flush(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 286 proxy.Flush(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
286 MessageLoop::current()->Run(); 287 RunLoop().Run();
287 EXPECT_EQ(File::FILE_OK, error_); 288 EXPECT_EQ(File::FILE_OK, error_);
288 289
289 // Verify the written data. 290 // Verify the written data.
290 char buffer[10]; 291 char buffer[10];
291 EXPECT_EQ(data_bytes, base::ReadFile(test_path(), buffer, data_bytes)); 292 EXPECT_EQ(data_bytes, base::ReadFile(test_path(), buffer, data_bytes));
292 for (int i = 0; i < data_bytes; ++i) { 293 for (int i = 0; i < data_bytes; ++i) {
293 EXPECT_EQ(data[i], buffer[i]); 294 EXPECT_EQ(data[i], buffer[i]);
294 } 295 }
295 } 296 }
296 297
297 #if defined(OS_ANDROID) 298 #if defined(OS_ANDROID)
298 // Flaky on Android, see http://crbug.com/489602 299 // Flaky on Android, see http://crbug.com/489602
299 #define MAYBE_SetTimes DISABLED_SetTimes 300 #define MAYBE_SetTimes DISABLED_SetTimes
300 #else 301 #else
301 #define MAYBE_SetTimes SetTimes 302 #define MAYBE_SetTimes SetTimes
302 #endif 303 #endif
303 TEST_F(FileProxyTest, MAYBE_SetTimes) { 304 TEST_F(FileProxyTest, MAYBE_SetTimes) {
304 FileProxy proxy(file_task_runner()); 305 FileProxy proxy(file_task_runner());
305 CreateProxy( 306 CreateProxy(
306 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_WRITE_ATTRIBUTES, 307 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_WRITE_ATTRIBUTES,
307 &proxy); 308 &proxy);
308 309
309 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345); 310 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
310 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765); 311 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
311 312
312 proxy.SetTimes(last_accessed_time, last_modified_time, 313 proxy.SetTimes(last_accessed_time, last_modified_time,
313 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 314 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
314 MessageLoop::current()->Run(); 315 RunLoop().Run();
315 EXPECT_EQ(File::FILE_OK, error_); 316 EXPECT_EQ(File::FILE_OK, error_);
316 317
317 File::Info info; 318 File::Info info;
318 GetFileInfo(test_path(), &info); 319 GetFileInfo(test_path(), &info);
319 320
320 // The returned values may only have the seconds precision, so we cast 321 // The returned values may only have the seconds precision, so we cast
321 // the double values to int here. 322 // the double values to int here.
322 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()), 323 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()),
323 static_cast<int>(info.last_modified.ToDoubleT())); 324 static_cast<int>(info.last_modified.ToDoubleT()));
324 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()), 325 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()),
325 static_cast<int>(info.last_accessed.ToDoubleT())); 326 static_cast<int>(info.last_accessed.ToDoubleT()));
326 } 327 }
327 328
328 TEST_F(FileProxyTest, SetLength_Shrink) { 329 TEST_F(FileProxyTest, SetLength_Shrink) {
329 // Setup. 330 // Setup.
330 const char kTestData[] = "0123456789"; 331 const char kTestData[] = "0123456789";
331 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10)); 332 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10));
332 File::Info info; 333 File::Info info;
333 GetFileInfo(test_path(), &info); 334 GetFileInfo(test_path(), &info);
334 ASSERT_EQ(10, info.size); 335 ASSERT_EQ(10, info.size);
335 336
336 // Run. 337 // Run.
337 FileProxy proxy(file_task_runner()); 338 FileProxy proxy(file_task_runner());
338 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy); 339 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
339 proxy.SetLength(7, 340 proxy.SetLength(7,
340 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 341 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
341 MessageLoop::current()->Run(); 342 RunLoop().Run();
342 343
343 // Verify. 344 // Verify.
344 GetFileInfo(test_path(), &info); 345 GetFileInfo(test_path(), &info);
345 ASSERT_EQ(7, info.size); 346 ASSERT_EQ(7, info.size);
346 347
347 char buffer[7]; 348 char buffer[7];
348 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7)); 349 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7));
349 int i = 0; 350 int i = 0;
350 for (; i < 7; ++i) 351 for (; i < 7; ++i)
351 EXPECT_EQ(kTestData[i], buffer[i]); 352 EXPECT_EQ(kTestData[i], buffer[i]);
352 } 353 }
353 354
354 TEST_F(FileProxyTest, SetLength_Expand) { 355 TEST_F(FileProxyTest, SetLength_Expand) {
355 // Setup. 356 // Setup.
356 const char kTestData[] = "9876543210"; 357 const char kTestData[] = "9876543210";
357 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10)); 358 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10));
358 File::Info info; 359 File::Info info;
359 GetFileInfo(test_path(), &info); 360 GetFileInfo(test_path(), &info);
360 ASSERT_EQ(10, info.size); 361 ASSERT_EQ(10, info.size);
361 362
362 // Run. 363 // Run.
363 FileProxy proxy(file_task_runner()); 364 FileProxy proxy(file_task_runner());
364 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy); 365 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
365 proxy.SetLength(53, 366 proxy.SetLength(53,
366 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 367 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
367 MessageLoop::current()->Run(); 368 RunLoop().Run();
368 369
369 // Verify. 370 // Verify.
370 GetFileInfo(test_path(), &info); 371 GetFileInfo(test_path(), &info);
371 ASSERT_EQ(53, info.size); 372 ASSERT_EQ(53, info.size);
372 373
373 char buffer[53]; 374 char buffer[53];
374 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); 375 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53));
375 int i = 0; 376 int i = 0;
376 for (; i < 10; ++i) 377 for (; i < 10; ++i)
377 EXPECT_EQ(kTestData[i], buffer[i]); 378 EXPECT_EQ(kTestData[i], buffer[i]);
378 for (; i < 53; ++i) 379 for (; i < 53; ++i)
379 EXPECT_EQ(0, buffer[i]); 380 EXPECT_EQ(0, buffer[i]);
380 } 381 }
381 382
382 } // namespace base 383 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_path_watcher_unittest.cc ('k') | base/files/file_util_proxy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698