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