Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / create_directory_unittest.cc
blob77d06a325134fec3a6c7bd77cc4f417bc294ce6a
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_directory.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 "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_capabilities/file_system_provider_capabilities_handler.h"
18 #include "chrome/common/extensions/api/file_system_provider_internal.h"
19 #include "extensions/browser/event_router.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace chromeos {
24 namespace file_system_provider {
25 namespace operations {
26 namespace {
28 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
29 const char kFileSystemId[] = "testing-file-system";
30 const int kRequestId = 2;
31 const base::FilePath::CharType kDirectoryPath[] =
32 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
34 } // namespace
36 class FileSystemProviderOperationsCreateDirectoryTest : public testing::Test {
37 protected:
38 FileSystemProviderOperationsCreateDirectoryTest() {}
39 ~FileSystemProviderOperationsCreateDirectoryTest() override {}
41 void SetUp() override {
42 MountOptions mount_options(kFileSystemId, "" /* display_name */);
43 mount_options.writable = true;
44 file_system_info_ = ProvidedFileSystemInfo(
45 kExtensionId, mount_options, base::FilePath(), false /* configurable */,
46 true /* watchable */, extensions::SOURCE_FILE);
49 ProvidedFileSystemInfo file_system_info_;
52 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, Execute) {
53 using extensions::api::file_system_provider::CreateDirectoryRequestedOptions;
55 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
56 util::StatusCallbackLog callback_log;
58 CreateDirectory create_directory(
59 NULL, file_system_info_, base::FilePath(kDirectoryPath),
60 true /* recursive */,
61 base::Bind(&util::LogStatusCallback, &callback_log));
62 create_directory.SetDispatchEventImplForTesting(
63 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
64 base::Unretained(&dispatcher)));
66 EXPECT_TRUE(create_directory.Execute(kRequestId));
68 ASSERT_EQ(1u, dispatcher.events().size());
69 extensions::Event* event = dispatcher.events()[0];
70 EXPECT_EQ(extensions::api::file_system_provider::OnCreateDirectoryRequested::
71 kEventName,
72 event->event_name);
73 base::ListValue* event_args = event->event_args.get();
74 ASSERT_EQ(1u, event_args->GetSize());
76 const base::DictionaryValue* options_as_value = NULL;
77 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
79 CreateDirectoryRequestedOptions options;
80 ASSERT_TRUE(
81 CreateDirectoryRequestedOptions::Populate(*options_as_value, &options));
82 EXPECT_EQ(kFileSystemId, options.file_system_id);
83 EXPECT_EQ(kRequestId, options.request_id);
84 EXPECT_EQ(kDirectoryPath, options.directory_path);
85 EXPECT_TRUE(options.recursive);
88 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, Execute_NoListener) {
89 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
90 util::StatusCallbackLog callback_log;
92 CreateDirectory create_directory(
93 NULL, file_system_info_, base::FilePath(kDirectoryPath),
94 true /* recursive */,
95 base::Bind(&util::LogStatusCallback, &callback_log));
96 create_directory.SetDispatchEventImplForTesting(
97 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
98 base::Unretained(&dispatcher)));
100 EXPECT_FALSE(create_directory.Execute(kRequestId));
103 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, Execute_ReadOnly) {
104 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
105 util::StatusCallbackLog callback_log;
107 const ProvidedFileSystemInfo read_only_file_system_info(
108 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
109 base::FilePath() /* mount_path */, false /* configurable */,
110 true /* watchable */, extensions::SOURCE_FILE);
112 CreateDirectory create_directory(
113 NULL, read_only_file_system_info, base::FilePath(kDirectoryPath),
114 true /* recursive */,
115 base::Bind(&util::LogStatusCallback, &callback_log));
116 create_directory.SetDispatchEventImplForTesting(
117 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
118 base::Unretained(&dispatcher)));
120 EXPECT_FALSE(create_directory.Execute(kRequestId));
123 TEST_F(FileSystemProviderOperationsCreateDirectoryTest, OnSuccess) {
124 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125 util::StatusCallbackLog callback_log;
127 CreateDirectory create_directory(
128 NULL, file_system_info_, base::FilePath(kDirectoryPath),
129 true /* recursive */,
130 base::Bind(&util::LogStatusCallback, &callback_log));
131 create_directory.SetDispatchEventImplForTesting(
132 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
133 base::Unretained(&dispatcher)));
135 EXPECT_TRUE(create_directory.Execute(kRequestId));
137 create_directory.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(FileSystemProviderOperationsCreateDirectoryTest, OnError) {
145 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
146 util::StatusCallbackLog callback_log;
148 CreateDirectory create_directory(
149 NULL, file_system_info_, base::FilePath(kDirectoryPath),
150 true /* recursive */,
151 base::Bind(&util::LogStatusCallback, &callback_log));
152 create_directory.SetDispatchEventImplForTesting(
153 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
154 base::Unretained(&dispatcher)));
156 EXPECT_TRUE(create_directory.Execute(kRequestId));
158 create_directory.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