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"
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"
24 namespace file_system_provider {
25 namespace operations {
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 {
40 Event(int chunk_length, bool has_more, base::File::Error result)
41 : chunk_length_(chunk_length), has_more_(has_more), result_(result) {}
44 int chunk_length() const { return chunk_length_; }
45 bool has_more() const { return has_more_; }
46 base::File::Error result() const { return result_; }
51 base::File::Error result_;
53 DISALLOW_COPY_AND_ASSIGN(Event);
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_; }
66 ScopedVector<Event> events_;
69 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
74 class FileSystemProviderOperationsReadFileTest : public testing::Test {
76 FileSystemProviderOperationsReadFileTest() {}
77 virtual ~FileSystemProviderOperationsReadFileTest() {}
79 virtual void SetUp() OVERRIDE {
81 ProvidedFileSystemInfo(kExtensionId,
83 "" /* display_name */,
85 base::FilePath() /* mount_path */);
86 io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength));
89 ProvidedFileSystemInfo file_system_info_;
90 scoped_refptr<net::IOBuffer> io_buffer_;
93 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) {
94 using extensions::api::file_system_provider::ReadFileRequestedOptions;
96 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
97 CallbackLogger callback_logger;
99 ReadFile read_file(NULL,
105 base::Bind(&CallbackLogger::OnReadFile,
106 base::Unretained(&callback_logger)));
107 read_file.SetDispatchEventImplForTesting(
108 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
109 base::Unretained(&dispatcher)));
111 EXPECT_TRUE(read_file.Execute(kRequestId));
113 ASSERT_EQ(1u, dispatcher.events().size());
114 extensions::Event* event = dispatcher.events()[0];
116 extensions::api::file_system_provider::OnReadFileRequested::kEventName,
118 base::ListValue* event_args = event->event_args.get();
119 ASSERT_EQ(1u, event_args->GetSize());
121 const base::DictionaryValue* options_as_value = NULL;
122 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
124 ReadFileRequestedOptions options;
125 ASSERT_TRUE(ReadFileRequestedOptions::Populate(*options_as_value, &options));
126 EXPECT_EQ(kFileSystemId, options.file_system_id);
127 EXPECT_EQ(kRequestId, options.request_id);
128 EXPECT_EQ(kFileHandle, options.open_request_id);
129 EXPECT_EQ(kOffset, static_cast<double>(options.offset));
130 EXPECT_EQ(kLength, options.length);
133 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) {
134 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
135 CallbackLogger callback_logger;
137 ReadFile read_file(NULL,
143 base::Bind(&CallbackLogger::OnReadFile,
144 base::Unretained(&callback_logger)));
145 read_file.SetDispatchEventImplForTesting(
146 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
147 base::Unretained(&dispatcher)));
149 EXPECT_FALSE(read_file.Execute(kRequestId));
152 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) {
153 using extensions::api::file_system_provider_internal::
154 ReadFileRequestedSuccess::Params;
156 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
157 CallbackLogger callback_logger;
159 ReadFile read_file(NULL,
165 base::Bind(&CallbackLogger::OnReadFile,
166 base::Unretained(&callback_logger)));
167 read_file.SetDispatchEventImplForTesting(
168 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
169 base::Unretained(&dispatcher)));
171 EXPECT_TRUE(read_file.Execute(kRequestId));
173 const std::string data = "ABCDE";
174 const bool has_more = false;
175 const int execution_time = 0;
177 base::ListValue value_as_list;
178 value_as_list.Set(0, new base::StringValue(kFileSystemId));
179 value_as_list.Set(1, new base::FundamentalValue(kRequestId));
181 2, base::BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size()));
182 value_as_list.Set(3, new base::FundamentalValue(has_more));
183 value_as_list.Set(4, new base::FundamentalValue(execution_time));
185 scoped_ptr<Params> params(Params::Create(value_as_list));
186 ASSERT_TRUE(params.get());
187 scoped_ptr<RequestValue> request_value(
188 RequestValue::CreateForReadFileSuccess(params.Pass()));
189 ASSERT_TRUE(request_value.get());
191 read_file.OnSuccess(kRequestId, request_value.Pass(), has_more);
193 ASSERT_EQ(1u, callback_logger.events().size());
194 CallbackLogger::Event* event = callback_logger.events()[0];
195 EXPECT_EQ(kLength, event->chunk_length());
196 EXPECT_FALSE(event->has_more());
197 EXPECT_EQ(data, std::string(io_buffer_->data(), kLength));
198 EXPECT_EQ(base::File::FILE_OK, event->result());
201 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) {
202 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
203 CallbackLogger callback_logger;
205 ReadFile read_file(NULL,
211 base::Bind(&CallbackLogger::OnReadFile,
212 base::Unretained(&callback_logger)));
213 read_file.SetDispatchEventImplForTesting(
214 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
215 base::Unretained(&dispatcher)));
217 EXPECT_TRUE(read_file.Execute(kRequestId));
219 read_file.OnError(kRequestId,
220 scoped_ptr<RequestValue>(new RequestValue()),
221 base::File::FILE_ERROR_TOO_MANY_OPENED);
223 ASSERT_EQ(1u, callback_logger.events().size());
224 CallbackLogger::Event* event = callback_logger.events()[0];
225 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
228 } // namespace operations
229 } // namespace file_system_provider
230 } // namespace chromeos