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