1 /*
2 * Copyright (c) 2025 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 <list.h>
17 #include "thermal_hookmgr.h"
18 #include <thermal_log.h>
19 #include <gtest/gtest.h>
20
21 using namespace OHOS;
22 using namespace PowerMgr;
23 using namespace testing;
24 using namespace ext;
25 namespace {
26 class ThermalHookMgrTest : public Test {
27 public:
28 inline static HOOK_INFO info {};
29 };
30
TestHook(const HOOK_INFO * info,void * context)31 int TestHook(const HOOK_INFO* info, void* context)
32 {
33 ThermalHookMgrTest::info = *info;
34 return *static_cast<int*>(context);
35 }
36
37 HWTEST_F(ThermalHookMgrTest, ThermalHookMgrTest000, TestSize.Level0)
38 {
39 THERMAL_HILOGI(LABEL_TEST, "ThermalHookMgrTest000 function start!");
40 auto hookMgr = GetThermalHookMgr();
41 ASSERT_NE(hookMgr, nullptr);
42 auto hookMgr2 = GetThermalHookMgr();
43 EXPECT_EQ(hookMgr, hookMgr2);
44 THERMAL_HILOGI(LABEL_TEST, "ThermalHookMgrTest000 function end!");
45 }
46
47 // execution with TRAVERSE_STOP_WHEN_ERROR
48 HWTEST_F(ThermalHookMgrTest, ThermalHookMgrTest001, TestSize.Level0)
49 {
50 THERMAL_HILOGI(LABEL_TEST, "ThermalHookMgrTest001 function start!");
51 auto hookMgr = GetThermalHookMgr();
52 ASSERT_NE(hookMgr, nullptr);
53 HOOK_EXEC_OPTIONS options {TRAVERSE_STOP_WHEN_ERROR, nullptr, nullptr};
54 HookMgrAdd(hookMgr, 0, 0, TestHook);
55 for (int retval : {0, -1, 1}) {
56 int ret = HookMgrExecute(hookMgr, 0, &retval, &options);
57 EXPECT_EQ(info.stage, 0);
58 EXPECT_EQ(info.prio, 0);
59 EXPECT_EQ(info.hook, TestHook);
60 EXPECT_EQ(ret, retval);
61 }
62 THERMAL_HILOGI(LABEL_TEST, "ThermalHookMgrTest001 function end!");
63 }
64
65 // task with greater priority number is executed later.
66 HWTEST_F(ThermalHookMgrTest, ThermalHookMgrTest002, TestSize.Level0)
67 {
68 THERMAL_HILOGI(LABEL_TEST, "ThermalHookMgrTest001 function start!");
69 auto hookMgr = GetThermalHookMgr();
70 ASSERT_NE(hookMgr, nullptr);
71 HookMgrAdd(hookMgr, 0, 0, TestHook);
72 HookMgrAdd(hookMgr, 0, 1, TestHook);
73 int retval = 0;
74 HookMgrExecute(hookMgr, 0, &retval, nullptr);
75 EXPECT_EQ(info.prio, 1);
76 THERMAL_HILOGI(LABEL_TEST, "ThermalHookMgrTest001 function end!");
77 }
78 } // namespace