1/*
2 * Copyright (c) 2023 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_HEAPSAMPLING_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPSAMPLING_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsHeapsamplingTest : public TestActions {
23public:
24    JsHeapsamplingTest()
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, "heapprofiler-enable"},
38            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39            {SocketAction::SEND, "sampling"},
40            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
41            // set breakpoint
42            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "sample.js 22"},
43            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44            // hit breakpoint after resume first time
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            // hit breakpoint after resume second time
50            {SocketAction::SEND, "resume"},
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            {SocketAction::SEND, "sampling-stop"},
55            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
56                return RecvHeapsamlingInfo(recv);
57            }},
58            {SocketAction::SEND, "heapprofiler-disable"},
59            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
60            // reply success and run
61            {SocketAction::SEND, "success"},
62            {SocketAction::SEND, "resume"},
63            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
64            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
65        };
66    }
67
68    bool RecvHeapsamlingInfo(std::string recv)
69    {
70        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
71        int id;
72        Result ret = json->GetInt("id", &id);
73        if (ret != Result::SUCCESS) {
74            return false;
75        }
76
77        std::unique_ptr<PtJson> result;
78        ret = json->GetObject("result", &result);
79        if (ret != Result::SUCCESS) {
80            return false;
81        }
82
83        std::unique_ptr<PtJson> profile;
84        ret = result->GetObject("profile", &profile);
85        if (ret != Result::SUCCESS) {
86            return false;
87        }
88        return true;
89    }
90
91    std::pair<std::string, std::string> GetEntryPoint() override
92    {
93        return {pandaFile_, entryPoint_};
94    }
95    ~JsHeapsamplingTest() = default;
96
97private:
98    std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
99    std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
100    std::string entryPoint_ = "_GLOBAL::func_main_0";
101};
102
103std::unique_ptr<TestActions> GetJsHeapsamplingTest()
104{
105    return std::make_unique<JsHeapsamplingTest>();
106}
107}  // namespace panda::ecmascript::tooling::test
108
109#endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPSAMPLING_TEST_H
110