1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_LOOP_TEST_H
17e509ee18Sopenharmony_ci#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_LOOP_TEST_H
18e509ee18Sopenharmony_ci
19e509ee18Sopenharmony_ci#include "tooling/test/client_utils/test_util.h"
20e509ee18Sopenharmony_ci
21e509ee18Sopenharmony_cinamespace panda::ecmascript::tooling::test {
22e509ee18Sopenharmony_ciclass JsStepoverLoopTest : public TestActions {
23e509ee18Sopenharmony_cipublic:
24e509ee18Sopenharmony_ci    JsStepoverLoopTest()
25e509ee18Sopenharmony_ci    {
26e509ee18Sopenharmony_ci        testAction = {
27e509ee18Sopenharmony_ci            {SocketAction::SEND, "enable"},
28e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29e509ee18Sopenharmony_ci            {SocketAction::SEND, "runtime-enable"},
30e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31e509ee18Sopenharmony_ci            {SocketAction::SEND, "run"},
32e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33e509ee18Sopenharmony_ci            // load common_func.js
34e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35e509ee18Sopenharmony_ci            // break on start
36e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37e509ee18Sopenharmony_ci            // set first breakpoint
38e509ee18Sopenharmony_ci            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 27"},
39e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
40e509ee18Sopenharmony_ci                [](auto recv, auto, auto) -> bool {
41e509ee18Sopenharmony_ci                    std::unique_ptr<PtJson> json = PtJson::Parse(recv);
42e509ee18Sopenharmony_ci                    DebuggerClient debuggerClient(0);
43e509ee18Sopenharmony_ci                    debuggerClient.RecvReply(std::move(json));
44e509ee18Sopenharmony_ci                    return true;
45e509ee18Sopenharmony_ci                }},
46e509ee18Sopenharmony_ci
47e509ee18Sopenharmony_ci            // hit breakpoint after resume first time
48e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
49e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
50e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
51e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
52e509ee18Sopenharmony_ci
53e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
54e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
55e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
56e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
57e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "for_loop", 27); }},
58e509ee18Sopenharmony_ci
59e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
60e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
61e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
62e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
63e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "for_loop", 28); }},
64e509ee18Sopenharmony_ci
65e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
66e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
67e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
68e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
69e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "for_loop", 26); }},
70e509ee18Sopenharmony_ci
71e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
72e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
73e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
74e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
75e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "for_loop", 27); }},
76e509ee18Sopenharmony_ci
77e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
78e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
79e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
80e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
81e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "for_loop", 28); }},
82e509ee18Sopenharmony_ci
83e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
84e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
85e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
86e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
87e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "for_loop", 26); }},
88e509ee18Sopenharmony_ci
89e509ee18Sopenharmony_ci            {SocketAction::SEND, "delete 1"},
90e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
91e509ee18Sopenharmony_ci
92e509ee18Sopenharmony_ci            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 35"},
93e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
94e509ee18Sopenharmony_ci                [](auto recv, auto, auto) -> bool {
95e509ee18Sopenharmony_ci                    std::unique_ptr<PtJson> json = PtJson::Parse(recv);
96e509ee18Sopenharmony_ci                    DebuggerClient debuggerClient(0);
97e509ee18Sopenharmony_ci                    debuggerClient.RecvReply(std::move(json));
98e509ee18Sopenharmony_ci                    return true;
99e509ee18Sopenharmony_ci                }},
100e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
101e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
102e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
103e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
104e509ee18Sopenharmony_ci
105e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
106e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
107e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
108e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
109e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 35); }},
110e509ee18Sopenharmony_ci
111e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
112e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
113e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
114e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
115e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 36); }},
116e509ee18Sopenharmony_ci
117e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
118e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
119e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
120e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
121e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 37); }},
122e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
123e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
124e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
125e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
126e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 38); }},
127e509ee18Sopenharmony_ci
128e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
129e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
130e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
131e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
132e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 35); }},
133e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
134e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
135e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
136e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
137e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 36); }},
138e509ee18Sopenharmony_ci
139e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
140e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
141e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
142e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
143e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 37); }},
144e509ee18Sopenharmony_ci            {SocketAction::SEND, "sov"},
145e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
146e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
147e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
148e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "while_loop", 38); }},
149e509ee18Sopenharmony_ci
150e509ee18Sopenharmony_ci            {SocketAction::SEND, "delete 1"},
151e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
152e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
153e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
154e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
155e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
156e509ee18Sopenharmony_ci
157e509ee18Sopenharmony_ci            // reply success and run
158e509ee18Sopenharmony_ci            {SocketAction::SEND, "success"},
159e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
160e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
161e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
162e509ee18Sopenharmony_ci        };
163e509ee18Sopenharmony_ci    }
164e509ee18Sopenharmony_ci
165e509ee18Sopenharmony_ci    bool RecvStepoverInfo(std::string recv, std::string funcName, int lineNumber)
166e509ee18Sopenharmony_ci    {
167e509ee18Sopenharmony_ci        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
168e509ee18Sopenharmony_ci        Result ret;
169e509ee18Sopenharmony_ci        std::string method = "";
170e509ee18Sopenharmony_ci        ret = json->GetString("method", &method);
171e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS || method != "Debugger.paused") {
172e509ee18Sopenharmony_ci            return false;
173e509ee18Sopenharmony_ci        }
174e509ee18Sopenharmony_ci
175e509ee18Sopenharmony_ci        std::unique_ptr<PtJson> params = nullptr;
176e509ee18Sopenharmony_ci        ret = json->GetObject("params", &params);
177e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
178e509ee18Sopenharmony_ci            return false;
179e509ee18Sopenharmony_ci        }
180e509ee18Sopenharmony_ci
181e509ee18Sopenharmony_ci        std::unique_ptr<PtJson> callFrames = nullptr;
182e509ee18Sopenharmony_ci        ret = params->GetArray("callFrames", &callFrames);
183e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
184e509ee18Sopenharmony_ci            return false;
185e509ee18Sopenharmony_ci        }
186e509ee18Sopenharmony_ci
187e509ee18Sopenharmony_ci        std::string functionName = "";
188e509ee18Sopenharmony_ci        ret = callFrames->Get(0)->GetString("functionName", &functionName);
189e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS || functionName != funcName) {
190e509ee18Sopenharmony_ci            return false;
191e509ee18Sopenharmony_ci        }
192e509ee18Sopenharmony_ci
193e509ee18Sopenharmony_ci        std::unique_ptr<PtJson> location = nullptr;
194e509ee18Sopenharmony_ci        ret = callFrames->Get(0)->GetObject("location", &location);
195e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
196e509ee18Sopenharmony_ci            return false;
197e509ee18Sopenharmony_ci        }
198e509ee18Sopenharmony_ci
199e509ee18Sopenharmony_ci        int lineNum = 0;
200e509ee18Sopenharmony_ci        ret = location->GetInt("lineNumber", &lineNum);
201e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS || lineNum != lineNumber) {
202e509ee18Sopenharmony_ci            return false;
203e509ee18Sopenharmony_ci        }
204e509ee18Sopenharmony_ci
205e509ee18Sopenharmony_ci        DebuggerClient debuggerClient(0);
206e509ee18Sopenharmony_ci        debuggerClient.PausedReply(std::move(json));
207e509ee18Sopenharmony_ci        return true;
208e509ee18Sopenharmony_ci    }
209e509ee18Sopenharmony_ci
210e509ee18Sopenharmony_ci    std::pair<std::string, std::string> GetEntryPoint() override
211e509ee18Sopenharmony_ci    {
212e509ee18Sopenharmony_ci        return {pandaFile_, entryPoint_};
213e509ee18Sopenharmony_ci    }
214e509ee18Sopenharmony_ci    ~JsStepoverLoopTest() = default;
215e509ee18Sopenharmony_ci
216e509ee18Sopenharmony_ciprivate:
217e509ee18Sopenharmony_ci    std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
218e509ee18Sopenharmony_ci    std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
219e509ee18Sopenharmony_ci    std::string entryPoint_ = "_GLOBAL::func_main_0";
220e509ee18Sopenharmony_ci};
221e509ee18Sopenharmony_ci
222e509ee18Sopenharmony_cistd::unique_ptr<TestActions> GetJsStepoverLoopTest()
223e509ee18Sopenharmony_ci{
224e509ee18Sopenharmony_ci    return std::make_unique<JsStepoverLoopTest>();
225e509ee18Sopenharmony_ci}
226e509ee18Sopenharmony_ci}  // namespace panda::ecmascript::tooling::test
227e509ee18Sopenharmony_ci
228e509ee18Sopenharmony_ci#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_LOOP_TEST_H
229