1 /*
2 * Copyright (c) 2023 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 "takeover_shutdown_callback_test.h"
17
18 #include <condition_variable>
19
20 #include "power_log.h"
21 #include "power_mgr_client.h"
22 #include "power_mgr_service.h"
23 #include "shutdown/shutdown_client.h"
24
25 #include "mock_power_action.h"
26 #include "mock_state_action.h"
27
28 namespace OHOS {
29 namespace PowerMgr {
30 namespace UnitTest {
31 namespace {
32 sptr<PowerMgrService> g_service;
33 MockPowerAction* g_mockPowerAction;
34 MockStateAction* g_mockStateAction;
35 bool g_isReboot;
36 bool g_isHighPriority;
37 bool g_isDefaultPriority;
38 bool g_isLowPriority;
39 }
40 using namespace testing::ext;
41
SetUpTestCase()42 void TakeOverShutdownCallbackTest::SetUpTestCase()
43 {
44 // create singleton service object at the beginning
45 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
46 g_service->OnStart();
47 }
48
TearDownTestCase()49 void TakeOverShutdownCallbackTest::TearDownTestCase()
50 {
51 g_service->OnStop();
52 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
53 }
54
SetUp()55 void TakeOverShutdownCallbackTest::SetUp()
56 {
57 g_isReboot = false;
58 g_isHighPriority = false;
59 g_isDefaultPriority = false;
60 g_isLowPriority = false;
61 g_mockPowerAction = new MockPowerAction();
62 g_mockStateAction = new MockStateAction();
63 auto shutdownController = g_service->GetShutdownController();
64 shutdownController->EnableMock(g_mockPowerAction, g_mockStateAction);
65 }
66
TearDown()67 void TakeOverShutdownCallbackTest::TearDown()
68 {}
69
OnTakeOverShutdown(bool isReboot)70 bool TakeOverShutdownCallbackTest::TakeOverShutdownCallback::OnTakeOverShutdown(bool isReboot)
71 {
72 POWER_HILOGI(LABEL_TEST, "OnTakeOverShutdown called, isReboot=%{public}d", isReboot);
73 g_isReboot = isReboot;
74 g_isDefaultPriority = true;
75 return true; // Take over the shutdown
76 }
77
OnTakeOverShutdown(bool isReboot)78 bool TakeOverShutdownCallbackTest::HighPriorityTakeOverShutdownCallback::OnTakeOverShutdown(bool isReboot)
79 {
80 g_isHighPriority = true;
81 return true; // Take over the shutdown
82 }
83
OnTakeOverShutdown(bool isReboot)84 bool TakeOverShutdownCallbackTest::LowPriorityTakeOverShutdownCallback::OnTakeOverShutdown(bool isReboot)
85 {
86 g_isLowPriority = true;
87 return true; // Take over the shutdown
88 }
89
OnTakeOverShutdown(bool isReboot)90 bool TakeOverShutdownCallbackTest::NotTakeOverShutdownCallback::OnTakeOverShutdown(bool isReboot)
91 {
92 POWER_HILOGI(LABEL_TEST, "OnTakeOverShutdown called, isReboot=%{public}d", isReboot);
93 return false; // Do not take over the shutdown
94 }
95
96 /**
97 * @tc.name: TakeOverShutdownCallback001
98 * @tc.desc: Test takeover shutdown callback for shutdown and reboot
99 * @tc.type: FUNC
100 */
101 HWTEST_F(TakeOverShutdownCallbackTest, TakeOverShutdownCallback001, TestSize.Level0)
102 {
103 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback001 start");
104 auto& shutdownClient = ShutdownClient::GetInstance();
105 auto callback = new TakeOverShutdownCallback();
106 shutdownClient.RegisterShutdownCallback(callback);
107
108 auto& powerMgrClient = PowerMgrClient::GetInstance();
109
110 EXPECT_CALL(*g_mockPowerAction, Reboot(std::string("test_reboot"))).Times(0);
111 powerMgrClient.RebootDevice("test_reboot"); // reboot will be taken over
112 EXPECT_TRUE(g_isReboot); // The callback param will be true for reboot
113
114 EXPECT_CALL(*g_mockPowerAction, Shutdown(std::string("test_shutdown"))).Times(0);
115 powerMgrClient.ShutDownDevice("test_shutdown"); // shutdown will be taken over
116 EXPECT_FALSE(g_isReboot); // The callback param will be false for shutdown
117 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback001 end");
118 }
119
120 /**
121 * @tc.name: ITakeOverShutdownCallback002
122 * @tc.desc: Test the low and default priority of takeover shutdown callback
123 * @tc.type: FUNC
124 */
125 HWTEST_F(TakeOverShutdownCallbackTest, ITakeOverShutdownCallback002, TestSize.Level0)
126 {
127 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback002 start");
128 auto& shutdownClient = ShutdownClient::GetInstance();
129 auto lowPriorityCallback = new LowPriorityTakeOverShutdownCallback();
130 shutdownClient.RegisterShutdownCallback(lowPriorityCallback, ShutdownPriority::LOW);
131 auto defaultPriorityCallback = new TakeOverShutdownCallback();
132 shutdownClient.RegisterShutdownCallback(defaultPriorityCallback, ShutdownPriority::DEFAULT);
133
134 auto& powerMgrClient = PowerMgrClient::GetInstance();
135 powerMgrClient.ShutDownDevice("test_shutdown"); // shutdown will be taken over
136
137 EXPECT_TRUE(g_isDefaultPriority); // Default priority callback will be called
138 EXPECT_FALSE(g_isLowPriority); // Low Priority callback will not be called
139 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback002 end");
140 }
141
142 /**
143 * @tc.name: TakeOverShutdownCallback003
144 * @tc.desc: Test the low and high priority of takeover shutdown callback
145 * @tc.type: FUNC
146 */
147 HWTEST_F(TakeOverShutdownCallbackTest, TakeOverShutdownCallback003, TestSize.Level0)
148 {
149 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback003 start");
150 auto& shutdownClient = ShutdownClient::GetInstance();
151 auto lowPriorityCallback = new LowPriorityTakeOverShutdownCallback();
152 shutdownClient.RegisterShutdownCallback(lowPriorityCallback, ShutdownPriority::LOW);
153 auto highPriorityCallback = new HighPriorityTakeOverShutdownCallback();
154 shutdownClient.RegisterShutdownCallback(highPriorityCallback, ShutdownPriority::HIGH);
155
156 auto& powerMgrClient = PowerMgrClient::GetInstance();
157 powerMgrClient.ShutDownDevice("test_shutdown"); // shutdown will be taken over
158
159 EXPECT_TRUE(g_isHighPriority); // High priority callback will be called
160 EXPECT_FALSE(g_isLowPriority); // Low Priority callback will not be called
161 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback003 end");
162 }
163
164 /**
165 * @tc.name: TakeOverShutdownCallback004
166 * @tc.desc: Test the default and high priority of takeover shutdown callback
167 * @tc.type: FUNC
168 */
169 HWTEST_F(TakeOverShutdownCallbackTest, TakeOverShutdownCallback004, TestSize.Level0)
170 {
171 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback004 start");
172 auto& shutdownClient = ShutdownClient::GetInstance();
173 auto defaultPriorityCallback = new TakeOverShutdownCallback();
174 shutdownClient.RegisterShutdownCallback(defaultPriorityCallback, ShutdownPriority::DEFAULT);
175 auto highPriorityCallback = new HighPriorityTakeOverShutdownCallback();
176 shutdownClient.RegisterShutdownCallback(highPriorityCallback, ShutdownPriority::HIGH);
177
178 auto& powerMgrClient = PowerMgrClient::GetInstance();
179 powerMgrClient.ShutDownDevice("test_shutdown"); // shutdown will be taken over
180
181 EXPECT_TRUE(g_isHighPriority); // High priority callback will be called
182 EXPECT_FALSE(g_isDefaultPriority); // Default Priority callback will not be called
183 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback004 end");
184 }
185
186 /**
187 * @tc.name: TakeOverShutdownCallback005
188 * @tc.desc: Test do not takeover the shutdown
189 * @tc.type: FUNC
190 */
191 HWTEST_F(TakeOverShutdownCallbackTest, TakeOverShutdownCallback005, TestSize.Level0)
192 {
193 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback005 start");
194 auto& shutdownClient = ShutdownClient::GetInstance();
195 auto notTakeOverCallback = new NotTakeOverShutdownCallback();
196 shutdownClient.RegisterShutdownCallback(notTakeOverCallback);
197
198 auto& powerMgrClient = PowerMgrClient::GetInstance();
199 EXPECT_CALL(*g_mockPowerAction, Reboot(std::string("test_reboot"))).Times(1);
200 powerMgrClient.RebootDevice("test_reboot"); // reboot will not be taken over
201
202 EXPECT_CALL(*g_mockPowerAction, Shutdown(std::string("test_shutdown"))).Times(1);
203 powerMgrClient.ShutDownDevice("test_shutdown"); // shutdown will not be taken over
204 POWER_HILOGD(LABEL_TEST, "TakeOverShutdownCallback005 end");
205 }
206 } // namespace UnitTest
207 } // namespace PowerMgr
208 } // namespace OHOS
209