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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 GetStringPropertyFromObject(idle_api.As<v8::Object>(), context, | 339 GetStringPropertyFromObject(idle_api.As<v8::Object>(), context, |
335 "hookedApiProperty")); | 340 "hookedApiProperty")); |
336 | 341 |
337 // Next, we need to check two pieces: first, that the custom handler was | 342 // Next, we need to check two pieces: first, that the custom handler was |
338 // called with the proper arguments.... | 343 // called with the proper arguments.... |
339 EXPECT_EQ("30", GetStringPropertyFromObject(global, context, "timeArg")); | 344 EXPECT_EQ("30", GetStringPropertyFromObject(global, context, "timeArg")); |
340 | 345 |
341 // ...and second, that the callback was called with the proper result. | 346 // ...and second, that the callback was called with the proper result. |
342 EXPECT_EQ("\"active\"", | 347 EXPECT_EQ("\"active\"", |
343 GetStringPropertyFromObject(global, context, "responseState")); | 348 GetStringPropertyFromObject(global, context, "responseState")); |
| 349 |
| 350 // Test the updateArgumentsPreValidate hook. |
| 351 { |
| 352 // Call the function correctly. |
| 353 const char kCallIdleSetInterval[] = |
| 354 "(function() {\n" |
| 355 " chrome.idle.setDetectionInterval(20);\n" |
| 356 "});"; |
| 357 |
| 358 v8::Local<v8::Function> call_idle_set_interval = |
| 359 FunctionFromString(context, kCallIdleSetInterval); |
| 360 RunFunctionOnGlobal(call_idle_set_interval, context, 0, nullptr); |
| 361 } |
| 362 |
| 363 // Since we don't have a custom request handler, the hook should have only |
| 364 // updated the arguments. The request then should have gone to the browser |
| 365 // normally. |
| 366 EXPECT_EQ("20", GetStringPropertyFromObject(global, context, "intervalArg")); |
| 367 EXPECT_EQ(extension->id(), last_params().extension_id); |
| 368 EXPECT_EQ("idle.setDetectionInterval", last_params().name); |
| 369 EXPECT_EQ(extension->url(), last_params().source_url); |
| 370 EXPECT_FALSE(last_params().has_callback); |
| 371 EXPECT_TRUE( |
| 372 last_params().arguments.Equals(ListValueFromString("[50]").get())); |
344 } | 373 } |
345 | 374 |
346 } // namespace extensions | 375 } // namespace extensions |
OLD | NEW |