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 "tooling/base/pt_events.h"
21#include "tooling/base/pt_types.h"
22#include "dispatcher.h"
23
24using namespace panda::ecmascript;
25using namespace panda::ecmascript::tooling;
26using ObjectType = RemoteObject::TypeName;
27using ObjectSubType = RemoteObject::SubTypeName;
28using ObjectClassName = RemoteObject::ClassName;
29
30namespace panda::test {
31class DebuggerEventsTest : public testing::Test {
32public:
33    static void SetUpTestCase()
34    {
35        GTEST_LOG_(INFO) << "SetUpTestCase";
36        Logger::InitializeStdLogging(Logger::Level::FATAL, LoggerComponentMaskAll);
37    }
38
39    static void TearDownTestCase()
40    {
41        Logger::InitializeStdLogging(Logger::Level::ERROR, LoggerComponentMaskAll);
42        GTEST_LOG_(INFO) << "TearDownCase";
43    }
44
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
52    void TearDown() override
53    {
54        TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
55    }
56
57protected:
58    EcmaVM *ecmaVm {nullptr};
59    EcmaHandleScope *scope {nullptr};
60    JSThread *thread {nullptr};
61};
62
63HWTEST_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
87HWTEST_F_L0(DebuggerEventsTest, PausedToJsonTest)
88{
89    Paused paused;
90    auto callFrames_ = std::vector<std::unique_ptr<CallFrame>>();
91    std::unique_ptr<CallFrame> callFrame = std::make_unique<CallFrame>();
92    std::unique_ptr<Location> location = std::make_unique<Location>();
93    location->SetScriptId(13).SetLine(16);
94
95    std::unique_ptr<RemoteObject> res = std::make_unique<RemoteObject>();
96    res->SetType("idle2");
97    callFrame->SetCallFrameId(55);
98    callFrame->SetLocation(std::move(location));
99    callFrame->SetThis(std::move(res));
100    callFrames_.emplace_back(std::move(callFrame));
101
102    paused.SetCallFrames(std::move(callFrames_))
103        .SetReason(PauseReason::EXCEPTION);
104
105    std::unique_ptr<PtJson> json = paused.ToJson();
106    std::unique_ptr<PtJson> params;
107    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
108
109    std::string reason;
110    ASSERT_EQ(params->GetString("reason", &reason), Result::SUCCESS);
111    EXPECT_EQ("exception", reason);
112    std::unique_ptr<PtJson> callFrames;
113    ASSERT_EQ(params->GetArray("callFrames", &callFrames), Result::SUCCESS);
114    ASSERT_NE(callFrames, nullptr);
115    EXPECT_EQ(callFrames->GetSize(), 1);
116
117    Paused paused1;
118    std::unique_ptr<RemoteObject> obj = std::make_unique<RemoteObject>();
119    paused1.SetData(std::move(obj)).SetReason(PauseReason::AMBIGUOUS).SetReason(PauseReason::ASSERT)
120        .SetReason(PauseReason::DEBUGCOMMAND).SetReason(PauseReason::DOM).SetReason(PauseReason::EVENTLISTENER)
121        .SetReason(PauseReason::OOM).SetReason(PauseReason::OTHER).SetReason(PauseReason::PROMISEREJECTION)
122        .SetReason(PauseReason::XHR).SetReason(PauseReason::BREAK_ON_START);
123    std::unique_ptr<PtJson> json1 = paused1.ToJson();
124    ASSERT_EQ(json1->GetObject("params", &params), Result::SUCCESS);
125}
126
127HWTEST_F_L0(DebuggerEventsTest, NativeCallingToJsonTest)
128{
129    NativeCalling nativeCalling;
130    nativeCalling.SetNativeAddress(nullptr);
131    std::unique_ptr<PtJson> json = nativeCalling.ToJson();
132
133    std::string method;
134    ASSERT_EQ(json->GetString("method", &method), Result::SUCCESS);
135    EXPECT_EQ(nativeCalling.GetName(), method);
136    std::unique_ptr<PtJson> params;
137    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
138
139    int64_t nativeAddress;
140    ASSERT_EQ(params->GetInt64("nativeAddress", &nativeAddress), Result::SUCCESS);
141}
142
143HWTEST_F_L0(DebuggerEventsTest, ResumedToJsonTest)
144{
145    Resumed resumed;
146    std::unique_ptr<PtJson> json = resumed.ToJson();
147
148    std::string method;
149    ASSERT_EQ(json->GetString("method", &method), Result::SUCCESS);
150    EXPECT_EQ(resumed.GetName(), method);
151    std::unique_ptr<PtJson> params;
152    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
153}
154
155HWTEST_F_L0(DebuggerEventsTest, ScriptFailedToParseToJsonTest)
156{
157    ScriptFailedToParse parsed;
158    parsed.SetScriptId(100)
159        .SetUrl("use/test.js")
160        .SetStartLine(0)
161        .SetStartColumn(4)
162        .SetEndLine(10)
163        .SetEndColumn(10)
164        .SetExecutionContextId(2)
165        .SetHash("hash0001")
166        .SetSourceMapURL("usr/")
167        .SetHasSourceURL(true)
168        .SetIsModule(true)
169        .SetLength(34)
170        .SetCodeOffset(432)
171        .SetScriptLanguage("JavaScript")
172        .SetEmbedderName("hh");
173
174    std::unique_ptr<PtJson> json = parsed.ToJson();
175    std::unique_ptr<PtJson> params;
176    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
177
178    std::string tmpStr;
179    ASSERT_EQ(params->GetString("scriptId", &tmpStr), Result::SUCCESS);
180    EXPECT_EQ("100", tmpStr);
181
182    ASSERT_EQ(params->GetString("url", &tmpStr), Result::SUCCESS);
183    EXPECT_EQ("use/test.js", tmpStr);
184
185    int tmpInt;
186    ASSERT_EQ(params->GetInt("startLine", &tmpInt), Result::SUCCESS);
187    EXPECT_EQ(tmpInt, 0);
188
189    ASSERT_EQ(params->GetInt("startColumn", &tmpInt), Result::SUCCESS);
190    EXPECT_EQ(tmpInt, 4);
191
192    ASSERT_EQ(params->GetInt("endLine", &tmpInt), Result::SUCCESS);
193    EXPECT_EQ(tmpInt, 10);
194
195    ASSERT_EQ(params->GetInt("endColumn", &tmpInt), Result::SUCCESS);
196    EXPECT_EQ(tmpInt, 10);
197
198    ASSERT_EQ(params->GetInt("executionContextId", &tmpInt), Result::SUCCESS);
199    EXPECT_EQ(tmpInt, 2);
200
201    ASSERT_EQ(params->GetString("hash", &tmpStr), Result::SUCCESS);
202    EXPECT_EQ("hash0001", tmpStr);
203
204    ASSERT_EQ(params->GetString("sourceMapURL", &tmpStr), Result::SUCCESS);
205    EXPECT_EQ("usr/", tmpStr);
206
207    bool tmpBool;
208    ASSERT_EQ(params->GetBool("hasSourceURL", &tmpBool), Result::SUCCESS);
209    ASSERT_TRUE(tmpBool);
210
211    ASSERT_EQ(params->GetBool("isModule", &tmpBool), Result::SUCCESS);
212    ASSERT_TRUE(tmpBool);
213
214    ASSERT_EQ(params->GetInt("length", &tmpInt), Result::SUCCESS);
215    EXPECT_EQ(tmpInt, 34);
216
217    ASSERT_EQ(params->GetInt("codeOffset", &tmpInt), Result::SUCCESS);
218    EXPECT_EQ(tmpInt, 432);
219
220    ASSERT_EQ(params->GetString("scriptLanguage", &tmpStr), Result::SUCCESS);
221    EXPECT_EQ("JavaScript", tmpStr);
222
223    ASSERT_EQ(params->GetString("embedderName", &tmpStr), Result::SUCCESS);
224    EXPECT_EQ("hh", tmpStr);
225
226    ScriptFailedToParse parsed1;
227    json = parsed1.ToJson();
228    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
229}
230
231HWTEST_F_L0(DebuggerEventsTest, ScriptParsedToJsonTest)
232{
233    ScriptParsed parsed;
234    parsed.SetScriptId(10)
235        .SetUrl("use/test.js")
236        .SetStartLine(0)
237        .SetStartColumn(4)
238        .SetEndLine(10)
239        .SetEndColumn(10)
240        .SetExecutionContextId(2)
241        .SetHash("hash0001")
242        .SetIsLiveEdit(true)
243        .SetSourceMapURL("usr/")
244        .SetHasSourceURL(true)
245        .SetIsModule(true)
246        .SetLength(34)
247        .SetCodeOffset(432)
248        .SetScriptLanguage("JavaScript")
249        .SetEmbedderName("hh");
250
251    std::unique_ptr<PtJson> json = parsed.ToJson();
252    std::unique_ptr<PtJson> params;
253    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
254
255    std::string tmpStr;
256    ASSERT_EQ(params->GetString("scriptId", &tmpStr), Result::SUCCESS);
257    EXPECT_EQ("10", tmpStr);
258
259    ASSERT_EQ(params->GetString("url", &tmpStr), Result::SUCCESS);
260    EXPECT_EQ("use/test.js", tmpStr);
261
262    int tmpInt;
263    ASSERT_EQ(params->GetInt("startLine", &tmpInt), Result::SUCCESS);
264    EXPECT_EQ(tmpInt, 0);
265
266    ASSERT_EQ(params->GetInt("startColumn", &tmpInt), Result::SUCCESS);
267    EXPECT_EQ(tmpInt, 4);
268
269    ASSERT_EQ(params->GetInt("endLine", &tmpInt), Result::SUCCESS);
270    EXPECT_EQ(tmpInt, 10);
271
272    ASSERT_EQ(params->GetInt("endColumn", &tmpInt), Result::SUCCESS);
273    EXPECT_EQ(tmpInt, 10);
274
275    ASSERT_EQ(params->GetInt("executionContextId", &tmpInt), Result::SUCCESS);
276    EXPECT_EQ(tmpInt, 2);
277
278    ASSERT_EQ(params->GetString("hash", &tmpStr), Result::SUCCESS);
279    EXPECT_EQ("hash0001", tmpStr);
280
281    bool tmpBool;
282    ASSERT_EQ(params->GetBool("isLiveEdit", &tmpBool), Result::SUCCESS);
283    ASSERT_TRUE(tmpBool);
284
285    ASSERT_EQ(params->GetString("sourceMapURL", &tmpStr), Result::SUCCESS);
286    EXPECT_EQ("usr/", tmpStr);
287
288    ASSERT_EQ(params->GetBool("hasSourceURL", &tmpBool), Result::SUCCESS);
289    ASSERT_TRUE(tmpBool);
290
291    ASSERT_EQ(params->GetBool("isModule", &tmpBool), Result::SUCCESS);
292    ASSERT_TRUE(tmpBool);
293
294    ASSERT_EQ(params->GetInt("length", &tmpInt), Result::SUCCESS);
295    EXPECT_EQ(tmpInt, 34);
296
297    ASSERT_EQ(params->GetInt("codeOffset", &tmpInt), Result::SUCCESS);
298    EXPECT_EQ(tmpInt, 432);
299
300    ASSERT_EQ(params->GetString("scriptLanguage", &tmpStr), Result::SUCCESS);
301    EXPECT_EQ("JavaScript", tmpStr);
302
303    ASSERT_EQ(params->GetString("embedderName", &tmpStr), Result::SUCCESS);
304    EXPECT_EQ("hh", tmpStr);
305}
306
307HWTEST_F_L0(DebuggerEventsTest, ConsoleProfileFinishedToJsonTest)
308{
309    ConsoleProfileFinished consoleProfileFinished;
310
311    auto location = std::make_unique<Location>();
312    location->SetScriptId(13).SetLine(20);
313    std::vector<std::unique_ptr<ProfileNode>> v;
314    auto profile = std::make_unique<Profile>();
315    profile->SetNodes(std::move(v))
316        .SetStartTime(0)
317        .SetEndTime(15)
318        .SetSamples(std::vector<int32_t>{})
319        .SetTimeDeltas(std::vector<int32_t>{});
320    consoleProfileFinished.SetId("11").SetLocation(std::move(location)).SetProfile(std::move(profile)).SetTitle("001");
321
322    std::unique_ptr<PtJson> json = consoleProfileFinished.ToJson();
323    std::unique_ptr<PtJson> params;
324    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
325
326    std::string tmpStr;
327    ASSERT_EQ(params->GetString("id", &tmpStr), Result::SUCCESS);
328    EXPECT_EQ("11", tmpStr);
329
330    std::unique_ptr<PtJson> tmpJson;
331    ASSERT_EQ(params->GetObject("location", &tmpJson), Result::SUCCESS);
332    ASSERT_EQ(params->GetObject("profile", &tmpJson), Result::SUCCESS);
333
334    ASSERT_EQ(params->GetString("title", &tmpStr), Result::SUCCESS);
335    EXPECT_EQ("001", tmpStr);
336
337    ConsoleProfileFinished consoleProfileFinished1;
338    auto location1 = std::make_unique<Location>();
339    auto profile1 = std::make_unique<Profile>();
340    consoleProfileFinished1.SetLocation(std::move(location1)).SetProfile(std::move(profile1));
341    json = consoleProfileFinished1.ToJson();
342    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
343}
344
345HWTEST_F_L0(DebuggerEventsTest, ConsoleProfileStartedToJsonTest)
346{
347    ConsoleProfileStarted consoleProfileStarted;
348
349    auto location = std::make_unique<Location>();
350    location->SetScriptId(17).SetLine(30);
351    consoleProfileStarted.SetId("12").SetLocation(std::move(location)).SetTitle("002");
352
353    std::unique_ptr<PtJson> json = consoleProfileStarted.ToJson();
354    std::unique_ptr<PtJson> params;
355    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
356
357    std::string tmpStr;
358    ASSERT_EQ(params->GetString("id", &tmpStr), Result::SUCCESS);
359    EXPECT_EQ("12", tmpStr);
360
361    std::unique_ptr<PtJson> tmpJson = consoleProfileStarted.GetLocation()->ToJson();
362    ASSERT_EQ(tmpJson->GetString("scriptId", &tmpStr), Result::SUCCESS);
363    EXPECT_EQ("17", tmpStr);
364    int tmpInt;
365    ASSERT_EQ(tmpJson->GetInt("lineNumber", &tmpInt), Result::SUCCESS);
366    EXPECT_EQ(tmpInt, 30);
367
368    ASSERT_EQ(params->GetString("title", &tmpStr), Result::SUCCESS);
369    EXPECT_EQ("002", tmpStr);
370
371    ConsoleProfileStarted consoleProfileStarted1;
372    auto location1 = std::make_unique<Location>();
373    consoleProfileStarted1.SetLocation(std::move(location1));
374    json = consoleProfileStarted1.ToJson();
375    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
376}
377
378HWTEST_F_L0(DebuggerEventsTest, PreciseCoverageDeltaUpdateToJsonTest)
379{
380    PreciseCoverageDeltaUpdate preciseCoverageDeltaUpdate;
381
382    std::vector<std::unique_ptr<ScriptCoverage>> v;
383    preciseCoverageDeltaUpdate.SetOccasion("percise")
384        .SetResult(std::move(v))
385        .SetTimestamp(77);
386
387    std::unique_ptr<PtJson> json = preciseCoverageDeltaUpdate.ToJson();
388    std::unique_ptr<PtJson> params;
389    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
390
391    int64_t tmpInt;
392    ASSERT_EQ(params->GetInt64("timestamp", &tmpInt), Result::SUCCESS);
393    EXPECT_EQ(tmpInt, 77);
394
395    std::string tmpStr;
396    ASSERT_EQ(params->GetString("occasion", &tmpStr), Result::SUCCESS);
397    EXPECT_EQ("percise", tmpStr);
398
399    std::unique_ptr<PtJson> tmpArray;
400    ASSERT_EQ(params->GetArray("result", &tmpArray), Result::SUCCESS);
401
402    PreciseCoverageDeltaUpdate preciseCoverageDeltaUpdate1;
403    std::unique_ptr<ScriptCoverage> scriptCoverage;
404    std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{
405          "scriptId":"1001",
406          "url":"url17",
407          "functions":[{"functionName":"Create0",
408          "ranges":[{"startOffset":0, "endOffset":13, "count":13}],
409          "isBlockCoverage":true}]}})";
410    scriptCoverage = ScriptCoverage::Create(DispatchRequest(msg).GetParams());
411    v.push_back(std::move(scriptCoverage));
412    preciseCoverageDeltaUpdate1.SetResult(std::move(v));
413    json = preciseCoverageDeltaUpdate1.ToJson();
414    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
415    ASSERT_EQ(params->GetArray("result", &tmpArray), Result::SUCCESS);
416}
417
418HWTEST_F_L0(DebuggerEventsTest, HeapStatsUpdateToJsonTest)
419{
420    HeapStatsUpdate heapStatsUpdate;
421    heapStatsUpdate.SetStatsUpdate(std::vector<int32_t> {});
422
423    std::unique_ptr<PtJson> json = heapStatsUpdate.ToJson();
424    std::unique_ptr<PtJson> params;
425    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
426
427    std::unique_ptr<PtJson> tmpArray;
428    ASSERT_EQ(params->GetArray("statsUpdate", &tmpArray), Result::SUCCESS);
429
430    HeapStatsUpdate heapStatsUpdate1;
431    heapStatsUpdate1.SetStatsUpdate(std::vector<int32_t> {1, 12, 20});
432    json = heapStatsUpdate1.ToJson();
433    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
434}
435
436HWTEST_F_L0(DebuggerEventsTest, LastSeenObjectIdToJsonTest)
437{
438    LastSeenObjectId lastSeenObjectId;
439    lastSeenObjectId.SetLastSeenObjectId(10).SetTimestamp(77);
440
441    std::unique_ptr<PtJson> json = lastSeenObjectId.ToJson();
442    std::unique_ptr<PtJson> params;
443    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
444
445    int64_t tmpInt64;
446    ASSERT_EQ(params->GetInt64("timestamp", &tmpInt64), Result::SUCCESS);
447    EXPECT_EQ(tmpInt64, 77);
448
449    int tmpInt;
450    ASSERT_EQ(params->GetInt("lastSeenObjectId", &tmpInt), Result::SUCCESS);
451    EXPECT_EQ(tmpInt, 10);
452}
453
454HWTEST_F_L0(DebuggerEventsTest, ReportHeapSnapshotProgressToJsonTest)
455{
456    ReportHeapSnapshotProgress reportHeapSnapshotProgress;
457    reportHeapSnapshotProgress.SetDone(10).SetTotal(100);
458
459    std::unique_ptr<PtJson> json = reportHeapSnapshotProgress.ToJson();
460    std::unique_ptr<PtJson> params;
461    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
462
463    int tmpInt;
464    ASSERT_EQ(params->GetInt("done", &tmpInt), Result::SUCCESS);
465    EXPECT_EQ(tmpInt, 10);
466
467    ASSERT_EQ(params->GetInt("total", &tmpInt), Result::SUCCESS);
468    EXPECT_EQ(tmpInt, 100);
469}
470
471HWTEST_F_L0(DebuggerEventsTest, BufferUsageToJsonTest)
472{
473    BufferUsage bufferUsage;
474    bufferUsage.SetPercentFull(17).SetEventCount(15).SetValue(12);
475
476    std::unique_ptr<PtJson> json = bufferUsage.ToJson();
477    std::unique_ptr<PtJson> params;
478    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
479
480    int tmpInt;
481    ASSERT_EQ(params->GetInt("percentFull", &tmpInt), Result::SUCCESS);
482    EXPECT_EQ(tmpInt, 17);
483
484    ASSERT_EQ(params->GetInt("eventCount", &tmpInt), Result::SUCCESS);
485    EXPECT_EQ(tmpInt, 15);
486
487    ASSERT_EQ(params->GetInt("value", &tmpInt), Result::SUCCESS);
488    EXPECT_EQ(tmpInt, 12);
489}
490
491HWTEST_F_L0(DebuggerEventsTest, DataCollectedToJsonTest)
492{
493    std::unique_ptr<std::vector<TraceEvent>> traceEvents = std::make_unique<std::vector<TraceEvent>>();
494    int64_t ts = 604898475815;
495    TraceEvent event("timeline", "UpdateCounters", "I", getpid(), 1415);
496    event.SetTs(ts);
497    event.SetTts(ts);
498    event.SetS("t");
499    std::string args = "{\"data\":{\"jsHeapSizeUsed\":" + std::to_string(1024) + "}}";
500    event.SetArgs(args);
501    traceEvents->emplace_back(event);
502
503    DataCollected dataCollected;
504    dataCollected.SetTraceEvents(std::move(traceEvents));
505
506    std::unique_ptr<PtJson> json = dataCollected.ToJson();
507    std::string method;
508    ASSERT_EQ(json->GetString("method", &method), Result::SUCCESS);
509    EXPECT_EQ(dataCollected.GetName(), method);
510
511    std::unique_ptr<PtJson> params;
512    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
513
514    std::unique_ptr<PtJson> tmpArray;
515    ASSERT_EQ(params->GetArray("value", &tmpArray), Result::SUCCESS);
516}
517
518HWTEST_F_L0(DebuggerEventsTest, TracingCompleteToJsonTest)
519{
520    TracingComplete tracingComplete;
521    auto traceFormat = std::make_unique<StreamFormat>();
522    auto streamCompression = std::make_unique<StreamCompression>();
523    tracingComplete.SetDataLossOccurred(true)
524                    .SetTraceFormat(std::move(traceFormat))
525                    .SetStreamCompression(std::move(streamCompression));
526
527    std::unique_ptr<PtJson> json = tracingComplete.ToJson();
528    std::unique_ptr<PtJson> params;
529    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
530
531    bool tmpBool;
532    ASSERT_EQ(params->GetBool("dataLossOccurred", &tmpBool), Result::SUCCESS);
533
534    std::string tmpStr;
535    ASSERT_EQ(params->GetString("traceFormat", &tmpStr), Result::SUCCESS);
536    ASSERT_EQ(params->GetString("streamCompression", &tmpStr), Result::SUCCESS);
537}
538
539HWTEST_F_L0(DebuggerEventsTest, MixedStackToJsonTest)
540{
541    MixedStack mixedStack;
542    auto callFrames_ = std::vector<std::unique_ptr<CallFrame>>();
543    std::unique_ptr<CallFrame> callFrame = std::make_unique<CallFrame>();
544    std::unique_ptr<Location> location = std::make_unique<Location>();
545    location->SetScriptId(13).SetLine(16);
546
547    std::unique_ptr<RemoteObject> res = std::make_unique<RemoteObject>();
548    res->SetType("idle2");
549
550    callFrame->SetCallFrameId(55);
551    callFrame->SetLocation(std::move(location));
552    callFrame->SetThis(std::move(res));
553    callFrames_.emplace_back(std::move(callFrame));
554    mixedStack.SetCallFrames(std::move(callFrames_));
555
556    std::vector<void *> p;
557    int a = 5;
558    void * ptr = &a;
559    p.emplace_back(std::move(ptr));
560    mixedStack.SetNativePointers(std::move(p));
561    std::unique_ptr<PtJson> json = mixedStack.ToJson();
562
563    std::string method;
564    ASSERT_EQ(json->GetString("method", &method), Result::SUCCESS);
565    EXPECT_EQ(mixedStack.GetName(), method);
566    std::unique_ptr<PtJson> params;
567    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
568    std::unique_ptr<PtJson> callFrames;
569    ASSERT_EQ(params->GetArray("callFrames", &callFrames), Result::SUCCESS);
570    ASSERT_NE(callFrames, nullptr);
571    EXPECT_EQ(callFrames->GetSize(), 1);
572    std::unique_ptr<PtJson> nativePointer;
573    ASSERT_EQ(params->GetArray("nativePointer", &nativePointer), Result::SUCCESS);
574    ASSERT_NE(nativePointer, nullptr);
575    EXPECT_EQ(nativePointer->GetSize(), 1);
576}
577
578HWTEST_F_L0(DebuggerEventsTest, AddHeapSnapshotChunkToJsonTest)
579{
580    AddHeapSnapshotChunk addHeapSnapshotChunk;
581    addHeapSnapshotChunk.SetChunk("Chunk0001");
582
583    std::unique_ptr<PtJson> json = addHeapSnapshotChunk.ToJson();
584    std::unique_ptr<PtJson> params;
585    ASSERT_EQ(json->GetObject("params", &params), Result::SUCCESS);
586    std::string method;
587    ASSERT_EQ(json->GetString("method", &method), Result::SUCCESS);
588    EXPECT_EQ(addHeapSnapshotChunk.GetName(), method);
589    std::string tmpStr;
590    ASSERT_EQ(params->GetString("Chunk", &tmpStr), Result::SUCCESS);
591    EXPECT_EQ("Chunk0001", tmpStr);
592}
593}  // namespace panda::test