| 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 214 |
| 214 EXPECT_TRUE(did_run_js()); | 215 EXPECT_TRUE(did_run_js()); |
| 215 v8::Local<v8::Value> result = | 216 v8::Local<v8::Value> result = |
| 216 GetPropertyFromObject(context->Global(), context, "result"); | 217 GetPropertyFromObject(context->Global(), context, "result"); |
| 217 ASSERT_FALSE(result.IsEmpty()); | 218 ASSERT_FALSE(result.IsEmpty()); |
| 218 ASSERT_TRUE(result->IsArray()); | 219 ASSERT_TRUE(result->IsArray()); |
| 219 ArgumentList args; | 220 ArgumentList args; |
| 220 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); | 221 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); |
| 221 ASSERT_EQ(5u, args.size()); | 222 ASSERT_EQ(5u, args.size()); |
| 222 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); | 223 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); |
| 223 EXPECT_EQ("{}", V8ToString(args[1], context)); | 224 EXPECT_EQ(base::StringPrintf("{\"id\":%d}", request_id), |
| 225 V8ToString(args[1], context)); |
| 224 EXPECT_EQ(callback, args[2]); | 226 EXPECT_EQ(callback, args[2]); |
| 225 EXPECT_EQ("\"response\"", V8ToString(args[3], context)); | 227 EXPECT_EQ("\"response\"", V8ToString(args[3], context)); |
| 226 EXPECT_EQ("\"arguments\"", V8ToString(args[4], context)); | 228 EXPECT_EQ("\"arguments\"", V8ToString(args[4], context)); |
| 227 | 229 |
| 228 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 230 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
| 229 } | 231 } |
| 230 | 232 |
| 231 // Test that having a custom callback without an extension-provided callback | 233 // Test that having a custom callback without an extension-provided callback |
| 232 // doesn't crash. | 234 // doesn't crash. |
| 233 TEST_F(APIRequestHandlerTest, CustomCallbackArgumentsWithEmptyCallback) { | 235 TEST_F(APIRequestHandlerTest, CustomCallbackArgumentsWithEmptyCallback) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 254 | 256 |
| 255 EXPECT_TRUE(did_run_js()); | 257 EXPECT_TRUE(did_run_js()); |
| 256 v8::Local<v8::Value> result = | 258 v8::Local<v8::Value> result = |
| 257 GetPropertyFromObject(context->Global(), context, "result"); | 259 GetPropertyFromObject(context->Global(), context, "result"); |
| 258 ASSERT_FALSE(result.IsEmpty()); | 260 ASSERT_FALSE(result.IsEmpty()); |
| 259 ASSERT_TRUE(result->IsArray()); | 261 ASSERT_TRUE(result->IsArray()); |
| 260 ArgumentList args; | 262 ArgumentList args; |
| 261 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); | 263 ASSERT_TRUE(gin::Converter<ArgumentList>::FromV8(isolate(), result, &args)); |
| 262 ASSERT_EQ(3u, args.size()); | 264 ASSERT_EQ(3u, args.size()); |
| 263 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); | 265 EXPECT_EQ("\"method\"", V8ToString(args[0], context)); |
| 264 EXPECT_EQ("{}", V8ToString(args[1], context)); | 266 EXPECT_EQ(base::StringPrintf("{\"id\":%d}", request_id), |
| 267 V8ToString(args[1], context)); |
| 265 EXPECT_TRUE(args[2]->IsUndefined()); | 268 EXPECT_TRUE(args[2]->IsUndefined()); |
| 266 | 269 |
| 267 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 270 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
| 268 } | 271 } |
| 269 | 272 |
| 270 // Test user gestures being curried around for API requests. | 273 // Test user gestures being curried around for API requests. |
| 271 TEST_F(APIRequestHandlerTest, UserGestureTest) { | 274 TEST_F(APIRequestHandlerTest, UserGestureTest) { |
| 272 v8::HandleScope handle_scope(isolate()); | 275 v8::HandleScope handle_scope(isolate()); |
| 273 v8::Local<v8::Context> context = MainContext(); | 276 v8::Local<v8::Context> context = MainContext(); |
| 274 | 277 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 v8::Local<v8::Function>(), binding::RequestThread::UI); | 431 v8::Local<v8::Function>(), binding::RequestThread::UI); |
| 429 request_handler.CompleteRequest(request_id, base::ListValue(), | 432 request_handler.CompleteRequest(request_id, base::ListValue(), |
| 430 "some error"); | 433 "some error"); |
| 431 ASSERT_TRUE(logged_error); | 434 ASSERT_TRUE(logged_error); |
| 432 EXPECT_EQ("Unchecked runtime.lastError: some error", *logged_error); | 435 EXPECT_EQ("Unchecked runtime.lastError: some error", *logged_error); |
| 433 logged_error.reset(); | 436 logged_error.reset(); |
| 434 } | 437 } |
| 435 } | 438 } |
| 436 | 439 |
| 437 } // namespace extensions | 440 } // namespace extensions |
| OLD | NEW |