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_STEPOVER_SWITCH_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_SWITCH_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsStepoverSwitchTest : public TestActions {
23public:
24    JsStepoverSwitchTest()
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,
40                [](auto recv, auto, auto) -> bool {
41                    std::unique_ptr<PtJson> json = PtJson::Parse(recv);
42                    DebuggerClient debuggerClient(0);
43                    debuggerClient.RecvReply(std::move(json));
44                    return true;
45                }},
46
47            // hit breakpoint after resume first time
48            {SocketAction::SEND, "resume"},
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
53            {SocketAction::SEND, "sov"},
54            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
55            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
56            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
57                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 46); }},
58            {SocketAction::SEND, "sov"},
59            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
60            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
61            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
62                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 47); }},
63            {SocketAction::SEND, "sov"},
64            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
65            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
67                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 44); }},
68            {SocketAction::SEND, "sov"},
69            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
70            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
71            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
72                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 45); }},
73            {SocketAction::SEND, "sov"},
74            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
75            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
76            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
77                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 46); }},
78            {SocketAction::SEND, "sov"},
79            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
80            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
81            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
82                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 48); }},
83            {SocketAction::SEND, "sov"},
84            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
85            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
86            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
87                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 49); }},
88            {SocketAction::SEND, "sov"},
89            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
90            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
91            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
92                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 44); }},
93            {SocketAction::SEND, "sov"},
94            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
95            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
96            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
97                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 45); }},
98            {SocketAction::SEND, "sov"},
99            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
100            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
101            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
102                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 46); }},
103            {SocketAction::SEND, "sov"},
104            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
105            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
106            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
107                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 48); }},
108            {SocketAction::SEND, "sov"},
109            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
110            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
111            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
112                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 50); }},
113            {SocketAction::SEND, "sov"},
114            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
115            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
116            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
117                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "loop_switch", 51); }},
118
119            {SocketAction::SEND, "delete 1"},
120            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
121            {SocketAction::SEND, "resume"},
122            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
123            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
124            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
125
126            // reply success and run
127            {SocketAction::SEND, "success"},
128            {SocketAction::SEND, "resume"},
129            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
130            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
131        };
132    }
133
134    bool RecvStepoverInfo(std::string recv, std::string funcName, int lineNumber)
135    {
136        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
137        Result ret;
138        std::string method = "";
139        ret = json->GetString("method", &method);
140        if (ret != Result::SUCCESS || method != "Debugger.paused") {
141            return false;
142        }
143
144        std::unique_ptr<PtJson> params = nullptr;
145        ret = json->GetObject("params", &params);
146        if (ret != Result::SUCCESS) {
147            return false;
148        }
149
150        std::unique_ptr<PtJson> callFrames = nullptr;
151        ret = params->GetArray("callFrames", &callFrames);
152        if (ret != Result::SUCCESS) {
153            return false;
154        }
155
156        std::string functionName = "";
157        ret = callFrames->Get(0)->GetString("functionName", &functionName);
158        if (ret != Result::SUCCESS || functionName != funcName) {
159            return false;
160        }
161
162        std::unique_ptr<PtJson> location = nullptr;
163        ret = callFrames->Get(0)->GetObject("location", &location);
164        if (ret != Result::SUCCESS) {
165            return false;
166        }
167
168        int lineNum = 0;
169        ret = location->GetInt("lineNumber", &lineNum);
170        if (ret != Result::SUCCESS || lineNum != lineNumber) {
171            return false;
172        }
173
174        DebuggerClient debuggerClient(0);
175        debuggerClient.PausedReply(std::move(json));
176        return true;
177    }
178
179    std::pair<std::string, std::string> GetEntryPoint() override
180    {
181        return {pandaFile_, entryPoint_};
182    }
183    ~JsStepoverSwitchTest() = default;
184
185private:
186    std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
187    std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
188    std::string entryPoint_ = "_GLOBAL::func_main_0";
189};
190
191std::unique_ptr<TestActions> GetJsStepoverSwitchTest()
192{
193    return std::make_unique<JsStepoverSwitchTest>();
194}
195}  // namespace panda::ecmascript::tooling::test
196
197#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_SWITCH_TEST_H
198