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