Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfxjs_engine_unittest.cpp
Go to the documentation of this file.
1// Copyright 2018 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "fxjs/cfxjs_engine.h"
6
7#include <memory>
8
9#include "fxjs/cjs_object.h"
10#include "testing/fxv8_unittest.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "v8/include/v8-context.h"
13#include "v8/include/v8-isolate.h"
14
16 public:
17 FXJSEngineUnitTest() = default;
19
20 // FXV8UnitTest:
21 void SetUp() override {
23 FXJS_Initialize(1, isolate());
24 engine_ = std::make_unique<CFXJS_Engine>(isolate());
25 }
27
28 CFXJS_Engine* engine() const { return engine_.get(); }
29
30 private:
31 std::unique_ptr<CFXJS_Engine> engine_;
32};
33
34static bool perm_created = false;
35static bool perm_destroyed = false;
36static bool temp_created = false;
37static bool temp_destroyed = false;
38
40 // Reset variables since there might be multiple iterations.
41 perm_created = false;
42 perm_destroyed = false;
43 temp_created = false;
44 temp_destroyed = false;
45
46 v8::Isolate::Scope isolate_scope(isolate());
47 v8::HandleScope handle_scope(isolate());
48
49 // Object: 1
50 engine()->DefineObj(
51 "perm", FXJSOBJTYPE_DYNAMIC,
52 [](CFXJS_Engine* pEngine, v8::Local<v8::Object> obj,
53 v8::Local<v8::Object> proxy) {
54 pEngine->SetObjectPrivate(obj,
55 std::make_unique<CJS_Object>(proxy, nullptr));
56 perm_created = true;
57 },
58 [](v8::Local<v8::Object> obj) {
59 perm_destroyed = true;
60 CFXJS_Engine::SetObjectPrivate(obj, nullptr);
61 });
62
63 // Object: 2
64 engine()->DefineObj(
65 "temp", FXJSOBJTYPE_DYNAMIC,
66 [](CFXJS_Engine* pEngine, v8::Local<v8::Object> obj,
67 v8::Local<v8::Object> proxy) {
68 pEngine->SetObjectPrivate(obj,
69 std::make_unique<CJS_Object>(proxy, nullptr));
70 temp_created = true;
71 },
72 [](v8::Local<v8::Object> obj) {
73 temp_destroyed = true;
74 CFXJS_Engine::SetObjectPrivate(obj, nullptr);
75 });
76
77 engine()->InitializeEngine();
78
79 v8::Context::Scope context_scope(engine()->GetV8Context());
80 v8::Local<v8::Object> perm =
81 engine()->NewFXJSBoundObject(1, FXJSOBJTYPE_DYNAMIC);
82 EXPECT_FALSE(perm.IsEmpty());
83 EXPECT_TRUE(perm_created);
84 EXPECT_FALSE(perm_destroyed);
85
86 {
87 v8::HandleScope inner_handle_scope(isolate());
88 v8::Local<v8::Object> temp =
89 engine()->NewFXJSBoundObject(2, FXJSOBJTYPE_DYNAMIC);
90 EXPECT_FALSE(temp.IsEmpty());
91 EXPECT_TRUE(temp_created);
92 EXPECT_FALSE(temp_destroyed);
93 }
94
95 absl::optional<IJS_Runtime::JS_Error> err = engine()->Execute(L"gc();");
96 EXPECT_FALSE(err);
97
98 EXPECT_TRUE(perm_created);
99 EXPECT_FALSE(perm_destroyed);
100 EXPECT_TRUE(temp_created);
101 // TODO(tsepez): temp_destroyed should be true, but it isnt.
102 // EXPECT_TRUE(temp_destroyed);
103
104 engine()->ReleaseEngine();
105 EXPECT_TRUE(perm_created);
106 EXPECT_TRUE(perm_destroyed);
107 EXPECT_TRUE(temp_created);
108 EXPECT_TRUE(temp_destroyed);
109}
void FXJS_Release()
@ FXJSOBJTYPE_DYNAMIC
TEST_F(FXJSEngineUnitTest, GC)
static bool temp_created
static bool temp_destroyed
static bool perm_created
static bool perm_destroyed
CFXJS_Engine * engine() const
~FXJSEngineUnitTest() override=default
FXJSEngineUnitTest()=default
v8::Isolate * isolate() const
void SetUp() override