[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 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 | |
[email protected] | cf7405f | 2013-01-26 00:21:58 | [diff] [blame] | 5 | #include "base/files/file_util_proxy.h" |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 6 | |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | e3177dd | 2014-08-13 20:22:14 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
| 16 | class FileUtilProxyTest : public testing::Test { |
| 17 | public: |
| 18 | FileUtilProxyTest() |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 19 | : file_thread_("FileUtilProxyTestFileThread"), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 20 | error_(File::FILE_OK), |
[email protected] | c9f977b | 2013-04-25 12:17:15 | [diff] [blame] | 21 | weak_factory_(this) {} |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 22 | |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 23 | void SetUp() override { |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 24 | ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 25 | ASSERT_TRUE(file_thread_.Start()); |
| 26 | } |
| 27 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 28 | void DidFinish(File::Error error) { |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 29 | error_ = error; |
[email protected] | 91cae25 | 2013-01-10 14:56:17 | [diff] [blame] | 30 | MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 33 | void DidGetFileInfo(File::Error error, |
| 34 | const File::Info& file_info) { |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 35 | error_ = error; |
| 36 | file_info_ = file_info; |
[email protected] | 91cae25 | 2013-01-10 14:56:17 | [diff] [blame] | 37 | MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 40 | protected: |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 41 | TaskRunner* file_task_runner() const { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 42 | return file_thread_.task_runner().get(); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 43 | } |
| 44 | const FilePath& test_dir_path() const { return dir_.path(); } |
| 45 | const FilePath test_path() const { return dir_.path().AppendASCII("test"); } |
| 46 | |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 47 | MessageLoopForIO message_loop_; |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 48 | Thread file_thread_; |
| 49 | |
| 50 | ScopedTempDir dir_; |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 51 | File::Error error_; |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 52 | FilePath path_; |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 53 | File::Info file_info_; |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 54 | std::vector<char> buffer_; |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 55 | WeakPtrFactory<FileUtilProxyTest> weak_factory_; |
| 56 | }; |
| 57 | |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 58 | |
| 59 | TEST_F(FileUtilProxyTest, GetFileInfo_File) { |
| 60 | // Setup. |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 61 | ASSERT_EQ(4, WriteFile(test_path(), "test", 4)); |
[email protected] | 54124ed | 2014-01-07 10:06:58 | [diff] [blame] | 62 | File::Info expected_info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 63 | GetFileInfo(test_path(), &expected_info); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 64 | |
| 65 | // Run. |
| 66 | FileUtilProxy::GetFileInfo( |
| 67 | file_task_runner(), |
| 68 | test_path(), |
| 69 | Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); |
| 70 | MessageLoop::current()->Run(); |
| 71 | |
| 72 | // Verify. |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 73 | EXPECT_EQ(File::FILE_OK, error_); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 74 | EXPECT_EQ(expected_info.size, file_info_.size); |
| 75 | EXPECT_EQ(expected_info.is_directory, file_info_.is_directory); |
| 76 | EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link); |
| 77 | EXPECT_EQ(expected_info.last_modified, file_info_.last_modified); |
| 78 | EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed); |
| 79 | EXPECT_EQ(expected_info.creation_time, file_info_.creation_time); |
| 80 | } |
| 81 | |
| 82 | TEST_F(FileUtilProxyTest, GetFileInfo_Directory) { |
| 83 | // Setup. |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 84 | ASSERT_TRUE(base::CreateDirectory(test_path())); |
[email protected] | 54124ed | 2014-01-07 10:06:58 | [diff] [blame] | 85 | File::Info expected_info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 86 | GetFileInfo(test_path(), &expected_info); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 87 | |
| 88 | // Run. |
| 89 | FileUtilProxy::GetFileInfo( |
| 90 | file_task_runner(), |
| 91 | test_path(), |
| 92 | Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); |
| 93 | MessageLoop::current()->Run(); |
| 94 | |
| 95 | // Verify. |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 96 | EXPECT_EQ(File::FILE_OK, error_); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 97 | EXPECT_EQ(expected_info.size, file_info_.size); |
| 98 | EXPECT_EQ(expected_info.is_directory, file_info_.is_directory); |
| 99 | EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link); |
| 100 | EXPECT_EQ(expected_info.last_modified, file_info_.last_modified); |
| 101 | EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed); |
| 102 | EXPECT_EQ(expected_info.creation_time, file_info_.creation_time); |
| 103 | } |
| 104 | |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 105 | TEST_F(FileUtilProxyTest, Touch) { |
[email protected] | d1b2908 | 2014-05-22 03:39:05 | [diff] [blame] | 106 | ASSERT_EQ(4, WriteFile(test_path(), "test", 4)); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 107 | Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345); |
| 108 | Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765); |
| 109 | |
| 110 | FileUtilProxy::Touch( |
| 111 | file_task_runner(), |
[email protected] | d1b2908 | 2014-05-22 03:39:05 | [diff] [blame] | 112 | test_path(), |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 113 | last_accessed_time, |
| 114 | last_modified_time, |
| 115 | Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); |
| 116 | MessageLoop::current()->Run(); |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 117 | EXPECT_EQ(File::FILE_OK, error_); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 118 | |
[email protected] | 54124ed | 2014-01-07 10:06:58 | [diff] [blame] | 119 | File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 120 | GetFileInfo(test_path(), &info); |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 121 | |
| 122 | // The returned values may only have the seconds precision, so we cast |
| 123 | // the double values to int here. |
| 124 | EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()), |
| 125 | static_cast<int>(info.last_modified.ToDoubleT())); |
| 126 | EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()), |
| 127 | static_cast<int>(info.last_accessed.ToDoubleT())); |
| 128 | } |
| 129 | |
[email protected] | 77e07b8 | 2012-04-23 18:40:57 | [diff] [blame] | 130 | } // namespace base |