Remove an old PangoFontDescription forward declaration.
[chromium-blink-merge.git] / base / files / file_util_proxy_unittest.cc
blob5eb68193687234b6a302fa8fd23718e0e784dd15
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/message_loop/message_loop.h"
12 #include "base/threading/thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace base {
17 class FileUtilProxyTest : public testing::Test {
18 public:
19 FileUtilProxyTest()
20 : file_thread_("FileUtilProxyTestFileThread"),
21 error_(File::FILE_OK),
22 weak_factory_(this) {}
24 void SetUp() override {
25 ASSERT_TRUE(dir_.CreateUniqueTempDir());
26 ASSERT_TRUE(file_thread_.Start());
29 void DidFinish(File::Error error) {
30 error_ = error;
31 MessageLoop::current()->QuitWhenIdle();
34 void DidGetFileInfo(File::Error error,
35 const File::Info& file_info) {
36 error_ = error;
37 file_info_ = file_info;
38 MessageLoop::current()->QuitWhenIdle();
41 protected:
42 TaskRunner* file_task_runner() const {
43 return file_thread_.message_loop_proxy().get();
45 const FilePath& test_dir_path() const { return dir_.path(); }
46 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
48 MessageLoopForIO message_loop_;
49 Thread file_thread_;
51 ScopedTempDir dir_;
52 File::Error error_;
53 FilePath path_;
54 File::Info file_info_;
55 std::vector<char> buffer_;
56 WeakPtrFactory<FileUtilProxyTest> weak_factory_;
60 TEST_F(FileUtilProxyTest, GetFileInfo_File) {
61 // Setup.
62 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
63 File::Info expected_info;
64 GetFileInfo(test_path(), &expected_info);
66 // Run.
67 FileUtilProxy::GetFileInfo(
68 file_task_runner(),
69 test_path(),
70 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
71 MessageLoop::current()->Run();
73 // Verify.
74 EXPECT_EQ(File::FILE_OK, error_);
75 EXPECT_EQ(expected_info.size, file_info_.size);
76 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
77 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
78 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
79 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed);
80 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
83 TEST_F(FileUtilProxyTest, GetFileInfo_Directory) {
84 // Setup.
85 ASSERT_TRUE(base::CreateDirectory(test_path()));
86 File::Info expected_info;
87 GetFileInfo(test_path(), &expected_info);
89 // Run.
90 FileUtilProxy::GetFileInfo(
91 file_task_runner(),
92 test_path(),
93 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
94 MessageLoop::current()->Run();
96 // Verify.
97 EXPECT_EQ(File::FILE_OK, error_);
98 EXPECT_EQ(expected_info.size, file_info_.size);
99 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
100 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
101 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
102 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed);
103 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
106 TEST_F(FileUtilProxyTest, Touch) {
107 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
108 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
109 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
111 FileUtilProxy::Touch(
112 file_task_runner(),
113 test_path(),
114 last_accessed_time,
115 last_modified_time,
116 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
117 MessageLoop::current()->Run();
118 EXPECT_EQ(File::FILE_OK, error_);
120 File::Info info;
121 GetFileInfo(test_path(), &info);
123 // The returned values may only have the seconds precision, so we cast
124 // the double values to int here.
125 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()),
126 static_cast<int>(info.last_modified.ToDoubleT()));
127 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()),
128 static_cast<int>(info.last_accessed.ToDoubleT()));
131 } // namespace base