1 /*
2 * Copyright (c) 2023-2024 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 #include "power_suspend_controller_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19
20 #ifdef POWERMGR_GTEST
21 #define private public
22 #define protected public
23 #endif
24
25 #include <datetime_ex.h>
26 #include <input_manager.h>
27 #include <securec.h>
28
29 #include "power_mgr_service.h"
30 #include "power_state_callback_stub.h"
31 #include "power_state_machine.h"
32 #include "setting_helper.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::PowerMgr;
36 using namespace OHOS;
37 using namespace std;
38 static sptr<PowerMgrService> g_service;
39 static constexpr int SLEEP_WAIT_TIME_S = 2;
40
SetUpTestCase(void)41 void PowerSuspendControllerTest::SetUpTestCase(void)
42 {
43 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
44 g_service->OnStart();
45 }
46
TearDownTestCase(void)47 void PowerSuspendControllerTest::TearDownTestCase(void)
48 {
49 g_service->OnStop();
50 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
51 }
52
53 class SuspendPowerStateCallback : public PowerStateCallbackStub {
54 public:
SuspendPowerStateCallback(std::shared_ptr<SuspendController> controller)55 explicit SuspendPowerStateCallback(std::shared_ptr<SuspendController> controller) : controller_(controller) {};
56 virtual ~SuspendPowerStateCallback() = default;
OnPowerStateChanged(PowerState state)57 void OnPowerStateChanged(PowerState state) override
58 {
59 auto controller = controller_.lock();
60 if (controller == nullptr) {
61 POWER_HILOGI(FEATURE_SUSPEND, "OnPowerStateChanged: No controller");
62 return;
63 }
64 if (state == PowerState::AWAKE) {
65 POWER_HILOGI(FEATURE_SUSPEND, "Turn awake, stop sleep timer");
66 controller->StopSleep();
67 }
68 }
69
70 private:
71 std::weak_ptr<SuspendController> controller_;
72 };
73
74 namespace {
75 /**
76 * @tc.name: PowerSuspendControllerTest001
77 * @tc.desc: test OnPowerStateChanged(exception)
78 * @tc.type: FUNC
79 * @tc.require: issueI7COGR
80 */
81 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest001, TestSize.Level0)
82 {
83 GTEST_LOG_(INFO) << "PowerSuspend001: start";
84 g_service->SuspendControllerInit();
85 g_service->WakeupControllerInit();
86 sptr<SuspendPowerStateCallback> callback = new SuspendPowerStateCallback(g_service->suspendController_);
87 callback->controller_.reset();
88 callback->OnPowerStateChanged(PowerState::AWAKE);
89 auto controller = callback->controller_.lock();
90 EXPECT_EQ(controller, nullptr);
91 GTEST_LOG_(INFO) << "PowerSuspendControllerTest001: end";
92 }
93
94 /**
95 * @tc.name: PowerWakeupTest002
96 * @tc.desc: test ExecSuspendMonitorByReason(Normal and exception)
97 * @tc.type: FUNC
98 * @tc.require: issueI7COGR
99 */
100 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest002, TestSize.Level0)
101 {
102 GTEST_LOG_(INFO) << "PowerSuspendControllerTest002: start";
103 sleep(SLEEP_WAIT_TIME_S);
104 g_service->SuspendControllerInit();
105 g_service->suspendController_->ExecSuspendMonitorByReason(SuspendDeviceType ::SUSPEND_DEVICE_REASON_POWER_KEY);
106 auto monitor = g_service->suspendController_->monitorMap_[SuspendDeviceType ::SUSPEND_DEVICE_REASON_POWER_KEY];
107 EXPECT_TRUE(monitor != nullptr);
108 GTEST_LOG_(INFO) << "PowerSuspendControllerTest002: end";
109 }
110
111 /**
112 * @tc.name: PowerSuspendControllerTest003
113 * @tc.desc: test RegisterSettingsObserver(exception 1, nothing!)
114 * @tc.type: FUNC
115 * @tc.require: issueI7COGR
116 */
117 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest003, TestSize.Level0)
118 {
119 GTEST_LOG_(INFO) << "PowerSuspendControllerTest003: start";
120 g_service->SuspendControllerInit();
121 g_service->suspendController_->RegisterSettingsObserver();
122 EXPECT_TRUE(g_service->suspendController_ != nullptr);
123
124 GTEST_LOG_(INFO) << "PowerSuspendControllerTest003: end";
125 }
126
127 /**
128 * @tc.name: PowerSuspendControllerTest004
129 * @tc.desc: test Execute
130 * @tc.type: FUNC
131 * @tc.require: issueI7COGR
132 */
133 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest004, TestSize.Level0)
134 {
135 GTEST_LOG_(INFO) << "PowerSuspendControllerTest004: start";
136 g_service->SuspendControllerInit();
137 g_service->suspendController_->Execute();
138 EXPECT_TRUE(g_service->suspendController_ != nullptr);
139 GTEST_LOG_(INFO) << "PowerSuspendControllerTest004: end";
140 }
141
142 /**
143 * @tc.name: PowerSuspendControllerTest005
144 * @tc.desc: test CreateMonitor
145 * @tc.type: FUNC
146 * @tc.require: issueI7G6OY
147 */
148 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest005, TestSize.Level0)
149 {
150 GTEST_LOG_(INFO) << "PowerSuspendControllerTest005: start";
151 g_service->SuspendControllerInit();
152
153 SuspendSource source(SuspendDeviceType::SUSPEND_DEVICE_REASON_STR, 1, 0);
154 std::shared_ptr<SuspendMonitor> monitor = SuspendMonitor::CreateMonitor(source);
155 EXPECT_TRUE(monitor == nullptr);
156 GTEST_LOG_(INFO) << "PowerSuspendControllerTest005: end";
157 }
158
159 /**
160 * @tc.name: PowerSuspendControllerTest006
161 * @tc.desc: test mapSuspendDeviceType
162 * @tc.type: FUNC
163 * @tc.require: issueI7G6OY
164 */
165 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest006, TestSize.Level0)
166 {
167 GTEST_LOG_(INFO) << "PowerSuspendControllerTest006: start";
168 g_service->SuspendControllerInit();
169 std::string key = " ";
170 SuspendDeviceType suspendDeviceType = SuspendSources::mapSuspendDeviceType(key);
171 EXPECT_TRUE(static_cast<uint32_t>(suspendDeviceType) ==
172 static_cast<uint32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN));
173 GTEST_LOG_(INFO) << "PowerSuspendControllerTest006: end";
174 }
175
176 /**
177 * @tc.name: PowerSuspendControllerTest007
178 * @tc.desc: test RecordPowerKeyDown
179 * @tc.type: FUNC
180 * @tc.require: issueI7COGR
181 */
182 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest007, TestSize.Level0)
183 {
184 GTEST_LOG_(INFO) << "PowerSuspendControllerTest007: start";
185
186 g_service->SuspendControllerInit();
187 g_service->suspendController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF);
188 g_service->suspendController_->RecordPowerKeyDown();
189 EXPECT_TRUE(
190 g_service->suspendController_->stateMachine_->stateAction_->GetDisplayState() == DisplayState::DISPLAY_OFF);
191 g_service->suspendController_->powerkeyDownWhenScreenOff_ = false;
192
193 GTEST_LOG_(INFO) << "PowerSuspendControllerTest007: end";
194 }
195
196 /**
197 * @tc.name: PowerSuspendControllerTest008
198 * @tc.desc: test GetPowerkeyDownWhenScreenOff
199 * @tc.type: FUNC
200 * @tc.require: issueI7COGR
201 */
202 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest008, TestSize.Level0)
203 {
204 GTEST_LOG_(INFO) << "PowerSuspendControllerTest008: start";
205
206 g_service->SuspendControllerInit();
207 bool powerKeyDown = g_service->suspendController_->GetPowerkeyDownWhenScreenOff();
208 EXPECT_TRUE(powerKeyDown == false);
209
210 GTEST_LOG_(INFO) << "PowerSuspendControllerTest008: end";
211 }
212
213 /**
214 * @tc.name: PowerSuspendControllerTest009
215 * @tc.desc: test ControlListener(Normal)
216 * @tc.type: FUNC
217 * @tc.require: issueI7COGR
218 */
219 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest009, TestSize.Level0)
220 {
221 GTEST_LOG_(INFO) << "PowerSuspendControllerTest009: start";
222
223 g_service->SuspendControllerInit();
224
225 g_service->SuspendDevice(
226 static_cast<int64_t>(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
227 g_service->suspendController_->ControlListener(SuspendDeviceType ::SUSPEND_DEVICE_REASON_POWER_KEY, 1, 0);
228 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
229 WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerSuspendControllerTest009");
230
231 g_service->suspendController_->stateMachine_->EmplaceInactive();
232 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
233 WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerSuspendControllerTest009");
234 g_service->suspendController_->ControlListener(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, 1, 0);
235 EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() != PowerState::AWAKE);
236
237 g_service->suspendController_->stateMachine_->EmplaceFreeze();
238 bool ret = g_service->suspendController_->stateMachine_->SetState(
239 PowerState::FREEZE, StateChangeReason::STATE_CHANGE_REASON_UNKNOWN);
240 if (ret == false) {
241 GTEST_LOG_(INFO) << "PowerSuspendControllerTest009: FREEZE set Failed!";
242 }
243 g_service->suspendController_->ControlListener(SuspendDeviceType ::SUSPEND_DEVICE_REASON_POWER_KEY, 1, 0);
244 uint32_t tmp = static_cast<uint32_t>(g_service->suspendController_->stateMachine_->GetState());
245 GTEST_LOG_(INFO) << "PowerSuspendControllerTest009: get State:" << tmp;
246 EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() == PowerState::FREEZE);
247
248 GTEST_LOG_(INFO) << "PowerSuspendControllerTest009: end";
249 }
250
251 /**
252 * @tc.name: PowerSuspendControllerTest011
253 * @tc.desc: test HandleAction
254 * @tc.type: FUNC
255 * @tc.require: issueI7COGR
256 */
257 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest011, TestSize.Level0)
258 {
259 GTEST_LOG_(INFO) << "PowerSuspendControllerTest011: start";
260 g_service->SuspendControllerInit();
261 g_service->suspendController_->HandleAction(
262 SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, static_cast<uint32_t>(SuspendAction::ACTION_NONE));
263 g_service->suspendController_->HandleAction(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION,
264 static_cast<uint32_t>(SuspendAction::ACTION_AUTO_SUSPEND));
265 EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() == PowerState::SLEEP);
266 GTEST_LOG_(INFO) << "PowerSuspendControllerTest011: end";
267 }
268
269 /**
270 * @tc.name: PowerSuspendControllerTest012
271 * @tc.desc: test HandleForceSleep
272 * @tc.type: FUNC
273 * @tc.require: issueI7COGR
274 */
275 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest012, TestSize.Level0)
276 {
277 GTEST_LOG_(INFO) << "PowerSuspendControllerTest012: start";
278 g_service->SuspendControllerInit();
279 g_service->suspendController_->stateMachine_->controllerMap_.clear();
280 g_service->suspendController_->HandleForceSleep(SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND);
281 EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() == PowerState::SLEEP);
282 g_service->suspendController_->stateMachine_->InitStateMap();
283 GTEST_LOG_(INFO) << "PowerSuspendControllerTest012: end";
284 }
285
286 /**
287 * @tc.name: PowerSuspendControllerTest013
288 * @tc.desc: test HandleHibernate
289 * @tc.type: FUNC
290 * @tc.require: issueI7COGR
291 */
292 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest013, TestSize.Level0)
293 {
294 GTEST_LOG_(INFO) << "PowerSuspendControllerTest013: start";
295 g_service->SuspendControllerInit();
296 g_service->suspendController_->stateMachine_->controllerMap_.clear();
297 g_service->suspendController_->HandleHibernate(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT);
298 EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() != PowerState::HIBERNATE);
299 g_service->suspendController_->stateMachine_->InitStateMap();
300
301 GTEST_LOG_(INFO) << "PowerSuspendControllerTest013: end";
302 }
303
304 /**
305 * @tc.name: PowerSuspendControllerTest014
306 * @tc.desc: test HandleShutdown
307 * @tc.type: FUNC
308 * @tc.require: issueI7COGR
309 */
310 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest014, TestSize.Level0)
311 {
312 GTEST_LOG_(INFO) << "PowerSuspendControllerTest014: start";
313 g_service->SuspendControllerInit();
314 EXPECT_TRUE(g_service->suspendController_ != nullptr);
315 GTEST_LOG_(INFO) << "PowerSuspendControllerTest014: end";
316 }
317
318 /**
319 * @tc.name: PowerSuspendControllerTest016
320 * @tc.desc: test getSourceKeys
321 * @tc.type: FUNC
322 * @tc.require: issueI7COGR
323 */
324 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest016, TestSize.Level0)
325 {
326 GTEST_LOG_(INFO) << "PowerSuspendControllerTest016: start";
327 std::shared_ptr<SuspendSources> sources = SuspendSourceParser::ParseSources();
328 std::vector<std::string> tmp = sources->getSourceKeys();
329 EXPECT_TRUE(tmp.size() != 0);
330 GTEST_LOG_(INFO) << "PowerSuspendControllerTest016: end";
331 }
332 } // namespace