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#include "agent/dom_impl.h"
17#include "ecmascript/tests/test_helper.h"
18#include "protocol_handler.h"
19
20using namespace panda::ecmascript;
21using namespace panda::ecmascript::tooling;
22
23namespace panda::test {
24class DomImplTest : public testing::Test {
25public:
26    static void SetUpTestCase()
27    {
28        GTEST_LOG_(INFO) << "SetUpTestCase";
29    }
30
31    static void TearDownTestCase()
32    {
33        GTEST_LOG_(INFO) << "TearDownCase";
34    }
35
36    void SetUp() override
37    {
38        TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39    }
40
41    void TearDown() override
42    {
43        TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44    }
45
46protected:
47    EcmaVM *ecmaVm {nullptr};
48    EcmaHandleScope *scope {nullptr};
49    JSThread *thread {nullptr};
50};
51
52HWTEST_F_L0(DomImplTest, DispatchTest_001)
53{
54    std::string outStrForCallbackCheck = "";
55    std::function<void(const void*, const std::string &)> callback =
56        [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
57            outStrForCallbackCheck = inStrOfReply;};
58    ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
59
60    auto domImpl = std::make_unique<DomImpl>();
61    auto dispatcherImpl = std::make_unique<DomImpl::DispatcherImpl>(protocolChannel, std::move(domImpl));
62    std::string msg = std::string() + R"({"id":0,"method":"Debugger.disable","params":{}})";
63    DispatchRequest request(msg);
64    dispatcherImpl->Dispatch(request);
65    EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
66}
67
68HWTEST_F_L0(DomImplTest, DispatchTest_002)
69{
70    std::string outStrForCallbackCheck = "";
71    std::function<void(const void*, const std::string &)> callback =
72        [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
73            outStrForCallbackCheck = inStrOfReply;};
74    ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
75
76    auto domImpl = std::make_unique<DomImpl>();
77    auto dispatcherImpl = std::make_unique<DomImpl::DispatcherImpl>(protocolChannel, std::move(domImpl));
78    std::string msg = "DomImplTestCommand";
79    DispatchRequest request(msg);
80    dispatcherImpl->Dispatch(request);
81    EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":-1,"result":{"code":1,"message":"Unknown method: "}})");
82}
83}  // namespace panda::test