mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Yeunjoo Choi | 7073d6e | 2021-05-11 01:40:48 | [diff] [blame] | 5 | #include "chrome/browser/ash/file_system_provider/operations/create_file.h" |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 6 | |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 7 | #include <memory> |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 11 | #include "base/bind.h" |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 12 | #include "base/files/file.h" |
| 13 | #include "base/files/file_path.h" |
Yeunjoo Choi | 627505b7b | 2021-05-13 15:56:31 | [diff] [blame^] | 14 | #include "chrome/browser/ash/file_system_provider/icon_set.h" |
Yeunjoo Choi | 7073d6e | 2021-05-11 01:40:48 | [diff] [blame] | 15 | #include "chrome/browser/ash/file_system_provider/operations/test_util.h" |
Yeunjoo Choi | 627505b7b | 2021-05-13 15:56:31 | [diff] [blame^] | 16 | #include "chrome/browser/ash/file_system_provider/provided_file_system_interface.h" |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 17 | #include "chrome/common/extensions/api/file_system_provider.h" |
mtomasz | b9c370a | 2015-05-15 11:54:18 | [diff] [blame] | 18 | #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h" |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 19 | #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 20 | #include "extensions/browser/event_router.h" |
DongJun Kim | febb3c2 | 2019-10-21 02:08:06 | [diff] [blame] | 21 | #include "storage/browser/file_system/async_file_util.h" |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 23 | |
| 24 | namespace chromeos { |
| 25 | namespace file_system_provider { |
| 26 | namespace operations { |
| 27 | namespace { |
| 28 | |
| 29 | const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 30 | const char kFileSystemId[] = "testing-file-system"; |
| 31 | const int kRequestId = 2; |
mtomasz | f729881 | 2014-12-26 02:28:58 | [diff] [blame] | 32 | const base::FilePath::CharType kFilePath[] = |
| 33 | FILE_PATH_LITERAL("/kitty/and/puppy/happy"); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | class FileSystemProviderOperationsCreateFileTest : public testing::Test { |
| 38 | protected: |
| 39 | FileSystemProviderOperationsCreateFileTest() {} |
dcheng | b35a132 | 2015-01-16 02:21:32 | [diff] [blame] | 40 | ~FileSystemProviderOperationsCreateFileTest() override {} |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 41 | |
dcheng | b35a132 | 2015-01-16 02:21:32 | [diff] [blame] | 42 | void SetUp() override { |
mtomasz | 00336db6f | 2014-10-14 05:57:37 | [diff] [blame] | 43 | MountOptions mount_options(kFileSystemId, "" /* display_name */); |
| 44 | mount_options.writable = true; |
mtomasz | b9c370a | 2015-05-15 11:54:18 | [diff] [blame] | 45 | file_system_info_ = ProvidedFileSystemInfo( |
| 46 | kExtensionId, mount_options, base::FilePath(), false /* configurable */, |
Bailey Berro | f5b3dd6 | 2018-01-18 06:30:14 | [diff] [blame] | 47 | true /* watchable */, extensions::SOURCE_FILE, IconSet()); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ProvidedFileSystemInfo file_system_info_; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) { |
mtomasz | 091a0ad7 | 2014-09-12 00:35:38 | [diff] [blame] | 54 | using extensions::api::file_system_provider::CreateFileRequestedOptions; |
| 55 | |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 56 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 57 | util::StatusCallbackLog callback_log; |
| 58 | |
Jan Wilken Dörrie | 6a3b5738 | 2020-04-20 07:55:51 | [diff] [blame] | 59 | CreateFile create_file( |
| 60 | NULL, file_system_info_, base::FilePath(kFilePath), |
| 61 | base::BindOnce(&util::LogStatusCallback, &callback_log)); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 62 | create_file.SetDispatchEventImplForTesting( |
Reilly Grant | 9a4299c | 2021-01-26 03:37:37 | [diff] [blame] | 63 | base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 64 | base::Unretained(&dispatcher))); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 65 | |
| 66 | EXPECT_TRUE(create_file.Execute(kRequestId)); |
| 67 | |
| 68 | ASSERT_EQ(1u, dispatcher.events().size()); |
avi | 3dbcc20 | 2017-03-13 02:48:36 | [diff] [blame] | 69 | extensions::Event* event = dispatcher.events()[0].get(); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 70 | EXPECT_EQ( |
| 71 | extensions::api::file_system_provider::OnCreateFileRequested::kEventName, |
| 72 | event->event_name); |
| 73 | base::ListValue* event_args = event->event_args.get(); |
| 74 | ASSERT_EQ(1u, event_args->GetSize()); |
| 75 | |
mtomasz | 091a0ad7 | 2014-09-12 00:35:38 | [diff] [blame] | 76 | const base::DictionaryValue* options_as_value = NULL; |
| 77 | ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 78 | |
mtomasz | 091a0ad7 | 2014-09-12 00:35:38 | [diff] [blame] | 79 | CreateFileRequestedOptions options; |
| 80 | ASSERT_TRUE( |
| 81 | CreateFileRequestedOptions::Populate(*options_as_value, &options)); |
| 82 | EXPECT_EQ(kFileSystemId, options.file_system_id); |
| 83 | EXPECT_EQ(kRequestId, options.request_id); |
| 84 | EXPECT_EQ(kFilePath, options.file_path); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) { |
| 88 | util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
| 89 | util::StatusCallbackLog callback_log; |
| 90 | |
Jan Wilken Dörrie | 6a3b5738 | 2020-04-20 07:55:51 | [diff] [blame] | 91 | CreateFile create_file( |
| 92 | NULL, file_system_info_, base::FilePath(kFilePath), |
| 93 | base::BindOnce(&util::LogStatusCallback, &callback_log)); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 94 | create_file.SetDispatchEventImplForTesting( |
Reilly Grant | 9a4299c | 2021-01-26 03:37:37 | [diff] [blame] | 95 | base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 96 | base::Unretained(&dispatcher))); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 97 | |
| 98 | EXPECT_FALSE(create_file.Execute(kRequestId)); |
| 99 | } |
| 100 | |
mtomasz@chromium.org | 5b48ea6 | 2014-07-28 08:48:47 | [diff] [blame] | 101 | TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_ReadOnly) { |
| 102 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 103 | util::StatusCallbackLog callback_log; |
| 104 | |
| 105 | const ProvidedFileSystemInfo read_only_file_system_info( |
mtomasz | b9c370a | 2015-05-15 11:54:18 | [diff] [blame] | 106 | kExtensionId, MountOptions(kFileSystemId, "" /* display_name */), |
| 107 | base::FilePath() /* mount_path */, false /* configurable */, |
Bailey Berro | f5b3dd6 | 2018-01-18 06:30:14 | [diff] [blame] | 108 | true /* watchable */, extensions::SOURCE_FILE, IconSet()); |
mtomasz@chromium.org | 5b48ea6 | 2014-07-28 08:48:47 | [diff] [blame] | 109 | |
Jan Wilken Dörrie | 6a3b5738 | 2020-04-20 07:55:51 | [diff] [blame] | 110 | CreateFile create_file( |
| 111 | NULL, read_only_file_system_info, base::FilePath(kFilePath), |
| 112 | base::BindOnce(&util::LogStatusCallback, &callback_log)); |
mtomasz@chromium.org | 5b48ea6 | 2014-07-28 08:48:47 | [diff] [blame] | 113 | create_file.SetDispatchEventImplForTesting( |
Reilly Grant | 9a4299c | 2021-01-26 03:37:37 | [diff] [blame] | 114 | base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 115 | base::Unretained(&dispatcher))); |
mtomasz@chromium.org | 5b48ea6 | 2014-07-28 08:48:47 | [diff] [blame] | 116 | |
| 117 | EXPECT_FALSE(create_file.Execute(kRequestId)); |
| 118 | } |
| 119 | |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 120 | TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) { |
| 121 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 122 | util::StatusCallbackLog callback_log; |
| 123 | |
Jan Wilken Dörrie | 6a3b5738 | 2020-04-20 07:55:51 | [diff] [blame] | 124 | CreateFile create_file( |
| 125 | NULL, file_system_info_, base::FilePath(kFilePath), |
| 126 | base::BindOnce(&util::LogStatusCallback, &callback_log)); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 127 | create_file.SetDispatchEventImplForTesting( |
Reilly Grant | 9a4299c | 2021-01-26 03:37:37 | [diff] [blame] | 128 | base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 129 | base::Unretained(&dispatcher))); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 130 | |
| 131 | EXPECT_TRUE(create_file.Execute(kRequestId)); |
| 132 | |
Peter Boström | 6b70182 | 2021-04-15 03:53:08 | [diff] [blame] | 133 | create_file.OnSuccess(kRequestId, std::make_unique<RequestValue>(), |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 134 | false /* has_more */); |
| 135 | ASSERT_EQ(1u, callback_log.size()); |
| 136 | EXPECT_EQ(base::File::FILE_OK, callback_log[0]); |
| 137 | } |
| 138 | |
| 139 | TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) { |
| 140 | util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 141 | util::StatusCallbackLog callback_log; |
| 142 | |
Jan Wilken Dörrie | 6a3b5738 | 2020-04-20 07:55:51 | [diff] [blame] | 143 | CreateFile create_file( |
| 144 | NULL, file_system_info_, base::FilePath(kFilePath), |
| 145 | base::BindOnce(&util::LogStatusCallback, &callback_log)); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 146 | create_file.SetDispatchEventImplForTesting( |
Reilly Grant | 9a4299c | 2021-01-26 03:37:37 | [diff] [blame] | 147 | base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 148 | base::Unretained(&dispatcher))); |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 149 | |
| 150 | EXPECT_TRUE(create_file.Execute(kRequestId)); |
| 151 | |
Peter Boström | 6b70182 | 2021-04-15 03:53:08 | [diff] [blame] | 152 | create_file.OnError(kRequestId, std::make_unique<RequestValue>(), |
mtomasz@chromium.org | 8878714 | 2014-07-23 06:55:09 | [diff] [blame] | 153 | base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 154 | ASSERT_EQ(1u, callback_log.size()); |
| 155 | EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); |
| 156 | } |
| 157 | |
| 158 | } // namespace operations |
| 159 | } // namespace file_system_provider |
| 160 | } // namespace chromeos |