• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest001 start.");
84     GTEST_LOG_(INFO) << "PowerSuspend001: start";
85     g_service->SuspendControllerInit();
86     g_service->WakeupControllerInit();
87     sptr<SuspendPowerStateCallback> callback = new SuspendPowerStateCallback(g_service->suspendController_);
88     callback->controller_.reset();
89     callback->OnPowerStateChanged(PowerState::AWAKE);
90     auto controller = callback->controller_.lock();
91     EXPECT_EQ(controller, nullptr);
92     GTEST_LOG_(INFO) << "PowerSuspendControllerTest001:  end";
93     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest001 end.");
94 }
95 
96 /**
97  * @tc.name: PowerSuspendControllerTest002
98  * @tc.desc: test ExecSuspendMonitorByReason(Normal and exception)
99  * @tc.type: FUNC
100  * @tc.require: issueI7COGR
101  */
102 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest002, TestSize.Level0)
103 {
104     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest002 start.");
105     GTEST_LOG_(INFO) << "PowerSuspendControllerTest002: start";
106     sleep(SLEEP_WAIT_TIME_S);
107     g_service->SuspendControllerInit();
108     g_service->suspendController_->ExecSuspendMonitorByReason(SuspendDeviceType ::SUSPEND_DEVICE_REASON_POWER_KEY);
109     auto monitor = g_service->suspendController_->monitorMap_[SuspendDeviceType ::SUSPEND_DEVICE_REASON_POWER_KEY];
110     EXPECT_TRUE(monitor != nullptr);
111     GTEST_LOG_(INFO) << "PowerSuspendControllerTest002:  end";
112     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest002 end.");
113 }
114 
115 /**
116  * @tc.name: PowerSuspendControllerTest003
117  * @tc.desc: test RegisterSettingsObserver(exception 1, nothing!)
118  * @tc.type: FUNC
119  * @tc.require: issueI7COGR
120  */
121 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest003, TestSize.Level0)
122 {
123     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest003 start.");
124     GTEST_LOG_(INFO) << "PowerSuspendControllerTest003: start";
125     g_service->SuspendControllerInit();
126     g_service->suspendController_->RegisterSettingsObserver();
127     EXPECT_TRUE(g_service->suspendController_ != nullptr);
128     GTEST_LOG_(INFO) << "PowerSuspendControllerTest003:  end";
129     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest003 end.");
130 }
131 
132 /**
133  * @tc.name: PowerSuspendControllerTest004
134  * @tc.desc: test Execute
135  * @tc.type: FUNC
136  * @tc.require: issueI7COGR
137  */
138 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest004, TestSize.Level0)
139 {
140     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest004 start.");
141     GTEST_LOG_(INFO) << "PowerSuspendControllerTest004: start";
142     g_service->SuspendControllerInit();
143     g_service->suspendController_->Execute();
144     EXPECT_TRUE(g_service->suspendController_ != nullptr);
145     GTEST_LOG_(INFO) << "PowerSuspendControllerTest004:  end";
146     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest004 end.");
147 }
148 
149 /**
150  * @tc.name: PowerSuspendControllerTest005
151  * @tc.desc: test CreateMonitor
152  * @tc.type: FUNC
153  * @tc.require: issueI7G6OY
154  */
155 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest005, TestSize.Level0)
156 {
157     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest005 start.");
158     GTEST_LOG_(INFO) << "PowerSuspendControllerTest005: start";
159     g_service->SuspendControllerInit();
160     SuspendSource source(SuspendDeviceType::SUSPEND_DEVICE_REASON_STR, 1, 0);
161     std::shared_ptr<SuspendMonitor> monitor = SuspendMonitor::CreateMonitor(source);
162     EXPECT_TRUE(monitor == nullptr);
163     GTEST_LOG_(INFO) << "PowerSuspendControllerTest005:  end";
164     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest005 end.");
165 }
166 
167 /**
168  * @tc.name: PowerSuspendControllerTest006
169  * @tc.desc: test mapSuspendDeviceType
170  * @tc.type: FUNC
171  * @tc.require: issueI7G6OY
172  */
173 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest006, TestSize.Level0)
174 {
175     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest006 start.");
176     GTEST_LOG_(INFO) << "PowerSuspendControllerTest006: start";
177     g_service->SuspendControllerInit();
178     std::string key = " ";
179     SuspendDeviceType suspendDeviceType = SuspendSources::mapSuspendDeviceType(key);
180     EXPECT_TRUE(static_cast<uint32_t>(suspendDeviceType) ==
181         static_cast<uint32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN));
182     GTEST_LOG_(INFO) << "PowerSuspendControllerTest006:  end";
183     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest006 end.");
184 }
185 
186 /**
187  * @tc.name: PowerSuspendControllerTest007
188  * @tc.desc: test RecordPowerKeyDown
189  * @tc.type: FUNC
190  * @tc.require: issueI7COGR
191  */
192 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest007, TestSize.Level0)
193 {
194     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest007 start.");
195     GTEST_LOG_(INFO) << "PowerSuspendControllerTest007: start";
196     g_service->SuspendControllerInit();
197     g_service->suspendController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF);
198     g_service->suspendController_->RecordPowerKeyDown();
199     EXPECT_TRUE(
200         g_service->suspendController_->stateMachine_->stateAction_->GetDisplayState() == DisplayState::DISPLAY_OFF);
201     g_service->suspendController_->powerkeyDownWhenScreenOff_ = false;
202     GTEST_LOG_(INFO) << "PowerSuspendControllerTest007:  end";
203     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest007 end.");
204 }
205 
206 /**
207  * @tc.name: PowerSuspendControllerTest008
208  * @tc.desc: test GetPowerkeyDownWhenScreenOff
209  * @tc.type: FUNC
210  * @tc.require: issueI7COGR
211  */
212 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest008, TestSize.Level0)
213 {
214     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest008 start.");
215     GTEST_LOG_(INFO) << "PowerSuspendControllerTest008: start";
216 
217     g_service->SuspendControllerInit();
218     bool powerKeyDown = g_service->suspendController_->GetPowerkeyDownWhenScreenOff();
219     EXPECT_TRUE(powerKeyDown == false);
220 
221     GTEST_LOG_(INFO) << "PowerSuspendControllerTest008:  end";
222     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest008 end.");
223 }
224 
225 /**
226  * @tc.name: PowerSuspendControllerTest009
227  * @tc.desc: test ControlListener(Normal)
228  * @tc.type: FUNC
229  * @tc.require: issueI7COGR
230  */
231 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest009, TestSize.Level0)
232 {
233     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest009 start.");
234     GTEST_LOG_(INFO) << "PowerSuspendControllerTest009: start";
235 
236     g_service->SuspendControllerInit();
237 
238     g_service->SuspendDevice(
239         static_cast<int64_t>(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
240     g_service->suspendController_->ControlListener(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, 1, 0);
241     g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
242         WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerSuspendControllerTest009");
243 
244     g_service->suspendController_->stateMachine_->EmplaceInactive();
245     g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
246         WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerSuspendControllerTest009");
247     g_service->suspendController_->ControlListener(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, 1, 0);
248     EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() != PowerState::AWAKE);
249 
250     g_service->suspendController_->stateMachine_->EmplaceFreeze();
251     bool ret = g_service->suspendController_->stateMachine_->SetState(
252         PowerState::FREEZE, StateChangeReason::STATE_CHANGE_REASON_UNKNOWN);
253     if (ret == false) {
254         GTEST_LOG_(INFO) << "PowerSuspendControllerTest009:  FREEZE set  Failed!";
255     }
256     g_service->suspendController_->ControlListener(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, 1, 0);
257     uint32_t tmp = static_cast<uint32_t>(g_service->suspendController_->stateMachine_->GetState());
258     GTEST_LOG_(INFO) << "PowerSuspendControllerTest009: get State:" << tmp;
259     EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() == PowerState::FREEZE);
260 
261     GTEST_LOG_(INFO) << "PowerSuspendControllerTest009:  end";
262     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest009 end.");
263 }
264 
265 /**
266  * @tc.name: PowerSuspendControllerTest010
267  * @tc.desc: test HandleAction
268  * @tc.type: FUNC
269  * @tc.require: issueI7COGR
270  */
271 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest010, TestSize.Level0)
272 {
273     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest010 start.");
274     GTEST_LOG_(INFO) << "PowerSuspendControllerTest010: start";
275     g_service->SuspendControllerInit();
276     g_service->suspendController_->HandleAction(
277         SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, static_cast<uint32_t>(SuspendAction::ACTION_NONE));
278     g_service->suspendController_->HandleAction(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION,
279         static_cast<uint32_t>(SuspendAction::ACTION_AUTO_SUSPEND));
280     EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() == PowerState::SLEEP);
281     GTEST_LOG_(INFO) << "PowerSuspendControllerTest010:  end";
282     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest010 end.");
283 }
284 
285 /**
286  * @tc.name: PowerSuspendControllerTest011
287  * @tc.desc: test HandleForceSleep
288  * @tc.type: FUNC
289  * @tc.require: issueI7COGR
290  */
291 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest011, TestSize.Level0)
292 {
293     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest011 start.");
294     GTEST_LOG_(INFO) << "PowerSuspendControllerTest011: start";
295     g_service->SuspendControllerInit();
296     g_service->suspendController_->stateMachine_->controllerMap_.clear();
297     g_service->suspendController_->HandleForceSleep(SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND);
298     EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() == PowerState::SLEEP);
299     g_service->suspendController_->stateMachine_->InitStateMap();
300     GTEST_LOG_(INFO) << "PowerSuspendControllerTest011:  end";
301     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest011 end.");
302 }
303 
304 /**
305  * @tc.name: PowerSuspendControllerTest012
306  * @tc.desc: test HandleHibernate
307  * @tc.type: FUNC
308  * @tc.require: issueI7COGR
309  */
310 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest012, TestSize.Level0)
311 {
312     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest012 start.");
313     GTEST_LOG_(INFO) << "PowerSuspendControllerTest012: start";
314     g_service->SuspendControllerInit();
315     g_service->suspendController_->stateMachine_->controllerMap_.clear();
316     g_service->suspendController_->HandleHibernate(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT);
317     EXPECT_TRUE(g_service->suspendController_->stateMachine_->GetState() != PowerState::HIBERNATE);
318     g_service->suspendController_->stateMachine_->InitStateMap();
319 
320     GTEST_LOG_(INFO) << "PowerSuspendControllerTest012:  end";
321     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest012 end.");
322 }
323 
324 /**
325  * @tc.name: PowerSuspendControllerTest013
326  * @tc.desc: test HandleShutdown
327  * @tc.type: FUNC
328  * @tc.require: issueI7COGR
329  */
330 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest013, TestSize.Level0)
331 {
332     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest013 start.");
333     GTEST_LOG_(INFO) << "PowerSuspendControllerTest013: start";
334     g_service->SuspendControllerInit();
335     EXPECT_TRUE(g_service->suspendController_ != nullptr);
336     GTEST_LOG_(INFO) << "PowerSuspendControllerTest013:  end";
337     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest013 end.");
338 }
339 
340 /**
341  * @tc.name: PowerSuspendControllerTest014
342  * @tc.desc: test getSourceKeys
343  * @tc.type: FUNC
344  * @tc.require: issueI7COGR
345  */
346 HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest014, TestSize.Level0)
347 {
348     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest014 start.");
349     GTEST_LOG_(INFO) << "PowerSuspendControllerTest014: start";
350     std::shared_ptr<SuspendSources> sources = SuspendSourceParser::ParseSources();
351     std::vector<std::string> tmp = sources->getSourceKeys();
352     EXPECT_TRUE(tmp.size() != 0);
353     GTEST_LOG_(INFO) << "PowerSuspendControllerTest014:  end";
354     POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest014 end.");
355 }
356 } // namespace