Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / create_file_unittest.cc
blob4acf9feff22b9e5e22740270075dead580afce79
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.
5 #include "chrome/browser/chromeos/file_system_provider/operations/create_file.h"
7 #include <string>
8 #include <vector>
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
15 #include "chrome/common/extensions/api/file_system_provider.h"
16 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.h"
17 #include "chrome/common/extensions/api/file_system_provider_internal.h"
18 #include "extensions/browser/event_router.h"
19 #include "storage/browser/fileapi/async_file_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const base::FilePath::CharType kFilePath[] =
31 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
33 } // namespace
35 class FileSystemProviderOperationsCreateFileTest : public testing::Test {
36 protected:
37 FileSystemProviderOperationsCreateFileTest() {}
38 ~FileSystemProviderOperationsCreateFileTest() override {}
40 void SetUp() override {
41 MountOptions mount_options(kFileSystemId, "" /* display_name */);
42 mount_options.writable = true;
43 file_system_info_ = ProvidedFileSystemInfo(
44 kExtensionId, mount_options, base::FilePath(), false /* configurable */,
45 true /* watchable */, extensions::SOURCE_FILE);
48 ProvidedFileSystemInfo file_system_info_;
51 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) {
52 using extensions::api::file_system_provider::CreateFileRequestedOptions;
54 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
55 util::StatusCallbackLog callback_log;
57 CreateFile create_file(NULL, file_system_info_, base::FilePath(kFilePath),
58 base::Bind(&util::LogStatusCallback, &callback_log));
59 create_file.SetDispatchEventImplForTesting(
60 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
61 base::Unretained(&dispatcher)));
63 EXPECT_TRUE(create_file.Execute(kRequestId));
65 ASSERT_EQ(1u, dispatcher.events().size());
66 extensions::Event* event = dispatcher.events()[0];
67 EXPECT_EQ(
68 extensions::api::file_system_provider::OnCreateFileRequested::kEventName,
69 event->event_name);
70 base::ListValue* event_args = event->event_args.get();
71 ASSERT_EQ(1u, event_args->GetSize());
73 const base::DictionaryValue* options_as_value = NULL;
74 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
76 CreateFileRequestedOptions options;
77 ASSERT_TRUE(
78 CreateFileRequestedOptions::Populate(*options_as_value, &options));
79 EXPECT_EQ(kFileSystemId, options.file_system_id);
80 EXPECT_EQ(kRequestId, options.request_id);
81 EXPECT_EQ(kFilePath, options.file_path);
84 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) {
85 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
86 util::StatusCallbackLog callback_log;
88 CreateFile create_file(NULL, file_system_info_, base::FilePath(kFilePath),
89 base::Bind(&util::LogStatusCallback, &callback_log));
90 create_file.SetDispatchEventImplForTesting(
91 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
92 base::Unretained(&dispatcher)));
94 EXPECT_FALSE(create_file.Execute(kRequestId));
97 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_ReadOnly) {
98 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
99 util::StatusCallbackLog callback_log;
101 const ProvidedFileSystemInfo read_only_file_system_info(
102 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
103 base::FilePath() /* mount_path */, false /* configurable */,
104 true /* watchable */, extensions::SOURCE_FILE);
106 CreateFile create_file(NULL, read_only_file_system_info,
107 base::FilePath(kFilePath),
108 base::Bind(&util::LogStatusCallback, &callback_log));
109 create_file.SetDispatchEventImplForTesting(
110 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
111 base::Unretained(&dispatcher)));
113 EXPECT_FALSE(create_file.Execute(kRequestId));
116 TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) {
117 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
118 util::StatusCallbackLog callback_log;
120 CreateFile create_file(NULL, file_system_info_, base::FilePath(kFilePath),
121 base::Bind(&util::LogStatusCallback, &callback_log));
122 create_file.SetDispatchEventImplForTesting(
123 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
124 base::Unretained(&dispatcher)));
126 EXPECT_TRUE(create_file.Execute(kRequestId));
128 create_file.OnSuccess(kRequestId,
129 scoped_ptr<RequestValue>(new RequestValue()),
130 false /* has_more */);
131 ASSERT_EQ(1u, callback_log.size());
132 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
135 TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) {
136 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
137 util::StatusCallbackLog callback_log;
139 CreateFile create_file(NULL, file_system_info_, base::FilePath(kFilePath),
140 base::Bind(&util::LogStatusCallback, &callback_log));
141 create_file.SetDispatchEventImplForTesting(
142 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
143 base::Unretained(&dispatcher)));
145 EXPECT_TRUE(create_file.Execute(kRequestId));
147 create_file.OnError(kRequestId,
148 scoped_ptr<RequestValue>(new RequestValue()),
149 base::File::FILE_ERROR_TOO_MANY_OPENED);
150 ASSERT_EQ(1u, callback_log.size());
151 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
154 } // namespace operations
155 } // namespace file_system_provider
156 } // namespace chromeos