Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / read_file_unittest.cc
blob6a8383446992ba193037f5b100c7d7e3ffa7108a
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/read_file.h"
7 #include <string>
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.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 "net/base/io_buffer.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 int kFileHandle = 3;
32 const int kOffset = 10;
33 const int kLength = 5;
35 // Callback invocation logger. Acts as a fileapi end-point.
36 class CallbackLogger {
37 public:
38 class Event {
39 public:
40 Event(int chunk_length, bool has_more, base::File::Error result)
41 : chunk_length_(chunk_length), has_more_(has_more), result_(result) {}
42 virtual ~Event() {}
44 int chunk_length() const { return chunk_length_; }
45 bool has_more() const { return has_more_; }
46 base::File::Error result() const { return result_; }
48 private:
49 int chunk_length_;
50 bool has_more_;
51 base::File::Error result_;
53 DISALLOW_COPY_AND_ASSIGN(Event);
56 CallbackLogger() {}
57 virtual ~CallbackLogger() {}
59 void OnReadFile(int chunk_length, bool has_more, base::File::Error result) {
60 events_.push_back(new Event(chunk_length, has_more, result));
63 ScopedVector<Event>& events() { return events_; }
65 private:
66 ScopedVector<Event> events_;
68 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
71 } // namespace
73 class FileSystemProviderOperationsReadFileTest : public testing::Test {
74 protected:
75 FileSystemProviderOperationsReadFileTest() {}
76 ~FileSystemProviderOperationsReadFileTest() override {}
78 void SetUp() override {
79 file_system_info_ = ProvidedFileSystemInfo(
80 kExtensionId,
81 MountOptions(kFileSystemId, "" /* display_name */),
82 base::FilePath());
83 io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength));
86 ProvidedFileSystemInfo file_system_info_;
87 scoped_refptr<net::IOBuffer> io_buffer_;
90 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) {
91 using extensions::api::file_system_provider::ReadFileRequestedOptions;
93 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
94 CallbackLogger callback_logger;
96 ReadFile read_file(NULL,
97 file_system_info_,
98 kFileHandle,
99 io_buffer_.get(),
100 kOffset,
101 kLength,
102 base::Bind(&CallbackLogger::OnReadFile,
103 base::Unretained(&callback_logger)));
104 read_file.SetDispatchEventImplForTesting(
105 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
106 base::Unretained(&dispatcher)));
108 EXPECT_TRUE(read_file.Execute(kRequestId));
110 ASSERT_EQ(1u, dispatcher.events().size());
111 extensions::Event* event = dispatcher.events()[0];
112 EXPECT_EQ(
113 extensions::api::file_system_provider::OnReadFileRequested::kEventName,
114 event->event_name);
115 base::ListValue* event_args = event->event_args.get();
116 ASSERT_EQ(1u, event_args->GetSize());
118 const base::DictionaryValue* options_as_value = NULL;
119 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
121 ReadFileRequestedOptions options;
122 ASSERT_TRUE(ReadFileRequestedOptions::Populate(*options_as_value, &options));
123 EXPECT_EQ(kFileSystemId, options.file_system_id);
124 EXPECT_EQ(kRequestId, options.request_id);
125 EXPECT_EQ(kFileHandle, options.open_request_id);
126 EXPECT_EQ(kOffset, static_cast<double>(options.offset));
127 EXPECT_EQ(kLength, options.length);
130 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) {
131 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
132 CallbackLogger callback_logger;
134 ReadFile read_file(NULL,
135 file_system_info_,
136 kFileHandle,
137 io_buffer_.get(),
138 kOffset,
139 kLength,
140 base::Bind(&CallbackLogger::OnReadFile,
141 base::Unretained(&callback_logger)));
142 read_file.SetDispatchEventImplForTesting(
143 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
144 base::Unretained(&dispatcher)));
146 EXPECT_FALSE(read_file.Execute(kRequestId));
149 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) {
150 using extensions::api::file_system_provider_internal::
151 ReadFileRequestedSuccess::Params;
153 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
154 CallbackLogger callback_logger;
156 ReadFile read_file(NULL,
157 file_system_info_,
158 kFileHandle,
159 io_buffer_.get(),
160 kOffset,
161 kLength,
162 base::Bind(&CallbackLogger::OnReadFile,
163 base::Unretained(&callback_logger)));
164 read_file.SetDispatchEventImplForTesting(
165 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
166 base::Unretained(&dispatcher)));
168 EXPECT_TRUE(read_file.Execute(kRequestId));
170 const std::string data = "ABCDE";
171 const bool has_more = false;
172 const int execution_time = 0;
174 base::ListValue value_as_list;
175 value_as_list.Set(0, new base::StringValue(kFileSystemId));
176 value_as_list.Set(1, new base::FundamentalValue(kRequestId));
177 value_as_list.Set(
178 2, base::BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size()));
179 value_as_list.Set(3, new base::FundamentalValue(has_more));
180 value_as_list.Set(4, new base::FundamentalValue(execution_time));
182 scoped_ptr<Params> params(Params::Create(value_as_list));
183 ASSERT_TRUE(params.get());
184 scoped_ptr<RequestValue> request_value(
185 RequestValue::CreateForReadFileSuccess(params.Pass()));
186 ASSERT_TRUE(request_value.get());
188 read_file.OnSuccess(kRequestId, request_value.Pass(), has_more);
190 ASSERT_EQ(1u, callback_logger.events().size());
191 CallbackLogger::Event* event = callback_logger.events()[0];
192 EXPECT_EQ(kLength, event->chunk_length());
193 EXPECT_FALSE(event->has_more());
194 EXPECT_EQ(data, std::string(io_buffer_->data(), kLength));
195 EXPECT_EQ(base::File::FILE_OK, event->result());
198 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) {
199 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
200 CallbackLogger callback_logger;
202 ReadFile read_file(NULL,
203 file_system_info_,
204 kFileHandle,
205 io_buffer_.get(),
206 kOffset,
207 kLength,
208 base::Bind(&CallbackLogger::OnReadFile,
209 base::Unretained(&callback_logger)));
210 read_file.SetDispatchEventImplForTesting(
211 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
212 base::Unretained(&dispatcher)));
214 EXPECT_TRUE(read_file.Execute(kRequestId));
216 read_file.OnError(kRequestId,
217 scoped_ptr<RequestValue>(new RequestValue()),
218 base::File::FILE_ERROR_TOO_MANY_OPENED);
220 ASSERT_EQ(1u, callback_logger.events().size());
221 CallbackLogger::Event* event = callback_logger.events()[0];
222 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
225 } // namespace operations
226 } // namespace file_system_provider
227 } // namespace chromeos