• 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_getcontroller_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 "power_state_callback_stub.h"
30 #include "power_state_machine.h"
31 #include "setting_helper.h"
32 
33 #include <iremote_object.h>
34 #include <system_ability.h>
35 
36 #include "actions/idevice_power_action.h"
37 #include "ipower_mgr.h"
38 #include "power_mgr_notify.h"
39 #include "power_mgr_stub.h"
40 #include "power_mode_module.h"
41 #include "power_save_mode.h"
42 #include "power_state_machine.h"
43 #include "running_lock_mgr.h"
44 #include "shutdown_controller.h"
45 #include "sp_singleton.h"
46 #include "suspend_controller.h"
47 #include "wakeup_controller.h"
48 
49 namespace OHOS {
50 namespace PowerMgr {
51 class RunningLockMgr;
52 class PowerMgrService final : public SystemAbility, public PowerMgrStub {
53     DECLARE_SYSTEM_ABILITY(PowerMgrService)
54     DECLARE_DELAYED_SP_SINGLETON(PowerMgrService);
55 
56 public:
57     void OnStart() override;
58     void OnStop() override;
59     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
60     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
61     PowerErrors RebootDevice(const std::string& reason) override;
62     PowerErrors RebootDeviceForDeprecated(const std::string& reason) override;
63     PowerErrors ShutDownDevice(const std::string& reason) override;
64     PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override;
65     PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override;
66     bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override;
67     bool OverrideScreenOffTime(int64_t timeout) override;
68     bool RestoreScreenOffTime() override;
69     PowerState GetState() override;
70     bool IsScreenOn() override;
71     bool ForceSuspendDevice(int64_t callTimeMs) override;
72     PowerErrors CreateRunningLock(
73         const sptr<IRemoteObject>& remoteObj, const RunningLockInfo& runningLockInfo) override;
74     bool ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj) override;
75     bool IsRunningLockTypeSupported(RunningLockType type) override;
76     bool Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMS) override;
77     bool UnLock(const sptr<IRemoteObject>& remoteObj) override;
78     void ForceUnLock(const sptr<IRemoteObject>& remoteObj);
79     bool IsUsed(const sptr<IRemoteObject>& remoteObj) override;
80     bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid) override;
81     bool ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos) override;
82     bool ResetRunningLocks() override;
83     bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
84     bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
85     bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
86     bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
87     bool SetDisplaySuspend(bool enable) override;
88     PowerErrors SetDeviceMode(const PowerMode& mode) override;
89     PowerMode GetDeviceMode() override;
90     std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override;
91     PowerErrors IsStandby(bool& isStandby) override;
92 
93     void RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority) override;
94     void UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback) override;
95 
96     void RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority) override;
97     void UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback) override;
98     void RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority) override;
99     void UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback) override;
100 
101     void HandleShutdownRequest();
102     void HandleKeyEvent(int32_t keyCode);
103     void HandlePointEvent(int32_t type);
104     void KeyMonitorInit();
105     void KeyMonitorCancel();
106     void SwitchSubscriberInit();
107     void SwitchSubscriberCancel();
108     void HallSensorSubscriberInit();
109     void HallSensorSubscriberCancel();
110     bool ShowPowerDialog();
111     bool CheckDialogAndShuttingDown();
112     void SuspendControllerInit();
113     void WakeupControllerInit();
114 
GetRunningLockMgr() const115     std::shared_ptr<RunningLockMgr> GetRunningLockMgr() const
116     {
117         return runningLockMgr_;
118     }
119 
GetPowerStateMachine() const120     std::shared_ptr<PowerStateMachine> GetPowerStateMachine() const
121     {
122         return powerStateMachine_;
123     }
124 
GetPowerMgrNotify() const125     std::shared_ptr<PowerMgrNotify> GetPowerMgrNotify() const
126     {
127         return powerMgrNotify_;
128     }
129 
GetSuspendController() const130     std::shared_ptr<SuspendController> GetSuspendController() const
131     {
132         return nullptr;
133     }
GetWakeupController() const134     std::shared_ptr<WakeupController> GetWakeupController() const
135     {
136         return nullptr;
137     }
IsServiceReady() const138     bool IsServiceReady() const
139     {
140         return ready_;
141     }
SetDisplayOffTime(int64_t time)142     void SetDisplayOffTime(int64_t time)
143     {
144         powerStateMachine_->SetDisplayOffTime(time);
145     }
SetSleepTime(int64_t time)146     void SetSleepTime(int64_t time)
147     {
148         powerStateMachine_->SetSleepTime(time);
149     }
150 
EnableMock(IDeviceStateAction * powerState,IDeviceStateAction * shutdownState,IDevicePowerAction * powerAction,IRunningLockAction * lockAction)151     void EnableMock(IDeviceStateAction* powerState, IDeviceStateAction* shutdownState, IDevicePowerAction* powerAction,
152         IRunningLockAction* lockAction)
153     {
154         POWER_HILOGI(LABEL_TEST, "Service EnableMock:%{public}d", mockCount_++);
155         runningLockMgr_->EnableMock(lockAction);
156         powerStateMachine_->EnableMock(powerState);
157         shutdownController_->EnableMock(powerAction, shutdownState);
158     }
MockProximity(uint32_t status)159     void MockProximity(uint32_t status)
160     {
161         POWER_HILOGI(LABEL_TEST, "MockProximity: fun is start");
162         runningLockMgr_->SetProximity(status);
163         POWER_HILOGI(LABEL_TEST, "MockProximity: fun is end");
164     }
MockSystemWakeup()165     void MockSystemWakeup()
166     {
167         PowerStateMachine::onWakeup();
168     }
GetShutdownController()169     std::shared_ptr<ShutdownController> GetShutdownController()
170     {
171         return shutdownController_;
172     }
173 
174     std::shared_ptr<SuspendController> suspendController_ = nullptr;
175     std::shared_ptr<WakeupController> wakeupController_ = nullptr;
176 
177 private:
178     class WakeupRunningLock {
179     public:
180         static void Create();
181         static void Lock();
182         static void Unlock();
183 
184     private:
185         WakeupRunningLock() = default;
186         ~WakeupRunningLock() = default;
187 
188         static sptr<RunningLockTokenStub> token_;
189         static const int32_t timeout = 10000; // 10seconds
190     };
191 
192     static constexpr int32_t LONG_PRESS_DELAY_MS = 3000;
193     static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000;
194     static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000;
195     static constexpr int32_t HALL_REPORT_INTERVAL = 0;
196     static constexpr uint32_t HALL_SAMPLING_RATE = 100000000;
197     bool Init();
198     bool PowerStateMachineInit();
199     void NotifyRunningLockChanged(bool isUnLock);
200     RunningLockParam FillRunningLockParam(const RunningLockInfo& info, int32_t timeOutMS = -1);
201     bool IsSupportSensor(SensorTypeId);
202     static void HallSensorCallback(SensorEvent* event);
203     static void RegisterBootCompletedCallback();
204 
GetPowerModeModule()205     inline PowerModeModule& GetPowerModeModule()
206     {
207         return powerModeModule_;
208     }
209 
210     bool ready_ {false};
211     static std::atomic_bool isBootCompleted_;
212     std::mutex wakeupMutex_;
213     std::mutex suspendMutex_;
214     std::mutex stateMutex_;
215     std::mutex shutdownMutex_;
216     std::mutex modeMutex_;
217     std::mutex screenMutex_;
218     std::mutex dumpMutex_;
219     std::mutex lockMutex_;
220     std::shared_ptr<RunningLockMgr> runningLockMgr_;
221     std::shared_ptr<PowerStateMachine> powerStateMachine_;
222     std::shared_ptr<PowerMgrNotify> powerMgrNotify_;
223     std::shared_ptr<ShutdownController> shutdownController_;
224     PowerModeModule powerModeModule_;
225     uint32_t mockCount_ {0};
226     bool isDialogShown_ {false};
227     int32_t powerkeyLongPressId_ {0};
228     int32_t switchId_ {0};
229     int32_t doubleClickId_ {0};
230     int32_t monitorId_ {0};
231     SensorUser sensorUser_;
232 };
233 } // namespace PowerMgr
234 } // namespace OHOS
235 
236 using namespace testing::ext;
237 using namespace OHOS::PowerMgr;
238 using namespace OHOS;
239 using namespace std;
240 static sptr<PowerMgrService> g_service;
241 static constexpr int SLEEP_WAIT_TIME_S = 2;
242 
243 class InputCallbackMock : public IInputEventConsumer {
244 public:
245     virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
246     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
247     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
248 };
249 
SetUpTestCase(void)250 void PowerGetControllerMockTest::SetUpTestCase(void)
251 {
252     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
253     g_service->OnStart();
254 }
255 
TearDownTestCase(void)256 void PowerGetControllerMockTest::TearDownTestCase(void)
257 {
258     g_service->OnStop();
259     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
260 }
261 
262 namespace {
263 /**
264  * @tc.name: PowerGetControllerMockTest001
265  * @tc.desc: test GetSourceList(exception)
266  * @tc.type: FUNC
267  * @tc.require: issueI7G6OY
268  */
269 HWTEST_F(PowerGetControllerMockTest, PowerGetControllerMockTest001, TestSize.Level0)
270 {
271     GTEST_LOG_(INFO) << "PowerGetControllerMockTest001: start";
272 
273     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
274     if (pmsTest_ == nullptr) {
275         GTEST_LOG_(INFO) << "PowerSuspendTest014: Failed to get PowerMgrService";
276     }
277 
278     pmsTest_->Init();
279     std::shared_ptr<PowerStateMachine> stateMachine = nullptr;
280     std::shared_ptr<WakeupController> wakeupController_ = std::make_shared<WakeupController>(stateMachine);
281     wakeupController_->Wakeup();
282     EXPECT_TRUE(wakeupController_ != nullptr);
283 
284     InputCallback* callback = new InputCallback();
285     InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
286     std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
287     keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
288     keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_F1);
289     callback_mock->OnInputEvent(keyEvent);
290     EXPECT_TRUE(callback_mock != nullptr);
291     delete callback;
292     sleep(SLEEP_WAIT_TIME_S);
293     GTEST_LOG_(INFO) << "PowerGetControllerMockTest001:  end";
294 }
295 } // namespace