OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2009 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/file_util.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/timer.h" | |
8 #include "chrome/browser/chrome_thread.h" | |
9 #include "chrome/browser/renderer_host/file_system_accessor.h" | |
10 #include "chrome/test/file_test_utils.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 class FileSystemAccessorTest : public testing::Test { | |
14 protected: | |
15 virtual void SetUp() { | |
16 // Make sure the current thread has a message loop. | |
17 EXPECT_TRUE(MessageLoop::current() != NULL); | |
18 | |
19 // Create FILE thread which is used to do file access. | |
20 file_thread_.reset(new ChromeThread(ChromeThread::FILE)); | |
21 | |
22 // Start FILE thread and verify FILE message loop exists. | |
23 file_thread_->Start(); | |
24 EXPECT_TRUE(file_thread_->message_loop() != NULL); | |
25 } | |
26 | |
27 virtual void TearDown() { | |
28 file_thread_->Stop(); | |
29 } | |
30 | |
31 void TestGetFileSize(const FilePath& path, | |
32 void* param, | |
33 int64 expected_result, | |
34 void* expected_param) { | |
35 // Initialize the actual result not equal to the expected result. | |
36 result_ = expected_result + 1; | |
37 param_ = NULL; | |
38 | |
39 FileSystemAccessor::RequestFileSize( | |
40 path, | |
41 param, | |
42 NewCallback(this, &FileSystemAccessorTest::GetFileSizeCallback)); | |
43 | |
44 // Time out if getting file size takes more than 10 seconds. | |
45 const int kGetFileSizeTimeoutSeconds = 10; | |
46 base::OneShotTimer<MessageLoop> timer; | |
47 timer.Start(base::TimeDelta::FromSeconds(kGetFileSizeTimeoutSeconds), | |
48 MessageLoop::current(), &MessageLoop::Quit); | |
49 | |
50 MessageLoop::current()->Run(); | |
51 | |
52 EXPECT_EQ(expected_result, result_); | |
53 EXPECT_EQ(expected_param, param_); | |
54 } | |
55 | |
56 private: | |
57 void GetFileSizeCallback(int64 result, void* param) { | |
58 result_ = result; | |
59 param_ = param; | |
60 MessageLoop::current()->Quit(); | |
61 } | |
62 | |
63 scoped_ptr<ChromeThread> file_thread_; | |
64 FilePath temp_file_; | |
65 int64 result_; | |
66 void* param_; | |
67 MessageLoop loop_; | |
68 }; | |
69 | |
70 TEST_F(FileSystemAccessorTest, GetFileSize) { | |
71 const std::string data("This is test data."); | |
72 | |
73 FilePath path; | |
74 FILE* file = file_util::CreateAndOpenTemporaryFile(&path); | |
75 EXPECT_TRUE(file != NULL); | |
76 size_t bytes_written = fwrite(data.data(), 1, data.length(), file); | |
77 EXPECT_TRUE(file_util::CloseFile(file)); | |
78 | |
79 FileAutoDeleter deleter(path); | |
80 | |
81 TestGetFileSize(path, NULL, bytes_written, NULL); | |
82 } | |
83 | |
84 TEST_F(FileSystemAccessorTest, GetFileSizeWithParam) { | |
85 const std::string data("This is test data."); | |
86 | |
87 FilePath path; | |
88 FILE* file = file_util::CreateAndOpenTemporaryFile(&path); | |
89 EXPECT_TRUE(file != NULL); | |
90 size_t bytes_written = fwrite(data.data(), 1, data.length(), file); | |
91 EXPECT_TRUE(file_util::CloseFile(file)); | |
92 | |
93 FileAutoDeleter deleter(path); | |
94 | |
95 int param = 100; | |
96 TestGetFileSize(path, static_cast<void*>(¶m), | |
97 bytes_written, static_cast<void*>(¶m)); | |
98 } | |
99 | |
100 TEST_F(FileSystemAccessorTest, GetFileSizeEmptyFile) { | |
101 FilePath path; | |
102 EXPECT_TRUE(file_util::CreateTemporaryFile(&path)); | |
103 FileAutoDeleter deleter(path); | |
104 | |
105 TestGetFileSize(path, NULL, 0, NULL); | |
106 } | |
107 | |
108 TEST_F(FileSystemAccessorTest, GetFileSizeNotFound) { | |
109 FilePath path; | |
110 EXPECT_TRUE(file_util::CreateNewTempDirectory( | |
111 FILE_PATH_LITERAL("chrome_test_"), &path)); | |
112 FileAutoDeleter deleter(path); | |
113 | |
114 TestGetFileSize(path.Append(FILE_PATH_LITERAL("foo.txt")), NULL, -1, NULL); | |
115 } | |
OLD | NEW |