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->SetBinding(obj, std::make_unique<CJS_Object>(proxy, nullptr));
55 perm_created = true;
56 },
57 [](v8::Local<v8::Object> obj) {
58 perm_destroyed = true;
59 CFXJS_Engine::SetBinding(obj, nullptr);
60 });
61
62 // Object: 2
63 engine()->DefineObj(
64 "temp", FXJSOBJTYPE_DYNAMIC,
65 [](CFXJS_Engine* pEngine, v8::Local<v8::Object> obj,
66 v8::Local<v8::Object> proxy) {
67 pEngine->SetBinding(obj, std::make_unique<CJS_Object>(proxy, nullptr));
68 temp_created = true;
69 },
70 [](v8::Local<v8::Object> obj) {
71 temp_destroyed = true;
72 CFXJS_Engine::SetBinding(obj, nullptr);
73 });
74
75 engine()->InitializeEngine();
76
77 v8::Context::Scope context_scope(engine()->GetV8Context());
78 v8::Local<v8::Object> perm =
79 engine()->NewFXJSBoundObject(1, FXJSOBJTYPE_DYNAMIC);
80 EXPECT_FALSE(perm.IsEmpty());
81 EXPECT_TRUE(perm_created);
82 EXPECT_FALSE(perm_destroyed);
83
84 {
85 v8::HandleScope inner_handle_scope(isolate());
86 v8::Local<v8::Object> temp =
87 engine()->NewFXJSBoundObject(2, FXJSOBJTYPE_DYNAMIC);
88 EXPECT_FALSE(temp.IsEmpty());
89 EXPECT_TRUE(temp_created);
90 EXPECT_FALSE(temp_destroyed);
91 }
92
93 std::optional<IJS_Runtime::JS_Error> err = engine()->Execute(L"gc();");
94 EXPECT_FALSE(err);
95
96 EXPECT_TRUE(perm_created);
97 EXPECT_FALSE(perm_destroyed);
98 EXPECT_TRUE(temp_created);
99 // TODO(tsepez): temp_destroyed should be true, but it isnt.
100 // EXPECT_TRUE(temp_destroyed);
101
102 engine()->ReleaseEngine();
103 EXPECT_TRUE(perm_created);
104 EXPECT_TRUE(perm_destroyed);
105 EXPECT_TRUE(temp_created);
106 EXPECT_TRUE(temp_destroyed);
107}
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