Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /gin /arguments_unittest.cc
blob: a68e257d90f4e79b76f0939cc013ff3c5f54b984 [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:01[diff] [blame]1// Copyright 2017 The Chromium Authors
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]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
Md Hasibul Hasanfcbd62e32024-04-10 10:38:39[diff] [blame]7#include<string_view>
8
Avi Drissman93a273dd2023-01-11 00:38:27[diff] [blame]9#include"base/functional/bind.h"
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]10#include"gin/converter.h"
11#include"gin/object_template_builder.h"
12#include"gin/public/isolate_holder.h"
13#include"gin/test/v8_test.h"
Dan Elphick05acd602021-08-30 15:22:07[diff] [blame]14#include"v8/include/v8-context.h"
15#include"v8/include/v8-forward.h"
16#include"v8/include/v8-function.h"
17#include"v8/include/v8-object.h"
18#include"v8/include/v8-primitive.h"
19#include"v8/include/v8-script.h"
20#include"v8/include/v8-template.h"
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]21
22namespace gin{
23
24usingArgumentsTest= V8Test;
25
26// Test that Arguments::GetHolderCreationContext returns the proper context.
27TEST_F(ArgumentsTest,TestArgumentsHolderCreationContext){
28 v8::Isolate* isolate= instance_->isolate();
29 v8::HandleScope handle_scope(isolate);
30
31 v8::Local<v8::Context> creation_context= context_.Get(instance_->isolate());
32
33auto check_creation_context=[](v8::Local<v8::Context> expected_context,
34 gin::Arguments* arguments){
35 EXPECT_EQ(expected_context, arguments->GetHolderCreationContext());
36};
37
38// Create an object that will compare GetHolderCreationContext() with
39// |creation_context|.
40 v8::Local<v8::ObjectTemplate> object_template=
41ObjectTemplateBuilder(isolate)
tzik6934a312018-03-08 01:03:16[diff] [blame]42.SetMethod(
43"checkCreationContext",
44 base::BindRepeating(check_creation_context, creation_context))
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]45.Build();
46
47 v8::Local<v8::Object> object=
48 object_template->NewInstance(creation_context).ToLocalChecked();
49
50// Call checkCreationContext() on the generated object using the passed-in
51// context as the current context.
52auto test_context=[object, isolate](v8::Local<v8::Context> context){
53 v8::Context::Scope context_scope(context);
54constchar kCallFunction[]="(function(o) { o.checkCreationContext(); })";
55 v8::Local<v8::Script> script=
56 v8::Script::Compile(context,StringToV8(isolate, kCallFunction))
57.ToLocalChecked();
58 v8::Local<v8::Function> function;
Adam Klein8a35b5b2018-01-17 00:51:00[diff] [blame]59 ASSERT_TRUE(ConvertFromV8(isolate, script->Run(context).ToLocalChecked(),
60&function));
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]61 v8::Local<v8::Value> args[]={object};
Daniel Chengef5f136902022-02-27 01:05:25[diff] [blame]62 function->Call(context, v8::Undefined(isolate), std::size(args), args)
Ross McIlroyd298cced2018-11-23 16:14:13[diff] [blame]63.ToLocalChecked();
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]64};
65
66// Test calling in the creation context.
67 test_context(creation_context);
68
69{
70// Create a second context, and test calling in that. The creation context
71// should be the same (even though the current context has changed).
72 v8::Local<v8::Context> second_context=
73 v8::Context::New(isolate,nullptr, v8::Local<v8::ObjectTemplate>());
74 test_context(second_context);
75}
76}
77
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]78TEST_F(ArgumentsTest,TestGetAll){
79 v8::Isolate* isolate= instance_->isolate();
80 v8::HandleScope handle_scope(isolate);
81 v8::Local<v8::Context> context= context_.Get(instance_->isolate());
82
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:00[diff] [blame]83using V8List= v8::LocalVector<v8::Value>;
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]84
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:00[diff] [blame]85 V8List list1(isolate,
86{
87 gin::ConvertToV8(isolate,1),
88 gin::StringToV8(isolate,"some string"),
89 gin::ConvertToV8(isolate, std::vector<double>({2.0,3.0})),
90});
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]91bool called1=false;
92
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:00[diff] [blame]93 V8List list2(isolate,{
94 gin::StringToV8(isolate,"some other string"),
95 gin::ConvertToV8(isolate,42),
96});
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]97bool called2=false;
98
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:00[diff] [blame]99 V8List list3(isolate);// Empty list.
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]100bool called3=false;
101
102auto check_arguments=[](V8List* expected,bool* called,
103 gin::Arguments* arguments){
104*called=true;
105 V8List actual= arguments->GetAll();
106 ASSERT_EQ(expected->size(), actual.size());
107for(size_t i=0; i< expected->size();++i)
108 EXPECT_EQ(expected->at(i), actual[i])<< i;
109};
110
111// Create an object that will compare GetHolderCreationContext() with
112// |creation_context|.
113 v8::Local<v8::ObjectTemplate> object_template=
114ObjectTemplateBuilder(isolate)
tzik6934a312018-03-08 01:03:16[diff] [blame]115.SetMethod("check1",
116 base::BindRepeating(check_arguments,&list1,&called1))
117.SetMethod("check2",
118 base::BindRepeating(check_arguments,&list2,&called2))
119.SetMethod("check3",
120 base::BindRepeating(check_arguments,&list3,&called3))
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]121.Build();
122
123 v8::Local<v8::Object> object=
124 object_template->NewInstance(context).ToLocalChecked();
125
Clemens Backese3dfd502025-07-11 13:55:25[diff] [blame]126auto do_check=[object, context, isolate](V8List& args,
127 std::string_view key){
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]128 v8::Local<v8::Value> val;
129 ASSERT_TRUE(
Clemens Backese3dfd502025-07-11 13:55:25[diff] [blame]130 object->Get(context, gin::StringToSymbol(isolate, key)).ToLocal(&val));
rdevlin.cronincd6754502017-04-19 16:14:14[diff] [blame]131 ASSERT_TRUE(val->IsFunction());
132 val.As<v8::Function>()
133->Call(context, object,static_cast<int>(args.size()), args.data())
134.ToLocalChecked();
135};
136
137 do_check(list1,"check1");
138 EXPECT_TRUE(called1);
139 do_check(list2,"check2");
140 EXPECT_TRUE(called2);
141 do_check(list3,"check3");
142 EXPECT_TRUE(called3);
143}
144
rdevlin.cronind982fdf2017-03-23 22:17:43[diff] [blame]145}// namespace gin

[8]ページ先頭

©2009-2025 Movatter.jp