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 "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 namespace file_system_provider
{
24 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
25 const char kFileSystemId
[] = "camera-pictures";
26 const char kDisplayName
[] = "Camera Pictures";
28 typedef std::vector
<base::File::Error
> StatusLog
;
29 typedef std::vector
<std::pair
<int, base::File::Error
>> OpenLog
;
31 // Writes a |result| to the |log| vector for a status callback.
32 void LogStatus(StatusLog
* log
, base::File::Error result
) {
33 log
->push_back(result
);
36 // Writes a |result| to the |log| vector for opening a file.
37 void LogOpen(OpenLog
* log
, int handle
, base::File::Error result
) {
38 log
->push_back(std::make_pair(handle
, result
));
43 class FileSystemProviderThrottledFileSystemTest
: public testing::Test
{
45 FileSystemProviderThrottledFileSystemTest() {}
46 ~FileSystemProviderThrottledFileSystemTest() override
{}
48 void SetUp() override
{}
50 // Initializes the throttled file system with |limit| number of opened files
51 // at once. If 0, then no limit.
52 void SetUpFileSystem(size_t limit
) {
53 MountOptions
options(kFileSystemId
, kDisplayName
);
54 options
.opened_files_limit
= limit
;
56 ProvidedFileSystemInfo
file_system_info(
57 kExtensionId
, options
, base::FilePath() /* mount_path */,
58 false /* configurable */, true /* watchable */,
59 extensions::SOURCE_FILE
);
61 file_system_
.reset(new ThrottledFileSystem(
62 make_scoped_ptr(new FakeProvidedFileSystem(file_system_info
))));
65 content::TestBrowserThreadBundle thread_bundle_
;
66 scoped_ptr
<ThrottledFileSystem
> file_system_
;
69 TEST_F(FileSystemProviderThrottledFileSystemTest
, OpenFile_LimitedToOneAtOnce
) {
72 OpenLog first_open_log
;
73 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
74 base::Bind(&LogOpen
, &first_open_log
));
76 OpenLog second_open_log
;
77 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
78 base::Bind(&LogOpen
, &second_open_log
));
80 base::RunLoop().RunUntilIdle();
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());
86 // Close the first file.
88 file_system_
->CloseFile(first_open_log
[0].first
,
89 base::Bind(&LogStatus
, &close_log
));
91 base::RunLoop().RunUntilIdle();
92 ASSERT_EQ(1u, close_log
.size());
93 EXPECT_EQ(base::File::FILE_OK
, close_log
[0]);
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
);
102 TEST_F(FileSystemProviderThrottledFileSystemTest
, OpenFile_NoLimit
) {
103 SetUpFileSystem(0); // No limit.
105 OpenLog first_open_log
;
106 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
107 base::Bind(&LogOpen
, &first_open_log
));
109 OpenLog second_open_log
;
110 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
111 base::Bind(&LogOpen
, &second_open_log
));
113 base::RunLoop().RunUntilIdle();
115 ASSERT_EQ(1u, first_open_log
.size());
116 EXPECT_EQ(base::File::FILE_OK
, first_open_log
[0].second
);
117 ASSERT_EQ(1u, second_open_log
.size());
118 EXPECT_EQ(base::File::FILE_OK
, second_open_log
[0].second
);
121 StatusLog first_close_log
;
122 file_system_
->CloseFile(first_open_log
[0].first
,
123 base::Bind(&LogStatus
, &first_close_log
));
125 StatusLog second_close_log
;
126 file_system_
->CloseFile(second_open_log
[0].first
,
127 base::Bind(&LogStatus
, &second_close_log
));
129 base::RunLoop().RunUntilIdle();
130 ASSERT_EQ(1u, first_close_log
.size());
131 EXPECT_EQ(base::File::FILE_OK
, first_close_log
[0]);
132 ASSERT_EQ(1u, second_close_log
.size());
133 EXPECT_EQ(base::File::FILE_OK
, second_close_log
[0]);
135 // Confirm, that files are not opened again.
136 EXPECT_EQ(1u, first_open_log
.size());
137 EXPECT_EQ(1u, second_open_log
.size());
140 TEST_F(FileSystemProviderThrottledFileSystemTest
, AbortAfterRun
) {
143 OpenLog first_open_log
;
144 AbortCallback abort_callback
=
145 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
146 base::Bind(&LogOpen
, &first_open_log
));
148 OpenLog second_open_log
;
149 file_system_
->OpenFile(base::FilePath(kFakeFilePath
), OPEN_FILE_MODE_READ
,
150 base::Bind(&LogOpen
, &second_open_log
));
152 base::RunLoop().RunUntilIdle();
154 ASSERT_EQ(1u, first_open_log
.size());
155 EXPECT_EQ(base::File::FILE_OK
, first_open_log
[0].second
);
156 EXPECT_EQ(0u, second_open_log
.size());
159 } // namespace file_system_provider
160 } // namespace chromeos