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
5#include "chrome/browser/chromeos/file_system_provider/operations/create_file.h"
6
7#include <string>
8#include <vector>
9
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"
21
22namespace chromeos {
23namespace file_system_provider {
24namespace operations {
25namespace {
26
27const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28const char kFileSystemId[] = "testing-file-system";
29const int kRequestId = 2;
30const base::FilePath::CharType kFilePath[] = "/kitty/and/puppy/happy";
31
32}  // namespace
33
34class FileSystemProviderOperationsCreateFileTest : public testing::Test {
35 protected:
36  FileSystemProviderOperationsCreateFileTest() {}
37  virtual ~FileSystemProviderOperationsCreateFileTest() {}
38
39  virtual void SetUp() OVERRIDE {
40    file_system_info_ =
41        ProvidedFileSystemInfo(kExtensionId,
42                               kFileSystemId,
43                               "" /* file_system_name */,
44                               true /* writable */,
45                               base::FilePath() /* mount_path */);
46  }
47
48  ProvidedFileSystemInfo file_system_info_;
49};
50
51TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) {
52  using extensions::api::file_system_provider::CreateFileRequestedOptions;
53
54  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
55  util::StatusCallbackLog callback_log;
56
57  CreateFile create_file(NULL,
58                         file_system_info_,
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)));
64
65  EXPECT_TRUE(create_file.Execute(kRequestId));
66
67  ASSERT_EQ(1u, dispatcher.events().size());
68  extensions::Event* event = dispatcher.events()[0];
69  EXPECT_EQ(
70      extensions::api::file_system_provider::OnCreateFileRequested::kEventName,
71      event->event_name);
72  base::ListValue* event_args = event->event_args.get();
73  ASSERT_EQ(1u, event_args->GetSize());
74
75  const base::DictionaryValue* options_as_value = NULL;
76  ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
77
78  CreateFileRequestedOptions options;
79  ASSERT_TRUE(
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);
84}
85
86TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) {
87  util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88  util::StatusCallbackLog callback_log;
89
90  CreateFile create_file(NULL,
91                         file_system_info_,
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)));
97
98  EXPECT_FALSE(create_file.Execute(kRequestId));
99}
100
101TEST_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(
106      kExtensionId,
107      kFileSystemId,
108      "" /* file_system_name */,
109      false /* writable */,
110      base::FilePath() /* mount_path */);
111
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)));
119
120  EXPECT_FALSE(create_file.Execute(kRequestId));
121}
122
123TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) {
124  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125  util::StatusCallbackLog callback_log;
126
127  CreateFile create_file(NULL,
128                         file_system_info_,
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)));
134
135  EXPECT_TRUE(create_file.Execute(kRequestId));
136
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]);
142}
143
144TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) {
145  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
146  util::StatusCallbackLog callback_log;
147
148  CreateFile create_file(NULL,
149                         file_system_info_,
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)));
155
156  EXPECT_TRUE(create_file.Execute(kRequestId));
157
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]);
163}
164
165}  // namespace operations
166}  // namespace file_system_provider
167}  // namespace chromeos
168