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 {
80 file_system_info_ = ProvidedFileSystemInfo(
82 MountOptions(kFileSystemId, "" /* display_name */),
84 io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength));
87 ProvidedFileSystemInfo file_system_info_;
88 scoped_refptr<net::IOBuffer> io_buffer_;
91 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) {
92 using extensions::api::file_system_provider::ReadFileRequestedOptions;
94 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
95 CallbackLogger callback_logger;
97 ReadFile read_file(NULL,
103 base::Bind(&CallbackLogger::OnReadFile,
104 base::Unretained(&callback_logger)));
105 read_file.SetDispatchEventImplForTesting(
106 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
107 base::Unretained(&dispatcher)));
109 EXPECT_TRUE(read_file.Execute(kRequestId));
111 ASSERT_EQ(1u, dispatcher.events().size());
112 extensions::Event* event = dispatcher.events()[0];
114 extensions::api::file_system_provider::OnReadFileRequested::kEventName,
116 base::ListValue* event_args = event->event_args.get();
117 ASSERT_EQ(1u, event_args->GetSize());
119 const base::DictionaryValue* options_as_value = NULL;
120 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
122 ReadFileRequestedOptions options;
123 ASSERT_TRUE(ReadFileRequestedOptions::Populate(*options_as_value, &options));
124 EXPECT_EQ(kFileSystemId, options.file_system_id);
125 EXPECT_EQ(kRequestId, options.request_id);
126 EXPECT_EQ(kFileHandle, options.open_request_id);
127 EXPECT_EQ(kOffset, static_cast<double>(options.offset));
128 EXPECT_EQ(kLength, options.length);
131 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) {
132 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
133 CallbackLogger callback_logger;
135 ReadFile read_file(NULL,
141 base::Bind(&CallbackLogger::OnReadFile,
142 base::Unretained(&callback_logger)));
143 read_file.SetDispatchEventImplForTesting(
144 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
145 base::Unretained(&dispatcher)));
147 EXPECT_FALSE(read_file.Execute(kRequestId));
150 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) {
151 using extensions::api::file_system_provider_internal::
152 ReadFileRequestedSuccess::Params;
154 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
155 CallbackLogger callback_logger;
157 ReadFile read_file(NULL,
163 base::Bind(&CallbackLogger::OnReadFile,
164 base::Unretained(&callback_logger)));
165 read_file.SetDispatchEventImplForTesting(
166 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
167 base::Unretained(&dispatcher)));
169 EXPECT_TRUE(read_file.Execute(kRequestId));
171 const std::string data = "ABCDE";
172 const bool has_more = false;
173 const int execution_time = 0;
175 base::ListValue value_as_list;
176 value_as_list.Set(0, new base::StringValue(kFileSystemId));
177 value_as_list.Set(1, new base::FundamentalValue(kRequestId));
179 2, base::BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size()));
180 value_as_list.Set(3, new base::FundamentalValue(has_more));
181 value_as_list.Set(4, new base::FundamentalValue(execution_time));
183 scoped_ptr<Params> params(Params::Create(value_as_list));
184 ASSERT_TRUE(params.get());
185 scoped_ptr<RequestValue> request_value(
186 RequestValue::CreateForReadFileSuccess(params.Pass()));
187 ASSERT_TRUE(request_value.get());
189 read_file.OnSuccess(kRequestId, request_value.Pass(), has_more);
191 ASSERT_EQ(1u, callback_logger.events().size());
192 CallbackLogger::Event* event = callback_logger.events()[0];
193 EXPECT_EQ(kLength, event->chunk_length());
194 EXPECT_FALSE(event->has_more());
195 EXPECT_EQ(data, std::string(io_buffer_->data(), kLength));
196 EXPECT_EQ(base::File::FILE_OK, event->result());
199 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) {
200 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
201 CallbackLogger callback_logger;
203 ReadFile read_file(NULL,
209 base::Bind(&CallbackLogger::OnReadFile,
210 base::Unretained(&callback_logger)));
211 read_file.SetDispatchEventImplForTesting(
212 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
213 base::Unretained(&dispatcher)));
215 EXPECT_TRUE(read_file.Execute(kRequestId));
217 read_file.OnError(kRequestId,
218 scoped_ptr<RequestValue>(new RequestValue()),
219 base::File::FILE_ERROR_TOO_MANY_OPENED);
221 ASSERT_EQ(1u, callback_logger.events().size());
222 CallbackLogger::Event* event = callback_logger.events()[0];
223 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
226 } // namespace operations
227 } // namespace file_system_provider
228 } // namespace chromeos