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_IN_DIFFERENT_BRANCH_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_IN_DIFFERENT_BRANCH_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsBreakpointInDifferentBranchTest : public TestActions {
23public:
24    JsBreakpointInDifferentBranchTest()
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 branch.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 "branch.js 32"},
39            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "branch.js 37"},
41            // hit breakpoint in 'if' branch
42            {SocketAction::SEND, "resume"},
43            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
45            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
47                [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 31); }},
48
49            // hit breakpoint in 'else' branch
50            {SocketAction::SEND, "resume"},
51            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
52            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
53            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
54                [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 36); }},
55
56            // reply success and run
57            {SocketAction::SEND, "success"},
58            {SocketAction::SEND, "resume"},
59            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
60            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
61        };
62    }
63
64    bool RecvHitBreakInfo(std::string recv, int line)
65    {
66        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
67        Result ret;
68        std::string method = "";
69        ret = json->GetString("method", &method);
70        if (ret != Result::SUCCESS || method != "Debugger.paused") {
71            return false;
72        }
73
74        std::unique_ptr<PtJson> params = nullptr;
75        ret = json->GetObject("params", &params);
76        if (ret != Result::SUCCESS) {
77            return false;
78        }
79
80        std::unique_ptr<PtJson> hitBreakpoints = nullptr;
81        ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
82        if (ret != Result::SUCCESS) {
83            return false;
84        }
85
86        std::string breakpoint = "";
87        breakpoint = hitBreakpoints->Get(0)->GetString();
88        if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
89            breakpoint.find(std::to_string(line)) == std::string::npos) {
90            return false;
91        }
92        return true;
93    }
94
95    std::pair<std::string, std::string> GetEntryPoint() override { return {pandaFile_, entryPoint_}; }
96    ~JsBreakpointInDifferentBranchTest() = default;
97
98private:
99    std::string pandaFile_ = DEBUGGER_ABC_DIR "branch.abc";
100    std::string sourceFile_ = DEBUGGER_JS_DIR "branch.js";
101    std::string entryPoint_ = "_GLOBAL::func_main_0";
102};
103
104std::unique_ptr<TestActions> GetJsBreakpointInDifferentBranchTest()
105{
106    return std::make_unique<JsBreakpointInDifferentBranchTest>();
107}
108} // namespace panda::ecmascript::tooling::test
109
110#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_IN_DIFFERENT_BRANCH_TEST_H
111