OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/chromeos/file_system_provider/throttled_file_system.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/files/file.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" |
| 14 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system
.h" |
| 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chromeos { |
| 20 namespace file_system_provider { |
| 21 namespace { |
| 22 |
| 23 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 24 const char kFileSystemId[] = "camera-pictures"; |
| 25 const char kDisplayName[] = "Camera Pictures"; |
| 26 |
| 27 typedef std::vector<base::File::Error> StatusLog; |
| 28 typedef std::vector<std::pair<int, base::File::Error>> OpenLog; |
| 29 |
| 30 // Writes a |result| to the |log| vector for a status callback. |
| 31 void LogStatus(StatusLog* log, base::File::Error result) { |
| 32 log->push_back(result); |
| 33 } |
| 34 |
| 35 // Writes a |result| to the |log| vector for opening a file. |
| 36 void LogOpen(OpenLog* log, int handle, base::File::Error result) { |
| 37 log->push_back(std::make_pair(handle, result)); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 class FileSystemProviderThrottledFileSystemTest : public testing::Test { |
| 43 protected: |
| 44 FileSystemProviderThrottledFileSystemTest() {} |
| 45 virtual ~FileSystemProviderThrottledFileSystemTest() {} |
| 46 |
| 47 virtual void SetUp() override {} |
| 48 |
| 49 // Initializes the throttled file system with |limit| number of opened files |
| 50 // at once. If 0, then no limit. |
| 51 void SetUpFileSystem(size_t limit) { |
| 52 MountOptions options(kFileSystemId, kDisplayName); |
| 53 if (limit) |
| 54 options.opened_files_limit = limit; |
| 55 |
| 56 ProvidedFileSystemInfo file_system_info(kExtensionId, options, |
| 57 base::FilePath() /* mount_path */); |
| 58 |
| 59 file_system_.reset(new ThrottledFileSystem( |
| 60 make_scoped_ptr(new FakeProvidedFileSystem(file_system_info)))); |
| 61 } |
| 62 |
| 63 content::TestBrowserThreadBundle thread_bundle_; |
| 64 scoped_ptr<ThrottledFileSystem> file_system_; |
| 65 }; |
| 66 |
| 67 TEST_F(FileSystemProviderThrottledFileSystemTest, OpenFile_LimitedToOneAtOnce) { |
| 68 SetUpFileSystem(1); |
| 69 |
| 70 OpenLog first_open_log; |
| 71 file_system_->OpenFile(base::FilePath(kFakeFilePath), |
| 72 ThrottledFileSystem::OPEN_FILE_MODE_READ, |
| 73 base::Bind(&LogOpen, &first_open_log)); |
| 74 |
| 75 OpenLog second_open_log; |
| 76 file_system_->OpenFile(base::FilePath(kFakeFilePath), |
| 77 ThrottledFileSystem::OPEN_FILE_MODE_READ, |
| 78 base::Bind(&LogOpen, &second_open_log)); |
| 79 |
| 80 base::RunLoop().RunUntilIdle(); |
| 81 |
| 82 ASSERT_EQ(1u, first_open_log.size()); |
| 83 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second); |
| 84 EXPECT_EQ(0u, second_open_log.size()); |
| 85 |
| 86 // Close the first file. |
| 87 StatusLog close_log; |
| 88 file_system_->CloseFile(first_open_log[0].first, |
| 89 base::Bind(&LogStatus, &close_log)); |
| 90 |
| 91 base::RunLoop().RunUntilIdle(); |
| 92 ASSERT_EQ(1u, close_log.size()); |
| 93 EXPECT_EQ(base::File::FILE_OK, close_log[0]); |
| 94 |
| 95 // The second enqueued file should be opened. |
| 96 ASSERT_EQ(1u, first_open_log.size()); |
| 97 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second); |
| 98 ASSERT_EQ(1u, second_open_log.size()); |
| 99 EXPECT_EQ(base::File::FILE_OK, second_open_log[0].second); |
| 100 } |
| 101 |
| 102 TEST_F(FileSystemProviderThrottledFileSystemTest, OpenFile_NoLimit) { |
| 103 SetUpFileSystem(0); // No limit. |
| 104 |
| 105 OpenLog first_open_log; |
| 106 file_system_->OpenFile(base::FilePath(kFakeFilePath), |
| 107 ThrottledFileSystem::OPEN_FILE_MODE_READ, |
| 108 base::Bind(&LogOpen, &first_open_log)); |
| 109 |
| 110 OpenLog second_open_log; |
| 111 file_system_->OpenFile(base::FilePath(kFakeFilePath), |
| 112 ThrottledFileSystem::OPEN_FILE_MODE_READ, |
| 113 base::Bind(&LogOpen, &second_open_log)); |
| 114 |
| 115 base::RunLoop().RunUntilIdle(); |
| 116 |
| 117 ASSERT_EQ(1u, first_open_log.size()); |
| 118 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second); |
| 119 ASSERT_EQ(1u, second_open_log.size()); |
| 120 EXPECT_EQ(base::File::FILE_OK, second_open_log[0].second); |
| 121 |
| 122 // Close files. |
| 123 StatusLog first_close_log; |
| 124 file_system_->CloseFile(first_open_log[0].first, |
| 125 base::Bind(&LogStatus, &first_close_log)); |
| 126 |
| 127 StatusLog second_close_log; |
| 128 file_system_->CloseFile(second_open_log[0].first, |
| 129 base::Bind(&LogStatus, &second_close_log)); |
| 130 |
| 131 base::RunLoop().RunUntilIdle(); |
| 132 ASSERT_EQ(1u, first_close_log.size()); |
| 133 EXPECT_EQ(base::File::FILE_OK, first_close_log[0]); |
| 134 ASSERT_EQ(1u, second_close_log.size()); |
| 135 EXPECT_EQ(base::File::FILE_OK, second_close_log[0]); |
| 136 |
| 137 // Confirm, that files are not opened again. |
| 138 EXPECT_EQ(1u, first_open_log.size()); |
| 139 EXPECT_EQ(1u, second_open_log.size()); |
| 140 } |
| 141 |
| 142 TEST_F(FileSystemProviderThrottledFileSystemTest, AbortAfterRun) { |
| 143 SetUpFileSystem(1); |
| 144 |
| 145 OpenLog first_open_log; |
| 146 AbortCallback abort_callback = file_system_->OpenFile( |
| 147 base::FilePath(kFakeFilePath), ThrottledFileSystem::OPEN_FILE_MODE_READ, |
| 148 base::Bind(&LogOpen, &first_open_log)); |
| 149 |
| 150 OpenLog second_open_log; |
| 151 file_system_->OpenFile(base::FilePath(kFakeFilePath), |
| 152 ThrottledFileSystem::OPEN_FILE_MODE_READ, |
| 153 base::Bind(&LogOpen, &second_open_log)); |
| 154 |
| 155 base::RunLoop().RunUntilIdle(); |
| 156 |
| 157 ASSERT_EQ(1u, first_open_log.size()); |
| 158 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second); |
| 159 EXPECT_EQ(0u, second_open_log.size()); |
| 160 |
| 161 // The first file is opened, so the opening operation has completed, then |
| 162 // aborting it should result in an error. This is tested, as from the queue |
| 163 // point of view, the opening task stays in the queue until closing the file. |
| 164 StatusLog abort_log; |
| 165 abort_callback.Run(base::Bind(&LogStatus, &abort_log)); |
| 166 base::RunLoop().RunUntilIdle(); |
| 167 |
| 168 ASSERT_EQ(1u, abort_log.size()); |
| 169 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, abort_log[0]); |
| 170 |
| 171 // Confirm, that the second task is not executed after a invalid abort of the |
| 172 // first one. |
| 173 EXPECT_EQ(1u, first_open_log.size()); |
| 174 EXPECT_EQ(0u, second_open_log.size()); |
| 175 } |
| 176 } // namespace file_system_provider |
| 177 } // namespace chromeos |
OLD | NEW |