rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gin/arguments.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "gin/converter.h" |
| 9 | #include "gin/object_template_builder.h" |
| 10 | #include "gin/public/isolate_holder.h" |
| 11 | #include "gin/test/v8_test.h" |
| 12 | #include "v8/include/v8.h" |
| 13 | |
| 14 | namespace gin { |
| 15 | |
| 16 | using ArgumentsTest = V8Test; |
| 17 | |
| 18 | // Test that Arguments::GetHolderCreationContext returns the proper context. |
| 19 | TEST_F(ArgumentsTest, TestArgumentsHolderCreationContext) { |
| 20 | v8::Isolate* isolate = instance_->isolate(); |
| 21 | v8::HandleScope handle_scope(isolate); |
| 22 | |
| 23 | v8::Local<v8::Context> creation_context = context_.Get(instance_->isolate()); |
| 24 | |
| 25 | auto check_creation_context = [](v8::Local<v8::Context> expected_context, |
| 26 | gin::Arguments* arguments) { |
| 27 | EXPECT_EQ(expected_context, arguments->GetHolderCreationContext()); |
| 28 | }; |
| 29 | |
| 30 | // Create an object that will compare GetHolderCreationContext() with |
| 31 | // |creation_context|. |
| 32 | v8::Local<v8::ObjectTemplate> object_template = |
| 33 | ObjectTemplateBuilder(isolate) |
| 34 | .SetMethod("checkCreationContext", |
| 35 | base::Bind(check_creation_context, creation_context)) |
| 36 | .Build(); |
| 37 | |
| 38 | v8::Local<v8::Object> object = |
| 39 | object_template->NewInstance(creation_context).ToLocalChecked(); |
| 40 | |
| 41 | // Call checkCreationContext() on the generated object using the passed-in |
| 42 | // context as the current context. |
| 43 | auto test_context = [object, isolate](v8::Local<v8::Context> context) { |
| 44 | v8::Context::Scope context_scope(context); |
| 45 | const char kCallFunction[] = "(function(o) { o.checkCreationContext(); })"; |
| 46 | v8::Local<v8::Script> script = |
| 47 | v8::Script::Compile(context, StringToV8(isolate, kCallFunction)) |
| 48 | .ToLocalChecked(); |
| 49 | v8::Local<v8::Function> function; |
| 50 | ASSERT_TRUE(ConvertFromV8(isolate, script->Run(), &function)); |
| 51 | v8::Local<v8::Value> args[] = {object}; |
| 52 | function->Call(v8::Undefined(isolate), arraysize(args), args); |
| 53 | }; |
| 54 | |
| 55 | // Test calling in the creation context. |
| 56 | test_context(creation_context); |
| 57 | |
| 58 | { |
| 59 | // Create a second context, and test calling in that. The creation context |
| 60 | // should be the same (even though the current context has changed). |
| 61 | v8::Local<v8::Context> second_context = |
| 62 | v8::Context::New(isolate, nullptr, v8::Local<v8::ObjectTemplate>()); |
| 63 | test_context(second_context); |
| 64 | } |
| 65 | } |
| 66 | |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 67 | TEST_F(ArgumentsTest, TestGetAll) { |
| 68 | v8::Isolate* isolate = instance_->isolate(); |
| 69 | v8::HandleScope handle_scope(isolate); |
| 70 | v8::Local<v8::Context> context = context_.Get(instance_->isolate()); |
| 71 | |
| 72 | using V8List = std::vector<v8::Local<v8::Value>>; |
| 73 | |
| 74 | V8List list1 = { |
| 75 | gin::ConvertToV8(isolate, 1), gin::StringToV8(isolate, "some string"), |
| 76 | gin::ConvertToV8(context, std::vector<double>({2.0, 3.0})) |
| 77 | .ToLocalChecked(), |
| 78 | }; |
| 79 | bool called1 = false; |
| 80 | |
| 81 | V8List list2 = { |
| 82 | gin::StringToV8(isolate, "some other string"), |
| 83 | gin::ConvertToV8(isolate, 42), |
| 84 | }; |
| 85 | bool called2 = false; |
| 86 | |
| 87 | V8List list3; // Empty list. |
| 88 | bool called3 = false; |
| 89 | |
| 90 | auto check_arguments = [](V8List* expected, bool* called, |
| 91 | gin::Arguments* arguments) { |
| 92 | *called = true; |
| 93 | V8List actual = arguments->GetAll(); |
| 94 | ASSERT_EQ(expected->size(), actual.size()); |
| 95 | for (size_t i = 0; i < expected->size(); ++i) |
| 96 | EXPECT_EQ(expected->at(i), actual[i]) << i; |
| 97 | }; |
| 98 | |
| 99 | // Create an object that will compare GetHolderCreationContext() with |
| 100 | // |creation_context|. |
| 101 | v8::Local<v8::ObjectTemplate> object_template = |
| 102 | ObjectTemplateBuilder(isolate) |
| 103 | .SetMethod("check1", base::Bind(check_arguments, &list1, &called1)) |
| 104 | .SetMethod("check2", base::Bind(check_arguments, &list2, &called2)) |
| 105 | .SetMethod("check3", base::Bind(check_arguments, &list3, &called3)) |
| 106 | .Build(); |
| 107 | |
| 108 | v8::Local<v8::Object> object = |
| 109 | object_template->NewInstance(context).ToLocalChecked(); |
| 110 | |
| 111 | auto do_check = [object, context](V8List& args, base::StringPiece key) { |
| 112 | v8::Local<v8::Value> val; |
| 113 | ASSERT_TRUE( |
| 114 | object->Get(context, gin::StringToSymbol(context->GetIsolate(), key)) |
| 115 | .ToLocal(&val)); |
| 116 | ASSERT_TRUE(val->IsFunction()); |
| 117 | val.As<v8::Function>() |
| 118 | ->Call(context, object, static_cast<int>(args.size()), args.data()) |
| 119 | .ToLocalChecked(); |
| 120 | }; |
| 121 | |
| 122 | do_check(list1, "check1"); |
| 123 | EXPECT_TRUE(called1); |
| 124 | do_check(list2, "check2"); |
| 125 | EXPECT_TRUE(called2); |
| 126 | do_check(list3, "check3"); |
| 127 | EXPECT_TRUE(called3); |
| 128 | } |
| 129 | |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 130 | } // namespace gin |