OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "gin/arguments.h" | 5 #include "gin/arguments.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "gin/converter.h" | 8 #include "gin/converter.h" |
9 #include "gin/object_template_builder.h" | 9 #include "gin/object_template_builder.h" |
10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 { | 58 { |
59 // Create a second context, and test calling in that. The creation context | 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). | 60 // should be the same (even though the current context has changed). |
61 v8::Local<v8::Context> second_context = | 61 v8::Local<v8::Context> second_context = |
62 v8::Context::New(isolate, nullptr, v8::Local<v8::ObjectTemplate>()); | 62 v8::Context::New(isolate, nullptr, v8::Local<v8::ObjectTemplate>()); |
63 test_context(second_context); | 63 test_context(second_context); |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
| 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, 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 |
67 } // namespace gin | 130 } // namespace gin |
OLD | NEW |