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_WATCH_OTHER_TYPE_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_OTHER_TYPE_TEST_H
18
19 #include "tooling/dynamic/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsWatchOtherTypeTest : public TestActions {
23 public:
JsWatchOtherTypeTest()24 JsWatchOtherTypeTest()
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 watch_variable.js
34 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35 // break on start
36 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
37 [](auto recv, auto, auto) -> bool {
38 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
39 DebuggerClient debuggerClient(0);
40 debuggerClient.RecvReply(std::move(json));
41 return true;
42 }},
43
44 // set breakpoint
45 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "watch_variable.js 327"},
46 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
47
48 // hit breakpoint after resume first time
49 {SocketAction::SEND, "resume"},
50 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
51 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
52 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
53
54 // watch
55 {SocketAction::SEND, "watch generator0"},
56 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
57 [this](auto recv, auto, auto) -> bool {
58 return RecvWatchFunctionInfo(recv, "Generator", "function* generator0( { [js code] }");
59 }},
60 {SocketAction::SEND, "watch regexp0"},
61 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
62 [this](auto recv, auto, auto) -> bool {
63 return RecvWatchSubtypeInfo(recv, "regexp", "RegExp", "/^\\d+\\.\\d+$/i");
64 }},
65 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66 {SocketAction::SEND, "watch iterator0"},
67 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
68 [this](auto recv, auto, auto) -> bool {
69 return RecvWatchFunctionInfo(recv, "Function",
70 "function [Symbol.iterator]( { [native code] }");
71 }},
72
73 {SocketAction::SEND, "resume"},
74 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
75 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
76 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
77
78 // reply success and run
79 {SocketAction::SEND, "success"},
80 {SocketAction::SEND, "resume"},
81 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
82 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
83 };
84 }
85
RecvWatchSubtypeInfo(std::string recv,std::string var_type,std::string var_class,std::string var_info)86 bool RecvWatchSubtypeInfo(std::string recv, std::string var_type, std::string var_class,
87 std::string var_info)
88 {
89 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
90 Result ret;
91 int id = 0;
92 ret = json->GetInt("id", &id);
93 if (ret != Result::SUCCESS) {
94 return false;
95 }
96
97 std::unique_ptr<PtJson> result = nullptr;
98 ret = json->GetObject("result", &result);
99 if (ret != Result::SUCCESS) {
100 return false;
101 }
102
103 std::unique_ptr<PtJson> watchResult = nullptr;
104 ret = result->GetObject("result", &watchResult);
105 if (ret != Result::SUCCESS) {
106 return false;
107 }
108
109 std::string type = "";
110 ret = watchResult->GetString("type", &type);
111 if (ret != Result::SUCCESS || type != "object") {
112 return false;
113 }
114
115 std::string subtype = "";
116 ret = watchResult->GetString("subtype", &subtype);
117 if (ret != Result::SUCCESS || subtype != var_type) {
118 return false;
119 }
120
121 std::string className = "";
122 ret = watchResult->GetString("className", &className);
123 if (ret != Result::SUCCESS || className != var_class) {
124 return false;
125 }
126
127 std::string value = "";
128 ret = watchResult->GetString("unserializableValue", &value);
129 if (ret != Result::SUCCESS || value != var_info) {
130 return false;
131 }
132
133 std::string description = "";
134 ret = watchResult->GetString("description", &description);
135 if (ret != Result::SUCCESS || description != var_info) {
136 return false;
137 }
138
139 DebuggerClient debuggerClient(0);
140 debuggerClient.RecvReply(std::move(json));
141 return true;
142 }
143
RecvWatchFunctionInfo(std::string recv,std::string func_class,std::string func_info)144 bool RecvWatchFunctionInfo(std::string recv, std::string func_class, std::string func_info)
145 {
146 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
147 Result ret;
148 int id = 0;
149 ret = json->GetInt("id", &id);
150 if (ret != Result::SUCCESS) {
151 return false;
152 }
153
154 std::unique_ptr<PtJson> result = nullptr;
155 ret = json->GetObject("result", &result);
156 if (ret != Result::SUCCESS) {
157 return false;
158 }
159
160 std::unique_ptr<PtJson> watchResult = nullptr;
161 ret = result->GetObject("result", &watchResult);
162 if (ret != Result::SUCCESS) {
163 return false;
164 }
165
166 std::string type = "";
167 ret = watchResult->GetString("type", &type);
168 if (ret != Result::SUCCESS || type != "function") {
169 return false;
170 }
171
172 std::string className = "";
173 ret = watchResult->GetString("className", &className);
174 if (ret != Result::SUCCESS || className != func_class) {
175 return false;
176 }
177
178 std::string value = "";
179 ret = watchResult->GetString("unserializableValue", &value);
180 if (ret != Result::SUCCESS || value != func_info) {
181 return false;
182 }
183
184 std::string description = "";
185 ret = watchResult->GetString("description", &description);
186 if (ret != Result::SUCCESS || description != func_info) {
187 return false;
188 }
189
190 DebuggerClient debuggerClient(0);
191 debuggerClient.RecvReply(std::move(json));
192 return true;
193 }
194
GetEntryPoint()195 std::pair<std::string, std::string> GetEntryPoint() override
196 {
197 return {pandaFile_, entryPoint_};
198 }
199 ~JsWatchOtherTypeTest() = default;
200
201 private:
202 std::string pandaFile_ = DEBUGGER_ABC_DIR "watch_variable.abc";
203 std::string sourceFile_ = DEBUGGER_JS_DIR "watch_variable.js";
204 std::string entryPoint_ = "watch_variable";
205 };
206
GetJsWatchOtherTypeTest()207 std::unique_ptr<TestActions> GetJsWatchOtherTypeTest()
208 {
209 return std::make_unique<JsWatchOtherTypeTest>();
210 }
211 } // namespace panda::ecmascript::tooling::test
212
213 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_OTHER_TYPE_TEST_H
214