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.
5 #include "chrome/browser/chromeos/file_system_provider/throttled_file_system.h"
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"
20 namespace file_system_provider
{
23 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
24 const char kFileSystemId
[] = "camera-pictures";
25 const char kDisplayName
[] = "Camera Pictures";
27 typedef std::vector
<base::File::Error
> StatusLog
;
28 typedef std::vector
<std::pair
<int, base::File::Error
>> OpenLog
;
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
);
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
));
42 class FileSystemProviderThrottledFileSystemTest
: public testing::Test
{
44 FileSystemProviderThrottledFileSystemTest() {}
45 ~FileSystemProviderThrottledFileSystemTest() override
{}
47 void SetUp() override
{}
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 options
.opened_files_limit
= limit
;
55 ProvidedFileSystemInfo
file_system_info(kExtensionId
, options
,
56 base::FilePath() /* mount_path */);
58 file_system_
.reset(new ThrottledFileSystem(
59 make_scoped_ptr(new FakeProvidedFileSystem(file_system_info
))));
62 content::TestBrowserThreadBundle thread_bundle_
;
63 scoped_ptr
<ThrottledFileSystem
> file_system_
;
66 TEST_F(FileSystemProviderThrottledFileSystemTest
, OpenFile_LimitedToOneAtOnce
) {
69 OpenLog first_open_log
;
70 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
71 base::Bind(&LogOpen
, &first_open_log
));
73 OpenLog second_open_log
;
74 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
75 base::Bind(&LogOpen
, &second_open_log
));
77 base::RunLoop().RunUntilIdle();
79 ASSERT_EQ(1u, first_open_log
.size());
80 EXPECT_EQ(base::File::FILE_OK
, first_open_log
[0].second
);
81 EXPECT_EQ(0u, second_open_log
.size());
83 // Close the first file.
85 file_system_
->CloseFile(first_open_log
[0].first
,
86 base::Bind(&LogStatus
, &close_log
));
88 base::RunLoop().RunUntilIdle();
89 ASSERT_EQ(1u, close_log
.size());
90 EXPECT_EQ(base::File::FILE_OK
, close_log
[0]);
92 // The second enqueued file should be opened.
93 ASSERT_EQ(1u, first_open_log
.size());
94 EXPECT_EQ(base::File::FILE_OK
, first_open_log
[0].second
);
95 ASSERT_EQ(1u, second_open_log
.size());
96 EXPECT_EQ(base::File::FILE_OK
, second_open_log
[0].second
);
99 TEST_F(FileSystemProviderThrottledFileSystemTest
, OpenFile_NoLimit
) {
100 SetUpFileSystem(0); // No limit.
102 OpenLog first_open_log
;
103 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
104 base::Bind(&LogOpen
, &first_open_log
));
106 OpenLog second_open_log
;
107 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
108 base::Bind(&LogOpen
, &second_open_log
));
110 base::RunLoop().RunUntilIdle();
112 ASSERT_EQ(1u, first_open_log
.size());
113 EXPECT_EQ(base::File::FILE_OK
, first_open_log
[0].second
);
114 ASSERT_EQ(1u, second_open_log
.size());
115 EXPECT_EQ(base::File::FILE_OK
, second_open_log
[0].second
);
118 StatusLog first_close_log
;
119 file_system_
->CloseFile(first_open_log
[0].first
,
120 base::Bind(&LogStatus
, &first_close_log
));
122 StatusLog second_close_log
;
123 file_system_
->CloseFile(second_open_log
[0].first
,
124 base::Bind(&LogStatus
, &second_close_log
));
126 base::RunLoop().RunUntilIdle();
127 ASSERT_EQ(1u, first_close_log
.size());
128 EXPECT_EQ(base::File::FILE_OK
, first_close_log
[0]);
129 ASSERT_EQ(1u, second_close_log
.size());
130 EXPECT_EQ(base::File::FILE_OK
, second_close_log
[0]);
132 // Confirm, that files are not opened again.
133 EXPECT_EQ(1u, first_open_log
.size());
134 EXPECT_EQ(1u, second_open_log
.size());
137 TEST_F(FileSystemProviderThrottledFileSystemTest
, AbortAfterRun
) {
140 OpenLog first_open_log
;
141 AbortCallback abort_callback
=
142 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
143 base::Bind(&LogOpen
, &first_open_log
));
145 OpenLog second_open_log
;
146 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
147 base::Bind(&LogOpen
, &second_open_log
));
149 base::RunLoop().RunUntilIdle();
151 ASSERT_EQ(1u, first_open_log
.size());
152 EXPECT_EQ(base::File::FILE_OK
, first_open_log
[0].second
);
153 EXPECT_EQ(0u, second_open_log
.size());
156 } // namespace file_system_provider
157 } // namespace chromeos