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