1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#include "agent/overlay_impl.h"
17e509ee18Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
18e509ee18Sopenharmony_ci#include "protocol_handler.h"
19e509ee18Sopenharmony_ci
20e509ee18Sopenharmony_ciusing namespace panda::ecmascript;
21e509ee18Sopenharmony_ciusing namespace panda::ecmascript::tooling;
22e509ee18Sopenharmony_ci
23e509ee18Sopenharmony_cinamespace panda::test {
24e509ee18Sopenharmony_ciclass OverlayImplTest : public testing::Test {
25e509ee18Sopenharmony_cipublic:
26e509ee18Sopenharmony_ci    static void SetUpTestCase()
27e509ee18Sopenharmony_ci    {
28e509ee18Sopenharmony_ci        GTEST_LOG_(INFO) << "SetUpTestCase";
29e509ee18Sopenharmony_ci    }
30e509ee18Sopenharmony_ci
31e509ee18Sopenharmony_ci    static void TearDownTestCase()
32e509ee18Sopenharmony_ci    {
33e509ee18Sopenharmony_ci        GTEST_LOG_(INFO) << "TearDownCase";
34e509ee18Sopenharmony_ci    }
35e509ee18Sopenharmony_ci
36e509ee18Sopenharmony_ci    void SetUp() override
37e509ee18Sopenharmony_ci    {
38e509ee18Sopenharmony_ci        TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39e509ee18Sopenharmony_ci    }
40e509ee18Sopenharmony_ci
41e509ee18Sopenharmony_ci    void TearDown() override
42e509ee18Sopenharmony_ci    {
43e509ee18Sopenharmony_ci        TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44e509ee18Sopenharmony_ci    }
45e509ee18Sopenharmony_ci
46e509ee18Sopenharmony_ciprotected:
47e509ee18Sopenharmony_ci    EcmaVM *ecmaVm {nullptr};
48e509ee18Sopenharmony_ci    EcmaHandleScope *scope {nullptr};
49e509ee18Sopenharmony_ci    JSThread *thread {nullptr};
50e509ee18Sopenharmony_ci};
51e509ee18Sopenharmony_ci
52e509ee18Sopenharmony_ciHWTEST_F_L0(OverlayImplTest, DispatchTest_001)
53e509ee18Sopenharmony_ci{
54e509ee18Sopenharmony_ci    std::string outStrForCallbackCheck = "";
55e509ee18Sopenharmony_ci    std::function<void(const void*, const std::string &)> callback =
56e509ee18Sopenharmony_ci        [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
57e509ee18Sopenharmony_ci            outStrForCallbackCheck = inStrOfReply;};
58e509ee18Sopenharmony_ci    ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
59e509ee18Sopenharmony_ci
60e509ee18Sopenharmony_ci    auto overlayImpl = std::make_unique<OverlayImpl>();
61e509ee18Sopenharmony_ci    auto dispatcherImpl = std::make_unique<OverlayImpl::DispatcherImpl>(protocolChannel, std::move(overlayImpl));
62e509ee18Sopenharmony_ci    std::string msg = std::string() + R"({"id":0,"method":"Debugger.disable","params":{}})";
63e509ee18Sopenharmony_ci    DispatchRequest request(msg);
64e509ee18Sopenharmony_ci    dispatcherImpl->Dispatch(request);
65e509ee18Sopenharmony_ci    EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
66e509ee18Sopenharmony_ci}
67e509ee18Sopenharmony_ci
68e509ee18Sopenharmony_ciHWTEST_F_L0(OverlayImplTest, DispatchTest_002)
69e509ee18Sopenharmony_ci{
70e509ee18Sopenharmony_ci    std::string outStrForCallbackCheck = "";
71e509ee18Sopenharmony_ci    std::function<void(const void*, const std::string &)> callback =
72e509ee18Sopenharmony_ci        [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
73e509ee18Sopenharmony_ci            outStrForCallbackCheck = inStrOfReply;};
74e509ee18Sopenharmony_ci    ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
75e509ee18Sopenharmony_ci
76e509ee18Sopenharmony_ci    auto overlayImpl = std::make_unique<OverlayImpl>();
77e509ee18Sopenharmony_ci    auto dispatcherImpl = std::make_unique<OverlayImpl::DispatcherImpl>(protocolChannel, std::move(overlayImpl));
78e509ee18Sopenharmony_ci    std::string msg = "OverlayImplTestCommand";
79e509ee18Sopenharmony_ci    DispatchRequest request(msg);
80e509ee18Sopenharmony_ci    dispatcherImpl->Dispatch(request);
81e509ee18Sopenharmony_ci    EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":-1,"result":{"code":1,"message":"Unknown method: "}})");
82e509ee18Sopenharmony_ci}
83e509ee18Sopenharmony_ci}  // namespace panda::test