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