| 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 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 private: | 45 private: |
| 46 ScopedVector<extensions::Event> events_; | 46 ScopedVector<extensions::Event> events_; |
| 47 bool dispatch_reply_; | 47 bool dispatch_reply_; |
| 48 | 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl); | 49 DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl); |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // Callback invocation logger. Acts as a fileapi end-point. | 52 // Callback invocation logger. Acts as a fileapi end-point. |
| 53 class CallbackLogger { | 53 class CallbackLogger { |
| 54 public: | 54 public: |
| 55 class Event { |
| 56 public: |
| 57 Event(int file_handle, base::File::Error result) |
| 58 : file_handle_(file_handle), result_(result) {} |
| 59 virtual ~Event() {} |
| 60 |
| 61 int file_handle() { return file_handle_; } |
| 62 base::File::Error result() { return result_; } |
| 63 |
| 64 private: |
| 65 int file_handle_; |
| 66 base::File::Error result_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Event); |
| 69 }; |
| 70 |
| 55 CallbackLogger() : weak_ptr_factory_(this) {} | 71 CallbackLogger() : weak_ptr_factory_(this) {} |
| 56 virtual ~CallbackLogger() {} | 72 virtual ~CallbackLogger() {} |
| 57 | 73 |
| 58 void OnOpenFile(base::File::Error result) { events_.push_back(result); } | 74 void OnOpenFile(int file_handle, base::File::Error result) { |
| 75 events_.push_back(new Event(file_handle, result)); |
| 76 } |
| 59 | 77 |
| 60 std::vector<base::File::Error>& events() { return events_; } | 78 ScopedVector<Event>& events() { return events_; } |
| 61 | 79 |
| 62 base::WeakPtr<CallbackLogger> GetWeakPtr() { | 80 base::WeakPtr<CallbackLogger> GetWeakPtr() { |
| 63 return weak_ptr_factory_.GetWeakPtr(); | 81 return weak_ptr_factory_.GetWeakPtr(); |
| 64 } | 82 } |
| 65 | 83 |
| 66 private: | 84 private: |
| 67 std::vector<base::File::Error> events_; | 85 ScopedVector<Event> events_; |
| 86 bool dispatch_reply_; |
| 68 base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; | 87 base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; |
| 69 | 88 |
| 70 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); | 89 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); |
| 71 }; | 90 }; |
| 72 | 91 |
| 73 } // namespace | 92 } // namespace |
| 74 | 93 |
| 75 class FileSystemProviderOperationsOpenFileTest : public testing::Test { | 94 class FileSystemProviderOperationsOpenFileTest : public testing::Test { |
| 76 protected: | 95 protected: |
| 77 FileSystemProviderOperationsOpenFileTest() {} | 96 FileSystemProviderOperationsOpenFileTest() {} |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 open_file.SetDispatchEventImplForTesting( | 192 open_file.SetDispatchEventImplForTesting( |
| 174 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 193 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 175 base::Unretained(&dispatcher))); | 194 base::Unretained(&dispatcher))); |
| 176 | 195 |
| 177 EXPECT_TRUE(open_file.Execute(kRequestId)); | 196 EXPECT_TRUE(open_file.Execute(kRequestId)); |
| 178 | 197 |
| 179 open_file.OnSuccess(kRequestId, | 198 open_file.OnSuccess(kRequestId, |
| 180 scoped_ptr<RequestValue>(new RequestValue()), | 199 scoped_ptr<RequestValue>(new RequestValue()), |
| 181 false /* has_next */); | 200 false /* has_next */); |
| 182 ASSERT_EQ(1u, callback_logger.events().size()); | 201 ASSERT_EQ(1u, callback_logger.events().size()); |
| 183 EXPECT_EQ(base::File::FILE_OK, callback_logger.events()[0]); | 202 CallbackLogger::Event* event = callback_logger.events()[0]; |
| 203 EXPECT_EQ(base::File::FILE_OK, event->result()); |
| 204 EXPECT_LT(0, event->file_handle()); |
| 184 } | 205 } |
| 185 | 206 |
| 186 TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) { | 207 TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) { |
| 187 using extensions::api::file_system_provider::EntryMetadata; | 208 using extensions::api::file_system_provider::EntryMetadata; |
| 188 using extensions::api::file_system_provider_internal::OpenFileRequestedError:: | 209 using extensions::api::file_system_provider_internal::OpenFileRequestedError:: |
| 189 Params; | 210 Params; |
| 190 | 211 |
| 191 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 212 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 192 CallbackLogger callback_logger; | 213 CallbackLogger callback_logger; |
| 193 | 214 |
| 194 OpenFile open_file( | 215 OpenFile open_file( |
| 195 NULL, | 216 NULL, |
| 196 file_system_info_, | 217 file_system_info_, |
| 197 base::FilePath::FromUTF8Unsafe(kFilePath), | 218 base::FilePath::FromUTF8Unsafe(kFilePath), |
| 198 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ, | 219 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ, |
| 199 false /* create */, | 220 false /* create */, |
| 200 base::Bind(&CallbackLogger::OnOpenFile, callback_logger.GetWeakPtr())); | 221 base::Bind(&CallbackLogger::OnOpenFile, callback_logger.GetWeakPtr())); |
| 201 open_file.SetDispatchEventImplForTesting( | 222 open_file.SetDispatchEventImplForTesting( |
| 202 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 223 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 203 base::Unretained(&dispatcher))); | 224 base::Unretained(&dispatcher))); |
| 204 | 225 |
| 205 EXPECT_TRUE(open_file.Execute(kRequestId)); | 226 EXPECT_TRUE(open_file.Execute(kRequestId)); |
| 206 | 227 |
| 207 open_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED); | 228 open_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 208 ASSERT_EQ(1u, callback_logger.events().size()); | 229 ASSERT_EQ(1u, callback_logger.events().size()); |
| 209 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, | 230 CallbackLogger::Event* event = callback_logger.events()[0]; |
| 210 callback_logger.events()[0]); | 231 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); |
| 232 ASSERT_EQ(0, event->file_handle()); |
| 211 } | 233 } |
| 212 | 234 |
| 213 } // namespace operations | 235 } // namespace operations |
| 214 } // namespace file_system_provider | 236 } // namespace file_system_provider |
| 215 } // namespace chromeos | 237 } // namespace chromeos |
| OLD | NEW |