• 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 #include "power_wakeup_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19 
20 #include "input_device.h"
21 #include "pointer_event.h"
22 #include <datetime_ex.h>
23 #include <input_manager.h>
24 #include <securec.h>
25 
26 #include "power_mgr_service.h"
27 #include "power_state_machine.h"
28 #include "setting_helper.h"
29 #include "json/reader.h"
30 
31 using namespace testing::ext;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS;
34 using namespace std;
35 static sptr<PowerMgrService> g_service;
36 static constexpr int SLEEP_WAIT_TIME_S = 2;
37 
38 class InputCallbackMock : public IInputEventConsumer {
39 public:
40     virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
41     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
42     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
43 };
44 
SetUpTestCase(void)45 void PowerWakeupTest::SetUpTestCase(void)
46 {
47     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
48     g_service->OnStart();
49 }
50 
TearDownTestCase(void)51 void PowerWakeupTest::TearDownTestCase(void)
52 {
53     g_service->OnStop();
54     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
55 }
56 
57 namespace {
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)58 MMI::PointerEvent::PointerItem CreatePointerItem(
59     int32_t pointerId, int32_t deviceId, const std::pair<int32_t, int32_t>& displayLocation, bool isPressed)
60 {
61     MMI::PointerEvent::PointerItem item;
62     item.SetPointerId(pointerId);
63     item.SetDeviceId(deviceId);
64     item.SetDisplayX(displayLocation.first);
65     item.SetDisplayY(displayLocation.second);
66     item.SetPressed(isPressed);
67     return item;
68 }
69 
70 /**
71  * @tc.name: PowerWakeupTest001
72  * @tc.desc: test ExecWakeupMonitorByReason(Normal and exception)
73  * @tc.type: FUNC
74  * @tc.require: issueI7COGR
75  */
76 HWTEST_F(PowerWakeupTest, PowerWakeupTest001, TestSize.Level0)
77 {
78     GTEST_LOG_(INFO) << "PowerWakeup001: start";
79     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
80     if (pmsTest_ == nullptr) {
81         GTEST_LOG_(INFO) << "PowerWakeupTest001: Failed to get PowerMgrService";
82     }
83 
84     pmsTest_->Init();
85     pmsTest_->WakeupControllerInit();
86     pmsTest_->wakeupController_->ExecWakeupMonitorByReason(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
87     auto monitor = pmsTest_->wakeupController_->monitorMap_[WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON];
88     EXPECT_TRUE(monitor != nullptr);
89 
90     GTEST_LOG_(INFO) << "PowerWakeupTest001:  end";
91 }
92 
93 /**
94  * @tc.name: PowerWakeupTest002
95  * @tc.desc: test Wakeup(Normal)
96  * @tc.type: FUNC
97  * @tc.require: issueI7COGR
98  */
99 HWTEST_F(PowerWakeupTest, PowerWakeupTest002, TestSize.Level0)
100 {
101     GTEST_LOG_(INFO) << "PowerWakeupTest002: start";
102     sleep(SLEEP_WAIT_TIME_S);
103     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
104     if (pmsTest_ == nullptr) {
105         GTEST_LOG_(INFO) << "PowerWakeupTest002: Failed to get PowerMgrService";
106     }
107     pmsTest_->Init();
108     pmsTest_->WakeupControllerInit();
109     // test Normal
110     pmsTest_->wakeupController_->Wakeup();
111     EXPECT_TRUE(pmsTest_->wakeupController_ != nullptr);
112     GTEST_LOG_(INFO) << "PowerWakeupTest002:  end";
113 }
114 
115 /**
116  * @tc.name: PowerWakeupTest003
117  * @tc.desc: test ControlListener(Normal)
118  * @tc.type: FUNC
119  * @tc.require: issueI7COGR
120  */
121 HWTEST_F(PowerWakeupTest, PowerWakeupTest003, TestSize.Level0)
122 {
123     GTEST_LOG_(INFO) << "PowerWakeupTest003: start";
124 
125     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
126     if (pmsTest_ == nullptr) {
127         GTEST_LOG_(INFO) << "PowerWakeupTest003: Failed to get PowerMgrService";
128     }
129     pmsTest_->Init();
130     pmsTest_->WakeupControllerInit();
131 
132     pmsTest_->wakeupController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF);
133     pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
134     EXPECT_TRUE(pmsTest_->wakeupController_ != nullptr);
135 
136     pmsTest_->wakeupController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_ON);
137     pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
138     EXPECT_TRUE(static_cast<uint32_t>(pmsTest_->wakeupController_->stateMachine_->GetState()) ==
139         static_cast<uint32_t>(PowerState::AWAKE));
140 
141     pmsTest_->wakeupController_->stateMachine_->EmplaceAwake();
142     pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
143     EXPECT_TRUE(static_cast<uint32_t>(pmsTest_->wakeupController_->stateMachine_->GetState()) ==
144         static_cast<uint32_t>(PowerState::AWAKE));
145 
146     pmsTest_->wakeupController_->stateMachine_->SetState(
147         PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT);
148     pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
149     EXPECT_TRUE(static_cast<uint32_t>(pmsTest_->wakeupController_->stateMachine_->GetState()) ==
150         static_cast<uint32_t>(PowerState::AWAKE));
151 
152     pmsTest_->wakeupController_->stateMachine_->SetState(
153         PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT);
154     pmsTest_->suspendController_->stateMachine_->controllerMap_.clear();
155     pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
156     EXPECT_TRUE(static_cast<uint32_t>(pmsTest_->wakeupController_->stateMachine_->GetState()) ==
157         static_cast<uint32_t>(PowerState::INACTIVE));
158 
159     GTEST_LOG_(INFO) << "PowerWakeupTest003:  end";
160 }
161 
162 /**
163  * @tc.name: PowerWakeupTest004
164  * @tc.desc: test GetTargetPath
165  * @tc.type: FUNC
166  * @tc.require: issueI7G6OY
167  */
168 HWTEST_F(PowerWakeupTest, PowerWakeupTest004, TestSize.Level0)
169 {
170     GTEST_LOG_(INFO) << "PowerWakeupTest004: start";
171     std::string targetPath;
172     WakeupSourceParser::GetTargetPath(targetPath);
173     EXPECT_TRUE(targetPath.size() != 0);
174     GTEST_LOG_(INFO) << "PowerWakeupTest004:  end";
175 }
176 
177 /**
178  * @tc.name: PowerWakeupTest005
179  * @tc.desc: test CreateMonitor
180  * @tc.type: FUNC
181  * @tc.require: issueI7G6OY
182  */
183 HWTEST_F(PowerWakeupTest, PowerWakeupTest005, TestSize.Level0)
184 {
185     GTEST_LOG_(INFO) << "PowerWakeupTest005: start";
186     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
187     if (pmsTest_ == nullptr) {
188         GTEST_LOG_(INFO) << "PowerWakeupTest005: Failed to get PowerMgrService";
189     }
190 
191     pmsTest_->Init();
192     pmsTest_->WakeupControllerInit();
193     WakeupSource source1(WakeupDeviceType::WAKEUP_DEVICE_SINGLE_CLICK, 1, 0);
194     std::shared_ptr<WakeupMonitor> monitor1 = WakeupMonitor::CreateMonitor(source1);
195     EXPECT_TRUE(monitor1 != nullptr);
196 
197     WakeupSource source2(WakeupDeviceType::WAKEUP_DEVICE_MAX, 1, 0);
198     std::shared_ptr<WakeupMonitor> monitor2 = WakeupMonitor::CreateMonitor(source2);
199     EXPECT_TRUE(static_cast<uint32_t>(source2.reason_) == static_cast<uint32_t>(WakeupDeviceType::WAKEUP_DEVICE_MAX));
200     GTEST_LOG_(INFO) << "PowerWakeupTest005:  end";
201 }
202 
203 /**
204  * @tc.name: PowerWakeupTest006
205  * @tc.desc: test Cancel(Normal and exception)
206  * @tc.type: FUNC
207  * @tc.require: issueI7COGR
208  */
209 HWTEST_F(PowerWakeupTest, PowerWakeupTest006, TestSize.Level0)
210 {
211     GTEST_LOG_(INFO) << "PowerWakeupTest006: start";
212 
213     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
214     if (pmsTest_ == nullptr) {
215         GTEST_LOG_(INFO) << "PowerWakeupTest006: Failed to get PowerMgrService";
216     }
217     pmsTest_->Init();
218     pmsTest_->WakeupControllerInit();
219     pmsTest_->wakeupController_->Cancel();
220     EXPECT_TRUE(pmsTest_->wakeupController_->monitorMap_.size() == 0);
221     GTEST_LOG_(INFO) << "PowerWakeupTest006:  end";
222 }
223 
224 /**
225  * @tc.name: PowerWakeupTest007
226  * @tc.desc: test OnInputEvent(Normal)
227  * @tc.type: FUNC
228  * @tc.require: issueI7COGR
229  */
230 HWTEST_F(PowerWakeupTest, PowerWakeupTest007, TestSize.Level0)
231 {
232     GTEST_LOG_(INFO) << "PowerWakeupTest007: start";
233 
234     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
235     if (pmsTest_ == nullptr) {
236         GTEST_LOG_(INFO) << "PowerWakeupTest007: Failed to get PowerMgrService";
237     }
238     pmsTest_->Init();
239     pmsTest_->WakeupControllerInit();
240     InputCallback* callback = new InputCallback();
241     InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
242     std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
243     keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
244 
245     keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_F1);
246     callback_mock->OnInputEvent(keyEvent);
247     keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_0);
248     callback_mock->OnInputEvent(keyEvent);
249     keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_F2);
250     callback_mock->OnInputEvent(keyEvent);
251     delete callback;
252     EXPECT_TRUE(pmsTest_->wakeupController_ != nullptr);
253     GTEST_LOG_(INFO) << "PowerWakeupTest007:  end";
254 }
255 
256 /**
257  * @tc.name: PowerWakeupTest008
258  * @tc.desc: test OnInputEvent(Normal 1)
259  * @tc.type: FUNC
260  * @tc.require: issueI7COGR
261  */
262 HWTEST_F(PowerWakeupTest, PowerWakeupTest008, TestSize.Level0)
263 {
264     GTEST_LOG_(INFO) << "PowerWakeupTest008: start";
265 
266     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
267     if (pmsTest_ == nullptr) {
268         GTEST_LOG_(INFO) << "PowerWakeupTest008: Failed to get PowerMgrService";
269     }
270     pmsTest_->Init();
271     pmsTest_->WakeupControllerInit();
272 
273     constexpr int32_t DRAG_DST_X {500};
274     constexpr int32_t DRAG_DST_Y {500};
275     int32_t deviceMouseId {0};
276 
277     InputCallback* callback = new InputCallback();
278     InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
279     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
280     MMI::PointerEvent::PointerItem curPointerItem;
281     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
282     pointerEvent->SetPointerId(0);
283 
284     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
285     curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
286     pointerEvent->AddPointerItem(curPointerItem);
287     callback_mock->OnInputEvent(pointerEvent);
288 
289     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
290     curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
291     pointerEvent->AddPointerItem(curPointerItem);
292     callback_mock->OnInputEvent(pointerEvent);
293 
294     pointerEvent->SetSourceType(PointerEvent::TOOL_TYPE_PEN);
295     curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
296     pointerEvent->AddPointerItem(curPointerItem);
297     callback_mock->OnInputEvent(pointerEvent);
298     EXPECT_TRUE(pmsTest_->wakeupController_ != nullptr);
299     delete callback;
300 }
301 
302 /**
303  * @tc.name: PowerWakeupTest009
304  * @tc.desc: test OnInputEvent(Normal 2)
305  * @tc.type: FUNC
306  * @tc.require: issueI7COGR
307  */
308 HWTEST_F(PowerWakeupTest, PowerWakeupTest009, TestSize.Level0)
309 {
310     GTEST_LOG_(INFO) << "PowerWakeupTest009: start";
311 
312     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
313     if (pmsTest_ == nullptr) {
314         GTEST_LOG_(INFO) << "PowerWakeupTest009: Failed to get PowerMgrService";
315     }
316     pmsTest_->Init();
317     pmsTest_->WakeupControllerInit();
318 
319     constexpr int32_t DRAG_DST_X {500};
320     constexpr int32_t DRAG_DST_Y {500};
321     int32_t deviceMouseId {0};
322 
323     InputCallback* callback = new InputCallback();
324     InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
325     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
326     MMI::PointerEvent::PointerItem curPointerItem;
327     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
328     pointerEvent->SetPointerId(0);
329 
330     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
331     curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
332     pointerEvent->AddPointerItem(curPointerItem);
333     callback_mock->OnInputEvent(pointerEvent);
334 
335     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
336     curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
337     pointerEvent->AddPointerItem(curPointerItem);
338     callback_mock->OnInputEvent(pointerEvent);
339 
340     std::shared_ptr<MMI::PointerEvent> pointerEvent2 = MMI::PointerEvent::Create();
341     pointerEvent2->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
342     pointerEvent2->SetPointerId(0);
343     curPointerItem = CreatePointerItem(1, PointerEvent::TOOL_TYPE_PEN, {DRAG_DST_X, DRAG_DST_Y}, true);
344     curPointerItem.SetToolType(PointerEvent::TOOL_TYPE_PEN);
345     pointerEvent2->AddPointerItem(curPointerItem);
346     callback_mock->OnInputEvent(pointerEvent2);
347 
348     curPointerItem = CreatePointerItem(0, PointerEvent::TOOL_TYPE_PEN, {DRAG_DST_X, DRAG_DST_Y}, true);
349     curPointerItem.SetToolType(PointerEvent::TOOL_TYPE_PEN);
350     pointerEvent2->AddPointerItem(curPointerItem);
351     callback_mock->OnInputEvent(pointerEvent2);
352 
353     delete callback;
354     EXPECT_TRUE(pmsTest_->wakeupController_ != nullptr);
355     GTEST_LOG_(INFO) << "PowerWakeupTest009:  end";
356 }
357 
358 /**
359  * @tc.name: PowerWakeupTest010
360  * @tc.desc: test getSourceKeys
361  * @tc.type: FUNC
362  * @tc.require: issueI7COGR
363  */
364 HWTEST_F(PowerWakeupTest, PowerWakeupTest010, TestSize.Level0)
365 {
366     GTEST_LOG_(INFO) << "PowerWakeupTest010: start";
367 
368     std::shared_ptr<WakeupSources> sources = WakeupSourceParser::ParseSources();
369     std::vector<std::string> tmp = sources->getSourceKeys();
370     EXPECT_TRUE(tmp.size() != 0);
371     GTEST_LOG_(INFO) << "PowerWakeupTest010:  end";
372 }
373 
374 /**
375  * @tc.name: PowerWakeupTest011
376  * @tc.desc: test ParseSourcesProc(exception)
377  * @tc.type: FUNC
378  * @tc.require: issueI7COGR
379  */
380 HWTEST_F(PowerWakeupTest, PowerWakeupTest011, TestSize.Level0)
381 {
382     GTEST_LOG_(INFO) << "PowerWakeupTest011: start";
383 
384     static const std::string jsonStr =
385         "{\"powerkey\": {\"enable\": false},\"keyborad\": {\"enable\": false},\"mouse\": {\"enable\": "
386         "false},\"touchscreen\": {\"enable\": false,\"click\": 2},\"touchpad\": {\"enable\": false},\"pen\": "
387         "{\"enable\": "
388         "false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": true},\"xxx\": {\"enable\": false}}";
389 
390     std::shared_ptr<WakeupSources> parseSources = std::make_shared<WakeupSources>();
391     Json::Reader reader;
392     Json::Value root;
393 
394     if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) {
395         GTEST_LOG_(INFO) << "PowerWakeupTest011: json parse error";
396     }
397 
398     Json::Value::Members members = root.getMemberNames();
399     for (auto iter = members.begin(); iter != members.end(); iter++) {
400         std::string key = *iter;
401         Json::Value valueObj = root[key];
402         WakeupSourceParser::ParseSourcesProc(parseSources, valueObj, key);
403     }
404     EXPECT_TRUE(parseSources->GetSourceList().size() != 0);
405     GTEST_LOG_(INFO) << "PowerWakeupTest011:  end";
406 }
407 } // namespace