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_RECURSION_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_RECURSION_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsCpuprofileRecursionTest : public TestActions {
23 public:
JsCpuprofileRecursionTest()24 JsCpuprofileRecursionTest()
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 common_func.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
40 {SocketAction::SEND, "cpuprofile"},
41 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
42 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 67"},
43 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44 {SocketAction::SEND, "resume"},
45 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
46 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
47 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
48 {SocketAction::SEND, "cpuprofile-stop"},
49 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
50 [this](auto recv, auto, auto) -> bool { return RecvCpuprofileInfo(recv); }},
51
52 {SocketAction::SEND, "cpuprofile"},
53 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
54 {SocketAction::SEND, "resume"},
55 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
56 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
57 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
58 {SocketAction::SEND, "cpuprofile-stop"},
59 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
60 [this](auto recv, auto, auto) -> bool { return RecvCpuprofileInfo(recv); }},
61
62 {SocketAction::SEND, "cpuprofile-disable"},
63 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
64
65 {SocketAction::SEND, "resume"},
66 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
67 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
68 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
69
70 // reply success and run
71 {SocketAction::SEND, "success"},
72 {SocketAction::SEND, "resume"},
73 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
74 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
75 };
76 }
77
RecvCpuprofileInfo(std::string recv)78 bool RecvCpuprofileInfo(std::string recv)
79 {
80 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
81 Result ret;
82 int id = 0;
83 ret = json->GetInt("id", &id);
84 if (ret != Result::SUCCESS) {
85 return false;
86 }
87
88 std::unique_ptr<PtJson> result = nullptr;
89 ret = json->GetObject("result", &result);
90 if (ret != Result::SUCCESS) {
91 return false;
92 }
93
94 std::unique_ptr<PtJson> profile = nullptr;
95 ret = result->GetObject("profile", &profile);
96 if (ret != Result::SUCCESS) {
97 return false;
98 }
99 return true;
100 }
101
GetEntryPoint()102 std::pair<std::string, std::string> GetEntryPoint() override
103 {
104 return {pandaFile_, entryPoint_};
105 }
106 ~JsCpuprofileRecursionTest() = default;
107
108 private:
109 std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
110 std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
111 std::string entryPoint_ = "_GLOBAL::func_main_0";
112 };
113
GetJsCpuprofileRecursionTest()114 std::unique_ptr<TestActions> GetJsCpuprofileRecursionTest()
115 {
116 return std::make_unique<JsCpuprofileRecursionTest>();
117 }
118 } // namespace panda::ecmascript::tooling::test
119
120 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_LOOP_TEST_H
121