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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 v8::Local<v8::Value> power_api = | 209 v8::Local<v8::Value> power_api = |
210 V8ValueFromScriptSource(context, "chrome.power"); | 210 V8ValueFromScriptSource(context, "chrome.power"); |
211 ASSERT_FALSE(power_api.IsEmpty()); | 211 ASSERT_FALSE(power_api.IsEmpty()); |
212 ASSERT_TRUE(power_api->IsObject()); | 212 ASSERT_TRUE(power_api->IsObject()); |
213 v8::Local<v8::Value> request_keep_awake = GetPropertyFromObject( | 213 v8::Local<v8::Value> request_keep_awake = GetPropertyFromObject( |
214 power_api.As<v8::Object>(), context, "requestKeepAwake"); | 214 power_api.As<v8::Object>(), context, "requestKeepAwake"); |
215 ASSERT_FALSE(request_keep_awake.IsEmpty()); | 215 ASSERT_FALSE(request_keep_awake.IsEmpty()); |
216 EXPECT_TRUE(request_keep_awake->IsFunction()); | 216 EXPECT_TRUE(request_keep_awake->IsFunction()); |
217 } | 217 } |
218 | 218 |
| 219 TEST_F(NativeExtensionBindingsSystemUnittest, Events) { |
| 220 scoped_refptr<Extension> extension = |
| 221 CreateExtension("foo", {"idle", "power"}); |
| 222 RegisterExtension(extension->id()); |
| 223 |
| 224 v8::HandleScope handle_scope(isolate()); |
| 225 v8::Local<v8::Context> context = ContextLocal(); |
| 226 |
| 227 ScriptContext* script_context = CreateScriptContext( |
| 228 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
| 229 script_context->set_url(extension->url()); |
| 230 |
| 231 bindings_system()->UpdateBindingsForContext(script_context); |
| 232 |
| 233 { |
| 234 const char kAddStateChangedListeners[] = |
| 235 "(function() {\n" |
| 236 " chrome.idle.onStateChanged.addListener(function() {\n" |
| 237 " this.didThrow = true;\n" |
| 238 " throw new Error('Error!!!');\n" |
| 239 " });\n" |
| 240 " chrome.idle.onStateChanged.addListener(function(newState) {\n" |
| 241 " this.newState = newState;\n" |
| 242 " });\n" |
| 243 "});"; |
| 244 |
| 245 v8::Local<v8::Function> add_listeners = |
| 246 FunctionFromString(context, kAddStateChangedListeners); |
| 247 RunFunctionOnGlobal(add_listeners, context, 0, nullptr); |
| 248 } |
| 249 |
| 250 bindings_system()->DispatchEventInContext( |
| 251 "idle.onStateChanged", ListValueFromString("['idle']").get(), |
| 252 nullptr, script_context); |
| 253 EXPECT_EQ( |
| 254 "\"idle\"", |
| 255 GetStringPropertyFromObject(context->Global(), context, "newState")); |
| 256 EXPECT_EQ( |
| 257 "true", |
| 258 GetStringPropertyFromObject(context->Global(), context, "didThrow")); |
| 259 } |
| 260 |
219 // Tests that referencing the same API multiple times returns the same object; | 261 // Tests that referencing the same API multiple times returns the same object; |
220 // i.e. chrome.foo === chrome.foo. | 262 // i.e. chrome.foo === chrome.foo. |
221 TEST_F(NativeExtensionBindingsSystemUnittest, APIObjectsAreEqual) { | 263 TEST_F(NativeExtensionBindingsSystemUnittest, APIObjectsAreEqual) { |
222 scoped_refptr<Extension> extension = CreateExtension("foo", {"idle"}); | 264 scoped_refptr<Extension> extension = CreateExtension("foo", {"idle"}); |
223 RegisterExtension(extension->id()); | 265 RegisterExtension(extension->id()); |
224 | 266 |
225 v8::HandleScope handle_scope(isolate()); | 267 v8::HandleScope handle_scope(isolate()); |
226 v8::Local<v8::Context> context = ContextLocal(); | 268 v8::Local<v8::Context> context = ContextLocal(); |
227 | 269 |
228 ScriptContext* script_context = CreateScriptContext( | 270 ScriptContext* script_context = CreateScriptContext( |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 EXPECT_EQ("20", GetStringPropertyFromObject(global, context, "intervalArg")); | 413 EXPECT_EQ("20", GetStringPropertyFromObject(global, context, "intervalArg")); |
372 EXPECT_EQ(extension->id(), last_params().extension_id); | 414 EXPECT_EQ(extension->id(), last_params().extension_id); |
373 EXPECT_EQ("idle.setDetectionInterval", last_params().name); | 415 EXPECT_EQ("idle.setDetectionInterval", last_params().name); |
374 EXPECT_EQ(extension->url(), last_params().source_url); | 416 EXPECT_EQ(extension->url(), last_params().source_url); |
375 EXPECT_FALSE(last_params().has_callback); | 417 EXPECT_FALSE(last_params().has_callback); |
376 EXPECT_TRUE( | 418 EXPECT_TRUE( |
377 last_params().arguments.Equals(ListValueFromString("[50]").get())); | 419 last_params().arguments.Equals(ListValueFromString("[50]").get())); |
378 } | 420 } |
379 | 421 |
380 } // namespace extensions | 422 } // namespace extensions |
OLD | NEW |