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 "device_power_action.h"
17
18 #include "init_reboot.h"
19 #include "list.h"
20 #include "power_hookmgr.h"
21 #include "power_log.h"
22 #include <gtest/gtest.h>
23 #include <string>
24
25 using namespace testing;
26 using namespace ext;
27 using namespace OHOS;
28 using namespace PowerMgr;
29 namespace {
30 int g_retval = 0;
31 bool g_invoked = false;
32 std::string g_cmdResult;
33 } // namespace
34
DoRebootExt(const char * mode,const char * option)35 int DoRebootExt(const char* mode, const char* option)
36 {
37 g_cmdResult = mode;
38 return 0;
39 }
40
HookMgrExecute(HOOK_MGR * hookMgr,int stage,void * context,const HOOK_EXEC_OPTIONS * options)41 int HookMgrExecute(HOOK_MGR* hookMgr, int stage, void* context, const HOOK_EXEC_OPTIONS* options)
42 {
43 g_invoked = true;
44 return g_retval;
45 }
46
47 class DevicePowerActionTest : public DevicePowerAction, public Test {};
48
49 namespace {
50 HWTEST_F(DevicePowerActionTest, DevicePowerActionTest001, TestSize.Level0)
51 {
52 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest001 start!");
53 g_cmdResult.clear();
54 g_retval = 0;
55 g_invoked = false;
56 Reboot("");
57 EXPECT_TRUE(g_invoked);
58 EXPECT_EQ(g_cmdResult, "invalid_cmd");
59 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest001 end!");
60 }
61
62 HWTEST_F(DevicePowerActionTest, DevicePowerActionTest002, TestSize.Level0)
63 {
64 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest002 start!");
65 g_cmdResult.clear();
66 g_retval = -1;
67 g_invoked = false;
68 Reboot("");
69 EXPECT_TRUE(g_invoked);
70 EXPECT_EQ(g_cmdResult, "");
71 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest002 end!");
72 }
73
74 HWTEST_F(DevicePowerActionTest, DevicePowerActionTest003, TestSize.Level0)
75 {
76 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest003 start!");
77 g_cmdResult.clear();
78 g_retval = -1;
79 g_invoked = false;
80 Reboot("updater");
81 EXPECT_TRUE(g_invoked);
82 EXPECT_EQ(g_cmdResult, "updater");
83 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest003 end!");
84 }
85
86 HWTEST_F(DevicePowerActionTest, DevicePowerActionTest004, TestSize.Level0)
87 {
88 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest004 start!");
89 g_cmdResult.clear();
90 Shutdown("");
91 EXPECT_EQ(g_cmdResult, "shutdown");
92 POWER_HILOGI(LABEL_TEST, "DevicePowerActionTest004 end!");
93 }
94 } // namespace