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