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_SWITCH_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOUT_SWITCH_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsStepoutSwitchTest : public TestActions {
23public:
24    JsStepoutSwitchTest()
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, "so"},
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 RecvStepoutInfo(recv, "loop_switch", 45); }},
54            {SocketAction::SEND, "so"},
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 RecvStepoutInfo(recv, "loop_switch", 48); }},
59            {SocketAction::SEND, "so"},
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 RecvStepoutInfo(recv, "loop_switch", 45); }},
64            {SocketAction::SEND, "so"},
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 RecvStepoutInfo(recv, "loop_switch", 48); }},
69            {SocketAction::SEND, "so"},
70            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
71            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
72            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
73                [this](auto recv, auto, auto) -> bool { return RecvStepoutInfo(recv, "func_main_0", 66); }},
74
75            {SocketAction::SEND, "resume"},
76            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
77            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
78            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
79
80            // reply success and run
81            {SocketAction::SEND, "success"},
82            {SocketAction::SEND, "resume"},
83            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
84            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
85        };
86    }
87
88    bool RecvStepoutInfo(std::string recv, std::string funcName, int lineNumber)
89    {
90        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
91        Result ret;
92        std::string method = "";
93        ret = json->GetString("method", &method);
94        if (ret != Result::SUCCESS || method != "Debugger.paused") {
95            return false;
96        }
97
98        std::unique_ptr<PtJson> params = nullptr;
99        ret = json->GetObject("params", &params);
100        if (ret != Result::SUCCESS) {
101            return false;
102        }
103
104        std::unique_ptr<PtJson> callFrames = nullptr;
105        ret = params->GetArray("callFrames", &callFrames);
106        if (ret != Result::SUCCESS) {
107            return false;
108        }
109
110        std::string functionName = "";
111        ret = callFrames->Get(0)->GetString("functionName", &functionName);
112        if (ret != Result::SUCCESS || functionName != funcName) {
113            return false;
114        }
115
116        std::unique_ptr<PtJson> location = nullptr;
117        ret = callFrames->Get(0)->GetObject("location", &location);
118        if (ret != Result::SUCCESS) {
119            return false;
120        }
121
122        int lineNum = 0;
123        ret = location->GetInt("lineNumber", &lineNum);
124        if (ret != Result::SUCCESS || lineNum != lineNumber) {
125            return false;
126        }
127
128        DebuggerClient debuggerClient(0);
129        debuggerClient.PausedReply(std::move(json));
130        return true;
131    }
132
133    std::pair<std::string, std::string> GetEntryPoint() override
134    {
135        return {pandaFile_, entryPoint_};
136    }
137    ~JsStepoutSwitchTest() = default;
138
139private:
140    std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
141    std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
142    std::string entryPoint_ = "_GLOBAL::func_main_0";
143};
144
145std::unique_ptr<TestActions> GetJsStepoutSwitchTest()
146{
147    return std::make_unique<JsStepoutSwitchTest>();
148}
149}  // namespace panda::ecmascript::tooling::test
150
151#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOUT_SWITCH_TEST_H
152