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
21 namespace panda::ecmascript::tooling::test {
22 class JsCpuprofileTest : public TestActions {
23 public:
JsCpuprofileTest()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
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, "cpuprofile-stop"},
51 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
52 [](auto recv, auto, auto) -> bool {
53 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
54 Result ret;
55 int id = 0;
56 ret = json->GetInt("id", &id);
57 if (ret != Result::SUCCESS) {
58 return false;
59 }
60
61 std::unique_ptr<PtJson> result = nullptr;
62 ret = json->GetObject("result", &result);
63 if (ret != Result::SUCCESS) {
64 return false;
65 }
66
67 std::unique_ptr<PtJson> profile = nullptr;
68 ret = result->GetObject("profile", &profile);
69 if (ret != Result::SUCCESS) {
70 return false;
71 }
72 return true;
73 }},
74 {SocketAction::SEND, "cpuprofile-disable"},
75 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
76 {SocketAction::SEND, "resume"},
77 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
78 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
79 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
80
81 // reply success and run
82 {SocketAction::SEND, "success"},
83 {SocketAction::SEND, "resume"},
84 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
85 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
86 };
87 }
88
GetEntryPoint()89 std::pair<std::string, std::string> GetEntryPoint() override
90 {
91 return {pandaFile_, entryPoint_};
92 }
93 ~JsCpuprofileTest() = default;
94
95 private:
96 std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
97 std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
98 std::string entryPoint_ = "_GLOBAL::func_main_0";
99 };
100
GetJsCpuprofileTest()101 std::unique_ptr<TestActions> GetJsCpuprofileTest()
102 {
103 return std::make_unique<JsCpuprofileTest>();
104 }
105 } // namespace panda::ecmascript::tooling::test
106
107 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_TEST_H
108