blob: b573939b14640dadeeba439b41ccd2e592624778 [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:
dsinclair884b4f92016-06-06 09:49:32 -070022 void ExecuteInCurrentContext(const CFX_WideString& script) {
Tom Sepez9967cc52016-03-24 11:45:37 -070023 FXJSErr error;
dsinclair884b4f92016-06-06 09:49:32 -070024 int sts = FXJS_Execute(isolate(), script, &error);
Tom Sepez9967cc52016-03-24 11:45:37 -070025 EXPECT_EQ(0, sts);
26 }
27 void CheckAssignmentInCurrentContext(double expected) {
28 v8::Local<v8::Object> This = FXJS_GetThisObj(isolate());
tsepezd0b6ed12016-08-11 19:50:57 -070029 v8::Local<v8::Value> fred =
30 FXJS_GetObjectProperty(isolate(), This, L"fred");
Tom Sepez9967cc52016-03-24 11:45:37 -070031 EXPECT_TRUE(fred->IsNumber());
32 EXPECT_EQ(expected, fred->ToNumber(isolate()->GetCurrentContext())
33 .ToLocalChecked()
34 ->Value());
35 }
36};
Tom Sepeza72e8e22015-10-07 10:17:53 -070037
Lei Zhang1ac47eb2015-12-21 11:04:44 -080038TEST_F(FXJSV8EmbedderTest, Getters) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070039 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070040 v8::HandleScope handle_scope(isolate());
41 v8::Context::Scope context_scope(GetV8Context());
42
dsinclair884b4f92016-06-06 09:49:32 -070043 ExecuteInCurrentContext(CFX_WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070044 CheckAssignmentInCurrentContext(kExpected1);
45}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070046
tsepeza4941912016-08-15 11:40:12 -070047TEST_F(FXJSV8EmbedderTest, MultipleEngines) {
Tom Sepez9967cc52016-03-24 11:45:37 -070048 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez9967cc52016-03-24 11:45:37 -070049 v8::HandleScope handle_scope(isolate());
50
51 v8::Global<v8::Context> global_context1;
52 std::vector<v8::Global<v8::Object>*> static_objects1;
tsepeza4941912016-08-15 11:40:12 -070053 FXJS_InitializeEngine(isolate(), nullptr, &global_context1, &static_objects1);
Tom Sepez9967cc52016-03-24 11:45:37 -070054
55 v8::Global<v8::Context> global_context2;
56 std::vector<v8::Global<v8::Object>*> static_objects2;
tsepeza4941912016-08-15 11:40:12 -070057 FXJS_InitializeEngine(isolate(), nullptr, &global_context2, &static_objects2);
Tom Sepez9967cc52016-03-24 11:45:37 -070058
59 v8::Context::Scope context_scope(GetV8Context());
dsinclair884b4f92016-06-06 09:49:32 -070060 ExecuteInCurrentContext(CFX_WideString(kScript0));
Tom Sepez9967cc52016-03-24 11:45:37 -070061 CheckAssignmentInCurrentContext(kExpected0);
62
63 {
64 v8::Local<v8::Context> context1 =
65 v8::Local<v8::Context>::New(isolate(), global_context1);
weilidb444d22016-06-02 15:48:15 -070066 v8::Context::Scope context_scope1(context1);
dsinclair884b4f92016-06-06 09:49:32 -070067 ExecuteInCurrentContext(CFX_WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070068 CheckAssignmentInCurrentContext(kExpected1);
69 }
tsepeza4941912016-08-15 11:40:12 -070070 FXJS_ReleaseEngine(isolate(), &global_context1, &static_objects1);
Tom Sepez9967cc52016-03-24 11:45:37 -070071
72 {
73 v8::Local<v8::Context> context2 =
74 v8::Local<v8::Context>::New(isolate(), global_context2);
weilidb444d22016-06-02 15:48:15 -070075 v8::Context::Scope context_scope2(context2);
dsinclair884b4f92016-06-06 09:49:32 -070076 ExecuteInCurrentContext(CFX_WideString(kScript2));
Tom Sepez9967cc52016-03-24 11:45:37 -070077 CheckAssignmentInCurrentContext(kExpected2);
78 }
tsepeza4941912016-08-15 11:40:12 -070079 FXJS_ReleaseEngine(isolate(), &global_context2, &static_objects2);
Tom Sepez9967cc52016-03-24 11:45:37 -070080
81 CheckAssignmentInCurrentContext(kExpected0);
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070082}