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