[M108 Migration] Encrypt password field in AutoFill DB
[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/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 = std::vector<v8::Local<v8::Value>>;
82
83   V8List list1 = {
84       gin::ConvertToV8(isolate, 1), gin::StringToV8(isolate, "some string"),
85       gin::ConvertToV8(isolate, std::vector<double>({2.0, 3.0})),
86   };
87   bool called1 = false;
88
89   V8List list2 = {
90       gin::StringToV8(isolate, "some other string"),
91       gin::ConvertToV8(isolate, 42),
92   };
93   bool called2 = false;
94
95   V8List list3;  // Empty list.
96   bool called3 = false;
97
98   auto check_arguments = [](V8List* expected, bool* called,
99                             gin::Arguments* arguments) {
100     *called = true;
101     V8List actual = arguments->GetAll();
102     ASSERT_EQ(expected->size(), actual.size());
103     for (size_t i = 0; i < expected->size(); ++i)
104       EXPECT_EQ(expected->at(i), actual[i]) << i;
105   };
106
107   // Create an object that will compare GetHolderCreationContext() with
108   // |creation_context|.
109   v8::Local<v8::ObjectTemplate> object_template =
110       ObjectTemplateBuilder(isolate)
111           .SetMethod("check1",
112                      base::BindRepeating(check_arguments, &list1, &called1))
113           .SetMethod("check2",
114                      base::BindRepeating(check_arguments, &list2, &called2))
115           .SetMethod("check3",
116                      base::BindRepeating(check_arguments, &list3, &called3))
117           .Build();
118
119   v8::Local<v8::Object> object =
120       object_template->NewInstance(context).ToLocalChecked();
121
122   auto do_check = [object, context](V8List& args, base::StringPiece key) {
123     v8::Local<v8::Value> val;
124     ASSERT_TRUE(
125         object->Get(context, gin::StringToSymbol(context->GetIsolate(), key))
126             .ToLocal(&val));
127     ASSERT_TRUE(val->IsFunction());
128     val.As<v8::Function>()
129         ->Call(context, object, static_cast<int>(args.size()), args.data())
130         .ToLocalChecked();
131   };
132
133   do_check(list1, "check1");
134   EXPECT_TRUE(called1);
135   do_check(list2, "check2");
136   EXPECT_TRUE(called2);
137   do_check(list3, "check3");
138   EXPECT_TRUE(called3);
139 }
140
141 }  // namespace gin