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 
16 #include "power_mgr_st_suspend_test.h"
17 #include "power_state_machine.h"
18 #include "suspend_controller.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::PowerMgr;
22 using namespace OHOS;
23 using namespace std;
24 
25 static sptr<PowerMgrService> g_service;
26 static constexpr int SLEEP_AFTER_LOCK_TIME_US = 1000 * 1000;
27 
SetUpTestCase(void)28 void PowerMgrSTSuspendTest::SetUpTestCase(void)
29 {
30     // create singleton service object at the beginning
31     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
32     g_service->OnStart();
33     g_service->GetPowerStateMachine()->SetSleepTime(SLEEP_WAIT_TIME_S);
34 }
35 
TearDownTestCase(void)36 void PowerMgrSTSuspendTest::TearDownTestCase(void)
37 {
38     g_service->OnStop();
39     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
40 }
41 
HandleAutoSleep(SuspendDeviceType reason)42 void SuspendController::HandleAutoSleep(SuspendDeviceType reason)
43 {
44     POWER_HILOGI(FEATURE_INPUT, "auto suspend by reason=%{public}d", reason);
45 
46     bool ret = stateMachine_->SetState(
47         PowerState::SLEEP, stateMachine_->GetReasionBySuspendType(reason));
48     if (ret) {
49         POWER_HILOGI(FEATURE_INPUT, "State changed, Mock suspend intreface");
50     } else {
51         POWER_HILOGI(FEATURE_INPUT, "auto suspend: State change failed");
52     }
53 }
54 
55 namespace {
56 /**
57  * @tc.name: PowerMgrMockSuspend001
58  * @tc.desc: test suspend by mock
59  * @tc.type: FUNC
60  * @tc.require: issueI5MJZJ
61  */
62 HWTEST_F(PowerMgrSTSuspendTest, PowerMgrMockSuspend001, TestSize.Level2)
63 {
64     GTEST_LOG_(INFO) << "PowerMgrMockSuspend001: start";
65 
66     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
67     if (pms == nullptr) {
68         GTEST_LOG_(INFO) << "PowerMgrMockSuspend001: Failed to get PowerMgrService";
69     }
70 
71     pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test"));
72     pms->SuspendControllerInit();
73     auto suspendController = pms->GetSuspendController();
74     suspendController->ExecSuspendMonitorByReason(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY);
75     sleep(SLEEP_WAIT_TIME_S + ONE_SECOND);
76     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
77 
78     GTEST_LOG_(INFO) << "PowerMgrMockSuspend001: end";
79 }
80 
81 /**
82  * @tc.name: PowerMgrMockSuspend002
83  * @tc.desc: test proximity screen control RunningLock by mock
84  * @tc.type: FUNC
85  * @tc.require: issueI5MJZJ
86  */
87 HWTEST_F(PowerMgrSTSuspendTest, PowerMgrMockSuspend002, TestSize.Level2)
88 {
89     GTEST_LOG_(INFO) << "PowerMgrMockSuspend002: start";
90 
91     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
92     if (pms == nullptr) {
93         GTEST_LOG_(INFO) << "PowerMgrMockSuspend002: Failed to get PowerMgrService";
94     }
95 
96     pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test"));
97     auto powerStateMachine = pms->GetPowerStateMachine();
98     powerStateMachine->SetDisplayOffTime(SET_DISPLAY_OFF_TIME, false);
99 
100     sptr<IRemoteObject> token = new RunningLockTokenStub();
101     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
102     pms->CreateRunningLock(token, info);
103     pms->Lock(token);
104     EXPECT_EQ(pms->IsUsed(token), true);
105     sleep(SLEEP_WAIT_TIME_S);
106 
107     pms->UnLock(token);
108     EXPECT_EQ(pms->IsUsed(token), false);
109     sleep(SLEEP_WAIT_TIME_S + 3 * ONE_SECOND);
110     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
111 
112     powerStateMachine->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME, false);
113     GTEST_LOG_(INFO) << "PowerMgrMockSuspend002: end";
114 }
115 
116 /**
117  * @tc.name: PowerMgrMock03
118  * @tc.desc: test Screen RunningLock by mock
119  * @tc.type: FUNC
120  * @tc.require: issueI5MJZJ
121  */
122 HWTEST_F(PowerMgrSTSuspendTest, PowerMgrMock03, TestSize.Level2)
123 {
124     GTEST_LOG_(INFO) << "PowerMgrMock03: start";
125 
126     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
127     if (pms == nullptr) {
128         GTEST_LOG_(INFO) << "PowerMgrMock03: Failed to get PowerMgrService";
129     }
130 
131     sptr<IRemoteObject> token = new RunningLockTokenStub();
132     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
133     pms->CreateRunningLock(token, info);
134 
135     pms->Lock(token);
136     usleep(SLEEP_AFTER_LOCK_TIME_US);
137     EXPECT_EQ(pms->IsUsed(token), true);
138 
139     pms->SuspendControllerInit();
140     auto suspendController = pms->GetSuspendController();
141     suspendController->ExecSuspendMonitorByReason(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY);
142     sleep(SLEEP_WAIT_TIME_S + ONE_SECOND);
143     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
144 
145     pms->UnLock(token);
146 
147     EXPECT_EQ(pms->IsUsed(token), false);
148     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
149 
150     GTEST_LOG_(INFO) << "PowerMgrMock03: end";
151 }
152 } // namespace