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 #ifndef RESSCHED_SERVICES_RESSCHEDMGR_TEST_UNITTEST_INCLUDE_MOCK_H
17 #define RESSCHED_SERVICES_RESSCHEDMGR_TEST_UNITTEST_INCLUDE_MOCK_H
18 
19 #include <algorithm>
20 #include <dlfcn.h>
21 #include <iostream>
22 #include <memory>
23 #include <thread>
24 #include "datetime_ex.h"
25 #include "event_runner.h"
26 #include "plugin_mgr.h"
27 
28 namespace OHOS {
29 namespace ResourceSchedule {
30 class MockPluginMgr : public PluginMgr {
31 public:
32     MockPluginMgr() = default;
33     const std::string TEST_PREFIX_RES_SWITCH_PATH =
34         "/data/test/resource/resschedfwk/parseswitch/res_sched_plugin_switch.xml";
35     const std::string TEST_PREFIX_RES_PATH = "/data/test/resource/resschedfwk/parseconfig/res_sched_config.xml";
36     const std::string MOCK_RUNNER_NAME = "mockRssDispatcher";
37     const int32_t DISPATCH_WARNING_TIME = 1; // ms
38     const int32_t DISPATCH_TIME_OUT = 10; // ms
39     enum : int32_t {
40         SWITCH_NULL,
41         LOAD_CONFIG_FAIL,
42         LOAD_CUST_CONFIG_FAIL,
43         INIT_SUCCESS
44     };
45     int32_t initStatus;
46 
Init()47     void Init()
48     {
49         if (pluginSwitch_) {
50             initStatus = SWITCH_NULL;
51             return;
52         }
53 
54         if (!pluginSwitch_) {
55             pluginSwitch_ = std::make_unique<PluginSwitch>();
56             std::vector<std::string> contents;
57             GetConfigContent(-1, TEST_PREFIX_RES_SWITCH_PATH, contents);
58             bool loadRet = false;
59             for (auto content : contents) {
60                 if (pluginSwitch_->LoadFromConfigContent(content)) {
61                     loadRet = true;
62                 }
63             }
64             if (!loadRet) {
65                 initStatus = LOAD_CONFIG_FAIL;
66                 return;
67             }
68         }
69 
70         if (!configReader_) {
71             configReader_ = std::make_unique<ConfigReader>();
72             std::vector<std::string> contents;
73             PluginMgr::GetInstance().GetConfigContent(-1, TEST_PREFIX_RES_PATH, contents);
74             bool loadRet = false;
75             for (auto content : contents) {
76                 if (configReader_->LoadFromConfigContent(content)) {
77                     loadRet = true;
78                 }
79             }
80             if (!loadRet) {
81                 initStatus = LOAD_CONFIG_FAIL;
82                 return;
83             }
84         }
85         initStatus = INIT_SUCCESS;
86     }
87 };
88 } // namespace ResourceSchedule
89 } // namespace OHOS
90 
91 #endif // RESSCHED_SERVICES_RESSCHEDMGR_TEST_UNITTEST_INCLUDEMOCK_H
92