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