1 /*
2  * Copyright (c) 2021 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_HEAPPROFILER_DUMP_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPPROFILER_DUMP_TEST_H
18 
19 #include "tooling/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsHeapProfilerDumpTest : public TestActions {
23 public:
JsHeapProfilerDumpTest()24     JsHeapProfilerDumpTest()
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 export_variable_second.js
34             {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
36             {SocketAction::SEND, "resume"},
37             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
38             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39 
40             {SocketAction::SEND, "hd"},
41             {SocketAction::RECV, "HeapProfiler.reportHeapSnapshotProgress", ActionRule::CUSTOM_RULE,
42                 [] (auto recv, auto, auto) -> bool {
43                 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
44                 Result ret;
45                 std::unique_ptr<PtJson> params;
46                 ret = json->GetObject("params", &params);
47                 if (ret != Result::SUCCESS) {
48                     return false;
49                 }
50 
51                 int32_t done;
52                 ret = params->GetInt("done", &done);
53                 if (ret != Result::SUCCESS || done != 0) {
54                     return false;
55                 }
56 
57                 int32_t total;
58                 ret = params->GetInt("total", &total);
59                 if (ret != Result::SUCCESS) {
60                     return false;
61                 }
62                 return true;
63             }},
64             {SocketAction::RECV, "HeapProfiler.reportHeapSnapshotProgress", ActionRule::CUSTOM_RULE,
65                 [] (auto recv, auto, auto) -> bool {
66                 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
67                 Result ret;
68                 std::unique_ptr<PtJson> params;
69                 ret = json->GetObject("params", &params);
70                 if (ret != Result::SUCCESS) {
71                     return false;
72                 }
73 
74                 int32_t done;
75                 ret = params->GetInt("done", &done);
76                 if (ret != Result::SUCCESS) {
77                     return false;
78                 }
79 
80                 int32_t total;
81                 ret = params->GetInt("total", &total);
82                 if (ret != Result::SUCCESS) {
83                     return false;
84                 }
85                 return true;
86             }},
87             {SocketAction::RECV, "HeapProfiler.addHeapSnapshotChunk", ActionRule::CUSTOM_RULE,
88                 [] (auto recv, auto, auto) -> bool {
89                 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
90                 Result ret;
91                 std::unique_ptr<PtJson> params;
92                 ret = json->GetObject("params", &params);
93                 if (ret != Result::SUCCESS) {
94                     return false;
95                 }
96 
97                 std::string chunk;
98                 ret = params->GetString("chunk", &chunk);
99                 if (ret != Result::SUCCESS) {
100                     return false;
101                 }
102                 return true;
103             }},
104             // reply success and run
105             {SocketAction::SEND, "success"},
106         };
107     }
108 
109     std::pair<std::string, std::string> GetEntryPoint() override
110     {
111         return {pandaFile_, entryPoint_};
112     }
113     ~JsHeapProfilerDumpTest() = default;
114 
115 private:
116     std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
117     std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
118     std::string entryPoint_ = "_GLOBAL::func_main_0";
119 };
120 
GetJsHeapProfilerDumpTest()121 std::unique_ptr<TestActions> GetJsHeapProfilerDumpTest()
122 {
123     return std::make_unique<JsHeapProfilerDumpTest>();
124 }
125 }  // namespace panda::ecmascript::tooling::test
126 
127 #endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPPROFILER_DUMP_TEST_H
128