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/scoped_file_opener.h"
9 #include "base/files/file.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/thread_task_runner_handle.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 class TestingProvidedFileSystem
: public FakeProvidedFileSystem
{
25 TestingProvidedFileSystem()
26 : FakeProvidedFileSystem(ProvidedFileSystemInfo()) {}
28 ~TestingProvidedFileSystem() override
{}
30 AbortCallback
OpenFile(const base::FilePath
& file_path
,
32 const OpenFileCallback
& callback
) override
{
33 open_callback_
= callback
;
34 return base::Bind(&TestingProvidedFileSystem::AbortOpen
,
35 base::Unretained(this));
38 AbortCallback
CloseFile(
40 const storage::AsyncFileUtil::StatusCallback
& callback
) override
{
41 close_requests_
.push_back(file_handle
);
42 callback
.Run(base::File::FILE_OK
);
43 return AbortCallback();
46 const OpenFileCallback
& open_callback() const { return open_callback_
; }
47 const std::vector
<int> close_requests() const { return close_requests_
; }
50 OpenFileCallback open_callback_
;
51 std::vector
<int> close_requests_
;
54 open_callback_
.Run(-1, base::File::FILE_ERROR_ABORT
);
55 open_callback_
= OpenFileCallback();
59 typedef std::vector
<std::pair
<int, base::File::Error
>> OpenLog
;
61 void LogOpen(OpenLog
* log
, int file_handle
, base::File::Error result
) {
62 log
->push_back(std::make_pair(file_handle
, result
));
67 TEST(ScopedFileOpenerTest
, AbortWhileOpening
) {
68 TestingProvidedFileSystem file_system
;
69 content::TestBrowserThreadBundle thread_bundle
;
72 ScopedFileOpener
file_opener(&file_system
, base::FilePath(),
74 base::Bind(&LogOpen
, &log
));
75 base::RunLoop().RunUntilIdle();
76 EXPECT_FALSE(file_system
.open_callback().is_null());
78 ASSERT_EQ(1u, log
.size());
79 EXPECT_EQ(-1, log
[0].first
);
80 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, log
[0].second
);
82 base::RunLoop().RunUntilIdle();
83 EXPECT_EQ(0u, file_system
.close_requests().size());
86 TEST(ScopedFileOpenerTest
, CloseWhileOpening
) {
87 TestingProvidedFileSystem file_system
;
88 content::TestBrowserThreadBundle thread_bundle
;
91 ScopedFileOpener
file_opener(&file_system
, base::FilePath(),
93 base::Bind(&LogOpen
, &log
));
94 base::RunLoop().RunUntilIdle();
95 ASSERT_FALSE(file_system
.open_callback().is_null());
96 // Complete opening asynchronously, so after trying to abort.
97 const ProvidedFileSystemInterface::OpenFileCallback open_callback
=
98 file_system
.open_callback();
99 base::ThreadTaskRunnerHandle::Get()->PostTask(
100 FROM_HERE
, base::Bind(open_callback
, 123, base::File::FILE_OK
));
103 // Wait until the open callback is called asynchonously.
104 base::RunLoop().RunUntilIdle();
106 ASSERT_EQ(1u, log
.size());
107 EXPECT_EQ(-1, log
[0].first
);
108 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, log
[0].second
);
110 ASSERT_EQ(1u, file_system
.close_requests().size());
111 EXPECT_EQ(123, file_system
.close_requests()[0]);
114 TEST(ScopedFileOpenerTest
, CloseAfterOpening
) {
115 TestingProvidedFileSystem file_system
;
116 content::TestBrowserThreadBundle thread_bundle
;
119 ScopedFileOpener
file_opener(&file_system
, base::FilePath(),
121 base::Bind(&LogOpen
, &log
));
122 base::RunLoop().RunUntilIdle();
123 ASSERT_FALSE(file_system
.open_callback().is_null());
124 file_system
.open_callback().Run(123, base::File::FILE_OK
);
127 ASSERT_EQ(1u, log
.size());
128 EXPECT_EQ(123, log
[0].first
);
129 EXPECT_EQ(base::File::FILE_OK
, log
[0].second
);
131 ASSERT_EQ(1u, file_system
.close_requests().size());
132 EXPECT_EQ(123, file_system
.close_requests()[0]);
135 } // namespace file_system_provider
136 } // namespace chromeos