blob: a9422ece987fe129d07fba5eefcdadaf7d513152 [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 Sepezb7c7df62018-02-09 19:08:59 +00005#include "fxjs/cfxjs_engine.h"
6
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07007#include "testing/gtest/include/gtest/gtest.h"
Dan Sinclair61046b92016-02-18 14:48:48 -05008#include "testing/js_embedder_test.h"
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07009
10namespace {
11
Tom Sepez9967cc52016-03-24 11:45:37 -070012const double kExpected0 = 6.0;
13const double kExpected1 = 7.0;
14const double kExpected2 = 8.0;
15
16const wchar_t kScript0[] = L"fred = 6";
17const wchar_t kScript1[] = L"fred = 7";
18const wchar_t kScript2[] = L"fred = 8";
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070019
20} // namespace
21
Tom Sepez10569fc2018-06-07 00:42:46 +000022using CFXJSEngineEmbedderTest = JSEmbedderTest;
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000023
Tom Sepez10569fc2018-06-07 00:42:46 +000024void CheckAssignmentInEngineContext(CFXJS_Engine* current_engine,
25 double expected) {
26 v8::Context::Scope context_scope(current_engine->GetV8Context());
27 v8::Local<v8::Object> This = current_engine->GetThisObj();
28 v8::Local<v8::Value> fred = current_engine->GetObjectProperty(This, L"fred");
29 EXPECT_TRUE(fred->IsNumber());
30 EXPECT_EQ(expected, current_engine->ToDouble(fred));
31}
Tom Sepeza72e8e22015-10-07 10:17:53 -070032
Tom Sepezb7c7df62018-02-09 19:08:59 +000033TEST_F(CFXJSEngineEmbedderTest, Getters) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070034 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070035 v8::HandleScope handle_scope(isolate());
36 v8::Context::Scope context_scope(GetV8Context());
37
Tom Sepez10569fc2018-06-07 00:42:46 +000038 Optional<IJS_Runtime::JS_Error> err = engine()->Execute(WideString(kScript1));
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000039 EXPECT_FALSE(err);
Tom Sepez10569fc2018-06-07 00:42:46 +000040 CheckAssignmentInEngineContext(engine(), kExpected1);
Tom Sepez9967cc52016-03-24 11:45:37 -070041}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070042
Tom Sepezb7c7df62018-02-09 19:08:59 +000043TEST_F(CFXJSEngineEmbedderTest, MultipleEngines) {
Tom Sepez9967cc52016-03-24 11:45:37 -070044 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez9967cc52016-03-24 11:45:37 -070045 v8::HandleScope handle_scope(isolate());
46
weili0b2a9872016-09-21 11:50:43 -070047 CFXJS_Engine engine1(isolate());
tsepezb4694242016-08-15 16:44:55 -070048 engine1.InitializeEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070049
weili0b2a9872016-09-21 11:50:43 -070050 CFXJS_Engine engine2(isolate());
tsepezb4694242016-08-15 16:44:55 -070051 engine2.InitializeEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070052
53 v8::Context::Scope context_scope(GetV8Context());
Ryan Harrison203339a2018-08-23 20:58:14 +000054 {
55 Optional<IJS_Runtime::JS_Error> err =
56 engine()->Execute(WideString(kScript0));
57 EXPECT_FALSE(err);
58 CheckAssignmentInEngineContext(engine(), kExpected0);
59 }
Tom Sepez9967cc52016-03-24 11:45:37 -070060 {
Tom Sepez10569fc2018-06-07 00:42:46 +000061 // engine1 executing in engine1's context doesn't affect main.
62 v8::Context::Scope context_scope1(engine1.GetV8Context());
63 Optional<IJS_Runtime::JS_Error> err = engine1.Execute(WideString(kScript1));
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000064 EXPECT_FALSE(err);
Tom Sepez10569fc2018-06-07 00:42:46 +000065 CheckAssignmentInEngineContext(engine(), kExpected0);
66 CheckAssignmentInEngineContext(&engine1, kExpected1);
Tom Sepez9967cc52016-03-24 11:45:37 -070067 }
Tom Sepez9967cc52016-03-24 11:45:37 -070068 {
Tom Sepez10569fc2018-06-07 00:42:46 +000069 // engine1 executing in engine2's context doesn't affect engine1.
70 v8::Context::Scope context_scope2(engine2.GetV8Context());
71 Optional<IJS_Runtime::JS_Error> err = engine1.Execute(WideString(kScript2));
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000072 EXPECT_FALSE(err);
Tom Sepez10569fc2018-06-07 00:42:46 +000073 CheckAssignmentInEngineContext(engine(), kExpected0);
74 CheckAssignmentInEngineContext(&engine1, kExpected1);
75 CheckAssignmentInEngineContext(&engine2, kExpected2);
Tom Sepez9967cc52016-03-24 11:45:37 -070076 }
Tom Sepez33c7ade2018-02-01 02:11:44 +000077 engine1.ReleaseEngine();
78 engine2.ReleaseEngine();
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070079}
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000080
81TEST_F(CFXJSEngineEmbedderTest, JSCompileError) {
82 v8::Isolate::Scope isolate_scope(isolate());
83 v8::HandleScope handle_scope(isolate());
84 v8::Context::Scope context_scope(GetV8Context());
85
86 Optional<IJS_Runtime::JS_Error> err =
Tom Sepez10569fc2018-06-07 00:42:46 +000087 engine()->Execute(L"functoon(x) { return x+1; }");
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000088 EXPECT_TRUE(err);
89 EXPECT_EQ(L"SyntaxError: Unexpected token {", err->exception);
90 EXPECT_EQ(1, err->line);
91 EXPECT_EQ(12, err->column);
92}
93
94TEST_F(CFXJSEngineEmbedderTest, JSRuntimeError) {
95 v8::Isolate::Scope isolate_scope(isolate());
96 v8::HandleScope handle_scope(isolate());
97 v8::Context::Scope context_scope(GetV8Context());
98
99 Optional<IJS_Runtime::JS_Error> err =
Tom Sepez10569fc2018-06-07 00:42:46 +0000100 engine()->Execute(L"let a = 3;\nundefined.colour");
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000101 EXPECT_TRUE(err);
102 EXPECT_EQ(L"TypeError: Cannot read property 'colour' of undefined",
103 err->exception);
104 EXPECT_EQ(2, err->line);
105 EXPECT_EQ(10, err->column);
106}