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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
11 #include "extensions/renderer/api_binding.h" | 11 #include "extensions/renderer/api_binding.h" |
12 #include "extensions/renderer/api_binding_test_util.h" | 12 #include "extensions/renderer/api_binding_test_util.h" |
| 13 #include "extensions/renderer/api_event_handler.h" |
13 #include "extensions/renderer/api_request_handler.h" | 14 #include "extensions/renderer/api_request_handler.h" |
14 #include "gin/converter.h" | 15 #include "gin/converter.h" |
15 #include "gin/public/context_holder.h" | 16 #include "gin/public/context_holder.h" |
16 #include "gin/public/isolate_holder.h" | 17 #include "gin/public/isolate_holder.h" |
17 #include "gin/test/v8_test.h" | 18 #include "gin/test/v8_test.h" |
18 #include "gin/try_catch.h" | 19 #include "gin/try_catch.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
21 | 22 |
22 namespace extensions { | 23 namespace extensions { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 192 } |
192 | 193 |
193 arguments_.reset(); | 194 arguments_.reset(); |
194 } | 195 } |
195 | 196 |
196 TEST_F(APIBindingTest, Test) { | 197 TEST_F(APIBindingTest, Test) { |
197 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 198 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
198 ASSERT_TRUE(functions); | 199 ASSERT_TRUE(functions); |
199 ArgumentSpec::RefMap refs; | 200 ArgumentSpec::RefMap refs; |
200 APIBinding binding( | 201 APIBinding binding( |
201 "test", *functions, nullptr, | 202 "test", *functions, nullptr, nullptr, |
202 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), | 203 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), |
203 &refs); | 204 &refs); |
204 EXPECT_TRUE(refs.empty()); | 205 EXPECT_TRUE(refs.empty()); |
205 | 206 |
206 v8::Isolate* isolate = instance_->isolate(); | 207 v8::Isolate* isolate = instance_->isolate(); |
207 | 208 |
208 v8::HandleScope handle_scope(isolate); | 209 v8::HandleScope handle_scope(isolate); |
209 v8::Local<v8::Context> context = | 210 v8::Local<v8::Context> context = |
210 v8::Local<v8::Context>::New(isolate, context_); | 211 v8::Local<v8::Context>::New(isolate, context_); |
211 | 212 |
| 213 APIEventHandler event_handler( |
| 214 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
212 v8::Local<v8::Object> binding_object = | 215 v8::Local<v8::Object> binding_object = |
213 binding.CreateInstance(context, isolate); | 216 binding.CreateInstance(context, isolate, &event_handler); |
214 | 217 |
215 ExpectPass(binding_object, "obj.oneString('foo');", "['foo']"); | 218 ExpectPass(binding_object, "obj.oneString('foo');", "['foo']"); |
216 ExpectPass(binding_object, "obj.oneString('');", "['']"); | 219 ExpectPass(binding_object, "obj.oneString('');", "['']"); |
217 ExpectFailure(binding_object, "obj.oneString(1);", kError); | 220 ExpectFailure(binding_object, "obj.oneString(1);", kError); |
218 ExpectFailure(binding_object, "obj.oneString();", kError); | 221 ExpectFailure(binding_object, "obj.oneString();", kError); |
219 ExpectFailure(binding_object, "obj.oneString({});", kError); | 222 ExpectFailure(binding_object, "obj.oneString({});", kError); |
220 ExpectFailure(binding_object, "obj.oneString('foo', 'bar');", kError); | 223 ExpectFailure(binding_object, "obj.oneString('foo', 'bar');", kError); |
221 | 224 |
222 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); | 225 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); |
223 ExpectPass(binding_object, "obj.stringAndInt('foo', -1);", "['foo',-1]"); | 226 ExpectPass(binding_object, "obj.stringAndInt('foo', -1);", "['foo',-1]"); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 " }]" | 296 " }]" |
294 "}]"; | 297 "}]"; |
295 | 298 |
296 std::unique_ptr<base::ListValue> functions = | 299 std::unique_ptr<base::ListValue> functions = |
297 ListValueFromString(kRefFunctions); | 300 ListValueFromString(kRefFunctions); |
298 ASSERT_TRUE(functions); | 301 ASSERT_TRUE(functions); |
299 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); | 302 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); |
300 ASSERT_TRUE(types); | 303 ASSERT_TRUE(types); |
301 ArgumentSpec::RefMap refs; | 304 ArgumentSpec::RefMap refs; |
302 APIBinding binding( | 305 APIBinding binding( |
303 "test", *functions, types.get(), | 306 "test", *functions, types.get(), nullptr, |
304 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), | 307 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), |
305 &refs); | 308 &refs); |
306 EXPECT_EQ(2u, refs.size()); | 309 EXPECT_EQ(2u, refs.size()); |
307 EXPECT_TRUE(base::ContainsKey(refs, "refObj")); | 310 EXPECT_TRUE(base::ContainsKey(refs, "refObj")); |
308 EXPECT_TRUE(base::ContainsKey(refs, "refEnum")); | 311 EXPECT_TRUE(base::ContainsKey(refs, "refEnum")); |
309 | 312 |
310 v8::Isolate* isolate = instance_->isolate(); | 313 v8::Isolate* isolate = instance_->isolate(); |
311 | 314 |
312 v8::HandleScope handle_scope(isolate); | 315 v8::HandleScope handle_scope(isolate); |
313 v8::Local<v8::Context> context = | 316 v8::Local<v8::Context> context = |
314 v8::Local<v8::Context>::New(isolate, context_); | 317 v8::Local<v8::Context>::New(isolate, context_); |
315 | 318 |
| 319 APIEventHandler event_handler( |
| 320 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
316 v8::Local<v8::Object> binding_object = | 321 v8::Local<v8::Object> binding_object = |
317 binding.CreateInstance(context, isolate); | 322 binding.CreateInstance(context, isolate, &event_handler); |
318 | 323 |
319 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})", | 324 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})", |
320 "[{'prop1':'foo'}]"); | 325 "[{'prop1':'foo'}]"); |
321 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", | 326 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", |
322 "[{'prop1':'foo','prop2':2}]"); | 327 "[{'prop1':'foo','prop2':2}]"); |
323 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", | 328 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", |
324 kError); | 329 kError); |
325 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); | 330 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); |
326 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); | 331 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); |
327 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); | 332 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); |
328 } | 333 } |
329 | 334 |
| 335 // Tests that events specified in the API are created as properties of the API |
| 336 // object. |
| 337 TEST_F(APIBindingTest, TestEventCreation) { |
| 338 const char kEvents[] = "[{'name': 'onFoo'}, {'name': 'onBar'}]"; |
| 339 std::unique_ptr<base::ListValue> events = ListValueFromString(kEvents); |
| 340 ASSERT_TRUE(events); |
| 341 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 342 ASSERT_TRUE(functions); |
| 343 ArgumentSpec::RefMap refs; |
| 344 APIBinding binding( |
| 345 "test", *functions, nullptr, events.get(), |
| 346 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), |
| 347 &refs); |
| 348 |
| 349 v8::Isolate* isolate = instance_->isolate(); |
| 350 |
| 351 v8::HandleScope handle_scope(isolate); |
| 352 v8::Local<v8::Context> context = |
| 353 v8::Local<v8::Context>::New(isolate, context_); |
| 354 |
| 355 APIEventHandler event_handler( |
| 356 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 357 v8::Local<v8::Object> binding_object = |
| 358 binding.CreateInstance(context, isolate, &event_handler); |
| 359 |
| 360 // Event behavior is tested in the APIEventHandler unittests as well as the |
| 361 // APIBindingsSystem tests, so we really only need to check that the events |
| 362 // are being initialized on the object. |
| 363 v8::Maybe<bool> has_on_foo = |
| 364 binding_object->Has(context, gin::StringToV8(isolate, "onFoo")); |
| 365 EXPECT_TRUE(has_on_foo.IsJust()); |
| 366 EXPECT_TRUE(has_on_foo.FromJust()); |
| 367 |
| 368 v8::Maybe<bool> has_on_bar = |
| 369 binding_object->Has(context, gin::StringToV8(isolate, "onBar")); |
| 370 EXPECT_TRUE(has_on_bar.IsJust()); |
| 371 EXPECT_TRUE(has_on_bar.FromJust()); |
| 372 |
| 373 v8::Maybe<bool> has_on_baz = |
| 374 binding_object->Has(context, gin::StringToV8(isolate, "onBaz")); |
| 375 EXPECT_TRUE(has_on_baz.IsJust()); |
| 376 EXPECT_FALSE(has_on_baz.FromJust()); |
| 377 } |
| 378 |
330 } // namespace extensions | 379 } // namespace extensions |
OLD | NEW |