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#ifndef ECMASCRIPT_TOOLING_TEST_UTILS_TEST_UTIL_H
17#define ECMASCRIPT_TOOLING_TEST_UTILS_TEST_UTIL_H
18
19#include "tooling/test/client_utils/test_actions.h"
20
21#include "tooling/client/domain/debugger_client.h"
22#include "tooling/client/domain/runtime_client.h"
23#include "tooling/client/manager/domain_manager.h"
24#include "ecmascript/jspandafile/js_pandafile_manager.h"
25#include "ecmascript/debugger/js_debugger.h"
26#include "os/mutex.h"
27
28namespace OHOS::ArkCompiler::Toolchain {
29class WebSocketClient;
30} // namespace OHOS::ArkCompiler::Toolchain
31
32namespace panda::ecmascript::tooling::test {
33template<class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
34using CUnorderedMap = panda::ecmascript::CUnorderedMap<Key, T, Hash, KeyEqual>;
35using TestMap = CUnorderedMap<std::string, std::unique_ptr<TestActions>>;
36using namespace OHOS::ArkCompiler::Toolchain;
37
38#ifdef OHOS_PLATFORM
39#define DEBUGGER_LIBRARY "libark_inspector.z.so"
40#else
41#define DEBUGGER_LIBRARY "libark_inspector.so"
42#endif
43
44class TestUtil {
45public:
46    static void RegisterTest(const std::string &testName, std::unique_ptr<TestActions> test)
47    {
48        testMap_.insert({testName, std::move(test)});
49    }
50
51    static TestActions *GetTest(const std::string &name)
52    {
53        auto iter = std::find_if(testMap_.begin(), testMap_.end(), [&name](auto &it) {
54            return it.first == name;
55        });
56        if (iter != testMap_.end()) {
57            return iter->second.get();
58        }
59        LOG_DEBUGGER(FATAL) << "Test " << name << " not found";
60        return nullptr;
61    }
62
63    static TestMap &GetTests()
64    {
65        return testMap_;
66    }
67
68    static void ForkSocketClient(int port, const std::string &name);
69    static void HandleAcceptanceMessages(ActionInfo action, WebSocketClient &client, std::string &recv, bool &success);
70    static void SendMessage(ActionInfo action, std::string recv);
71
72private:
73    static void NotifyFail();
74    static void NotifySuccess();
75
76    static TestMap testMap_;
77};
78}  // namespace panda::ecmascript::tooling::test
79
80#endif  // ECMASCRIPT_TOOLING_TEST_UTILS_TEST_UTIL_H
81