1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_ASYNC_TEST_H
17e509ee18Sopenharmony_ci#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_ASYNC_TEST_H
18e509ee18Sopenharmony_ci
19e509ee18Sopenharmony_ci#include "tooling/test/client_utils/test_util.h"
20e509ee18Sopenharmony_ci
21e509ee18Sopenharmony_cinamespace panda::ecmascript::tooling::test {
22e509ee18Sopenharmony_ciclass JsHeapusageAsyncTest : public TestActions {
23e509ee18Sopenharmony_cipublic:
24e509ee18Sopenharmony_ci    JsHeapusageAsyncTest()
25e509ee18Sopenharmony_ci    {
26e509ee18Sopenharmony_ci        testAction = {
27e509ee18Sopenharmony_ci            {SocketAction::SEND, "enable"},
28e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29e509ee18Sopenharmony_ci            {SocketAction::SEND, "runtime-enable"},
30e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31e509ee18Sopenharmony_ci            {SocketAction::SEND, "run"},
32e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33e509ee18Sopenharmony_ci            // load async_func.js
34e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35e509ee18Sopenharmony_ci            // break on start
36e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37e509ee18Sopenharmony_ci            {SocketAction::SEND, "cpuprofile-enable"},
38e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39e509ee18Sopenharmony_ci
40e509ee18Sopenharmony_ci            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "async_func.js 18"},
41e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
42e509ee18Sopenharmony_ci
43e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
44e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
45e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
47e509ee18Sopenharmony_ci            {SocketAction::SEND, "heapusage"},
48e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
49e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
50e509ee18Sopenharmony_ci
51e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
52e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
53e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
54e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
55e509ee18Sopenharmony_ci            {SocketAction::SEND, "heapusage"},
56e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
57e509ee18Sopenharmony_ci                [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
58e509ee18Sopenharmony_ci
59e509ee18Sopenharmony_ci            // reply success and run
60e509ee18Sopenharmony_ci            {SocketAction::SEND, "success"},
61e509ee18Sopenharmony_ci            {SocketAction::SEND, "resume"},
62e509ee18Sopenharmony_ci            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
63e509ee18Sopenharmony_ci            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
64e509ee18Sopenharmony_ci        };
65e509ee18Sopenharmony_ci    }
66e509ee18Sopenharmony_ci
67e509ee18Sopenharmony_ci    bool RecvHeapusageInfo(std::string recv)
68e509ee18Sopenharmony_ci    {
69e509ee18Sopenharmony_ci        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
70e509ee18Sopenharmony_ci        Result ret;
71e509ee18Sopenharmony_ci        int id = 0;
72e509ee18Sopenharmony_ci        ret = json->GetInt("id", &id);
73e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
74e509ee18Sopenharmony_ci            return false;
75e509ee18Sopenharmony_ci        }
76e509ee18Sopenharmony_ci
77e509ee18Sopenharmony_ci        std::unique_ptr<PtJson> result = nullptr;
78e509ee18Sopenharmony_ci        ret = json->GetObject("result", &result);
79e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
80e509ee18Sopenharmony_ci            return false;
81e509ee18Sopenharmony_ci        }
82e509ee18Sopenharmony_ci
83e509ee18Sopenharmony_ci        int usedSize = 0;
84e509ee18Sopenharmony_ci        ret = result->GetInt("usedSize", &usedSize);
85e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
86e509ee18Sopenharmony_ci            return false;
87e509ee18Sopenharmony_ci        }
88e509ee18Sopenharmony_ci
89e509ee18Sopenharmony_ci        int totalSize = 0;
90e509ee18Sopenharmony_ci        ret = result->GetInt("totalSize", &totalSize);
91e509ee18Sopenharmony_ci        if (ret != Result::SUCCESS) {
92e509ee18Sopenharmony_ci            return false;
93e509ee18Sopenharmony_ci        }
94e509ee18Sopenharmony_ci        return true;
95e509ee18Sopenharmony_ci    }
96e509ee18Sopenharmony_ci
97e509ee18Sopenharmony_ci    std::pair<std::string, std::string> GetEntryPoint() override
98e509ee18Sopenharmony_ci    {
99e509ee18Sopenharmony_ci        return {pandaFile_, entryPoint_};
100e509ee18Sopenharmony_ci    }
101e509ee18Sopenharmony_ci    ~JsHeapusageAsyncTest() = default;
102e509ee18Sopenharmony_ci
103e509ee18Sopenharmony_ciprivate:
104e509ee18Sopenharmony_ci    std::string pandaFile_ = DEBUGGER_ABC_DIR "async_func.abc";
105e509ee18Sopenharmony_ci    std::string sourceFile_ = DEBUGGER_JS_DIR "async_func.js";
106e509ee18Sopenharmony_ci    std::string entryPoint_ = "_GLOBAL::func_main_0";
107e509ee18Sopenharmony_ci};
108e509ee18Sopenharmony_ci
109e509ee18Sopenharmony_cistd::unique_ptr<TestActions> GetJsHeapusageAsyncTest()
110e509ee18Sopenharmony_ci{
111e509ee18Sopenharmony_ci    return std::make_unique<JsHeapusageAsyncTest>();
112e509ee18Sopenharmony_ci}
113e509ee18Sopenharmony_ci}  // namespace panda::ecmascript::tooling::test
114e509ee18Sopenharmony_ci
115e509ee18Sopenharmony_ci#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_ASYNC_TEST_H
116