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