OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 base::File::Error result() const { return result_; } | 66 base::File::Error result() const { return result_; } |
67 | 67 |
68 private: | 68 private: |
69 int chunk_length_; | 69 int chunk_length_; |
70 bool has_more_; | 70 bool has_more_; |
71 base::File::Error result_; | 71 base::File::Error result_; |
72 | 72 |
73 DISALLOW_COPY_AND_ASSIGN(Event); | 73 DISALLOW_COPY_AND_ASSIGN(Event); |
74 }; | 74 }; |
75 | 75 |
76 CallbackLogger() : weak_ptr_factory_(this) {} | 76 CallbackLogger() {} |
77 virtual ~CallbackLogger() {} | 77 virtual ~CallbackLogger() {} |
78 | 78 |
79 void OnReadFile(int chunk_length, bool has_more, base::File::Error result) { | 79 void OnReadFile(int chunk_length, bool has_more, base::File::Error result) { |
80 events_.push_back(new Event(chunk_length, has_more, result)); | 80 events_.push_back(new Event(chunk_length, has_more, result)); |
81 } | 81 } |
82 | 82 |
83 ScopedVector<Event>& events() { return events_; } | 83 ScopedVector<Event>& events() { return events_; } |
84 | 84 |
85 base::WeakPtr<CallbackLogger> GetWeakPtr() { | |
86 return weak_ptr_factory_.GetWeakPtr(); | |
87 } | |
88 | |
89 private: | 85 private: |
90 ScopedVector<Event> events_; | 86 ScopedVector<Event> events_; |
91 bool dispatch_reply_; | 87 bool dispatch_reply_; |
92 base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; | |
93 | 88 |
94 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); | 89 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); |
95 }; | 90 }; |
96 | 91 |
97 } // namespace | 92 } // namespace |
98 | 93 |
99 class FileSystemProviderOperationsReadFileTest : public testing::Test { | 94 class FileSystemProviderOperationsReadFileTest : public testing::Test { |
100 protected: | 95 protected: |
101 FileSystemProviderOperationsReadFileTest() {} | 96 FileSystemProviderOperationsReadFileTest() {} |
102 virtual ~FileSystemProviderOperationsReadFileTest() {} | 97 virtual ~FileSystemProviderOperationsReadFileTest() {} |
103 | 98 |
104 virtual void SetUp() OVERRIDE { | 99 virtual void SetUp() OVERRIDE { |
105 file_system_info_ = | 100 file_system_info_ = |
106 ProvidedFileSystemInfo(kExtensionId, | 101 ProvidedFileSystemInfo(kExtensionId, |
107 kFileSystemId, | 102 kFileSystemId, |
108 "" /* file_system_name */, | 103 "" /* file_system_name */, |
109 base::FilePath() /* mount_path */); | 104 base::FilePath() /* mount_path */); |
110 io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength)); | 105 io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength)); |
111 } | 106 } |
112 | 107 |
113 ProvidedFileSystemInfo file_system_info_; | 108 ProvidedFileSystemInfo file_system_info_; |
114 scoped_refptr<net::IOBuffer> io_buffer_; | 109 scoped_refptr<net::IOBuffer> io_buffer_; |
115 }; | 110 }; |
116 | 111 |
117 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) { | 112 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) { |
118 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 113 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
119 CallbackLogger callback_logger; | 114 CallbackLogger callback_logger; |
120 | 115 |
121 ReadFile read_file( | 116 ReadFile read_file(NULL, |
122 NULL, | 117 file_system_info_, |
123 file_system_info_, | 118 kFileHandle, |
124 kFileHandle, | 119 io_buffer_.get(), |
125 io_buffer_.get(), | 120 kOffset, |
126 kOffset, | 121 kLength, |
127 kLength, | 122 base::Bind(&CallbackLogger::OnReadFile, |
128 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); | 123 base::Unretained(&callback_logger))); |
129 read_file.SetDispatchEventImplForTesting( | 124 read_file.SetDispatchEventImplForTesting( |
130 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 125 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
131 base::Unretained(&dispatcher))); | 126 base::Unretained(&dispatcher))); |
132 | 127 |
133 EXPECT_TRUE(read_file.Execute(kRequestId)); | 128 EXPECT_TRUE(read_file.Execute(kRequestId)); |
134 | 129 |
135 ASSERT_EQ(1u, dispatcher.events().size()); | 130 ASSERT_EQ(1u, dispatcher.events().size()); |
136 extensions::Event* event = dispatcher.events()[0]; | 131 extensions::Event* event = dispatcher.events()[0]; |
137 EXPECT_EQ( | 132 EXPECT_EQ( |
138 extensions::api::file_system_provider::OnReadFileRequested::kEventName, | 133 extensions::api::file_system_provider::OnReadFileRequested::kEventName, |
(...skipping 22 matching lines...) Expand all Loading... |
161 | 156 |
162 int event_length = -1; | 157 int event_length = -1; |
163 EXPECT_TRUE(options->GetInteger("length", &event_length)); | 158 EXPECT_TRUE(options->GetInteger("length", &event_length)); |
164 EXPECT_EQ(kLength, event_length); | 159 EXPECT_EQ(kLength, event_length); |
165 } | 160 } |
166 | 161 |
167 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) { | 162 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) { |
168 LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 163 LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
169 CallbackLogger callback_logger; | 164 CallbackLogger callback_logger; |
170 | 165 |
171 ReadFile read_file( | 166 ReadFile read_file(NULL, |
172 NULL, | 167 file_system_info_, |
173 file_system_info_, | 168 kFileHandle, |
174 kFileHandle, | 169 io_buffer_.get(), |
175 io_buffer_.get(), | 170 kOffset, |
176 kOffset, | 171 kLength, |
177 kLength, | 172 base::Bind(&CallbackLogger::OnReadFile, |
178 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); | 173 base::Unretained(&callback_logger))); |
179 read_file.SetDispatchEventImplForTesting( | 174 read_file.SetDispatchEventImplForTesting( |
180 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 175 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
181 base::Unretained(&dispatcher))); | 176 base::Unretained(&dispatcher))); |
182 | 177 |
183 EXPECT_FALSE(read_file.Execute(kRequestId)); | 178 EXPECT_FALSE(read_file.Execute(kRequestId)); |
184 } | 179 } |
185 | 180 |
186 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) { | 181 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) { |
187 using extensions::api::file_system_provider_internal:: | 182 using extensions::api::file_system_provider_internal:: |
188 ReadFileRequestedSuccess::Params; | 183 ReadFileRequestedSuccess::Params; |
189 | 184 |
190 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 185 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
191 CallbackLogger callback_logger; | 186 CallbackLogger callback_logger; |
192 | 187 |
193 ReadFile read_file( | 188 ReadFile read_file(NULL, |
194 NULL, | 189 file_system_info_, |
195 file_system_info_, | 190 kFileHandle, |
196 kFileHandle, | 191 io_buffer_.get(), |
197 io_buffer_.get(), | 192 kOffset, |
198 kOffset, | 193 kLength, |
199 kLength, | 194 base::Bind(&CallbackLogger::OnReadFile, |
200 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); | 195 base::Unretained(&callback_logger))); |
201 read_file.SetDispatchEventImplForTesting( | 196 read_file.SetDispatchEventImplForTesting( |
202 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 197 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
203 base::Unretained(&dispatcher))); | 198 base::Unretained(&dispatcher))); |
204 | 199 |
205 EXPECT_TRUE(read_file.Execute(kRequestId)); | 200 EXPECT_TRUE(read_file.Execute(kRequestId)); |
206 | 201 |
207 const std::string data = "ABCDE"; | 202 const std::string data = "ABCDE"; |
208 const bool has_more = false; | 203 const bool has_more = false; |
209 const int execution_time = 0; | 204 const int execution_time = 0; |
210 | 205 |
(...skipping 18 matching lines...) Expand all Loading... |
229 EXPECT_EQ(kLength, event->chunk_length()); | 224 EXPECT_EQ(kLength, event->chunk_length()); |
230 EXPECT_FALSE(event->has_more()); | 225 EXPECT_FALSE(event->has_more()); |
231 EXPECT_EQ(data, std::string(io_buffer_->data(), kLength)); | 226 EXPECT_EQ(data, std::string(io_buffer_->data(), kLength)); |
232 EXPECT_EQ(base::File::FILE_OK, event->result()); | 227 EXPECT_EQ(base::File::FILE_OK, event->result()); |
233 } | 228 } |
234 | 229 |
235 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) { | 230 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) { |
236 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 231 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
237 CallbackLogger callback_logger; | 232 CallbackLogger callback_logger; |
238 | 233 |
239 ReadFile read_file( | 234 ReadFile read_file(NULL, |
240 NULL, | 235 file_system_info_, |
241 file_system_info_, | 236 kFileHandle, |
242 kFileHandle, | 237 io_buffer_.get(), |
243 io_buffer_.get(), | 238 kOffset, |
244 kOffset, | 239 kLength, |
245 kLength, | 240 base::Bind(&CallbackLogger::OnReadFile, |
246 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); | 241 base::Unretained(&callback_logger))); |
247 read_file.SetDispatchEventImplForTesting( | 242 read_file.SetDispatchEventImplForTesting( |
248 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 243 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
249 base::Unretained(&dispatcher))); | 244 base::Unretained(&dispatcher))); |
250 | 245 |
251 EXPECT_TRUE(read_file.Execute(kRequestId)); | 246 EXPECT_TRUE(read_file.Execute(kRequestId)); |
252 | 247 |
253 read_file.OnError(kRequestId, | 248 read_file.OnError(kRequestId, |
254 scoped_ptr<RequestValue>(new RequestValue()), | 249 scoped_ptr<RequestValue>(new RequestValue()), |
255 base::File::FILE_ERROR_TOO_MANY_OPENED); | 250 base::File::FILE_ERROR_TOO_MANY_OPENED); |
256 | 251 |
257 ASSERT_EQ(1u, callback_logger.events().size()); | 252 ASSERT_EQ(1u, callback_logger.events().size()); |
258 CallbackLogger::Event* event = callback_logger.events()[0]; | 253 CallbackLogger::Event* event = callback_logger.events()[0]; |
259 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); | 254 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); |
260 } | 255 } |
261 | 256 |
262 } // namespace operations | 257 } // namespace operations |
263 } // namespace file_system_provider | 258 } // namespace file_system_provider |
264 } // namespace chromeos | 259 } // namespace chromeos |
OLD | NEW |