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_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsHeapusageTest : public TestActions {
23 public:
JsHeapusageTest()24 JsHeapusageTest()
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 // set breakpoint
38 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "sample.js 22"},
39 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40 // hit breakpoint after resume first time
41 {SocketAction::SEND, "resume"},
42 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
43 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
45 // hit breakpoint after resume second time
46 {SocketAction::SEND, "resume"},
47 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
48 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
49 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
50 [this](auto recv, auto, auto) -> bool {
51 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
52 Result ret;
53 std::string method = "";
54 ret = json->GetString("method", &method);
55 if (ret != Result::SUCCESS || method != "Debugger.paused") {
56 return false;
57 }
58
59 std::unique_ptr<PtJson> params = nullptr;
60 ret = json->GetObject("params", ¶ms);
61 if (ret != Result::SUCCESS) {
62 return false;
63 }
64
65 std::unique_ptr<PtJson> hitBreakpoints = nullptr;
66 ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
67 if (ret != Result::SUCCESS) {
68 return false;
69 }
70
71 std::string breakpoint = "";
72 breakpoint = hitBreakpoints->Get(0)->GetString();
73 if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
74 breakpoint.find("21") == std::string::npos) {
75 return false;
76 }
77 return true;
78 }},
79 {SocketAction::SEND, "heapusage"},
80 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
81 [](auto recv, auto, auto) -> bool {
82 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
83 Result ret;
84 int id = 0;
85 ret = json->GetInt("id", &id);
86 if (ret != Result::SUCCESS) {
87 return false;
88 }
89
90 std::unique_ptr<PtJson> result = nullptr;
91 ret = json->GetObject("result", &result);
92 if (ret != Result::SUCCESS) {
93 return false;
94 }
95
96 int usedSize = 0;
97 ret = result->GetInt("usedSize", &usedSize);
98 if (ret != Result::SUCCESS) {
99 return false;
100 }
101
102 int totalSize = 0;
103 ret = result->GetInt("totalSize", &totalSize);
104 if (ret != Result::SUCCESS) {
105 return false;
106 }
107 return true;
108 }},
109
110 // reply success and run
111 {SocketAction::SEND, "success"},
112 {SocketAction::SEND, "resume"},
113 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
114 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
115 };
116 }
117
118 std::pair<std::string, std::string> GetEntryPoint() override
119 {
120 return {pandaFile_, entryPoint_};
121 }
122 ~JsHeapusageTest() = default;
123
124 private:
125 std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
126 std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
127 std::string entryPoint_ = "_GLOBAL::func_main_0";
128 };
129
GetJsHeapusageTest()130 std::unique_ptr<TestActions> GetJsHeapusageTest()
131 {
132 return std::make_unique<JsHeapusageTest>();
133 }
134 } // namespace panda::ecmascript::tooling::test
135
136 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_TEST_H
137