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_STEPINTO_ASYNC_TEST_H 17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_ASYNC_TEST_H 18 19#include "tooling/test/client_utils/test_util.h" 20 21namespace panda::ecmascript::tooling::test { 22class JsStepintoAsyncTest : public TestActions { 23public: 24 JsStepintoAsyncTest() 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 async_func.js 34 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN}, 35 // break on start 36 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN}, 37 // set first breakpoint 38 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "async_func.js 25"}, 39 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 40 41 // hit breakpoint after resume first time 42 {SocketAction::SEND, "resume"}, 43 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 44 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 45 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 46 [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv); }}, 47 48 {SocketAction::SEND, "si"}, 49 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 50 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 51 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 52 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "helloAsync", 16); }}, 53 54 {SocketAction::SEND, "si"}, 55 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 56 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 57 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 58 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "helloAsync", 17); }}, 59 60 {SocketAction::SEND, "si"}, 61 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 62 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 63 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 64 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "helloAsync", 18); }}, 65 66 {SocketAction::SEND, "si"}, 67 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 68 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 69 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 70 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "helloAsync", 19); }}, 71 72 {SocketAction::SEND, "si"}, 73 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 74 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 75 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 76 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "helloAsync", 20); }}, 77 78 {SocketAction::SEND, "si"}, 79 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 80 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 81 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, 82 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "helloAsync", 21); }}, 83 84 {SocketAction::SEND, "resume"}, 85 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 86 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 87 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN}, 88 89 // reply success and run 90 {SocketAction::SEND, "success"}, 91 {SocketAction::SEND, "resume"}, 92 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 93 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 94 }; 95 } 96 97 bool RecvBreakInfo(std::string recv) 98 { 99 std::unique_ptr<PtJson> json = PtJson::Parse(recv); 100 Result ret; 101 std::string method = ""; 102 ret = json->GetString("method", &method); 103 if (ret != Result::SUCCESS || method != "Debugger.paused") { 104 return false; 105 } 106 107 std::unique_ptr<PtJson> params = nullptr; 108 ret = json->GetObject("params", ¶ms); 109 if (ret != Result::SUCCESS) { 110 return false; 111 } 112 113 std::unique_ptr<PtJson> hitBreakpoints = nullptr; 114 ret = params->GetArray("hitBreakpoints", &hitBreakpoints); 115 if (ret != Result::SUCCESS) { 116 return false; 117 } 118 119 std::string breakpoint = ""; 120 breakpoint = hitBreakpoints->Get(0)->GetString(); 121 if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos || 122 breakpoint.find("24") == std::string::npos) { 123 return false; 124 } 125 126 DebuggerClient debuggerClient(0); 127 debuggerClient.PausedReply(std::move(json)); 128 return true; 129 } 130 131 bool RecvStepintoInfo(std::string recv, std::string funcName, int lineNumber) 132 { 133 std::unique_ptr<PtJson> json = PtJson::Parse(recv); 134 Result ret; 135 std::string method = ""; 136 ret = json->GetString("method", &method); 137 if (ret != Result::SUCCESS || method != "Debugger.paused") { 138 return false; 139 } 140 141 std::unique_ptr<PtJson> params = nullptr; 142 ret = json->GetObject("params", ¶ms); 143 if (ret != Result::SUCCESS) { 144 return false; 145 } 146 147 std::unique_ptr<PtJson> callFrames = nullptr; 148 ret = params->GetArray("callFrames", &callFrames); 149 if (ret != Result::SUCCESS) { 150 return false; 151 } 152 153 std::string functionName = ""; 154 ret = callFrames->Get(0)->GetString("functionName", &functionName); 155 if (ret != Result::SUCCESS || functionName != funcName) { 156 return false; 157 } 158 159 std::unique_ptr<PtJson> location = nullptr; 160 ret = callFrames->Get(0)->GetObject("location", &location); 161 if (ret != Result::SUCCESS) { 162 return false; 163 } 164 165 int lineNum = 0; 166 ret = location->GetInt("lineNumber", &lineNum); 167 if (ret != Result::SUCCESS || lineNum != lineNumber) { 168 return false; 169 } 170 171 DebuggerClient debuggerClient(0); 172 debuggerClient.PausedReply(std::move(json)); 173 return true; 174 } 175 176 std::pair<std::string, std::string> GetEntryPoint() override 177 { 178 return {pandaFile_, entryPoint_}; 179 } 180 ~JsStepintoAsyncTest() = default; 181 182private: 183 std::string pandaFile_ = DEBUGGER_ABC_DIR "async_func.abc"; 184 std::string sourceFile_ = DEBUGGER_JS_DIR "async_func.js"; 185 std::string entryPoint_ = "_GLOBAL::func_main_0"; 186}; 187 188std::unique_ptr<TestActions> GetJsStepintoAsyncTest() 189{ 190 return std::make_unique<JsStepintoAsyncTest>(); 191} 192} // namespace panda::ecmascript::tooling::test 193 194#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_ASYNC_TEST_H 195