• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_->GetReasonBySuspendType(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     POWER_HILOGI(LABEL_TEST, "PowerMgrMockSuspend001 function start!");
66     int64_t PARM_ZERO = 0;
67     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
68     if (pms == nullptr) {
69         GTEST_LOG_(INFO) << "PowerMgrMockSuspend001: Failed to get PowerMgrService";
70     }
71     pms->WakeupDevice(PARM_ZERO, 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     POWER_HILOGI(LABEL_TEST, "PowerMgrMockSuspend001 function end!");
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     POWER_HILOGI(LABEL_TEST, "PowerMgrMockSuspend002 function start!");
91 #ifdef HAS_SENSORS_SENSOR_PART
92     int64_t PARM_ZERO = 0;
93     int PARM_THREE = 3;
94     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
95     if (pms == nullptr) {
96         GTEST_LOG_(INFO) << "PowerMgrMockSuspend002: Failed to get PowerMgrService";
97     }
98 
99     pms->WakeupDevice(PARM_ZERO, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test"));
100     auto powerStateMachine = pms->GetPowerStateMachine();
101     powerStateMachine->SetDisplayOffTime(SET_DISPLAY_OFF_TIME, false);
102 
103     sptr<IRemoteObject> token = new RunningLockTokenStub();
104     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
105     pms->CreateRunningLock(token, info);
106     pms->Lock(token);
107     EXPECT_EQ(pms->IsUsed(token), true);
108     sleep(SLEEP_WAIT_TIME_S);
109 
110     pms->UnLock(token);
111     EXPECT_EQ(pms->IsUsed(token), false);
112     sleep(SLEEP_WAIT_TIME_S + PARM_THREE * ONE_SECOND);
113     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
114 
115     powerStateMachine->SetDisplayOffTime(PowerStateMachine::DEFAULT_DISPLAY_OFF_TIME_MS, false);
116 #endif
117     POWER_HILOGI(LABEL_TEST, "PowerMgrMockSuspend002 function end!");
118     GTEST_LOG_(INFO) << "PowerMgrMockSuspend002: end";
119 }
120 
121 /**
122  * @tc.name: PowerMgrMock003
123  * @tc.desc: test Screen RunningLock by mock
124  * @tc.type: FUNC
125  * @tc.require: issueI5MJZJ
126  */
127 HWTEST_F(PowerMgrSTSuspendTest, PowerMgrMock003, TestSize.Level2)
128 {
129     GTEST_LOG_(INFO) << "PowerMgrMock003: start";
130     POWER_HILOGI(LABEL_TEST, "PowerMgrMock003 function start!");
131     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
132     if (pms == nullptr) {
133         GTEST_LOG_(INFO) << "PowerMgrMock003: Failed to get PowerMgrService";
134     }
135 
136     sptr<IRemoteObject> token = new RunningLockTokenStub();
137     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
138     pms->CreateRunningLock(token, info);
139 
140     pms->Lock(token);
141     usleep(SLEEP_AFTER_LOCK_TIME_US);
142     EXPECT_EQ(pms->IsUsed(token), true);
143 
144     pms->SuspendControllerInit();
145     auto suspendController = pms->GetSuspendController();
146     suspendController->ExecSuspendMonitorByReason(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY);
147     sleep(SLEEP_WAIT_TIME_S + ONE_SECOND);
148     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
149 
150     pms->UnLock(token);
151 
152     EXPECT_EQ(pms->IsUsed(token), false);
153     EXPECT_EQ(PowerState::SLEEP, pms->GetState());
154     POWER_HILOGI(LABEL_TEST, "PowerMgrMock003 function end!");
155     GTEST_LOG_(INFO) << "PowerMgrMock003: end";
156 }
157 } // namespace