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
5#include "base/files/file_util_proxy.h"
6
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"
14
15namespace base {
16
17class FileUtilProxyTest : public testing::Test {
18 public:
19  FileUtilProxyTest()
20      : file_thread_("FileUtilProxyTestFileThread"),
21        error_(File::FILE_OK),
22        created_(false),
23        bytes_written_(-1),
24        weak_factory_(this) {}
25
26  virtual void SetUp() OVERRIDE {
27    ASSERT_TRUE(dir_.CreateUniqueTempDir());
28    ASSERT_TRUE(file_thread_.Start());
29  }
30
31  void DidFinish(File::Error error) {
32    error_ = error;
33    MessageLoop::current()->QuitWhenIdle();
34  }
35
36  void DidGetFileInfo(File::Error error,
37                      const File::Info& file_info) {
38    error_ = error;
39    file_info_ = file_info;
40    MessageLoop::current()->QuitWhenIdle();
41  }
42
43 protected:
44  TaskRunner* file_task_runner() const {
45    return file_thread_.message_loop_proxy().get();
46  }
47  const FilePath& test_dir_path() const { return dir_.path(); }
48  const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
49
50  MessageLoopForIO message_loop_;
51  Thread file_thread_;
52
53  ScopedTempDir dir_;
54  File::Error error_;
55  bool created_;
56  FilePath path_;
57  File::Info file_info_;
58  std::vector<char> buffer_;
59  int bytes_written_;
60  WeakPtrFactory<FileUtilProxyTest> weak_factory_;
61};
62
63
64TEST_F(FileUtilProxyTest, GetFileInfo_File) {
65  // Setup.
66  ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
67  File::Info expected_info;
68  GetFileInfo(test_path(), &expected_info);
69
70  // Run.
71  FileUtilProxy::GetFileInfo(
72      file_task_runner(),
73      test_path(),
74      Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
75  MessageLoop::current()->Run();
76
77  // Verify.
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);
85}
86
87TEST_F(FileUtilProxyTest, GetFileInfo_Directory) {
88  // Setup.
89  ASSERT_TRUE(base::CreateDirectory(test_path()));
90  File::Info expected_info;
91  GetFileInfo(test_path(), &expected_info);
92
93  // Run.
94  FileUtilProxy::GetFileInfo(
95      file_task_runner(),
96      test_path(),
97      Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
98  MessageLoop::current()->Run();
99
100  // Verify.
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);
108}
109
110TEST_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);
114
115  FileUtilProxy::Touch(
116      file_task_runner(),
117      test_path(),
118      last_accessed_time,
119      last_modified_time,
120      Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
121  MessageLoop::current()->Run();
122  EXPECT_EQ(File::FILE_OK, error_);
123
124  File::Info info;
125  GetFileInfo(test_path(), &info);
126
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()));
133}
134
135}  // namespace base
136