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#ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_STEP_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_STEP_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsHeapusageStepTest : public TestActions {
23public:
24    JsHeapusageStepTest()
25    {
26        testAction = {
27            {SocketAction::SEND, "enable"},
28            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29            {SocketAction::SEND, "runtime-enable"},
30            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31            {SocketAction::SEND, "run"},
32            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33            // load sample.js
34            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35            // break on start
36            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "sample.js 23"},
38            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39
40            {SocketAction::SEND, "resume"},
41            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
42            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
43            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
44            {SocketAction::SEND, "heapusage"},
45            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
46                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
47
48            {SocketAction::SEND, "so"},
49            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
50            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
51            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
52            {SocketAction::SEND, "heapusage"},
53            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
54                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
55
56            {SocketAction::SEND, "si"},
57            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
58            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
59            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
60            {SocketAction::SEND, "heapusage"},
61            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
62                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
63
64            {SocketAction::SEND, "sov"},
65            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
66            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
67            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
68            {SocketAction::SEND, "heapusage"},
69            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
70                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
71
72            {SocketAction::SEND, "so"},
73            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
74            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
75            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
76            {SocketAction::SEND, "heapusage"},
77            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
78                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
79
80            {SocketAction::SEND, "sov"},
81            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
82            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
83            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
84            {SocketAction::SEND, "heapusage"},
85            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
86                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
87
88            // reply success and run
89            {SocketAction::SEND, "success"},
90            {SocketAction::SEND, "resume"},
91            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
92            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
93        };
94    }
95
96    bool RecvHeapusageInfo(std::string recv)
97    {
98        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
99        Result ret;
100        int id = 0;
101        ret = json->GetInt("id", &id);
102        if (ret != Result::SUCCESS) {
103            return false;
104        }
105
106        std::unique_ptr<PtJson> result = nullptr;
107        ret = json->GetObject("result", &result);
108        if (ret != Result::SUCCESS) {
109            return false;
110        }
111
112        int usedSize = 0;
113        ret = result->GetInt("usedSize", &usedSize);
114        if (ret != Result::SUCCESS) {
115            return false;
116        }
117
118        int totalSize = 0;
119        ret = result->GetInt("totalSize", &totalSize);
120        if (ret != Result::SUCCESS) {
121            return false;
122        }
123        return true;
124    }
125
126    std::pair<std::string, std::string> GetEntryPoint() override
127    {
128        return {pandaFile_, entryPoint_};
129    }
130    ~JsHeapusageStepTest() = default;
131
132private:
133    std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
134    std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
135    std::string entryPoint_ = "_GLOBAL::func_main_0";
136};
137
138std::unique_ptr<TestActions> GetJsHeapusageStepTest()
139{
140    return std::make_unique<JsHeapusageStepTest>();
141}
142}  // namespace panda::ecmascript::tooling::test
143
144#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_STEP_TEST_H
145