blob: b33ddca32d95e91c321c728b5d8b6d3aeca0f0b0 [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());
29 v8::Local<v8::Value> fred = FXJS_GetObjectElement(isolate(), This, L"fred");
30 EXPECT_TRUE(fred->IsNumber());
31 EXPECT_EQ(expected, fred->ToNumber(isolate()->GetCurrentContext())
32 .ToLocalChecked()
33 ->Value());
34 }
35};
Tom Sepeza72e8e22015-10-07 10:17:53 -070036
Lei Zhang1ac47eb2015-12-21 11:04:44 -080037TEST_F(FXJSV8EmbedderTest, Getters) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070038 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070039 v8::HandleScope handle_scope(isolate());
40 v8::Context::Scope context_scope(GetV8Context());
41
dsinclair884b4f92016-06-06 09:49:32 -070042 ExecuteInCurrentContext(CFX_WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070043 CheckAssignmentInCurrentContext(kExpected1);
44}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070045
Tom Sepez9967cc52016-03-24 11:45:37 -070046TEST_F(FXJSV8EmbedderTest, MultipleRutimes) {
47 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez9967cc52016-03-24 11:45:37 -070048 v8::HandleScope handle_scope(isolate());
49
50 v8::Global<v8::Context> global_context1;
51 std::vector<v8::Global<v8::Object>*> static_objects1;
52 FXJS_InitializeRuntime(isolate(), nullptr, &global_context1,
53 &static_objects1);
54
55 v8::Global<v8::Context> global_context2;
56 std::vector<v8::Global<v8::Object>*> static_objects2;
57 FXJS_InitializeRuntime(isolate(), nullptr, &global_context2,
58 &static_objects2);
59
60 v8::Context::Scope context_scope(GetV8Context());
dsinclair884b4f92016-06-06 09:49:32 -070061 ExecuteInCurrentContext(CFX_WideString(kScript0));
Tom Sepez9967cc52016-03-24 11:45:37 -070062 CheckAssignmentInCurrentContext(kExpected0);
63
64 {
65 v8::Local<v8::Context> context1 =
66 v8::Local<v8::Context>::New(isolate(), global_context1);
weilidb444d22016-06-02 15:48:15 -070067 v8::Context::Scope context_scope1(context1);
dsinclair884b4f92016-06-06 09:49:32 -070068 ExecuteInCurrentContext(CFX_WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070069 CheckAssignmentInCurrentContext(kExpected1);
70 }
71 FXJS_ReleaseRuntime(isolate(), &global_context1, &static_objects1);
72
73 {
74 v8::Local<v8::Context> context2 =
75 v8::Local<v8::Context>::New(isolate(), global_context2);
weilidb444d22016-06-02 15:48:15 -070076 v8::Context::Scope context_scope2(context2);
dsinclair884b4f92016-06-06 09:49:32 -070077 ExecuteInCurrentContext(CFX_WideString(kScript2));
Tom Sepez9967cc52016-03-24 11:45:37 -070078 CheckAssignmentInCurrentContext(kExpected2);
79 }
80 FXJS_ReleaseRuntime(isolate(), &global_context2, &static_objects2);
81
82 CheckAssignmentInCurrentContext(kExpected0);
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070083}