Roll src/third_party/WebKit 4619053:6b63e20 (svn 201059:201060)
[chromium-blink-merge.git] / base / files / file_util_proxy_unittest.cc
blob74083699f00f8ff2b0ed14698c9f4beb602c240f
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"
7 #include "base/bind.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/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
16 class FileUtilProxyTest : public testing::Test {
17 public:
18 FileUtilProxyTest()
19 : file_thread_("FileUtilProxyTestFileThread"),
20 error_(File::FILE_OK),
21 weak_factory_(this) {}
23 void SetUp() override {
24 ASSERT_TRUE(dir_.CreateUniqueTempDir());
25 ASSERT_TRUE(file_thread_.Start());
28 void DidFinish(File::Error error) {
29 error_ = error;
30 MessageLoop::current()->QuitWhenIdle();
33 void DidGetFileInfo(File::Error error,
34 const File::Info& file_info) {
35 error_ = error;
36 file_info_ = file_info;
37 MessageLoop::current()->QuitWhenIdle();
40 protected:
41 TaskRunner* file_task_runner() const {
42 return file_thread_.task_runner().get();
44 const FilePath& test_dir_path() const { return dir_.path(); }
45 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
47 MessageLoopForIO message_loop_;
48 Thread file_thread_;
50 ScopedTempDir dir_;
51 File::Error error_;
52 FilePath path_;
53 File::Info file_info_;
54 std::vector<char> buffer_;
55 WeakPtrFactory<FileUtilProxyTest> weak_factory_;
59 TEST_F(FileUtilProxyTest, GetFileInfo_File) {
60 // Setup.
61 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
62 File::Info expected_info;
63 GetFileInfo(test_path(), &expected_info);
65 // Run.
66 FileUtilProxy::GetFileInfo(
67 file_task_runner(),
68 test_path(),
69 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
70 MessageLoop::current()->Run();
72 // Verify.
73 EXPECT_EQ(File::FILE_OK, error_);
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);
82 TEST_F(FileUtilProxyTest, GetFileInfo_Directory) {
83 // Setup.
84 ASSERT_TRUE(base::CreateDirectory(test_path()));
85 File::Info expected_info;
86 GetFileInfo(test_path(), &expected_info);
88 // Run.
89 FileUtilProxy::GetFileInfo(
90 file_task_runner(),
91 test_path(),
92 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
93 MessageLoop::current()->Run();
95 // Verify.
96 EXPECT_EQ(File::FILE_OK, error_);
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);
105 TEST_F(FileUtilProxyTest, Touch) {
106 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
107 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
108 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
110 FileUtilProxy::Touch(
111 file_task_runner(),
112 test_path(),
113 last_accessed_time,
114 last_modified_time,
115 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
116 MessageLoop::current()->Run();
117 EXPECT_EQ(File::FILE_OK, error_);
119 File::Info info;
120 GetFileInfo(test_path(), &info);
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()));
130 } // namespace base