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_service_mock_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 "permission.h"
30 #include "power_mgr_service.h"
31 #include "power_state_callback_stub.h"
32 #include "power_state_machine.h"
33 #include "setting_helper.h"
34
35 using namespace testing::ext;
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace std;
39 static constexpr int SLEEP_WAIT_TIME_S = 2;
40
41 class InputCallbackMock : public IInputEventConsumer {
42 public:
43 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
44 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
45 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
46 };
47
48 template <>
GetInstance()49 sptr<PowerMgrService> DelayedSpSingleton<PowerMgrService>::GetInstance()
50 {
51 instance_ = nullptr;
52 return instance_;
53 }
54
IsSystem()55 bool Permission::IsSystem()
56 {
57 return true;
58 }
59
Init()60 bool PowerKeySuspendMonitor::Init()
61 {
62 if (powerkeyReleaseId_ >= 0) {
63 return true;
64 }
65 std::shared_ptr<OHOS::MMI::KeyOption> keyOption = std::make_shared<OHOS::MMI::KeyOption>();
66 std::set<int32_t> preKeys;
67
68 keyOption.reset();
69 keyOption = std::make_shared<OHOS::MMI::KeyOption>();
70 keyOption->SetPreKeys(preKeys);
71 keyOption->SetFinalKey(OHOS::MMI::KeyEvent::KEYCODE_POWER);
72 keyOption->SetFinalKeyDown(false);
73 keyOption->SetFinalKeyDownDuration(0);
74 powerkeyReleaseId_ = InputManager::GetInstance()->SubscribeKeyEvent(
75 keyOption, [this](std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) {
76 POWER_HILOGI(FEATURE_SUSPEND, "Receive key on notify");
77 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
78 if (pms == nullptr) {
79 return;
80 }
81 std::shared_ptr<SuspendController> suspendController = pms->GetSuspendController();
82 if (suspendController->GetPowerkeyDownWhenScreenOff()) {
83 POWER_HILOGI(FEATURE_SUSPEND, "no action suspend");
84 return;
85 }
86 Notify();
87 });
88 POWER_HILOGI(FEATURE_SUSPEND, "powerkeyReleaseId_=%{public}d", powerkeyReleaseId_);
89 return powerkeyReleaseId_ >= 0 ? true : false;
90 }
91
92 namespace {
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)93 MMI::PointerEvent::PointerItem CreatePointerItem(
94 int32_t pointerId, int32_t deviceId, const std::pair<int32_t, int32_t>& displayLocation, bool isPressed)
95 {
96 MMI::PointerEvent::PointerItem item;
97 item.SetPointerId(pointerId);
98 item.SetDeviceId(deviceId);
99 item.SetDisplayX(displayLocation.first);
100 item.SetDisplayY(displayLocation.second);
101 item.SetPressed(isPressed);
102 return item;
103 }
104 /**
105 * @tc.name: PowerServiceMockTest001
106 * @tc.desc: test GetInstance(exception)
107 * @tc.type: FUNC
108 * @tc.require: issueI7G6OY
109 */
110 HWTEST_F(PowerServiceMockTest, PowerServiceMockTest001, TestSize.Level0)
111 {
112 GTEST_LOG_(INFO) << "PowerServiceMockTest001: start";
113 std::shared_ptr<PowerStateMachine> stateMachine = nullptr;
114 std::shared_ptr<WakeupController> wakeupController_ = std::make_shared<WakeupController>(stateMachine);
115 wakeupController_->Wakeup();
116 auto pmsTest1 = DelayedSpSingleton<PowerMgrService>::GetInstance();
117 EXPECT_TRUE(pmsTest1 == nullptr);
118
119 wakeupController_->ControlListener(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
120 auto pmsTest2 = DelayedSpSingleton<PowerMgrService>::GetInstance();
121 EXPECT_TRUE(pmsTest2 == nullptr);
122
123 InputCallback* callback = new InputCallback();
124 InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
125 std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
126 keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
127 keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_F1);
128 callback_mock->OnInputEvent(keyEvent);
129 auto pmsTest3 = DelayedSpSingleton<PowerMgrService>::GetInstance();
130 EXPECT_TRUE(pmsTest3 == nullptr);
131
132 constexpr int32_t DRAG_DST_X {500};
133 constexpr int32_t DRAG_DST_Y {500};
134 int32_t deviceMouseId {0};
135 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
136 MMI::PointerEvent::PointerItem curPointerItem;
137 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
138 pointerEvent->SetPointerId(0);
139 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
140 curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
141 pointerEvent->AddPointerItem(curPointerItem);
142 callback_mock->OnInputEvent(pointerEvent);
143 delete callback;
144 auto pmsTest4 = DelayedSpSingleton<PowerMgrService>::GetInstance();
145 EXPECT_TRUE(pmsTest4 == nullptr);
146
147 GTEST_LOG_(INFO) << "PowerServiceMockTest001: end";
148 }
149
150 /**
151 * @tc.name: PowerServiceMockTest002
152 * @tc.desc: test ControlListener(exception)
153 * @tc.type: FUNC
154 * @tc.require: issueI7G6OY
155 */
156 HWTEST_F(PowerServiceMockTest, PowerServiceMockTest002, TestSize.Level0)
157 {
158 GTEST_LOG_(INFO) << "PowerServiceMockTest002: start";
159 sleep(SLEEP_WAIT_TIME_S);
160 std::shared_ptr<ShutdownController> shutdownController = nullptr;
161 std::shared_ptr<PowerStateMachine> stateMachine = nullptr;
162 std::shared_ptr<SuspendController> suspendController_ =
163 std::make_shared<SuspendController>(shutdownController, stateMachine);
164 suspendController_->ControlListener(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, 1, 0);
165 auto pmsTest1 = DelayedSpSingleton<PowerMgrService>::GetInstance();
166 EXPECT_TRUE(pmsTest1 == nullptr);
167
168 GTEST_LOG_(INFO) << "PowerServiceMockTest002: end";
169 }
170 } // namespace