• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/js_array.h"
17 #include "ecmascript/js_tagged_value-inl.h"
18 #include "ecmascript/object_factory.h"
19 #include "ecmascript/tests/test_helper.h"
20 #include "base/pt_events.h"
21 #include "base/pt_types.h"
22 #include "dispatcher.h"
23 
24 using namespace panda::ecmascript;
25 using namespace panda::ecmascript::tooling;
26 using ObjectType = RemoteObject::TypeName;
27 using ObjectSubType = RemoteObject::SubTypeName;
28 using ObjectClassName = RemoteObject::ClassName;
29 
30 namespace panda::test {
31 class DebuggerEventsTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         GTEST_LOG_(INFO) << "SetUpTestCase";
36         Logger::InitializeStdLogging(Logger::Level::FATAL, LoggerComponentMaskAll);
37     }
38 
TearDownTestCase()39     static void TearDownTestCase()
40     {
41         Logger::InitializeStdLogging(Logger::Level::ERROR, LoggerComponentMaskAll);
42         GTEST_LOG_(INFO) << "TearDownCase";
43     }
44 
SetUp()45     void SetUp() override
46     {
47         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
48         // Main logic is JSON parser, so not need trigger GC to decrease execute time
49         ecmaVm->SetEnableForceGC(false);
50     }
51 
TearDown()52     void TearDown() override
53     {
54         TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
55     }
56 
57 protected:
58     EcmaVM *ecmaVm {nullptr};
59     EcmaHandleScope *scope {nullptr};
60     JSThread *thread {nullptr};
61 };
62 
HWTEST_F_L0(DebuggerEventsTest,BreakpointResolvedToJsonTest)63 HWTEST_F_L0(DebuggerEventsTest, BreakpointResolvedToJsonTest)
64 {
65     BreakpointResolved breakpointResolved;
66 
67     auto location = std::make_unique<Location>();
68     location->SetScriptId(2).SetLine(99);
69     breakpointResolved.SetBreakpointId("00").SetLocation(std::move(location));
70 
71     std::unique_ptr<PtJson> json;
72     ASSERT_EQ(breakpointResolved.ToJson()->GetObject("params", &json), Result::SUCCESS);
73     std::string breakpointId;
74     ASSERT_EQ(json->GetString("breakpointId", &breakpointId), Result::SUCCESS);
75     EXPECT_EQ(breakpointId, "00");
76 
77     std::unique_ptr<PtJson> locationJson;
78     ASSERT_EQ(json->GetObject("location", &locationJson), Result::SUCCESS);
79     std::string scriptId;
80     ASSERT_EQ(locationJson->GetString("scriptId", &scriptId), Result::SUCCESS);
81     EXPECT_EQ(scriptId, "2");
82     int32_t lineNumber;
83     ASSERT_EQ(locationJson->GetInt("lineNumber", &lineNumber), Result::SUCCESS);
84     EXPECT_EQ(lineNumber, 99);
85 }
86 
HWTEST_F_L0(DebuggerEventsTest,PausedToJsonTest)87 HWTEST_F_L0(DebuggerEventsTest, PausedToJsonTest)
88 {
89     Paused paused;
90     std::vector<std::unique_ptr<CallFrame>> v;
91     paused.SetCallFrames(std::move(v))
92         .SetReason(PauseReason::EXCEPTION);
93 
94     std::unique_ptr<PtJson> json = paused.ToJson();
95     std::unique_ptr<PtJson> params;
96     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
97 
98     std::string reason;
99     ASSERT_EQ(params->GetString("reason", &reason), Result::SUCCESS);
100     EXPECT_EQ("exception", reason);
101     std::unique_ptr<PtJson> callFrames;
102     ASSERT_EQ(params->GetArray("callFrames", &callFrames), Result::SUCCESS);
103 }
104 
HWTEST_F_L0(DebuggerEventsTest,ResumedToJsonTest)105 HWTEST_F_L0(DebuggerEventsTest, ResumedToJsonTest)
106 {
107     Resumed resumed;
108     std::unique_ptr<PtJson> json = resumed.ToJson();
109 
110     std::string method;
111     ASSERT_EQ(json->GetString("method", &method), Result::SUCCESS);
112     EXPECT_EQ(resumed.GetName(), method);
113     std::unique_ptr<PtJson> params;
114     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
115 }
116 
HWTEST_F_L0(DebuggerEventsTest,ScriptFailedToParseToJsonTest)117 HWTEST_F_L0(DebuggerEventsTest, ScriptFailedToParseToJsonTest)
118 {
119     ScriptFailedToParse parsed;
120     parsed.SetScriptId(100)
121         .SetUrl("use/test.js")
122         .SetStartLine(0)
123         .SetStartColumn(4)
124         .SetEndLine(10)
125         .SetEndColumn(10)
126         .SetExecutionContextId(2)
127         .SetHash("hash0001")
128         .SetSourceMapURL("usr/")
129         .SetHasSourceURL(true)
130         .SetIsModule(true)
131         .SetLength(34)
132         .SetCodeOffset(432)
133         .SetScriptLanguage("JavaScript")
134         .SetEmbedderName("hh");
135 
136     std::unique_ptr<PtJson> json = parsed.ToJson();
137     std::unique_ptr<PtJson> params;
138     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
139 
140     std::string tmpStr;
141     ASSERT_EQ(params->GetString("scriptId", &tmpStr), Result::SUCCESS);
142     EXPECT_EQ("100", tmpStr);
143 
144     ASSERT_EQ(params->GetString("url", &tmpStr), Result::SUCCESS);
145     EXPECT_EQ("use/test.js", tmpStr);
146 
147     int tmpInt;
148     ASSERT_EQ(params->GetInt("startLine", &tmpInt), Result::SUCCESS);
149     EXPECT_EQ(tmpInt, 0);
150 
151     ASSERT_EQ(params->GetInt("startColumn", &tmpInt), Result::SUCCESS);
152     EXPECT_EQ(tmpInt, 4);
153 
154     ASSERT_EQ(params->GetInt("endLine", &tmpInt), Result::SUCCESS);
155     EXPECT_EQ(tmpInt, 10);
156 
157     ASSERT_EQ(params->GetInt("endColumn", &tmpInt), Result::SUCCESS);
158     EXPECT_EQ(tmpInt, 10);
159 
160     ASSERT_EQ(params->GetInt("executionContextId", &tmpInt), Result::SUCCESS);
161     EXPECT_EQ(tmpInt, 2);
162 
163     ASSERT_EQ(params->GetString("hash", &tmpStr), Result::SUCCESS);
164     EXPECT_EQ("hash0001", tmpStr);
165 
166     ASSERT_EQ(params->GetString("sourceMapURL", &tmpStr), Result::SUCCESS);
167     EXPECT_EQ("usr/", tmpStr);
168 
169     bool tmpBool;
170     ASSERT_EQ(params->GetBool("hasSourceURL", &tmpBool), Result::SUCCESS);
171     ASSERT_TRUE(tmpBool);
172 
173     ASSERT_EQ(params->GetBool("isModule", &tmpBool), Result::SUCCESS);
174     ASSERT_TRUE(tmpBool);
175 
176     ASSERT_EQ(params->GetInt("length", &tmpInt), Result::SUCCESS);
177     EXPECT_EQ(tmpInt, 34);
178 
179     ASSERT_EQ(params->GetInt("codeOffset", &tmpInt), Result::SUCCESS);
180     EXPECT_EQ(tmpInt, 432);
181 
182     ASSERT_EQ(params->GetString("scriptLanguage", &tmpStr), Result::SUCCESS);
183     EXPECT_EQ("JavaScript", tmpStr);
184 
185     ASSERT_EQ(params->GetString("embedderName", &tmpStr), Result::SUCCESS);
186     EXPECT_EQ("hh", tmpStr);
187 }
188 
HWTEST_F_L0(DebuggerEventsTest,ScriptParsedToJsonTest)189 HWTEST_F_L0(DebuggerEventsTest, ScriptParsedToJsonTest)
190 {
191     ScriptParsed parsed;
192     parsed.SetScriptId(10)
193         .SetUrl("use/test.js")
194         .SetStartLine(0)
195         .SetStartColumn(4)
196         .SetEndLine(10)
197         .SetEndColumn(10)
198         .SetExecutionContextId(2)
199         .SetHash("hash0001")
200         .SetIsLiveEdit(true)
201         .SetSourceMapURL("usr/")
202         .SetHasSourceURL(true)
203         .SetIsModule(true)
204         .SetLength(34)
205         .SetCodeOffset(432)
206         .SetScriptLanguage("JavaScript")
207         .SetEmbedderName("hh");
208 
209     std::unique_ptr<PtJson> json = parsed.ToJson();
210     std::unique_ptr<PtJson> params;
211     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
212 
213     std::string tmpStr;
214     ASSERT_EQ(params->GetString("scriptId", &tmpStr), Result::SUCCESS);
215     EXPECT_EQ("10", tmpStr);
216 
217     ASSERT_EQ(params->GetString("url", &tmpStr), Result::SUCCESS);
218     EXPECT_EQ("use/test.js", tmpStr);
219 
220     int tmpInt;
221     ASSERT_EQ(params->GetInt("startLine", &tmpInt), Result::SUCCESS);
222     EXPECT_EQ(tmpInt, 0);
223 
224     ASSERT_EQ(params->GetInt("startColumn", &tmpInt), Result::SUCCESS);
225     EXPECT_EQ(tmpInt, 4);
226 
227     ASSERT_EQ(params->GetInt("endLine", &tmpInt), Result::SUCCESS);
228     EXPECT_EQ(tmpInt, 10);
229 
230     ASSERT_EQ(params->GetInt("endColumn", &tmpInt), Result::SUCCESS);
231     EXPECT_EQ(tmpInt, 10);
232 
233     ASSERT_EQ(params->GetInt("executionContextId", &tmpInt), Result::SUCCESS);
234     EXPECT_EQ(tmpInt, 2);
235 
236     ASSERT_EQ(params->GetString("hash", &tmpStr), Result::SUCCESS);
237     EXPECT_EQ("hash0001", tmpStr);
238 
239     bool tmpBool;
240     ASSERT_EQ(params->GetBool("isLiveEdit", &tmpBool), Result::SUCCESS);
241     ASSERT_TRUE(tmpBool);
242 
243     ASSERT_EQ(params->GetString("sourceMapURL", &tmpStr), Result::SUCCESS);
244     EXPECT_EQ("usr/", tmpStr);
245 
246     ASSERT_EQ(params->GetBool("hasSourceURL", &tmpBool), Result::SUCCESS);
247     ASSERT_TRUE(tmpBool);
248 
249     ASSERT_EQ(params->GetBool("isModule", &tmpBool), Result::SUCCESS);
250     ASSERT_TRUE(tmpBool);
251 
252     ASSERT_EQ(params->GetInt("length", &tmpInt), Result::SUCCESS);
253     EXPECT_EQ(tmpInt, 34);
254 
255     ASSERT_EQ(params->GetInt("codeOffset", &tmpInt), Result::SUCCESS);
256     EXPECT_EQ(tmpInt, 432);
257 
258     ASSERT_EQ(params->GetString("scriptLanguage", &tmpStr), Result::SUCCESS);
259     EXPECT_EQ("JavaScript", tmpStr);
260 
261     ASSERT_EQ(params->GetString("embedderName", &tmpStr), Result::SUCCESS);
262     EXPECT_EQ("hh", tmpStr);
263 }
264 
HWTEST_F_L0(DebuggerEventsTest,ConsoleProfileFinishedToJsonTest)265 HWTEST_F_L0(DebuggerEventsTest, ConsoleProfileFinishedToJsonTest)
266 {
267     ConsoleProfileFinished consoleProfileFinished;
268 
269     auto location = std::make_unique<Location>();
270     location->SetScriptId(13).SetLine(20);
271     std::vector<std::unique_ptr<ProfileNode>> v;
272     auto profile = std::make_unique<Profile>();
273     profile->SetNodes(std::move(v))
274         .SetStartTime(0)
275         .SetEndTime(15)
276         .SetSamples(std::vector<int32_t>{})
277         .SetTimeDeltas(std::vector<int32_t>{});
278     consoleProfileFinished.SetId("11").SetLocation(std::move(location)).SetProfile(std::move(profile)).SetTitle("001");
279 
280     std::unique_ptr<PtJson> json = consoleProfileFinished.ToJson();
281     std::unique_ptr<PtJson> params;
282     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
283 
284     std::string tmpStr;
285     ASSERT_EQ(params->GetString("id", &tmpStr), Result::SUCCESS);
286     EXPECT_EQ("11", tmpStr);
287 
288     std::unique_ptr<PtJson> tmpJson;
289     ASSERT_EQ(params->GetObject("location", &tmpJson), Result::SUCCESS);
290     ASSERT_EQ(params->GetObject("profile", &tmpJson), Result::SUCCESS);
291 
292     ASSERT_EQ(params->GetString("title", &tmpStr), Result::SUCCESS);
293     EXPECT_EQ("001", tmpStr);
294 }
295 
HWTEST_F_L0(DebuggerEventsTest,ConsoleProfileStartedToJsonTest)296 HWTEST_F_L0(DebuggerEventsTest, ConsoleProfileStartedToJsonTest)
297 {
298     ConsoleProfileStarted consoleProfileStarted;
299 
300     auto location = std::make_unique<Location>();
301     location->SetScriptId(17).SetLine(30);
302     consoleProfileStarted.SetId("12").SetLocation(std::move(location)).SetTitle("002");
303 
304     std::unique_ptr<PtJson> json = consoleProfileStarted.ToJson();
305     std::unique_ptr<PtJson> params;
306     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
307 
308     std::string tmpStr;
309     ASSERT_EQ(params->GetString("id", &tmpStr), Result::SUCCESS);
310     EXPECT_EQ("12", tmpStr);
311 
312     std::unique_ptr<PtJson> tmpJson = consoleProfileStarted.GetLocation()->ToJson();
313     ASSERT_EQ(tmpJson->GetString("scriptId", &tmpStr), Result::SUCCESS);
314     EXPECT_EQ("17", tmpStr);
315     int tmpInt;
316     ASSERT_EQ(tmpJson->GetInt("lineNumber", &tmpInt), Result::SUCCESS);
317     EXPECT_EQ(tmpInt, 30);
318 
319     ASSERT_EQ(params->GetString("title", &tmpStr), Result::SUCCESS);
320     EXPECT_EQ("002", tmpStr);
321 }
322 
HWTEST_F_L0(DebuggerEventsTest,PreciseCoverageDeltaUpdateToJsonTest)323 HWTEST_F_L0(DebuggerEventsTest, PreciseCoverageDeltaUpdateToJsonTest)
324 {
325     PreciseCoverageDeltaUpdate preciseCoverageDeltaUpdate;
326 
327     std::vector<std::unique_ptr<ScriptCoverage>> v;
328     preciseCoverageDeltaUpdate.SetOccasion("percise")
329         .SetResult(std::move(v))
330         .SetTimestamp(77);
331 
332     std::unique_ptr<PtJson> json = preciseCoverageDeltaUpdate.ToJson();
333     std::unique_ptr<PtJson> params;
334     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
335 
336     int64_t tmpInt;
337     ASSERT_EQ(params->GetInt64("timestamp", &tmpInt), Result::SUCCESS);
338     EXPECT_EQ(tmpInt, 77);
339 
340     std::string tmpStr;
341     ASSERT_EQ(params->GetString("occasion", &tmpStr), Result::SUCCESS);
342     EXPECT_EQ("percise", tmpStr);
343 
344     std::unique_ptr<PtJson> tmpArray;
345     ASSERT_EQ(params->GetArray("result", &tmpArray), Result::SUCCESS);
346 }
347 
HWTEST_F_L0(DebuggerEventsTest,HeapStatsUpdateToJsonTest)348 HWTEST_F_L0(DebuggerEventsTest, HeapStatsUpdateToJsonTest)
349 {
350     HeapStatsUpdate heapStatsUpdate;
351     heapStatsUpdate.SetStatsUpdate(std::vector<int32_t> {});
352 
353     std::unique_ptr<PtJson> json = heapStatsUpdate.ToJson();
354     std::unique_ptr<PtJson> params;
355     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
356 
357     std::unique_ptr<PtJson> tmpArray;
358     ASSERT_EQ(params->GetArray("statsUpdate", &tmpArray), Result::SUCCESS);
359 }
360 
HWTEST_F_L0(DebuggerEventsTest,LastSeenObjectIdToJsonTest)361 HWTEST_F_L0(DebuggerEventsTest, LastSeenObjectIdToJsonTest)
362 {
363     LastSeenObjectId lastSeenObjectId;
364     lastSeenObjectId.SetLastSeenObjectId(10).SetTimestamp(77);
365 
366     std::unique_ptr<PtJson> json = lastSeenObjectId.ToJson();
367     std::unique_ptr<PtJson> params;
368     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
369 
370     int64_t tmpInt64;
371     ASSERT_EQ(params->GetInt64("timestamp", &tmpInt64), Result::SUCCESS);
372     EXPECT_EQ(tmpInt64, 77);
373 
374     int tmpInt;
375     ASSERT_EQ(params->GetInt("lastSeenObjectId", &tmpInt), Result::SUCCESS);
376     EXPECT_EQ(tmpInt, 10);
377 }
378 
HWTEST_F_L0(DebuggerEventsTest,ReportHeapSnapshotProgressToJsonTest)379 HWTEST_F_L0(DebuggerEventsTest, ReportHeapSnapshotProgressToJsonTest)
380 {
381     ReportHeapSnapshotProgress reportHeapSnapshotProgress;
382     reportHeapSnapshotProgress.SetDone(10).SetTotal(100);
383 
384     std::unique_ptr<PtJson> json = reportHeapSnapshotProgress.ToJson();
385     std::unique_ptr<PtJson> params;
386     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
387 
388     int tmpInt;
389     ASSERT_EQ(params->GetInt("done", &tmpInt), Result::SUCCESS);
390     EXPECT_EQ(tmpInt, 10);
391 
392     ASSERT_EQ(params->GetInt("total", &tmpInt), Result::SUCCESS);
393     EXPECT_EQ(tmpInt, 100);
394 }
395 
HWTEST_F_L0(DebuggerEventsTest,BufferUsageToJsonTest)396 HWTEST_F_L0(DebuggerEventsTest, BufferUsageToJsonTest)
397 {
398     BufferUsage bufferUsage;
399     bufferUsage.SetPercentFull(17).SetEventCount(15).SetValue(12);
400 
401     std::unique_ptr<PtJson> json = bufferUsage.ToJson();
402     std::unique_ptr<PtJson> params;
403     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
404 
405     int tmpInt;
406     ASSERT_EQ(params->GetInt("percentFull", &tmpInt), Result::SUCCESS);
407     EXPECT_EQ(tmpInt, 17);
408 
409     ASSERT_EQ(params->GetInt("eventCount", &tmpInt), Result::SUCCESS);
410     EXPECT_EQ(tmpInt, 15);
411 
412     ASSERT_EQ(params->GetInt("value", &tmpInt), Result::SUCCESS);
413     EXPECT_EQ(tmpInt, 12);
414 }
415 
HWTEST_F_L0(DebuggerEventsTest,TracingCompleteToJsonTest)416 HWTEST_F_L0(DebuggerEventsTest, TracingCompleteToJsonTest)
417 {
418     TracingComplete tracingComplete;
419     auto traceFormat = std::make_unique<StreamFormat>();
420     auto streamCompression = std::make_unique<StreamCompression>();
421     tracingComplete.SetDataLossOccurred(true)
422                     .SetTraceFormat(std::move(traceFormat))
423                     .SetStreamCompression(std::move(streamCompression));
424 
425     std::unique_ptr<PtJson> json = tracingComplete.ToJson();
426     std::unique_ptr<PtJson> params;
427     ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
428 
429     bool tmpBool;
430     ASSERT_EQ(params->GetBool("dataLossOccurred", &tmpBool), Result::SUCCESS);
431 
432     std::string tmpStr;
433     ASSERT_EQ(params->GetString("traceFormat", &tmpStr), Result::SUCCESS);
434     ASSERT_EQ(params->GetString("streamCompression", &tmpStr), Result::SUCCESS);
435 }
436 }  // namespace panda::test