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