| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "extensions/renderer/api_request_handler.h" | 5 #include "extensions/renderer/api_request_handler.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/optional.h" | 8 #include "base/optional.h" |
| 9 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "extensions/renderer/api_binding_test.h" | 11 #include "extensions/renderer/api_binding_test.h" |
| 11 #include "extensions/renderer/api_binding_test_util.h" | 12 #include "extensions/renderer/api_binding_test_util.h" |
| 12 #include "gin/converter.h" | 13 #include "gin/converter.h" |
| 13 #include "gin/function_template.h" | 14 #include "gin/function_template.h" |
| 14 #include "gin/public/context_holder.h" | 15 #include "gin/public/context_holder.h" |
| 15 #include "gin/public/isolate_holder.h" | 16 #include "gin/public/isolate_holder.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 18 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 18 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 19 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 ListValueFromString(kArguments); | 86 ListValueFromString(kArguments); |
| 86 ASSERT_TRUE(response_arguments); | 87 ASSERT_TRUE(response_arguments); |
| 87 request_handler.CompleteRequest(request_id, *response_arguments, | 88 request_handler.CompleteRequest(request_id, *response_arguments, |
| 88 std::string()); | 89 std::string()); |
| 89 | 90 |
| 90 EXPECT_TRUE(did_run_js()); | 91 EXPECT_TRUE(did_run_js()); |
| 91 EXPECT_EQ(ReplaceSingleQuotes(kArguments), | 92 EXPECT_EQ(ReplaceSingleQuotes(kArguments), |
| 92 GetStringPropertyFromObject(context->Global(), context, "result")); | 93 GetStringPropertyFromObject(context->Global(), context, "result")); |
| 93 | 94 |
| 94 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 95 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
| 96 |
| 97 request_id = request_handler.StartRequest( |
| 98 context, kMethod, base::MakeUnique<base::ListValue>(), |
| 99 v8::Local<v8::Function>(), v8::Local<v8::Function>(), |
| 100 binding::RequestThread::UI); |
| 101 EXPECT_NE(-1, request_id); |
| 102 request_handler.CompleteRequest(request_id, base::ListValue(), std::string()); |
| 95 } | 103 } |
| 96 | 104 |
| 97 // Tests that trying to run non-existent or invalided requests is a no-op. | 105 // Tests that trying to run non-existent or invalided requests is a no-op. |
| 98 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { | 106 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { |
| 99 v8::HandleScope handle_scope(isolate()); | 107 v8::HandleScope handle_scope(isolate()); |
| 100 v8::Local<v8::Context> context = MainContext(); | 108 v8::Local<v8::Context> context = MainContext(); |
| 101 | 109 |
| 102 APIRequestHandler request_handler( | 110 APIRequestHandler request_handler( |
| 103 base::Bind(&DoNothingWithRequest), | 111 base::Bind(&DoNothingWithRequest), |
| 104 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 112 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 221 |
| 214 EXPECT_TRUE(did_run_js()); | 222 EXPECT_TRUE(did_run_js()); |
| 215 v8::Local<v8::Value> result = | 223 v8::Local<v8::Value> result = |
| 216 GetPropertyFromObject(context->Global(), context, "result"); | 224 GetPropertyFromObject(context->Global(), context, "result"); |
| 217 ASSERT_FALSE(result.IsEmpty()); | 225 ASSERT_FALSE(result.IsEmpty()); |
| 218 ASSERT_TRUE(result->IsArray()); | 226 ASSERT_TRUE(result->IsArray()); |
| 219 ArgumentList args; | 227 ArgumentList args; |
| 220 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); | 228 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); |
| 221 ASSERT_EQ(5u, args.size()); | 229 ASSERT_EQ(5u, args.size()); |
| 222 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); | 230 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); |
| 223 EXPECT_EQ("{}", V8ToString(args[1], context)); | 231 EXPECT_EQ(base::StringPrintf("{\"id\":%d}", request_id), |
| 232 V8ToString(args[1], context)); |
| 224 EXPECT_EQ(callback, args[2]); | 233 EXPECT_EQ(callback, args[2]); |
| 225 EXPECT_EQ("\"response\"", V8ToString(args[3], context)); | 234 EXPECT_EQ("\"response\"", V8ToString(args[3], context)); |
| 226 EXPECT_EQ("\"arguments\"", V8ToString(args[4], context)); | 235 EXPECT_EQ("\"arguments\"", V8ToString(args[4], context)); |
| 227 | 236 |
| 228 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 237 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
| 229 } | 238 } |
| 230 | 239 |
| 231 // Test that having a custom callback without an extension-provided callback | 240 // Test that having a custom callback without an extension-provided callback |
| 232 // doesn't crash. | 241 // doesn't crash. |
| 233 TEST_F(APIRequestHandlerTest, CustomCallbackArgumentsWithEmptyCallback) { | 242 TEST_F(APIRequestHandlerTest, CustomCallbackArgumentsWithEmptyCallback) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 254 | 263 |
| 255 EXPECT_TRUE(did_run_js()); | 264 EXPECT_TRUE(did_run_js()); |
| 256 v8::Local<v8::Value> result = | 265 v8::Local<v8::Value> result = |
| 257 GetPropertyFromObject(context->Global(), context, "result"); | 266 GetPropertyFromObject(context->Global(), context, "result"); |
| 258 ASSERT_FALSE(result.IsEmpty()); | 267 ASSERT_FALSE(result.IsEmpty()); |
| 259 ASSERT_TRUE(result->IsArray()); | 268 ASSERT_TRUE(result->IsArray()); |
| 260 ArgumentList args; | 269 ArgumentList args; |
| 261 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); | 270 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); |
| 262 ASSERT_EQ(3u, args.size()); | 271 ASSERT_EQ(3u, args.size()); |
| 263 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); | 272 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); |
| 264 EXPECT_EQ("{}", V8ToString(args[1], context)); | 273 EXPECT_EQ(base::StringPrintf("{\"id\":%d}", request_id), |
| 274 V8ToString(args[1], context)); |
| 265 EXPECT_TRUE(args[2]->IsUndefined()); | 275 EXPECT_TRUE(args[2]->IsUndefined()); |
| 266 | 276 |
| 267 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 277 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
| 268 } | 278 } |
| 269 | 279 |
| 270 // Test user gestures being curried around for API requests. | 280 // Test user gestures being curried around for API requests. |
| 271 TEST_F(APIRequestHandlerTest, UserGestureTest) { | 281 TEST_F(APIRequestHandlerTest, UserGestureTest) { |
| 272 v8::HandleScope handle_scope(isolate()); | 282 v8::HandleScope handle_scope(isolate()); |
| 273 v8::Local<v8::Context> context = MainContext(); | 283 v8::Local<v8::Context> context = MainContext(); |
| 274 | 284 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 v8::Local<v8::Function>(), binding::RequestThread::UI); | 438 v8::Local<v8::Function>(), binding::RequestThread::UI); |
| 429 request_handler.CompleteRequest(request_id, base::ListValue(), | 439 request_handler.CompleteRequest(request_id, base::ListValue(), |
| 430 "some error"); | 440 "some error"); |
| 431 ASSERT_TRUE(logged_error); | 441 ASSERT_TRUE(logged_error); |
| 432 EXPECT_EQ("Unchecked runtime.lastError: some error", *logged_error); | 442 EXPECT_EQ("Unchecked runtime.lastError: some error", *logged_error); |
| 433 logged_error.reset(); | 443 logged_error.reset(); |
| 434 } | 444 } |
| 435 } | 445 } |
| 436 | 446 |
| 437 } // namespace extensions | 447 } // namespace extensions |
| OLD | NEW |