Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / operations / create_file_unittest.cc
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
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
26
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";
31
32 }  // namespace
33
34 class FileSystemProviderOperationsCreateFileTest : public testing::Test {
35  protected:
36   FileSystemProviderOperationsCreateFileTest() {}
37   virtual ~FileSystemProviderOperationsCreateFileTest() {}
38
39   virtual void SetUp() override {
40     MountOptions mount_options(kFileSystemId, "" /* display_name */);
41     mount_options.writable = true;
42     file_system_info_ =
43         ProvidedFileSystemInfo(kExtensionId, mount_options, base::FilePath());
44   }
45
46   ProvidedFileSystemInfo file_system_info_;
47 };
48
49 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute) {
50   using extensions::api::file_system_provider::CreateFileRequestedOptions;
51
52   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
53   util::StatusCallbackLog callback_log;
54
55   CreateFile create_file(NULL,
56                          file_system_info_,
57                          base::FilePath::FromUTF8Unsafe(kFilePath),
58                          base::Bind(&util::LogStatusCallback, &callback_log));
59   create_file.SetDispatchEventImplForTesting(
60       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
61                  base::Unretained(&dispatcher)));
62
63   EXPECT_TRUE(create_file.Execute(kRequestId));
64
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());
72
73   const base::DictionaryValue* options_as_value = NULL;
74   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
75
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);
82 }
83
84 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_NoListener) {
85   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
86   util::StatusCallbackLog callback_log;
87
88   CreateFile create_file(NULL,
89                          file_system_info_,
90                          base::FilePath::FromUTF8Unsafe(kFilePath),
91                          base::Bind(&util::LogStatusCallback, &callback_log));
92   create_file.SetDispatchEventImplForTesting(
93       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
94                  base::Unretained(&dispatcher)));
95
96   EXPECT_FALSE(create_file.Execute(kRequestId));
97 }
98
99 TEST_F(FileSystemProviderOperationsCreateFileTest, Execute_ReadOnly) {
100   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
101   util::StatusCallbackLog callback_log;
102
103   const ProvidedFileSystemInfo read_only_file_system_info(
104       kExtensionId,
105       MountOptions(kFileSystemId, "" /* display_name */),
106       base::FilePath() /* mount_path */);
107
108   CreateFile create_file(NULL,
109                          read_only_file_system_info,
110                          base::FilePath::FromUTF8Unsafe(kFilePath),
111                          base::Bind(&util::LogStatusCallback, &callback_log));
112   create_file.SetDispatchEventImplForTesting(
113       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
114                  base::Unretained(&dispatcher)));
115
116   EXPECT_FALSE(create_file.Execute(kRequestId));
117 }
118
119 TEST_F(FileSystemProviderOperationsCreateFileTest, OnSuccess) {
120   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
121   util::StatusCallbackLog callback_log;
122
123   CreateFile create_file(NULL,
124                          file_system_info_,
125                          base::FilePath::FromUTF8Unsafe(kFilePath),
126                          base::Bind(&util::LogStatusCallback, &callback_log));
127   create_file.SetDispatchEventImplForTesting(
128       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129                  base::Unretained(&dispatcher)));
130
131   EXPECT_TRUE(create_file.Execute(kRequestId));
132
133   create_file.OnSuccess(kRequestId,
134                         scoped_ptr<RequestValue>(new RequestValue()),
135                         false /* has_more */);
136   ASSERT_EQ(1u, callback_log.size());
137   EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
138 }
139
140 TEST_F(FileSystemProviderOperationsCreateFileTest, OnError) {
141   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
142   util::StatusCallbackLog callback_log;
143
144   CreateFile create_file(NULL,
145                          file_system_info_,
146                          base::FilePath::FromUTF8Unsafe(kFilePath),
147                          base::Bind(&util::LogStatusCallback, &callback_log));
148   create_file.SetDispatchEventImplForTesting(
149       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
150                  base::Unretained(&dispatcher)));
151
152   EXPECT_TRUE(create_file.Execute(kRequestId));
153
154   create_file.OnError(kRequestId,
155                       scoped_ptr<RequestValue>(new RequestValue()),
156                       base::File::FILE_ERROR_TOO_MANY_OPENED);
157   ASSERT_EQ(1u, callback_log.size());
158   EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
159 }
160
161 }  // namespace operations
162 }  // namespace file_system_provider
163 }  // namespace chromeos