OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/file_system_provider/operations/get_actions.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/json/json_reader.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/get_metadata.h
" |
| 16 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" |
| 17 #include "chrome/common/extensions/api/file_system_provider.h" |
| 18 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_sy
stem_provider_capabilities_handler.h" |
| 19 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 20 #include "extensions/browser/event_router.h" |
| 21 #include "storage/browser/fileapi/async_file_util.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace chromeos { |
| 25 namespace file_system_provider { |
| 26 namespace operations { |
| 27 namespace { |
| 28 |
| 29 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 30 const char kFileSystemId[] = "testing-file-system"; |
| 31 const int kRequestId = 2; |
| 32 const base::FilePath::CharType kEntryPath[] = FILE_PATH_LITERAL("/directory"); |
| 33 |
| 34 // Callback invocation logger. Acts as a fileapi end-point. |
| 35 class CallbackLogger { |
| 36 public: |
| 37 class Event { |
| 38 public: |
| 39 Event(const Actions& actions, base::File::Error result) |
| 40 : actions_(actions), result_(result) {} |
| 41 virtual ~Event() {} |
| 42 |
| 43 const Actions& actions() const { return actions_; } |
| 44 base::File::Error result() const { return result_; } |
| 45 |
| 46 private: |
| 47 Actions actions_; |
| 48 base::File::Error result_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(Event); |
| 51 }; |
| 52 |
| 53 CallbackLogger() {} |
| 54 virtual ~CallbackLogger() {} |
| 55 |
| 56 void OnGetActions(const Actions& actions, base::File::Error result) { |
| 57 events_.push_back(new Event(actions, result)); |
| 58 } |
| 59 |
| 60 const ScopedVector<Event>& events() const { return events_; } |
| 61 |
| 62 private: |
| 63 ScopedVector<Event> events_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); |
| 66 }; |
| 67 |
| 68 // Returns the request value as |result| in case of successful parse. |
| 69 void CreateRequestValueFromJSON(const std::string& json, |
| 70 scoped_ptr<RequestValue>* result) { |
| 71 using extensions::api::file_system_provider_internal:: |
| 72 GetActionsRequestedSuccess::Params; |
| 73 |
| 74 int json_error_code; |
| 75 std::string json_error_msg; |
| 76 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( |
| 77 json, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg); |
| 78 ASSERT_TRUE(value.get()) << json_error_msg; |
| 79 |
| 80 base::ListValue* value_as_list; |
| 81 ASSERT_TRUE(value->GetAsList(&value_as_list)); |
| 82 scoped_ptr<Params> params(Params::Create(*value_as_list)); |
| 83 ASSERT_TRUE(params.get()); |
| 84 *result = RequestValue::CreateForGetActionsSuccess(params.Pass()); |
| 85 ASSERT_TRUE(result->get()); |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 class FileSystemProviderOperationsGetActionsTest : public testing::Test { |
| 91 protected: |
| 92 FileSystemProviderOperationsGetActionsTest() {} |
| 93 ~FileSystemProviderOperationsGetActionsTest() override {} |
| 94 |
| 95 void SetUp() override { |
| 96 file_system_info_ = ProvidedFileSystemInfo( |
| 97 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */), |
| 98 base::FilePath(), false /* configurable */, extensions::SOURCE_FILE); |
| 99 } |
| 100 |
| 101 ProvidedFileSystemInfo file_system_info_; |
| 102 }; |
| 103 |
| 104 TEST_F(FileSystemProviderOperationsGetActionsTest, Execute) { |
| 105 using extensions::api::file_system_provider::GetActionsRequestedOptions; |
| 106 |
| 107 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 108 CallbackLogger callback_logger; |
| 109 |
| 110 GetActions get_actions(NULL, file_system_info_, base::FilePath(kEntryPath), |
| 111 base::Bind(&CallbackLogger::OnGetActions, |
| 112 base::Unretained(&callback_logger))); |
| 113 get_actions.SetDispatchEventImplForTesting( |
| 114 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 115 base::Unretained(&dispatcher))); |
| 116 |
| 117 EXPECT_TRUE(get_actions.Execute(kRequestId)); |
| 118 |
| 119 ASSERT_EQ(1u, dispatcher.events().size()); |
| 120 extensions::Event* event = dispatcher.events()[0]; |
| 121 EXPECT_EQ( |
| 122 extensions::api::file_system_provider::OnGetActionsRequested::kEventName, |
| 123 event->event_name); |
| 124 base::ListValue* event_args = event->event_args.get(); |
| 125 ASSERT_EQ(1u, event_args->GetSize()); |
| 126 |
| 127 const base::DictionaryValue* options_as_value = NULL; |
| 128 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); |
| 129 |
| 130 GetActionsRequestedOptions options; |
| 131 ASSERT_TRUE( |
| 132 GetActionsRequestedOptions::Populate(*options_as_value, &options)); |
| 133 EXPECT_EQ(kFileSystemId, options.file_system_id); |
| 134 EXPECT_EQ(kRequestId, options.request_id); |
| 135 EXPECT_EQ(kEntryPath, options.entry_path); |
| 136 } |
| 137 |
| 138 TEST_F(FileSystemProviderOperationsGetActionsTest, Execute_NoListener) { |
| 139 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
| 140 CallbackLogger callback_logger; |
| 141 |
| 142 GetActions get_actions(NULL, file_system_info_, base::FilePath(kEntryPath), |
| 143 base::Bind(&CallbackLogger::OnGetActions, |
| 144 base::Unretained(&callback_logger))); |
| 145 get_actions.SetDispatchEventImplForTesting( |
| 146 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 147 base::Unretained(&dispatcher))); |
| 148 |
| 149 EXPECT_FALSE(get_actions.Execute(kRequestId)); |
| 150 } |
| 151 |
| 152 TEST_F(FileSystemProviderOperationsGetActionsTest, OnSuccess) { |
| 153 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 154 CallbackLogger callback_logger; |
| 155 |
| 156 GetActions get_actions(NULL, file_system_info_, base::FilePath(kEntryPath), |
| 157 base::Bind(&CallbackLogger::OnGetActions, |
| 158 base::Unretained(&callback_logger))); |
| 159 get_actions.SetDispatchEventImplForTesting( |
| 160 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 161 base::Unretained(&dispatcher))); |
| 162 |
| 163 EXPECT_TRUE(get_actions.Execute(kRequestId)); |
| 164 |
| 165 // Sample input as JSON. Keep in sync with file_system_provider_api.idl. |
| 166 // As for now, it is impossible to create *::Params class directly, not from |
| 167 // base::Value. |
| 168 const std::string input = |
| 169 "[\n" |
| 170 " \"testing-file-system\",\n" // kFileSystemId |
| 171 " 2,\n" // kRequestId |
| 172 " [\n" |
| 173 " {\n" |
| 174 " \"id\": \"SAVE_FOR_OFFLINE\"\n" |
| 175 " },\n" |
| 176 " {\n" |
| 177 " \"id\": \"OFFLINE_NOT_NECESSARY\",\n" |
| 178 " \"title\": \"Ignored title.\"\n" |
| 179 " },\n" |
| 180 " {\n" |
| 181 " \"id\": \"SomeCustomActionId\",\n" |
| 182 " \"title\": \"Custom action.\"\n" |
| 183 " }\n" |
| 184 " ],\n" |
| 185 " 0\n" // execution_time |
| 186 "]\n"; |
| 187 scoped_ptr<RequestValue> request_value; |
| 188 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value)); |
| 189 |
| 190 const bool has_more = false; |
| 191 get_actions.OnSuccess(kRequestId, request_value.Pass(), has_more); |
| 192 |
| 193 ASSERT_EQ(1u, callback_logger.events().size()); |
| 194 CallbackLogger::Event* event = callback_logger.events()[0]; |
| 195 EXPECT_EQ(base::File::FILE_OK, event->result()); |
| 196 |
| 197 ASSERT_EQ(3u, event->actions().size()); |
| 198 const Action action_share = event->actions()[0]; |
| 199 EXPECT_EQ("SAVE_FOR_OFFLINE", action_share.id); |
| 200 EXPECT_EQ("", action_share.title); |
| 201 |
| 202 const Action action_pin_toggle = event->actions()[1]; |
| 203 EXPECT_EQ("OFFLINE_NOT_NECESSARY", action_pin_toggle.id); |
| 204 EXPECT_EQ("Ignored title.", action_pin_toggle.title); |
| 205 |
| 206 const Action action_custom = event->actions()[2]; |
| 207 EXPECT_EQ("SomeCustomActionId", action_custom.id); |
| 208 EXPECT_EQ("Custom action.", action_custom.title); |
| 209 } |
| 210 |
| 211 TEST_F(FileSystemProviderOperationsGetActionsTest, OnError) { |
| 212 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 213 CallbackLogger callback_logger; |
| 214 |
| 215 GetActions get_actions(NULL, file_system_info_, base::FilePath(kEntryPath), |
| 216 base::Bind(&CallbackLogger::OnGetActions, |
| 217 base::Unretained(&callback_logger))); |
| 218 get_actions.SetDispatchEventImplForTesting( |
| 219 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 220 base::Unretained(&dispatcher))); |
| 221 |
| 222 EXPECT_TRUE(get_actions.Execute(kRequestId)); |
| 223 |
| 224 get_actions.OnError(kRequestId, scoped_ptr<RequestValue>(new RequestValue()), |
| 225 base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 226 |
| 227 ASSERT_EQ(1u, callback_logger.events().size()); |
| 228 CallbackLogger::Event* event = callback_logger.events()[0]; |
| 229 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); |
| 230 ASSERT_EQ(0u, event->actions().size()); |
| 231 } |
| 232 |
| 233 } // namespace operations |
| 234 } // namespace file_system_provider |
| 235 } // namespace chromeos |
OLD | NEW |