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.
5 #include "base/files/file_util_proxy.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/threading/thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class FileUtilProxyTest
: public testing::Test
{
20 : file_thread_("FileUtilProxyTestFileThread"),
21 error_(File::FILE_OK
),
24 weak_factory_(this) {}
26 virtual void SetUp() OVERRIDE
{
27 ASSERT_TRUE(dir_
.CreateUniqueTempDir());
28 ASSERT_TRUE(file_thread_
.Start());
31 void DidFinish(File::Error error
) {
33 MessageLoop::current()->QuitWhenIdle();
36 void DidGetFileInfo(File::Error error
,
37 const File::Info
& file_info
) {
39 file_info_
= file_info
;
40 MessageLoop::current()->QuitWhenIdle();
44 TaskRunner
* file_task_runner() const {
45 return file_thread_
.message_loop_proxy().get();
47 const FilePath
& test_dir_path() const { return dir_
.path(); }
48 const FilePath
test_path() const { return dir_
.path().AppendASCII("test"); }
50 MessageLoopForIO message_loop_
;
57 File::Info file_info_
;
58 std::vector
<char> buffer_
;
60 WeakPtrFactory
<FileUtilProxyTest
> weak_factory_
;
64 TEST_F(FileUtilProxyTest
, GetFileInfo_File
) {
66 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
67 File::Info expected_info
;
68 GetFileInfo(test_path(), &expected_info
);
71 FileUtilProxy::GetFileInfo(
74 Bind(&FileUtilProxyTest::DidGetFileInfo
, weak_factory_
.GetWeakPtr()));
75 MessageLoop::current()->Run();
78 EXPECT_EQ(File::FILE_OK
, error_
);
79 EXPECT_EQ(expected_info
.size
, file_info_
.size
);
80 EXPECT_EQ(expected_info
.is_directory
, file_info_
.is_directory
);
81 EXPECT_EQ(expected_info
.is_symbolic_link
, file_info_
.is_symbolic_link
);
82 EXPECT_EQ(expected_info
.last_modified
, file_info_
.last_modified
);
83 EXPECT_EQ(expected_info
.last_accessed
, file_info_
.last_accessed
);
84 EXPECT_EQ(expected_info
.creation_time
, file_info_
.creation_time
);
87 TEST_F(FileUtilProxyTest
, GetFileInfo_Directory
) {
89 ASSERT_TRUE(base::CreateDirectory(test_path()));
90 File::Info expected_info
;
91 GetFileInfo(test_path(), &expected_info
);
94 FileUtilProxy::GetFileInfo(
97 Bind(&FileUtilProxyTest::DidGetFileInfo
, weak_factory_
.GetWeakPtr()));
98 MessageLoop::current()->Run();
101 EXPECT_EQ(File::FILE_OK
, error_
);
102 EXPECT_EQ(expected_info
.size
, file_info_
.size
);
103 EXPECT_EQ(expected_info
.is_directory
, file_info_
.is_directory
);
104 EXPECT_EQ(expected_info
.is_symbolic_link
, file_info_
.is_symbolic_link
);
105 EXPECT_EQ(expected_info
.last_modified
, file_info_
.last_modified
);
106 EXPECT_EQ(expected_info
.last_accessed
, file_info_
.last_accessed
);
107 EXPECT_EQ(expected_info
.creation_time
, file_info_
.creation_time
);
110 TEST_F(FileUtilProxyTest
, Touch
) {
111 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
112 Time last_accessed_time
= Time::Now() - TimeDelta::FromDays(12345);
113 Time last_modified_time
= Time::Now() - TimeDelta::FromHours(98765);
115 FileUtilProxy::Touch(
120 Bind(&FileUtilProxyTest::DidFinish
, weak_factory_
.GetWeakPtr()));
121 MessageLoop::current()->Run();
122 EXPECT_EQ(File::FILE_OK
, error_
);
125 GetFileInfo(test_path(), &info
);
127 // The returned values may only have the seconds precision, so we cast
128 // the double values to int here.
129 EXPECT_EQ(static_cast<int>(last_modified_time
.ToDoubleT()),
130 static_cast<int>(info
.last_modified
.ToDoubleT()));
131 EXPECT_EQ(static_cast<int>(last_accessed_time
.ToDoubleT()),
132 static_cast<int>(info
.last_accessed
.ToDoubleT()));