[go: nahoru, domu]

blob: 06b3d154cd9c5d3a135cd36107d87ba447aba0bf [file] [log] [blame]
mtomasz@chromium.org88787142014-07-23 06:55:091// 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 Choi7073d6e2021-05-11 01:40:485#include "chrome/browser/ash/file_system_provider/operations/create_file.h"
mtomasz@chromium.org88787142014-07-23 06:55:096
dcheng24002d02016-04-08 02:42:407#include <memory>
mtomasz@chromium.org88787142014-07-23 06:55:098#include <string>
9#include <vector>
10
Sebastien Marchandf1349f52019-01-25 03:16:4111#include "base/bind.h"
mtomasz@chromium.org88787142014-07-23 06:55:0912#include "base/files/file.h"
13#include "base/files/file_path.h"
Yeunjoo Choi627505b7b2021-05-13 15:56:3114#include "chrome/browser/ash/file_system_provider/icon_set.h"
Yeunjoo Choi7073d6e2021-05-11 01:40:4815#include "chrome/browser/ash/file_system_provider/operations/test_util.h"
Yeunjoo Choi627505b7b2021-05-13 15:56:3116#include "chrome/browser/ash/file_system_provider/provided_file_system_interface.h"
mtomasz@chromium.org88787142014-07-23 06:55:0917#include "chrome/common/extensions/api/file_system_provider.h"
mtomaszb9c370a2015-05-15 11:54:1818#include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
mtomasz@chromium.org88787142014-07-23 06:55:0919#include "chrome/common/extensions/api/file_system_provider_internal.h"
20#include "extensions/browser/event_router.h"
DongJun Kimfebb3c22019-10-21 02:08:0621#include "storage/browser/file_system/async_file_util.h"
mtomasz@chromium.org88787142014-07-23 06:55:0922#include "testing/gtest/include/gtest/gtest.h"
mtomasz@chromium.org88787142014-07-23 06:55:0923
24namespace chromeos {
25namespace file_system_provider {
26namespace operations {
27namespace {
28
29const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
30const char kFileSystemId[] = "testing-file-system";
31const int kRequestId = 2;
mtomaszf7298812014-12-26 02:28:5832const base::FilePath::CharType kFilePath[] =
33 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
mtomasz@chromium.org88787142014-07-23 06:55:0934
35} // namespace
36
37class FileSystemProviderOperationsCreateFileTest : public testing::Test {
38 protected:
39 FileSystemProviderOperationsCreateFileTest() {}
dchengb35a1322015-01-16 02:21:3240 ~FileSystemProviderOperationsCreateFileTest() override {}
mtomasz@chromium.org88787142014-07-23 06:55:0941
dchengb35a1322015-01-16 02:21:3242 void SetUp() override {
mtomasz00336db6f2014-10-14 05:57:3743 MountOptions mount_options(kFileSystemId, "" /* display_name */);
44 mount_options.writable = true;
mtomaszb9c370a2015-05-15 11:54:1845 file_system_info_ = ProvidedFileSystemInfo(
46 kExtensionId, mount_options, base::FilePath(), false /* configurable */,
Bailey Berrof5b3dd62018-01-18 06:30:1447 true /* watchable */, extensions::SOURCE_FILE, IconSet());
mtomasz@chromium.org88787142014-07-23 06:55:0948 }
49
50 ProvidedFileSystemInfo file_system_info_;
51};
52
53TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) {
mtomasz091a0ad72014-09-12 00:35:3854 using extensions::api::file_system_provider::CreateFileRequestedOptions;
55
mtomasz@chromium.org88787142014-07-23 06:55:0956 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
57 util::StatusCallbackLog callback_log;
58
Jan Wilken Dörrie6a3b57382020-04-20 07:55:5159 CreateFile create_file(
60 NULL, file_system_info_, base::FilePath(kFilePath),
61 base::BindOnce(&util::LogStatusCallback, &callback_log));
mtomasz@chromium.org88787142014-07-23 06:55:0962 create_file.SetDispatchEventImplForTesting(
Reilly Grant9a4299c2021-01-26 03:37:3763 base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
64 base::Unretained(&dispatcher)));
mtomasz@chromium.org88787142014-07-23 06:55:0965
66 EXPECT_TRUE(create_file.Execute(kRequestId));
67
68 ASSERT_EQ(1u, dispatcher.events().size());
avi3dbcc202017-03-13 02:48:3669 extensions::Event* event = dispatcher.events()[0].get();
mtomasz@chromium.org88787142014-07-23 06:55:0970 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
mtomasz091a0ad72014-09-12 00:35:3876 const base::DictionaryValue* options_as_value = NULL;
77 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
mtomasz@chromium.org88787142014-07-23 06:55:0978
mtomasz091a0ad72014-09-12 00:35:3879 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.org88787142014-07-23 06:55:0985}
86
87TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) {
88 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
89 util::StatusCallbackLog callback_log;
90
Jan Wilken Dörrie6a3b57382020-04-20 07:55:5191 CreateFile create_file(
92 NULL, file_system_info_, base::FilePath(kFilePath),
93 base::BindOnce(&util::LogStatusCallback, &callback_log));
mtomasz@chromium.org88787142014-07-23 06:55:0994 create_file.SetDispatchEventImplForTesting(
Reilly Grant9a4299c2021-01-26 03:37:3795 base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
96 base::Unretained(&dispatcher)));
mtomasz@chromium.org88787142014-07-23 06:55:0997
98 EXPECT_FALSE(create_file.Execute(kRequestId));
99}
100
mtomasz@chromium.org5b48ea62014-07-28 08:48:47101TEST_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(
mtomaszb9c370a2015-05-15 11:54:18106 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
107 base::FilePath() /* mount_path */, false /* configurable */,
Bailey Berrof5b3dd62018-01-18 06:30:14108 true /* watchable */, extensions::SOURCE_FILE, IconSet());
mtomasz@chromium.org5b48ea62014-07-28 08:48:47109
Jan Wilken Dörrie6a3b57382020-04-20 07:55:51110 CreateFile create_file(
111 NULL, read_only_file_system_info, base::FilePath(kFilePath),
112 base::BindOnce(&util::LogStatusCallback, &callback_log));
mtomasz@chromium.org5b48ea62014-07-28 08:48:47113 create_file.SetDispatchEventImplForTesting(
Reilly Grant9a4299c2021-01-26 03:37:37114 base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
115 base::Unretained(&dispatcher)));
mtomasz@chromium.org5b48ea62014-07-28 08:48:47116
117 EXPECT_FALSE(create_file.Execute(kRequestId));
118}
119
mtomasz@chromium.org88787142014-07-23 06:55:09120TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) {
121 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
122 util::StatusCallbackLog callback_log;
123
Jan Wilken Dörrie6a3b57382020-04-20 07:55:51124 CreateFile create_file(
125 NULL, file_system_info_, base::FilePath(kFilePath),
126 base::BindOnce(&util::LogStatusCallback, &callback_log));
mtomasz@chromium.org88787142014-07-23 06:55:09127 create_file.SetDispatchEventImplForTesting(
Reilly Grant9a4299c2021-01-26 03:37:37128 base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129 base::Unretained(&dispatcher)));
mtomasz@chromium.org88787142014-07-23 06:55:09130
131 EXPECT_TRUE(create_file.Execute(kRequestId));
132
Peter Boström6b701822021-04-15 03:53:08133 create_file.OnSuccess(kRequestId, std::make_unique<RequestValue>(),
mtomasz@chromium.org88787142014-07-23 06:55:09134 false /* has_more */);
135 ASSERT_EQ(1u, callback_log.size());
136 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
137}
138
139TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) {
140 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
141 util::StatusCallbackLog callback_log;
142
Jan Wilken Dörrie6a3b57382020-04-20 07:55:51143 CreateFile create_file(
144 NULL, file_system_info_, base::FilePath(kFilePath),
145 base::BindOnce(&util::LogStatusCallback, &callback_log));
mtomasz@chromium.org88787142014-07-23 06:55:09146 create_file.SetDispatchEventImplForTesting(
Reilly Grant9a4299c2021-01-26 03:37:37147 base::BindRepeating(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
148 base::Unretained(&dispatcher)));
mtomasz@chromium.org88787142014-07-23 06:55:09149
150 EXPECT_TRUE(create_file.Execute(kRequestId));
151
Peter Boström6b701822021-04-15 03:53:08152 create_file.OnError(kRequestId, std::make_unique<RequestValue>(),
mtomasz@chromium.org88787142014-07-23 06:55:09153 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