• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/tests/test_helper.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "debugger_service.h"
19 #include "protocol_handler.h"
20 #include "ecmascript/debugger/js_debugger_manager.h"
21 using namespace panda::ecmascript;
22 using namespace panda::ecmascript::tooling;
23 
24 namespace panda::test {
25 class DebuggerServiceTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase()
28     {
29         GTEST_LOG_(INFO) << "SetUpTestCase";
30     }
31 
TearDownTestCase()32     static void TearDownTestCase()
33     {
34         GTEST_LOG_(INFO) << "TearDownCase";
35     }
36 
SetUp()37     void SetUp() override
38     {
39         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
40     }
41 
TearDown()42     void TearDown() override
43     {
44         TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
45     }
46 
47 protected:
48     EcmaVM *ecmaVm {nullptr};
49     EcmaHandleScope *scope {nullptr};
50     JSThread *thread {nullptr};
51 };
52 
HWTEST_F_L0(DebuggerServiceTest,InitializeDebuggerTest)53 HWTEST_F_L0(DebuggerServiceTest, InitializeDebuggerTest)
54 {
55     ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
56     ASSERT_TRUE(handler == nullptr);
57     InitializeDebugger(ecmaVm, nullptr);
58     handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
59     ASSERT_TRUE(handler != nullptr);
60     InitializeDebugger(ecmaVm, nullptr);
61     ASSERT_TRUE(handler != nullptr);
62 }
63 
HWTEST_F_L0(DebuggerServiceTest,UninitializeDebuggerTest)64 HWTEST_F_L0(DebuggerServiceTest, UninitializeDebuggerTest)
65 {
66     UninitializeDebugger(ecmaVm);
67     InitializeDebugger(ecmaVm, nullptr);
68     ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
69     ASSERT_TRUE(handler != nullptr);
70     UninitializeDebugger(ecmaVm);
71     handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
72     ASSERT_TRUE(handler == nullptr);
73 }
74 
HWTEST_F_L0(DebuggerServiceTest,OnMessageTest)75 HWTEST_F_L0(DebuggerServiceTest, OnMessageTest)
76 {
77     ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
78     OnMessage(ecmaVm, "");
79     ASSERT_TRUE(handler == nullptr);
80     std::string result = "";
81     std::function<void(const void*, const std::string &)> callback =
82         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
83     InitializeDebugger(ecmaVm, callback);
84     std::string msg = std::string() + R"({"id":0,"method":"Tracing.Test","params":{}})";
85     OnMessage(ecmaVm, msg + "");
86     ProcessMessage(ecmaVm);
87     ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
88 }
89 
HWTEST_F_L0(DebuggerServiceTest,WaitForDebuggerTest)90 HWTEST_F_L0(DebuggerServiceTest, WaitForDebuggerTest)
91 {
92     ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
93     WaitForDebugger(ecmaVm);
94     ASSERT_TRUE(handler == nullptr);
95 }
96 
HWTEST_F_L0(DebuggerServiceTest,ProcessMessageTest)97 HWTEST_F_L0(DebuggerServiceTest, ProcessMessageTest)
98 {
99     ProtocolHandler *handler = ecmaVm->GetJsDebuggerManager()->GetDebuggerHandler();
100     ProcessMessage(ecmaVm);
101     ASSERT_TRUE(handler == nullptr);
102     std::string result = "";
103     std::function<void(const void*, const std::string &)> callback =
104         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
105     InitializeDebugger(ecmaVm, callback);
106     std::string msg = std::string() + R"({"id":0,"method":"Tracing.end","params":{}})";
107     OnMessage(ecmaVm, msg + "");
108     ProcessMessage(ecmaVm);
109     ASSERT_TRUE(result.find("\"id\":0,") != std::string::npos);
110 }
111 
HWTEST_F_L0(DebuggerServiceTest,GetDispatchStatusTest)112 HWTEST_F_L0(DebuggerServiceTest, GetDispatchStatusTest)
113 {
114     ProtocolHandler::DispatchStatus status = ProtocolHandler::DispatchStatus(GetDispatchStatus(ecmaVm));
115     ASSERT_TRUE(status == ProtocolHandler::DispatchStatus::UNKNOWN);
116     std::string result = "";
117     std::function<void(const void*, const std::string &)> callback =
118         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
119     InitializeDebugger(ecmaVm, callback);
120     status = ProtocolHandler::DispatchStatus(GetDispatchStatus(ecmaVm));
121     ASSERT_TRUE(status == ProtocolHandler::DispatchStatus::DISPATCHED);
122 }
123 }  // namespace panda::test