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_CLOSURE_VARIABLE_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_CLOSURE_VARIABLE_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsWatchClosureVariableTest : public TestActions {
23 public:
JsWatchClosureVariableTest()24 JsWatchClosureVariableTest()
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 closure_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 "closure_variable.js 20"},
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::CUSTOM_RULE,
53 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 19); }},
54
55 {SocketAction::SEND, "watch a"},
56 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
57 [this](auto recv, auto, auto) -> bool { return RecvWatchInfo(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
RecvHitBreakInfo(std::string recv,int line)67 bool RecvHitBreakInfo(std::string recv, int line)
68 {
69 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
70 Result ret;
71 std::string method = "";
72 ret = json->GetString("method", &method);
73 if (ret != Result::SUCCESS || method != "Debugger.paused") {
74 return false;
75 }
76
77 std::unique_ptr<PtJson> params = nullptr;
78 ret = json->GetObject("params", ¶ms);
79 if (ret != Result::SUCCESS) {
80 return false;
81 }
82
83 std::unique_ptr<PtJson> hitBreakpoints = nullptr;
84 ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
85 if (ret != Result::SUCCESS) {
86 return false;
87 }
88
89 std::string breakpoint = "";
90 breakpoint = hitBreakpoints->Get(0)->GetString();
91 if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
92 breakpoint.find(std::to_string(line)) == std::string::npos) {
93 return false;
94 }
95 return true;
96 }
RecvWatchInfo(std::string recv)97 bool RecvWatchInfo(std::string recv)
98 {
99 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
100 Result ret;
101 int id;
102 ret = json->GetInt("id", &id);
103 if (ret != Result::SUCCESS) {
104 return false;
105 }
106
107 std::unique_ptr<PtJson> result = nullptr;
108 ret = json->GetObject("result", &result);
109 if (ret != Result::SUCCESS) {
110 return false;
111 }
112
113 std::unique_ptr<PtJson> watchResult = nullptr;
114 ret = result->GetObject("result", &watchResult);
115 if (ret != Result::SUCCESS) {
116 return false;
117 }
118
119 std::string type = "";
120 ret = watchResult->GetString("type", &type);
121 if (ret != Result::SUCCESS || type != "number") {
122 return false;
123 }
124
125 std::string value = "";
126 ret = watchResult->GetString("unserializableValue", &value);
127 if (ret != Result::SUCCESS || value != "1") {
128 return false;
129 }
130
131 std::string description = "";
132 ret = watchResult->GetString("description", &description);
133 if (ret != Result::SUCCESS || description != "1") {
134 return false;
135 }
136 return true;
137 }
138
GetEntryPoint()139 std::pair<std::string, std::string> GetEntryPoint() override
140 {
141 return {pandaFile_, entryPoint_};
142 }
143 ~JsWatchClosureVariableTest() = default;
144
145 private:
146 std::string pandaFile_ = DEBUGGER_ABC_DIR "closure_variable.abc";
147 std::string sourceFile_ = DEBUGGER_JS_DIR "closure_variable.js";
148 std::string entryPoint_ = "_GLOBAL::func_main_0";
149 };
150
GetJsWatchClosureVariableTest()151 std::unique_ptr<TestActions> GetJsWatchClosureVariableTest()
152 {
153 return std::make_unique<JsWatchClosureVariableTest>();
154 }
155 } // namespace panda::ecmascript::tooling::test
156
157 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_CLOSURE_VARIABLE_TEST_H
158