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_ASYNC_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_ASYNC_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsBreakpointAsyncTest : public TestActions {
23public:
24    JsBreakpointAsyncTest()
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 "async_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::STRING_CONTAIN},
45            // hit breakpoint after resume second time
46            {SocketAction::SEND, "resume"},
47            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
48            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
49            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
50                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
51                Result ret;
52                std::string method;
53                ret = json->GetString("method", &method);
54                if (ret != Result::SUCCESS || method != "Debugger.paused") {
55                    return false;
56                }
57
58                std::unique_ptr<PtJson> params = nullptr;
59                ret = json->GetObject("params", &params);
60                if (ret != Result::SUCCESS) {
61                    return false;
62                }
63
64                std::unique_ptr<PtJson> hitBreakpoints = nullptr;
65                ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
66                if (ret != Result::SUCCESS) {
67                    return false;
68                }
69
70                std::string breakpoint;
71                breakpoint = hitBreakpoints->Get(0)->GetString();
72                if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
73                    breakpoint.find("17") == std::string::npos) {
74                    return false;
75                }
76                return true;
77            }},
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    std::pair<std::string, std::string> GetEntryPoint() override
87    {
88        return {pandaFile_, entryPoint_};
89    }
90    ~JsBreakpointAsyncTest() = default;
91
92private:
93    std::string pandaFile_ = DEBUGGER_ABC_DIR "async_func.abc";
94    std::string sourceFile_ = DEBUGGER_JS_DIR "async_func.js";
95    std::string entryPoint_ = "_GLOBAL::func_main_0";
96};
97
98std::unique_ptr<TestActions> GetJsBreakpointAsyncTest()
99{
100    return std::make_unique<JsBreakpointAsyncTest>();
101}
102}  // namespace panda::ecmascript::tooling::test
103
104#endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_ASYNC_TEST_H
105