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/native_extension_bindings_system.h" | 5 #include "extensions/renderer/native_extension_bindings_system.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 TEST_F(NativeExtensionBindingsSystemUnittest, TestBridgingToJSCustomBindings) { | 278 TEST_F(NativeExtensionBindingsSystemUnittest, TestBridgingToJSCustomBindings) { |
279 // Custom binding code. This basically utilizes the interface in binding.js in | 279 // Custom binding code. This basically utilizes the interface in binding.js in |
280 // order to test backwards compatibility. | 280 // order to test backwards compatibility. |
281 const char kCustomBinding[] = | 281 const char kCustomBinding[] = |
282 "apiBridge.registerCustomHook((api, extensionId, contextType) => {\n" | 282 "apiBridge.registerCustomHook((api, extensionId, contextType) => {\n" |
283 " api.apiFunctions.setHandleRequest('queryState',\n" | 283 " api.apiFunctions.setHandleRequest('queryState',\n" |
284 " (time, callback) => {\n" | 284 " (time, callback) => {\n" |
285 " this.timeArg = time;\n" | 285 " this.timeArg = time;\n" |
286 " callback('active');\n" | 286 " callback('active');\n" |
287 " });\n" | 287 " });\n" |
| 288 " api.apiFunctions.setUpdateArgumentsPreValidate(\n" |
| 289 " 'setDetectionInterval', (interval) => {\n" |
| 290 " this.intervalArg = interval;\n" |
| 291 " return [50];\n" |
| 292 " });\n" |
288 " this.hookedExtensionId = extensionId;\n" | 293 " this.hookedExtensionId = extensionId;\n" |
289 " this.hookedContextType = contextType;\n" | 294 " this.hookedContextType = contextType;\n" |
290 " api.compiledApi.hookedApiProperty = 'someProperty';\n" | 295 " api.compiledApi.hookedApiProperty = 'someProperty';\n" |
291 "});\n"; | 296 "});\n"; |
292 | 297 |
293 source_map()->RegisterModule("idle", kCustomBinding); | 298 source_map()->RegisterModule("idle", kCustomBinding); |
294 | 299 |
295 scoped_refptr<Extension> extension = CreateExtension("foo", {"idle"}); | 300 scoped_refptr<Extension> extension = CreateExtension("foo", {"idle"}); |
296 RegisterExtension(extension->id()); | 301 RegisterExtension(extension->id()); |
297 | 302 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 EXPECT_EQ("\"someProperty\"", | 347 EXPECT_EQ("\"someProperty\"", |
343 get_property_as_string(idle_api.As<v8::Object>(), | 348 get_property_as_string(idle_api.As<v8::Object>(), |
344 "hookedApiProperty")); | 349 "hookedApiProperty")); |
345 | 350 |
346 // Next, we need to check two pieces: first, that the custom handler was | 351 // Next, we need to check two pieces: first, that the custom handler was |
347 // called with the proper arguments.... | 352 // called with the proper arguments.... |
348 EXPECT_EQ("30", get_property_as_string(global, "timeArg")); | 353 EXPECT_EQ("30", get_property_as_string(global, "timeArg")); |
349 | 354 |
350 // ...and second, that the callback was called with the proper result. | 355 // ...and second, that the callback was called with the proper result. |
351 EXPECT_EQ("\"active\"", get_property_as_string(global, "responseState")); | 356 EXPECT_EQ("\"active\"", get_property_as_string(global, "responseState")); |
| 357 |
| 358 // Test the updateArgumentsPreValidate hook. |
| 359 { |
| 360 // Call the function correctly. |
| 361 const char kCallIdleSetInterval[] = |
| 362 "(function() {\n" |
| 363 " chrome.idle.setDetectionInterval(20);\n" |
| 364 "});"; |
| 365 |
| 366 v8::Local<v8::Function> call_idle_set_interval = |
| 367 FunctionFromString(context, kCallIdleSetInterval); |
| 368 RunFunctionOnGlobal(call_idle_set_interval, context, 0, nullptr); |
| 369 } |
| 370 |
| 371 // Since we don't have a custom request handler, the hook should have only |
| 372 // updated the arguments. The request then should have gone to the browser |
| 373 // normally. |
| 374 EXPECT_EQ("20", get_property_as_string(global, "intervalArg")); |
| 375 EXPECT_EQ(extension->id(), last_params().extension_id); |
| 376 EXPECT_EQ("idle.setDetectionInterval", last_params().name); |
| 377 EXPECT_EQ(extension->url(), last_params().source_url); |
| 378 EXPECT_FALSE(last_params().has_callback); |
| 379 EXPECT_TRUE( |
| 380 last_params().arguments.Equals(ListValueFromString("[50]").get())); |
352 } | 381 } |
353 | 382 |
354 } // namespace extensions | 383 } // namespace extensions |
OLD | NEW |