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"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
16 #include "chrome/common/extensions/api/file_system_provider.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"
23 namespace file_system_provider {
24 namespace operations {
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy";
34 class FileSystemProviderOperationsCreateFileTest : public testing::Test {
36 FileSystemProviderOperationsCreateFileTest() {}
37 virtual ~FileSystemProviderOperationsCreateFileTest() {}
39 virtual void SetUp() OVERRIDE {
41 ProvidedFileSystemInfo(kExtensionId,
43 "" /* file_system_name */,
45 base::FilePath() /* mount_path */);
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,
59 base::FilePath::FromUTF8Unsafe(kFilePath),
60 base::Bind(&util::LogStatusCallback, &callback_log));
61 create_file.SetDispatchEventImplForTesting(
62 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63 base::Unretained(&dispatcher)));
65 EXPECT_TRUE(create_file.Execute(kRequestId));
67 ASSERT_EQ(1u, dispatcher.events().size());
68 extensions::Event* event = dispatcher.events()[0];
70 extensions::api::file_system_provider::OnCreateFileRequested::kEventName,
72 base::ListValue* event_args = event->event_args.get();
73 ASSERT_EQ(1u, event_args->GetSize());
75 const base::DictionaryValue* options_as_value = NULL;
76 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
78 CreateFileRequestedOptions options;
80 CreateFileRequestedOptions::Populate(*options_as_value, &options));
81 EXPECT_EQ(kFileSystemId, options.file_system_id);
82 EXPECT_EQ(kRequestId, options.request_id);
83 EXPECT_EQ(kFilePath, options.file_path);
86 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) {
87 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88 util::StatusCallbackLog callback_log;
90 CreateFile create_file(NULL,
92 base::FilePath::FromUTF8Unsafe(kFilePath),
93 base::Bind(&util::LogStatusCallback, &callback_log));
94 create_file.SetDispatchEventImplForTesting(
95 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
96 base::Unretained(&dispatcher)));
98 EXPECT_FALSE(create_file.Execute(kRequestId));
101 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_ReadOnly) {
102 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
103 util::StatusCallbackLog callback_log;
105 const ProvidedFileSystemInfo read_only_file_system_info(
108 "" /* file_system_name */,
109 false /* writable */,
110 base::FilePath() /* mount_path */);
112 CreateFile create_file(NULL,
113 read_only_file_system_info,
114 base::FilePath::FromUTF8Unsafe(kFilePath),
115 base::Bind(&util::LogStatusCallback, &callback_log));
116 create_file.SetDispatchEventImplForTesting(
117 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
118 base::Unretained(&dispatcher)));
120 EXPECT_FALSE(create_file.Execute(kRequestId));
123 TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) {
124 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125 util::StatusCallbackLog callback_log;
127 CreateFile create_file(NULL,
129 base::FilePath::FromUTF8Unsafe(kFilePath),
130 base::Bind(&util::LogStatusCallback, &callback_log));
131 create_file.SetDispatchEventImplForTesting(
132 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
133 base::Unretained(&dispatcher)));
135 EXPECT_TRUE(create_file.Execute(kRequestId));
137 create_file.OnSuccess(kRequestId,
138 scoped_ptr<RequestValue>(new RequestValue()),
139 false /* has_more */);
140 ASSERT_EQ(1u, callback_log.size());
141 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
144 TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) {
145 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
146 util::StatusCallbackLog callback_log;
148 CreateFile create_file(NULL,
150 base::FilePath::FromUTF8Unsafe(kFilePath),
151 base::Bind(&util::LogStatusCallback, &callback_log));
152 create_file.SetDispatchEventImplForTesting(
153 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
154 base::Unretained(&dispatcher)));
156 EXPECT_TRUE(create_file.Execute(kRequestId));
158 create_file.OnError(kRequestId,
159 scoped_ptr<RequestValue>(new RequestValue()),
160 base::File::FILE_ERROR_TOO_MANY_OPENED);
161 ASSERT_EQ(1u, callback_log.size());
162 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
165 } // namespace operations
166 } // namespace file_system_provider
167 } // namespace chromeos