1 /*
2 * Copyright (c) 2023 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_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsWatchTest : public TestActions {
23 public:
JsWatchTest()24 JsWatchTest()
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 sample.js
34 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35 // break on start
36 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37 // set breakpoint
38 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "sample.js 22"},
39 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40 // hit breakpoint after resume first time
41 {SocketAction::SEND, "resume"},
42 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
43 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
45 // hit breakpoint after resume second time
46 {SocketAction::SEND, "resume"},
47 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
48 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
49 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [] (auto, auto, auto) -> bool {
50 return true; }, TestCase::WATCH},
51 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
52 return RecvWatchDescription(recv);
53 }},
54 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
55 return RecvWatchType(recv);
56 }},
57 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "sample.js 33"},
58 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
59 // hit breakpoint after resume first time
60 {SocketAction::SEND, "resume"},
61 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
62 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
63 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [] (auto, auto, auto) -> bool {
64 return true;
65 }, TestCase::WATCH},
66 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
67 return RecvWatchType(recv);
68 }},
69 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
70 return RecvWatchObject(recv);
71 }, TestCase::WATCH_OBJECT},
72 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
73 return RecvWatchDescriptionByJack(recv);
74 }},
75 // reply success and run
76 {SocketAction::SEND, "success"},
77 {SocketAction::SEND, "resume"},
78 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
79 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
80 };
81 }
82
RecvWatchDescription(std::string recv)83 bool RecvWatchDescription(std::string recv)
84 {
85 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
86 Result ret;
87 int id;
88 ret = json->GetInt("id", &id);
89 if (ret != Result::SUCCESS) {
90 return false;
91 }
92
93 std::unique_ptr<PtJson> result = nullptr;
94 ret = json->GetObject("result", &result);
95 if (ret != Result::SUCCESS) {
96 return false;
97 }
98
99 std::unique_ptr<PtJson> watchResult = nullptr;
100 ret = result->GetObject("result", &watchResult);
101 if (ret != Result::SUCCESS) {
102 return false;
103 }
104
105 std::string numResult;
106 ret = watchResult->GetString("description", &numResult);
107 if (ret != Result::SUCCESS) {
108 return false;
109 }
110
111 if (numResult != "5") {
112 return false;
113 }
114 return true;
115 }
116
RecvWatchType(std::string recv)117 bool RecvWatchType(std::string recv)
118 {
119 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
120 Result ret;
121 int id;
122 ret = json->GetInt("id", &id);
123 if (ret != Result::SUCCESS) {
124 return false;
125 }
126
127 std::unique_ptr<PtJson> result = nullptr;
128 ret = json->GetObject("result", &result);
129 if (ret != Result::SUCCESS) {
130 return false;
131 }
132
133 std::unique_ptr<PtJson> watchResult = nullptr;
134 ret = result->GetObject("result", &watchResult);
135 if (ret != Result::SUCCESS) {
136 return false;
137 }
138
139 std::string type;
140 ret = watchResult->GetString("type", &type);
141 if (ret != Result::SUCCESS) {
142 return false;
143 }
144
145 if (type != "undefined") {
146 return false;
147 }
148 return true;
149 }
150
RecvWatchObject(std::string recv)151 bool RecvWatchObject(std::string recv)
152 {
153 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
154 Result ret;
155 int id;
156 ret = json->GetInt("id", &id);
157 if (ret != Result::SUCCESS) {
158 return false;
159 }
160
161 std::unique_ptr<PtJson> result = nullptr;
162 ret = json->GetObject("result", &result);
163 if (ret != Result::SUCCESS) {
164 return false;
165 }
166
167 std::unique_ptr<PtJson> watchResult = nullptr;
168 ret = result->GetObject("result", &watchResult);
169 if (ret != Result::SUCCESS) {
170 return false;
171 }
172
173 std::string type;
174 ret = watchResult->GetString("type", &type);
175 if (ret != Result::SUCCESS) {
176 return false;
177 }
178
179 if (type != "object") {
180 return false;
181 }
182 return true;
183 }
184
RecvWatchDescriptionByJack(std::string recv)185 bool RecvWatchDescriptionByJack(std::string recv)
186 {
187 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
188 Result ret;
189 std::unique_ptr<PtJson> result = nullptr;
190 ret = json->GetObject("result", &result);
191 if (ret != Result::SUCCESS) {
192 return false;
193 }
194
195 std::unique_ptr<PtJson> watchResult = nullptr;
196 ret = result->GetArray("result", &watchResult);
197 if (ret != Result::SUCCESS) {
198 return false;
199 }
200
201 std::unique_ptr<PtJson> value = nullptr;
202 ret = watchResult->Get(0)->GetObject("value", &value);
203 if (ret != Result::SUCCESS) {
204 return false;
205 }
206
207 std::string description;
208 ret = value->GetString("description", &description);
209 if (ret != Result::SUCCESS) {
210 return false;
211 }
212 if (description != "jack") {
213 return false;
214 }
215 return true;
216 }
217
GetEntryPoint()218 std::pair<std::string, std::string> GetEntryPoint() override
219 {
220 return {pandaFile_, entryPoint_};
221 }
222 ~JsWatchTest() = default;
223
224 private:
225 std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
226 std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
227 std::string entryPoint_ = "_GLOBAL::func_main_0";
228 };
229
GetJsWatchTest()230 std::unique_ptr<TestActions> GetJsWatchTest()
231 {
232 return std::make_unique<JsWatchTest>();
233 }
234 } // namespace panda::ecmascript::tooling::test
235
236 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_TEST_H
237