Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / throttled_file_system_unittest.cc
blob02f1b2741f68a373e146b29009352e6719f15714
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"
7 #include <vector>
9 #include "base/files/file.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h"
13 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
15 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace chromeos {
20 namespace file_system_provider {
21 namespace {
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));
40 } // namespace
42 class FileSystemProviderThrottledFileSystemTest : public testing::Test {
43 protected:
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(
56 kExtensionId, options, base::FilePath() /* mount_path */,
57 false /* configurable */, true /* watchable */,
58 extensions::SOURCE_FILE);
60 file_system_.reset(new ThrottledFileSystem(
61 make_scoped_ptr(new FakeProvidedFileSystem(file_system_info))));
64 content::TestBrowserThreadBundle thread_bundle_;
65 scoped_ptr<ThrottledFileSystem> file_system_;
68 TEST_F(FileSystemProviderThrottledFileSystemTest, OpenFile_LimitedToOneAtOnce) {
69 SetUpFileSystem(1);
71 OpenLog first_open_log;
72 file_system_->OpenFile(base::FilePath(kFakeFilePath), OPEN_FILE_MODE_READ,
73 base::Bind(&LogOpen, &first_open_log));
75 OpenLog second_open_log;
76 file_system_->OpenFile(base::FilePath(kFakeFilePath), OPEN_FILE_MODE_READ,
77 base::Bind(&LogOpen, &second_open_log));
79 base::RunLoop().RunUntilIdle();
81 ASSERT_EQ(1u, first_open_log.size());
82 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second);
83 EXPECT_EQ(0u, second_open_log.size());
85 // Close the first file.
86 StatusLog close_log;
87 file_system_->CloseFile(first_open_log[0].first,
88 base::Bind(&LogStatus, &close_log));
90 base::RunLoop().RunUntilIdle();
91 ASSERT_EQ(1u, close_log.size());
92 EXPECT_EQ(base::File::FILE_OK, close_log[0]);
94 // The second enqueued file should be opened.
95 ASSERT_EQ(1u, first_open_log.size());
96 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second);
97 ASSERT_EQ(1u, second_open_log.size());
98 EXPECT_EQ(base::File::FILE_OK, second_open_log[0].second);
101 TEST_F(FileSystemProviderThrottledFileSystemTest, OpenFile_NoLimit) {
102 SetUpFileSystem(0); // No limit.
104 OpenLog first_open_log;
105 file_system_->OpenFile(base::FilePath(kFakeFilePath), OPEN_FILE_MODE_READ,
106 base::Bind(&LogOpen, &first_open_log));
108 OpenLog second_open_log;
109 file_system_->OpenFile(base::FilePath(kFakeFilePath), OPEN_FILE_MODE_READ,
110 base::Bind(&LogOpen, &second_open_log));
112 base::RunLoop().RunUntilIdle();
114 ASSERT_EQ(1u, first_open_log.size());
115 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second);
116 ASSERT_EQ(1u, second_open_log.size());
117 EXPECT_EQ(base::File::FILE_OK, second_open_log[0].second);
119 // Close files.
120 StatusLog first_close_log;
121 file_system_->CloseFile(first_open_log[0].first,
122 base::Bind(&LogStatus, &first_close_log));
124 StatusLog second_close_log;
125 file_system_->CloseFile(second_open_log[0].first,
126 base::Bind(&LogStatus, &second_close_log));
128 base::RunLoop().RunUntilIdle();
129 ASSERT_EQ(1u, first_close_log.size());
130 EXPECT_EQ(base::File::FILE_OK, first_close_log[0]);
131 ASSERT_EQ(1u, second_close_log.size());
132 EXPECT_EQ(base::File::FILE_OK, second_close_log[0]);
134 // Confirm, that files are not opened again.
135 EXPECT_EQ(1u, first_open_log.size());
136 EXPECT_EQ(1u, second_open_log.size());
139 TEST_F(FileSystemProviderThrottledFileSystemTest, AbortAfterRun) {
140 SetUpFileSystem(1);
142 OpenLog first_open_log;
143 AbortCallback abort_callback =
144 file_system_->OpenFile(base::FilePath(kFakeFilePath), OPEN_FILE_MODE_READ,
145 base::Bind(&LogOpen, &first_open_log));
147 OpenLog second_open_log;
148 file_system_->OpenFile(base::FilePath(kFakeFilePath), OPEN_FILE_MODE_READ,
149 base::Bind(&LogOpen, &second_open_log));
151 base::RunLoop().RunUntilIdle();
153 ASSERT_EQ(1u, first_open_log.size());
154 EXPECT_EQ(base::File::FILE_OK, first_open_log[0].second);
155 EXPECT_EQ(0u, second_open_log.size());
158 } // namespace file_system_provider
159 } // namespace chromeos