BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / read_file_unittest.cc
blobacfe4620812b9f998c3b3853e70856c10cee2340
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_capabilities/file_system_provider_capabilities_handler.h"
18 #include "chrome/common/extensions/api/file_system_provider_internal.h"
19 #include "extensions/browser/event_router.h"
20 #include "net/base/io_buffer.h"
21 #include "storage/browser/fileapi/async_file_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace chromeos {
25 namespace file_system_provider {
26 namespace operations {
27 namespace {
29 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
30 const char kFileSystemId[] = "testing-file-system";
31 const int kRequestId = 2;
32 const int kFileHandle = 3;
33 const int kOffset = 10;
34 const int kLength = 5;
36 // Callback invocation logger. Acts as a fileapi end-point.
37 class CallbackLogger {
38 public:
39 class Event {
40 public:
41 Event(int chunk_length, bool has_more, base::File::Error result)
42 : chunk_length_(chunk_length), has_more_(has_more), result_(result) {}
43 virtual ~Event() {}
45 int chunk_length() const { return chunk_length_; }
46 bool has_more() const { return has_more_; }
47 base::File::Error result() const { return result_; }
49 private:
50 int chunk_length_;
51 bool has_more_;
52 base::File::Error result_;
54 DISALLOW_COPY_AND_ASSIGN(Event);
57 CallbackLogger() {}
58 virtual ~CallbackLogger() {}
60 void OnReadFile(int chunk_length, bool has_more, base::File::Error result) {
61 events_.push_back(new Event(chunk_length, has_more, result));
64 ScopedVector<Event>& events() { return events_; }
66 private:
67 ScopedVector<Event> events_;
69 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
72 } // namespace
74 class FileSystemProviderOperationsReadFileTest : public testing::Test {
75 protected:
76 FileSystemProviderOperationsReadFileTest() {}
77 ~FileSystemProviderOperationsReadFileTest() override {}
79 void SetUp() override {
80 file_system_info_ = ProvidedFileSystemInfo(
81 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
82 base::FilePath(), false /* configurable */, true /* watchable */,
83 extensions::SOURCE_FILE);
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,
98 file_system_info_,
99 kFileHandle,
100 io_buffer_.get(),
101 kOffset,
102 kLength,
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];
113 EXPECT_EQ(
114 extensions::api::file_system_provider::OnReadFileRequested::kEventName,
115 event->event_name);
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,
136 file_system_info_,
137 kFileHandle,
138 io_buffer_.get(),
139 kOffset,
140 kLength,
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,
158 file_system_info_,
159 kFileHandle,
160 io_buffer_.get(),
161 kOffset,
162 kLength,
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));
178 value_as_list.Set(
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,
204 file_system_info_,
205 kFileHandle,
206 io_buffer_.get(),
207 kOffset,
208 kLength,
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