blob: d975264d1d730746398a381e37ed840375dac213 [file] [log] [blame]
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07001// Copyright 2015 PDFium 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
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07005#include "testing/gtest/include/gtest/gtest.h"
Dan Sinclair61046b92016-02-18 14:48:48 -05006#include "testing/js_embedder_test.h"
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07007
8namespace {
9
Tom Sepez9967cc52016-03-24 11:45:37 -070010const double kExpected0 = 6.0;
11const double kExpected1 = 7.0;
12const double kExpected2 = 8.0;
13
14const wchar_t kScript0[] = L"fred = 6";
15const wchar_t kScript1[] = L"fred = 7";
16const wchar_t kScript2[] = L"fred = 8";
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070017
18} // namespace
19
Tom Sepez9967cc52016-03-24 11:45:37 -070020class FXJSV8EmbedderTest : public JSEmbedderTest {
21 public:
Ryan Harrison275e2602017-09-18 14:23:18 -040022 void ExecuteInCurrentContext(const WideString& script) {
Tom Sepez9967cc52016-03-24 11:45:37 -070023 FXJSErr error;
tsepezb4694242016-08-15 16:44:55 -070024 int sts = engine()->Execute(script, &error);
Tom Sepez9967cc52016-03-24 11:45:37 -070025 EXPECT_EQ(0, sts);
26 }
27 void CheckAssignmentInCurrentContext(double expected) {
tsepezb4694242016-08-15 16:44:55 -070028 v8::Local<v8::Object> This = engine()->GetThisObj();
29 v8::Local<v8::Value> fred = engine()->GetObjectProperty(This, L"fred");
Tom Sepez9967cc52016-03-24 11:45:37 -070030 EXPECT_TRUE(fred->IsNumber());
tsepeze6cf0132017-01-18 14:38:18 -080031 EXPECT_EQ(expected, engine()->ToDouble(fred));
Tom Sepez9967cc52016-03-24 11:45:37 -070032 }
33};
Tom Sepeza72e8e22015-10-07 10:17:53 -070034
Lei Zhang1ac47eb2015-12-21 11:04:44 -080035TEST_F(FXJSV8EmbedderTest, Getters) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070036 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070037 v8::HandleScope handle_scope(isolate());
38 v8::Context::Scope context_scope(GetV8Context());
39
Ryan Harrison275e2602017-09-18 14:23:18 -040040 ExecuteInCurrentContext(WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070041 CheckAssignmentInCurrentContext(kExpected1);
42}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070043
tsepeza4941912016-08-15 11:40:12 -070044TEST_F(FXJSV8EmbedderTest, MultipleEngines) {
Tom Sepez9967cc52016-03-24 11:45:37 -070045 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez9967cc52016-03-24 11:45:37 -070046 v8::HandleScope handle_scope(isolate());
47
weili0b2a9872016-09-21 11:50:43 -070048 CFXJS_Engine engine1(isolate());
tsepezb4694242016-08-15 16:44:55 -070049 engine1.InitializeEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070050
weili0b2a9872016-09-21 11:50:43 -070051 CFXJS_Engine engine2(isolate());
tsepezb4694242016-08-15 16:44:55 -070052 engine2.InitializeEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070053
54 v8::Context::Scope context_scope(GetV8Context());
Ryan Harrison275e2602017-09-18 14:23:18 -040055 ExecuteInCurrentContext(WideString(kScript0));
Tom Sepez9967cc52016-03-24 11:45:37 -070056 CheckAssignmentInCurrentContext(kExpected0);
57
58 {
tsepezb4694242016-08-15 16:44:55 -070059 v8::Local<v8::Context> context1 = engine1.NewLocalContext();
weilidb444d22016-06-02 15:48:15 -070060 v8::Context::Scope context_scope1(context1);
Ryan Harrison275e2602017-09-18 14:23:18 -040061 ExecuteInCurrentContext(WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070062 CheckAssignmentInCurrentContext(kExpected1);
63 }
tsepezb4694242016-08-15 16:44:55 -070064
65 engine1.ReleaseEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070066
67 {
tsepezb4694242016-08-15 16:44:55 -070068 v8::Local<v8::Context> context2 = engine2.NewLocalContext();
weilidb444d22016-06-02 15:48:15 -070069 v8::Context::Scope context_scope2(context2);
Ryan Harrison275e2602017-09-18 14:23:18 -040070 ExecuteInCurrentContext(WideString(kScript2));
Tom Sepez9967cc52016-03-24 11:45:37 -070071 CheckAssignmentInCurrentContext(kExpected2);
72 }
Tom Sepez9967cc52016-03-24 11:45:37 -070073
tsepezb4694242016-08-15 16:44:55 -070074 engine2.ReleaseEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070075 CheckAssignmentInCurrentContext(kExpected0);
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070076}
tsepeze6cf0132017-01-18 14:38:18 -080077
78TEST_F(FXJSV8EmbedderTest, EmptyLocal) {
79 v8::Isolate::Scope isolate_scope(isolate());
80 v8::HandleScope handle_scope(isolate());
81 v8::Context::Scope context_scope(GetV8Context());
82
83 v8::Local<v8::Value> empty;
84 EXPECT_FALSE(engine()->ToBoolean(empty));
85 EXPECT_EQ(0, engine()->ToInt32(empty));
86 EXPECT_EQ(0.0, engine()->ToDouble(empty));
87 EXPECT_EQ(L"", engine()->ToWideString(empty));
88 EXPECT_TRUE(engine()->ToObject(empty).IsEmpty());
89 EXPECT_TRUE(engine()->ToArray(empty).IsEmpty());
90}
91
92TEST_F(FXJSV8EmbedderTest, NewNull) {
93 v8::Isolate::Scope isolate_scope(isolate());
94 v8::HandleScope handle_scope(isolate());
95 v8::Context::Scope context_scope(GetV8Context());
96
97 auto nullz = engine()->NewNull();
98 EXPECT_FALSE(engine()->ToBoolean(nullz));
99 EXPECT_EQ(0, engine()->ToInt32(nullz));
100 EXPECT_EQ(0.0, engine()->ToDouble(nullz));
dan sinclair5daf07a2017-10-24 21:46:57 -0400101 EXPECT_EQ(L"null", engine()->ToWideString(nullz));
tsepeze6cf0132017-01-18 14:38:18 -0800102 EXPECT_TRUE(engine()->ToObject(nullz).IsEmpty());
103 EXPECT_TRUE(engine()->ToArray(nullz).IsEmpty());
104}
105
dan sinclair5daf07a2017-10-24 21:46:57 -0400106TEST_F(FXJSV8EmbedderTest, NewUndefined) {
107 v8::Isolate::Scope isolate_scope(isolate());
108 v8::HandleScope handle_scope(isolate());
109 v8::Context::Scope context_scope(GetV8Context());
110
111 auto undef = engine()->NewUndefined();
112 EXPECT_FALSE(engine()->ToBoolean(undef));
113 EXPECT_EQ(0, engine()->ToInt32(undef));
114 EXPECT_TRUE(std::isnan(engine()->ToDouble(undef)));
115 EXPECT_EQ(L"undefined", engine()->ToWideString(undef));
116 EXPECT_TRUE(engine()->ToObject(undef).IsEmpty());
117 EXPECT_TRUE(engine()->ToArray(undef).IsEmpty());
118}
119
tsepeze6cf0132017-01-18 14:38:18 -0800120TEST_F(FXJSV8EmbedderTest, NewBoolean) {
121 v8::Isolate::Scope isolate_scope(isolate());
122 v8::HandleScope handle_scope(isolate());
123 v8::Context::Scope context_scope(GetV8Context());
124
125 auto boolz = engine()->NewBoolean(true);
126 EXPECT_TRUE(engine()->ToBoolean(boolz));
127 EXPECT_EQ(1, engine()->ToInt32(boolz));
128 EXPECT_EQ(1.0, engine()->ToDouble(boolz));
129 EXPECT_EQ(L"true", engine()->ToWideString(boolz));
130 EXPECT_TRUE(engine()->ToObject(boolz).IsEmpty());
131 EXPECT_TRUE(engine()->ToArray(boolz).IsEmpty());
132}
133
134TEST_F(FXJSV8EmbedderTest, NewNumber) {
135 v8::Isolate::Scope isolate_scope(isolate());
136 v8::HandleScope handle_scope(isolate());
137 v8::Context::Scope context_scope(GetV8Context());
138
139 auto num = engine()->NewNumber(42.1);
140 EXPECT_TRUE(engine()->ToBoolean(num));
141 EXPECT_EQ(42, engine()->ToInt32(num));
142 EXPECT_EQ(42.1, engine()->ToDouble(num));
143 EXPECT_EQ(L"42.1", engine()->ToWideString(num));
144 EXPECT_TRUE(engine()->ToObject(num).IsEmpty());
145 EXPECT_TRUE(engine()->ToArray(num).IsEmpty());
146}
147
148TEST_F(FXJSV8EmbedderTest, NewString) {
149 v8::Isolate::Scope isolate_scope(isolate());
150 v8::HandleScope handle_scope(isolate());
151 v8::Context::Scope context_scope(GetV8Context());
152
153 auto str = engine()->NewString(L"123");
154 EXPECT_TRUE(engine()->ToBoolean(str));
155 EXPECT_EQ(123, engine()->ToInt32(str));
156 EXPECT_EQ(123, engine()->ToDouble(str));
157 EXPECT_EQ(L"123", engine()->ToWideString(str));
158 EXPECT_TRUE(engine()->ToObject(str).IsEmpty());
159 EXPECT_TRUE(engine()->ToArray(str).IsEmpty());
160}
161
162TEST_F(FXJSV8EmbedderTest, NewDate) {
163 v8::Isolate::Scope isolate_scope(isolate());
164 v8::HandleScope handle_scope(isolate());
165 v8::Context::Scope context_scope(GetV8Context());
166
167 auto date = engine()->NewDate(1111111111);
168 EXPECT_TRUE(engine()->ToBoolean(date));
169 EXPECT_EQ(1111111111, engine()->ToInt32(date));
170 EXPECT_EQ(1111111111.0, engine()->ToDouble(date));
171 EXPECT_NE(L"", engine()->ToWideString(date)); // exact format varies.
172 EXPECT_TRUE(engine()->ToObject(date)->IsObject());
173 EXPECT_TRUE(engine()->ToArray(date).IsEmpty());
174}
175
176TEST_F(FXJSV8EmbedderTest, NewArray) {
177 v8::Isolate::Scope isolate_scope(isolate());
178 v8::HandleScope handle_scope(isolate());
179 v8::Context::Scope context_scope(GetV8Context());
180
181 auto array = engine()->NewArray();
182 EXPECT_EQ(0u, engine()->GetArrayLength(array));
183 EXPECT_FALSE(engine()->GetArrayElement(array, 2).IsEmpty());
184 EXPECT_TRUE(engine()->GetArrayElement(array, 2)->IsUndefined());
185 EXPECT_EQ(0u, engine()->GetArrayLength(array));
186
187 engine()->PutArrayElement(array, 3, engine()->NewNumber(12));
188 EXPECT_FALSE(engine()->GetArrayElement(array, 2).IsEmpty());
189 EXPECT_TRUE(engine()->GetArrayElement(array, 2)->IsUndefined());
190 EXPECT_FALSE(engine()->GetArrayElement(array, 3).IsEmpty());
191 EXPECT_TRUE(engine()->GetArrayElement(array, 3)->IsNumber());
192 EXPECT_EQ(4u, engine()->GetArrayLength(array));
193
194 EXPECT_TRUE(engine()->ToBoolean(array));
195 EXPECT_EQ(0, engine()->ToInt32(array));
196 double d = engine()->ToDouble(array);
197 EXPECT_NE(d, d); // i.e. NaN.
198 EXPECT_EQ(L",,,12", engine()->ToWideString(array));
199 EXPECT_TRUE(engine()->ToObject(array)->IsObject());
200 EXPECT_TRUE(engine()->ToArray(array)->IsArray());
201}
202
203TEST_F(FXJSV8EmbedderTest, NewObject) {
204 v8::Isolate::Scope isolate_scope(isolate());
205 v8::HandleScope handle_scope(isolate());
206 v8::Context::Scope context_scope(GetV8Context());
207
208 auto object = engine()->NewFxDynamicObj(-1);
Tom Sepez63b2fc72017-08-14 16:24:29 -0700209 ASSERT_FALSE(object.IsEmpty());
tsepeze6cf0132017-01-18 14:38:18 -0800210 EXPECT_EQ(0u, engine()->GetObjectPropertyNames(object).size());
211 EXPECT_FALSE(engine()->GetObjectProperty(object, L"clams").IsEmpty());
212 EXPECT_TRUE(engine()->GetObjectProperty(object, L"clams")->IsUndefined());
213 EXPECT_EQ(0u, engine()->GetObjectPropertyNames(object).size());
214
215 engine()->PutObjectProperty(object, L"clams", engine()->NewNumber(12));
216 EXPECT_FALSE(engine()->GetObjectProperty(object, L"clams").IsEmpty());
217 EXPECT_TRUE(engine()->GetObjectProperty(object, L"clams")->IsNumber());
218 EXPECT_EQ(1u, engine()->GetObjectPropertyNames(object).size());
219 EXPECT_EQ(L"clams", engine()->GetObjectPropertyNames(object)[0]);
220
221 EXPECT_TRUE(engine()->ToBoolean(object));
222 EXPECT_EQ(0, engine()->ToInt32(object));
223 double d = engine()->ToDouble(object);
224 EXPECT_NE(d, d); // i.e. NaN.
225 EXPECT_EQ(L"[object Object]", engine()->ToWideString(object));
226 EXPECT_TRUE(engine()->ToObject(object)->IsObject());
227 EXPECT_TRUE(engine()->ToArray(object).IsEmpty());
228}