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