1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ecmascript/tests/test_helper.h"
17#include "ecmascript/debugger/dropframe_manager.h"
18#include "ecmascript/jspandafile/js_pandafile_manager.h"
19#include "assembler/assembly-parser.h"
20
21using namespace panda::ecmascript;
22using namespace panda::ecmascript::tooling;
23using namespace panda::panda_file;
24using namespace panda::pandasm;
25
26namespace panda::ecmascript::tooling {
27class DropframeManagerFriendTest {
28public:
29    std::vector<std::tuple<JSHandle<JSTaggedValue>, uint16_t,
30        JSHandle<JSTaggedValue>>> GetLexModifyRecordOfTopFrameTest ()
31    {
32        return manager.GetLexModifyRecordOfTopFrame();
33    }
34
35    void NewLexModifyRecordLevelTest()
36    {
37        manager.NewLexModifyRecordLevel();
38    }
39
40    uint32_t GetLexModifyRecordLevelTest()
41    {
42        return manager.GetLexModifyRecordLevel();
43    }
44
45    void RemoveLexModifyRecordOfTopFrameTest(JSThread *thread)
46    {
47        manager.RemoveLexModifyRecordOfTopFrame(thread);
48    }
49
50    void PopMethodInfoTest()
51    {
52        manager.PopMethodInfo();
53    }
54
55    void PushMethodInfoTest(std::tuple<JSPandaFile*, panda_file::File::EntityId> methodInfo)
56    {
57        manager.PushMethodInfo(methodInfo);
58    }
59
60    bool CheckExitMethodInfoTest(std::tuple<JSPandaFile*, panda_file::File::EntityId> methodInfo)
61    {
62        return manager.CheckExitMethodInfo(methodInfo);
63    }
64
65    void MergeLexModifyRecordOfTopFrameTest(JSThread *thread)
66    {
67        manager.MergeLexModifyRecordOfTopFrame(thread);
68    }
69
70private:
71    DropframeManager manager;
72};
73}
74
75namespace panda::test {
76class DropframeManagerTest : public testing::Test {
77public:
78    static void SetUpTestCase()
79    {
80        GTEST_LOG_(INFO) << "SetUpTestCase";
81    }
82
83    static void TearDownTestCase()
84    {
85        GTEST_LOG_(INFO) << "TearDownCase";
86    }
87
88    void SetUp() override
89    {
90        TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
91    }
92
93    void TearDown() override
94    {
95        TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
96    }
97
98    EcmaVM *ecmaVm {nullptr};
99    EcmaHandleScope *scope {nullptr};
100    JSThread *thread {nullptr};
101};
102
103HWTEST_F_L0(DropframeManagerTest, GetLexModifyRecordOfTopFrameTest)
104{
105    DropframeManagerFriendTest manager;
106    manager.GetLexModifyRecordOfTopFrameTest();
107    uint32_t result = manager.GetLexModifyRecordLevelTest();
108    EXPECT_FALSE(result);
109
110    manager.NewLexModifyRecordLevelTest();
111    manager.GetLexModifyRecordOfTopFrameTest();
112    result = manager.GetLexModifyRecordLevelTest();
113    EXPECT_TRUE(result);
114}
115
116HWTEST_F_L0(DropframeManagerTest, RemoveLexModifyRecordOfTopFrameTest)
117{
118    DropframeManagerFriendTest manager;
119    JSThread *thread = ecmaVm->GetJSThread();
120    uint32_t result = manager.GetLexModifyRecordLevelTest();
121    manager.RemoveLexModifyRecordOfTopFrameTest(thread);
122    EXPECT_FALSE(result);
123
124    manager.NewLexModifyRecordLevelTest();
125    result = manager.GetLexModifyRecordLevelTest();
126    EXPECT_TRUE(result);
127
128    manager.RemoveLexModifyRecordOfTopFrameTest(thread);
129    result = manager.GetLexModifyRecordLevelTest();
130    EXPECT_FALSE(result);
131}
132
133HWTEST_F_L0(DropframeManagerTest, PopMethodInfoTest)
134{
135    DropframeManagerFriendTest manager;
136    Parser parser;
137    const char *filename = "__PandaFileTranslatorTest2.pa";
138    const char *data = R"(
139        .function any func_main_0(any a0, any a1, any a2) {
140            ldai 1
141            return
142        }
143    )";
144    auto res = parser.Parse(data);
145    JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
146    std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
147    std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(filename));
148    const panda_file::File *testpf = pf.get()->GetPandaFile();
149    CString descriptor = "example_descriptor";
150    JSPandaFile* jspandaFilePtr = new JSPandaFile(testpf, descriptor);
151
152    panda_file::File::EntityId entityId(42);
153    std::tuple<JSPandaFile*, panda_file::File::EntityId> methodInfo(jspandaFilePtr, entityId);
154
155    manager.PopMethodInfoTest();
156    manager.PushMethodInfoTest(methodInfo);
157    bool result = manager.CheckExitMethodInfoTest(methodInfo);
158    EXPECT_EQ(result, true);
159
160    manager.PopMethodInfoTest();
161    result = manager.CheckExitMethodInfoTest(methodInfo);
162    EXPECT_EQ(result, false);
163}
164
165HWTEST_F_L0(DropframeManagerTest, MergeLexModifyRecordOfTopFrameEmptyTest)
166{
167    DropframeManagerFriendTest manager;
168    JSThread *thread = ecmaVm->GetJSThread();
169    manager.MergeLexModifyRecordOfTopFrameTest(thread);
170    uint32_t result = manager.GetLexModifyRecordLevelTest();
171    EXPECT_FALSE(result);
172}
173}  // namespace panda::test
174