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_CPUPROFILE_TEST_H 17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_TEST_H 18 19#include "tooling/test/client_utils/test_util.h" 20 21namespace panda::ecmascript::tooling::test { 22class JsCpuprofileTest : public TestActions { 23public: 24 JsCpuprofileTest() 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 {SocketAction::SEND, "cpuprofile-enable"}, 38 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 39 {SocketAction::SEND, "cpuprofile"}, 40 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 41 // resume 42 {SocketAction::SEND, "resume"}, 43 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 44 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 45 // stop and disable 46 {SocketAction::SEND, "cpuprofile-stop"}, 47 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, 48 [](auto recv, auto, auto) -> bool { 49 std::unique_ptr<PtJson> json = PtJson::Parse(recv); 50 Result ret; 51 int id = 0; 52 ret = json->GetInt("id", &id); 53 if (ret != Result::SUCCESS) { 54 return false; 55 } 56 57 std::unique_ptr<PtJson> result = nullptr; 58 ret = json->GetObject("result", &result); 59 if (ret != Result::SUCCESS) { 60 return false; 61 } 62 63 std::unique_ptr<PtJson> profile = nullptr; 64 ret = result->GetObject("profile", &profile); 65 if (ret != Result::SUCCESS) { 66 return false; 67 } 68 return true; 69 }}, 70 {SocketAction::SEND, "cpuprofile-disable"}, 71 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 72 // reply success 73 {SocketAction::SEND, "success"}, 74 }; 75 } 76 77 std::pair<std::string, std::string> GetEntryPoint() override 78 { 79 return {pandaFile_, entryPoint_}; 80 } 81 ~JsCpuprofileTest() = default; 82 83private: 84 std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc"; 85 std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js"; 86 std::string entryPoint_ = "_GLOBAL::func_main_0"; 87}; 88 89std::unique_ptr<TestActions> GetJsCpuprofileTest() 90{ 91 return std::make_unique<JsCpuprofileTest>(); 92} 93} // namespace panda::ecmascript::tooling::test 94 95#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_TEST_H 96