| 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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 " chrome.idle.queryState(30, function(state) {\n" | 311 " chrome.idle.queryState(30, function(state) {\n" |
| 312 " this.responseState = state;\n" | 312 " this.responseState = state;\n" |
| 313 " });\n" | 313 " });\n" |
| 314 "});"; | 314 "});"; |
| 315 | 315 |
| 316 v8::Local<v8::Function> call_idle_query_state = | 316 v8::Local<v8::Function> call_idle_query_state = |
| 317 FunctionFromString(context, kCallIdleQueryState); | 317 FunctionFromString(context, kCallIdleQueryState); |
| 318 RunFunctionOnGlobal(call_idle_query_state, context, 0, nullptr); | 318 RunFunctionOnGlobal(call_idle_query_state, context, 0, nullptr); |
| 319 } | 319 } |
| 320 | 320 |
| 321 auto get_property_as_string = [&context](v8::Local<v8::Object> object, | |
| 322 base::StringPiece property_name) { | |
| 323 std::unique_ptr<base::Value> property = | |
| 324 GetBaseValuePropertyFromObject(object, context, property_name); | |
| 325 if (!property) | |
| 326 return std::string(); | |
| 327 return ValueToString(*property); | |
| 328 }; | |
| 329 | |
| 330 // To start, check that the properties we set when running the hooks are | 321 // To start, check that the properties we set when running the hooks are |
| 331 // correct. We do this after calling the function because the API objects (and | 322 // correct. We do this after calling the function because the API objects (and |
| 332 // thus the hooks) are set up lazily. | 323 // thus the hooks) are set up lazily. |
| 333 v8::Local<v8::Object> global = context->Global(); | 324 v8::Local<v8::Object> global = context->Global(); |
| 334 EXPECT_EQ(base::StringPrintf("\"%s\"", extension->id().c_str()), | 325 EXPECT_EQ(base::StringPrintf("\"%s\"", extension->id().c_str()), |
| 335 get_property_as_string(global, "hookedExtensionId")); | 326 GetStringPropertyFromObject(global, context, "hookedExtensionId")); |
| 336 EXPECT_EQ("\"BLESSED_EXTENSION\"", | 327 EXPECT_EQ("\"BLESSED_EXTENSION\"", |
| 337 get_property_as_string(global, "hookedContextType")); | 328 GetStringPropertyFromObject(global, context, "hookedContextType")); |
| 338 v8::Local<v8::Value> idle_api = | 329 v8::Local<v8::Value> idle_api = |
| 339 V8ValueFromScriptSource(context, "chrome.idle"); | 330 V8ValueFromScriptSource(context, "chrome.idle"); |
| 340 ASSERT_FALSE(idle_api.IsEmpty()); | 331 ASSERT_FALSE(idle_api.IsEmpty()); |
| 341 ASSERT_TRUE(idle_api->IsObject()); | 332 ASSERT_TRUE(idle_api->IsObject()); |
| 342 EXPECT_EQ("\"someProperty\"", | 333 EXPECT_EQ("\"someProperty\"", |
| 343 get_property_as_string(idle_api.As<v8::Object>(), | 334 GetStringPropertyFromObject(idle_api.As<v8::Object>(), context, |
| 344 "hookedApiProperty")); | 335 "hookedApiProperty")); |
| 345 | 336 |
| 346 // Next, we need to check two pieces: first, that the custom handler was | 337 // Next, we need to check two pieces: first, that the custom handler was |
| 347 // called with the proper arguments.... | 338 // called with the proper arguments.... |
| 348 EXPECT_EQ("30", get_property_as_string(global, "timeArg")); | 339 EXPECT_EQ("30", GetStringPropertyFromObject(global, context, "timeArg")); |
| 349 | 340 |
| 350 // ...and second, that the callback was called with the proper result. | 341 // ...and second, that the callback was called with the proper result. |
| 351 EXPECT_EQ("\"active\"", get_property_as_string(global, "responseState")); | 342 EXPECT_EQ("\"active\"", |
| 343 GetStringPropertyFromObject(global, context, "responseState")); |
| 352 } | 344 } |
| 353 | 345 |
| 354 } // namespace extensions | 346 } // namespace extensions |
| OLD | NEW |