1/*
2 * Copyright (c) 2023 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_ARROW_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_ARROW_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsBreakpointArrowTest : public TestActions {
23public:
24    JsBreakpointArrowTest()
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 sample.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 "arrow_func.js 18"},
39            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40            // hit breakpoint after resume first time
41            {SocketAction::SEND, "resume"},
42            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
43            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
45                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
46                Result ret;
47                std::string method;
48                ret = json->GetString("method", &method);
49                if (ret != Result::SUCCESS || method != "Debugger.paused") {
50                    return false;
51                }
52
53                std::unique_ptr<PtJson> params = nullptr;
54                ret = json->GetObject("params", &params);
55                if (ret != Result::SUCCESS) {
56                    return false;
57                }
58
59                std::unique_ptr<PtJson> hitBreakpoints = nullptr;
60                ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
61                if (ret != Result::SUCCESS) {
62                    return false;
63                }
64
65                std::string breakpoint;
66                breakpoint = hitBreakpoints->Get(0)->GetString();
67                if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
68                    breakpoint.find("17") == std::string::npos) {
69                    return false;
70                }
71                return true;
72            }},
73            // reply success and run
74            {SocketAction::SEND, "success"},
75            {SocketAction::SEND, "resume"},
76            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
77            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
78        };
79    }
80
81    std::pair<std::string, std::string> GetEntryPoint() override
82    {
83        return {pandaFile_, entryPoint_};
84    }
85    ~JsBreakpointArrowTest() = default;
86
87private:
88    std::string pandaFile_ = DEBUGGER_ABC_DIR "arrow_func.abc";
89    std::string sourceFile_ = DEBUGGER_JS_DIR "arrow_func.js";
90    std::string entryPoint_ = "_GLOBAL::func_main_0";
91};
92
93std::unique_ptr<TestActions> GetJsBreakpointArrowTest()
94{
95    return std::make_unique<JsBreakpointArrowTest>();
96}
97}  // namespace panda::ecmascript::tooling::test
98
99#endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_ARROW_TEST_H
100