• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SET_TYPE_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_SET_TYPE_TEST_H
18 
19 #include "tooling/dynamic/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsWatchSetTypeTest : public TestActions {
23 public:
JsWatchSetTypeTest()24     JsWatchSetTypeTest()
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 array0"},
56             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
57                 [this](auto recv, auto, auto) -> bool {
58                     return RecvWatchSubtypeInfo(recv, "array", "Array", "Array(2)");
59                 }},
60             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
61             {SocketAction::SEND, "watch arraybuffer0"},
62             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
63                 [this](auto recv, auto, auto) -> bool {
64                     return RecvWatchSubtypeInfo(recv, "arraybuffer", "Arraybuffer", "Arraybuffer(24)");
65                 }},
66             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
67             {SocketAction::SEND, "watch uint8array0"},
68             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
69                 [this](auto recv, auto, auto) -> bool { return RecvWatchObjectInfo(recv, "Uint8Array(24)"); }},
70             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
71             {SocketAction::SEND, "watch dataview0"},
72             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
73                 [this](auto recv, auto, auto) -> bool {
74                     return RecvWatchSubtypeInfo(recv, "dataview", "Dataview", "DataView(24)");
75                 }},
76             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
77             {SocketAction::SEND, "watch typedarray0"},
78             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
79                 [this](auto recv, auto, auto) -> bool { return RecvWatchObjectInfo(recv, "Uint8Array(0)"); }},
80             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
81             {SocketAction::SEND, "watch sharedarraybuffer0"},
82             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
83                 [this](auto recv, auto, auto) -> bool {
84                     return RecvWatchObjectInfo(recv, "SharedArrayBuffer(32)");
85                 }},
86             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
87 
88             {SocketAction::SEND, "watch weakref0"},
89             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
90                 [this](auto recv, auto, auto) -> bool { return RecvWatchObjectInfo(recv, "WeakRef {}"); }},
91             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
92 
93             {SocketAction::SEND, "watch obj0"},
94             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
95                 [this](auto recv, auto, auto) -> bool { return RecvWatchObjectInfo(recv, "Object"); }},
96             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
97 
98             {SocketAction::SEND, "watch map0"},
99             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
100                 [this](auto recv, auto, auto) -> bool {
101                     return RecvWatchSubtypeInfo(recv, "map", "Map", "Map(0)");
102                 }},
103             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
104             {SocketAction::SEND, "watch map1"},
105             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
106                 [this](auto recv, auto, auto) -> bool {
107                     return RecvWatchSubtypeInfo(recv, "map", "Map", "Map(0)");
108                 }},
109             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
110             {SocketAction::SEND, "watch map2"},
111             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
112                 [this](auto recv, auto, auto) -> bool {
113                     std::string map_info = "Map(2) {1 => 'hello', 2 => 'world'}";
114                     return RecvWatchSubtypeInfo(recv, "map", "Map", map_info);
115                 }},
116             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
117             {SocketAction::SEND, "watch map3"},
118             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
119                 [this](auto recv, auto, auto) -> bool {
120                     std::string map_info = "Map(1) {NaN => 'NaN'}";
121                     return RecvWatchSubtypeInfo(recv, "map", "Map", map_info);
122                 }},
123             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
124             {SocketAction::SEND, "watch map4"},
125             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
126                 [this](auto recv, auto, auto) -> bool {
127                     return RecvWatchSubtypeInfo(recv, "map", "Map", "Map(0)");
128                 }},
129             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
130             {SocketAction::SEND, "watch map5"},
131             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
132                 [this](auto recv, auto, auto) -> bool {
133                     std::string map_info = "Map(4) {0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three'}";
134                     return RecvWatchSubtypeInfo(recv, "map", "Map", map_info);
135                 }},
136             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
137 
138             {SocketAction::SEND, "watch set0"},
139             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
140                 [this](auto recv, auto, auto) -> bool {
141                     return RecvWatchSubtypeInfo(recv, "set", "Set", "Set(0)");
142                 }},
143             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
144             {SocketAction::SEND, "watch set1"},
145             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
146                 [this](auto recv, auto, auto) -> bool {
147                     std::string set_info = "Set(1) {1}";
148                     return RecvWatchSubtypeInfo(recv, "set", "Set", set_info);
149                 }},
150             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
151             {SocketAction::SEND, "watch set2"},
152             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
153                 [this](auto recv, auto, auto) -> bool {
154                     std::string set_info = "Set(7) {'h', 'e', 'l', 'o', 'w', ...}";
155                     return RecvWatchSubtypeInfo(recv, "set", "Set", set_info);
156                 }},
157             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
158             {SocketAction::SEND, "watch set3"},
159             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
160                 [this](auto recv, auto, auto) -> bool {
161                     std::string set_info = "Set(1) {Object}";
162                     return RecvWatchSubtypeInfo(recv, "set", "Set", set_info);
163                 }},
164             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
165             {SocketAction::SEND, "watch set4"},
166             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
167                 [this](auto recv, auto, auto) -> bool {
168                     return RecvWatchSubtypeInfo(recv, "set", "Set", "Set(0)");
169                 }},
170             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
171             {SocketAction::SEND, "watch set5"},
172             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
173                 [this](auto recv, auto, auto) -> bool {
174                     std::string set_info = "Set(2) {'Apple', 'Banana'}";
175                     return RecvWatchSubtypeInfo(recv, "set", "Set", set_info);
176                 }},
177             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
178 
179             {SocketAction::SEND, "resume"},
180             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
181             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
182             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
183 
184             // reply success and run
185             {SocketAction::SEND, "success"},
186             {SocketAction::SEND, "resume"},
187             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
188             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
189         };
190     }
191 
RecvWatchSubtypeInfo(std::string recv,std::string set_type,std::string set_class,std::string set_info)192     bool RecvWatchSubtypeInfo(std::string recv, std::string set_type, std::string set_class,
193                               std::string set_info)
194     {
195         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
196         Result ret;
197         int id = 0;
198         ret = json->GetInt("id", &id);
199         if (ret != Result::SUCCESS) {
200             return false;
201         }
202 
203         std::unique_ptr<PtJson> result = nullptr;
204         ret = json->GetObject("result", &result);
205         if (ret != Result::SUCCESS) {
206             return false;
207         }
208 
209         std::unique_ptr<PtJson> watchResult = nullptr;
210         ret = result->GetObject("result", &watchResult);
211         if (ret != Result::SUCCESS) {
212             return false;
213         }
214 
215         std::string type = "";
216         ret = watchResult->GetString("type", &type);
217         if (ret != Result::SUCCESS || type != "object") {
218             return false;
219         }
220 
221         std::string subtype = "";
222         ret = watchResult->GetString("subtype", &subtype);
223         if (ret != Result::SUCCESS || subtype != set_type) {
224             return false;
225         }
226 
227         std::string className = "";
228         ret = watchResult->GetString("className", &className);
229         if (ret != Result::SUCCESS || className != set_class) {
230             return false;
231         }
232 
233         std::string value = "";
234         ret = watchResult->GetString("unserializableValue", &value);
235         if (ret != Result::SUCCESS || value != set_info) {
236             return false;
237         }
238 
239         std::string description = "";
240         ret = watchResult->GetString("description", &description);
241         if (ret != Result::SUCCESS || description != set_info) {
242             return false;
243         }
244 
245         DebuggerClient debuggerClient(0);
246         debuggerClient.RecvReply(std::move(json));
247         return true;
248     }
249 
RecvWatchObjectInfo(std::string recv,std::string set_info)250     bool RecvWatchObjectInfo(std::string recv, std::string set_info)
251     {
252         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
253         Result ret;
254         int id = 0;
255         ret = json->GetInt("id", &id);
256         if (ret != Result::SUCCESS) {
257             return false;
258         }
259 
260         std::unique_ptr<PtJson> result = nullptr;
261         ret = json->GetObject("result", &result);
262         if (ret != Result::SUCCESS) {
263             return false;
264         }
265 
266         std::unique_ptr<PtJson> watchResult = nullptr;
267         ret = result->GetObject("result", &watchResult);
268         if (ret != Result::SUCCESS) {
269             return false;
270         }
271 
272         std::string type = "";
273         ret = watchResult->GetString("type", &type);
274         if (ret != Result::SUCCESS || type != "object") {
275             return false;
276         }
277 
278         std::string className = "";
279         ret = watchResult->GetString("className", &className);
280         if (ret != Result::SUCCESS || className != "Object") {
281             return false;
282         }
283 
284         std::string value = "";
285         ret = watchResult->GetString("unserializableValue", &value);
286         if (ret != Result::SUCCESS || value != set_info) {
287             return false;
288         }
289 
290         std::string description = "";
291         ret = watchResult->GetString("description", &description);
292         if (ret != Result::SUCCESS || description != set_info) {
293             return false;
294         }
295 
296         DebuggerClient debuggerClient(0);
297         debuggerClient.RecvReply(std::move(json));
298         return true;
299     }
300 
GetEntryPoint()301     std::pair<std::string, std::string> GetEntryPoint() override
302     {
303         return {pandaFile_, entryPoint_};
304     }
305     ~JsWatchSetTypeTest() = default;
306 
307 private:
308     std::string pandaFile_ = DEBUGGER_ABC_DIR "watch_variable.abc";
309     std::string sourceFile_ = DEBUGGER_JS_DIR "watch_variable.js";
310     std::string entryPoint_ = "watch_variable";
311 };
312 
GetJsWatchSetTypeTest()313 std::unique_ptr<TestActions> GetJsWatchSetTypeTest()
314 {
315     return std::make_unique<JsWatchSetTypeTest>();
316 }
317 }  // namespace panda::ecmascript::tooling::test
318 
319 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_SET_TYPE_TEST_H
320