summaryrefslogtreecommitdiff
path: root/fpdfsdk/jsapi/fxjs_v8_embeddertest.cpp
blob: 161487f356f89255048c88801858c5f8c6921050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2015 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "testing/js_embedder_test.h"

namespace {

const double kExpected0 = 6.0;
const double kExpected1 = 7.0;
const double kExpected2 = 8.0;

const wchar_t kScript0[] = L"fred = 6";
const wchar_t kScript1[] = L"fred = 7";
const wchar_t kScript2[] = L"fred = 8";

}  // namespace

class FXJSV8EmbedderTest : public JSEmbedderTest {
 public:
  void ExecuteInCurrentContext(const wchar_t* script) {
    FXJSErr error;
    int sts = FXJS_Execute(isolate(), nullptr, script, &error);
    EXPECT_EQ(0, sts);
  }
  void CheckAssignmentInCurrentContext(double expected) {
    v8::Local<v8::Object> This = FXJS_GetThisObj(isolate());
    v8::Local<v8::Value> fred = FXJS_GetObjectElement(isolate(), This, L"fred");
    EXPECT_TRUE(fred->IsNumber());
    EXPECT_EQ(expected, fred->ToNumber(isolate()->GetCurrentContext())
                            .ToLocalChecked()
                            ->Value());
  }
};

TEST_F(FXJSV8EmbedderTest, Getters) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());
  v8::Context::Scope context_scope(GetV8Context());

  ExecuteInCurrentContext(kScript1);
  CheckAssignmentInCurrentContext(kExpected1);
}

TEST_F(FXJSV8EmbedderTest, MultipleRutimes) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());

  v8::Global<v8::Context> global_context1;
  std::vector<v8::Global<v8::Object>*> static_objects1;
  FXJS_InitializeRuntime(isolate(), nullptr, &global_context1,
                         &static_objects1);

  v8::Global<v8::Context> global_context2;
  std::vector<v8::Global<v8::Object>*> static_objects2;
  FXJS_InitializeRuntime(isolate(), nullptr, &global_context2,
                         &static_objects2);

  v8::Context::Scope context_scope(GetV8Context());
  ExecuteInCurrentContext(kScript0);
  CheckAssignmentInCurrentContext(kExpected0);

  {
    v8::Local<v8::Context> context1 =
        v8::Local<v8::Context>::New(isolate(), global_context1);
    v8::Context::Scope context_scope(context1);
    ExecuteInCurrentContext(kScript1);
    CheckAssignmentInCurrentContext(kExpected1);
  }
  FXJS_ReleaseRuntime(isolate(), &global_context1, &static_objects1);

  {
    v8::Local<v8::Context> context2 =
        v8::Local<v8::Context>::New(isolate(), global_context2);
    v8::Context::Scope context_scope(context2);
    ExecuteInCurrentContext(kScript2);
    CheckAssignmentInCurrentContext(kExpected2);
  }
  FXJS_ReleaseRuntime(isolate(), &global_context2, &static_objects2);

  CheckAssignmentInCurrentContext(kExpected0);
}