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 "base/files/file_util_proxy.h" | 5 #include "base/files/file_util_proxy.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 MessageLoop::current()->Run(); | 125 MessageLoop::current()->Run(); |
126 | 126 |
127 EXPECT_EQ(File::FILE_OK, error_); | 127 EXPECT_EQ(File::FILE_OK, error_); |
128 EXPECT_TRUE(created_); | 128 EXPECT_TRUE(created_); |
129 EXPECT_NE(kInvalidPlatformFileValue, file_); | 129 EXPECT_NE(kInvalidPlatformFileValue, file_); |
130 EXPECT_TRUE(PathExists(test_path())); | 130 EXPECT_TRUE(PathExists(test_path())); |
131 } | 131 } |
132 | 132 |
133 TEST_F(FileUtilProxyTest, CreateOrOpen_Open) { | 133 TEST_F(FileUtilProxyTest, CreateOrOpen_Open) { |
134 // Creates a file. | 134 // Creates a file. |
135 file_util::WriteFile(test_path(), NULL, 0); | 135 WriteFile(test_path(), NULL, 0); |
136 ASSERT_TRUE(PathExists(test_path())); | 136 ASSERT_TRUE(PathExists(test_path())); |
137 | 137 |
138 // Opens the created file. | 138 // Opens the created file. |
139 FileUtilProxy::CreateOrOpen( | 139 FileUtilProxy::CreateOrOpen( |
140 file_task_runner(), | 140 file_task_runner(), |
141 test_path(), | 141 test_path(), |
142 PLATFORM_FILE_OPEN | PLATFORM_FILE_READ, | 142 PLATFORM_FILE_OPEN | PLATFORM_FILE_READ, |
143 Bind(&FileUtilProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); | 143 Bind(&FileUtilProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); |
144 MessageLoop::current()->Run(); | 144 MessageLoop::current()->Run(); |
145 | 145 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 std::string data; | 216 std::string data; |
217 EXPECT_TRUE(ReadFileToString(path_, &data)); | 217 EXPECT_TRUE(ReadFileToString(path_, &data)); |
218 EXPECT_EQ("test", data); | 218 EXPECT_EQ("test", data); |
219 | 219 |
220 // Make sure we can & do delete the created file to prevent leaks on the bots. | 220 // Make sure we can & do delete the created file to prevent leaks on the bots. |
221 EXPECT_TRUE(base::DeleteFile(path_, false)); | 221 EXPECT_TRUE(base::DeleteFile(path_, false)); |
222 } | 222 } |
223 | 223 |
224 TEST_F(FileUtilProxyTest, GetFileInfo_File) { | 224 TEST_F(FileUtilProxyTest, GetFileInfo_File) { |
225 // Setup. | 225 // Setup. |
226 ASSERT_EQ(4, file_util::WriteFile(test_path(), "test", 4)); | 226 ASSERT_EQ(4, WriteFile(test_path(), "test", 4)); |
227 File::Info expected_info; | 227 File::Info expected_info; |
228 GetFileInfo(test_path(), &expected_info); | 228 GetFileInfo(test_path(), &expected_info); |
229 | 229 |
230 // Run. | 230 // Run. |
231 FileUtilProxy::GetFileInfo( | 231 FileUtilProxy::GetFileInfo( |
232 file_task_runner(), | 232 file_task_runner(), |
233 test_path(), | 233 test_path(), |
234 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); | 234 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); |
235 MessageLoop::current()->Run(); | 235 MessageLoop::current()->Run(); |
236 | 236 |
(...skipping 28 matching lines...) Expand all Loading... |
265 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified); | 265 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified); |
266 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed); | 266 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed); |
267 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time); | 267 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time); |
268 } | 268 } |
269 | 269 |
270 TEST_F(FileUtilProxyTest, Read) { | 270 TEST_F(FileUtilProxyTest, Read) { |
271 // Setup. | 271 // Setup. |
272 const char expected_data[] = "bleh"; | 272 const char expected_data[] = "bleh"; |
273 int expected_bytes = arraysize(expected_data); | 273 int expected_bytes = arraysize(expected_data); |
274 ASSERT_EQ(expected_bytes, | 274 ASSERT_EQ(expected_bytes, |
275 file_util::WriteFile(test_path(), expected_data, expected_bytes)); | 275 WriteFile(test_path(), expected_data, expected_bytes)); |
276 | 276 |
277 // Run. | 277 // Run. |
278 FileUtilProxy::Read( | 278 FileUtilProxy::Read( |
279 file_task_runner(), | 279 file_task_runner(), |
280 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_READ), | 280 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_READ), |
281 0, // offset | 281 0, // offset |
282 128, | 282 128, |
283 Bind(&FileUtilProxyTest::DidRead, weak_factory_.GetWeakPtr())); | 283 Bind(&FileUtilProxyTest::DidRead, weak_factory_.GetWeakPtr())); |
284 MessageLoop::current()->Run(); | 284 MessageLoop::current()->Run(); |
285 | 285 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // the double values to int here. | 347 // the double values to int here. |
348 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()), | 348 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()), |
349 static_cast<int>(info.last_modified.ToDoubleT())); | 349 static_cast<int>(info.last_modified.ToDoubleT())); |
350 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()), | 350 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()), |
351 static_cast<int>(info.last_accessed.ToDoubleT())); | 351 static_cast<int>(info.last_accessed.ToDoubleT())); |
352 } | 352 } |
353 | 353 |
354 TEST_F(FileUtilProxyTest, Truncate_Shrink) { | 354 TEST_F(FileUtilProxyTest, Truncate_Shrink) { |
355 // Setup. | 355 // Setup. |
356 const char kTestData[] = "0123456789"; | 356 const char kTestData[] = "0123456789"; |
357 ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10)); | 357 ASSERT_EQ(10, WriteFile(test_path(), kTestData, 10)); |
358 File::Info info; | 358 File::Info info; |
359 GetFileInfo(test_path(), &info); | 359 GetFileInfo(test_path(), &info); |
360 ASSERT_EQ(10, info.size); | 360 ASSERT_EQ(10, info.size); |
361 | 361 |
362 // Run. | 362 // Run. |
363 FileUtilProxy::Truncate( | 363 FileUtilProxy::Truncate( |
364 file_task_runner(), | 364 file_task_runner(), |
365 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE), | 365 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE), |
366 7, | 366 7, |
367 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); | 367 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); |
368 MessageLoop::current()->Run(); | 368 MessageLoop::current()->Run(); |
369 | 369 |
370 // Verify. | 370 // Verify. |
371 GetFileInfo(test_path(), &info); | 371 GetFileInfo(test_path(), &info); |
372 ASSERT_EQ(7, info.size); | 372 ASSERT_EQ(7, info.size); |
373 | 373 |
374 char buffer[7]; | 374 char buffer[7]; |
375 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7)); | 375 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7)); |
376 int i = 0; | 376 int i = 0; |
377 for (; i < 7; ++i) | 377 for (; i < 7; ++i) |
378 EXPECT_EQ(kTestData[i], buffer[i]); | 378 EXPECT_EQ(kTestData[i], buffer[i]); |
379 } | 379 } |
380 | 380 |
381 TEST_F(FileUtilProxyTest, Truncate_Expand) { | 381 TEST_F(FileUtilProxyTest, Truncate_Expand) { |
382 // Setup. | 382 // Setup. |
383 const char kTestData[] = "9876543210"; | 383 const char kTestData[] = "9876543210"; |
384 ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10)); | 384 ASSERT_EQ(10, WriteFile(test_path(), kTestData, 10)); |
385 File::Info info; | 385 File::Info info; |
386 GetFileInfo(test_path(), &info); | 386 GetFileInfo(test_path(), &info); |
387 ASSERT_EQ(10, info.size); | 387 ASSERT_EQ(10, info.size); |
388 | 388 |
389 // Run. | 389 // Run. |
390 FileUtilProxy::Truncate( | 390 FileUtilProxy::Truncate( |
391 file_task_runner(), | 391 file_task_runner(), |
392 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE), | 392 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE), |
393 53, | 393 53, |
394 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); | 394 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); |
395 MessageLoop::current()->Run(); | 395 MessageLoop::current()->Run(); |
396 | 396 |
397 // Verify. | 397 // Verify. |
398 GetFileInfo(test_path(), &info); | 398 GetFileInfo(test_path(), &info); |
399 ASSERT_EQ(53, info.size); | 399 ASSERT_EQ(53, info.size); |
400 | 400 |
401 char buffer[53]; | 401 char buffer[53]; |
402 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); | 402 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53)); |
403 int i = 0; | 403 int i = 0; |
404 for (; i < 10; ++i) | 404 for (; i < 10; ++i) |
405 EXPECT_EQ(kTestData[i], buffer[i]); | 405 EXPECT_EQ(kTestData[i], buffer[i]); |
406 for (; i < 53; ++i) | 406 for (; i < 53; ++i) |
407 EXPECT_EQ(0, buffer[i]); | 407 EXPECT_EQ(0, buffer[i]); |
408 } | 408 } |
409 | 409 |
410 } // namespace base | 410 } // namespace base |
OLD | NEW |