1 /*
2 * Copyright (c) 2021-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_state_machine_test.h"
17
18 #include <iostream>
19
20 #include <datetime_ex.h>
21 #include <gtest/gtest.h>
22 #include <if_system_ability_manager.h>
23 #include <ipc_skeleton.h>
24 #include <string_ex.h>
25
26 #include "power_common.h"
27 #include "power_mgr_client.h"
28 #include "power_mgr_service.h"
29 #include "power_state_machine.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS;
34 using namespace std;
35
SetUpTestCase(void)36 void PowerStateMachineTest::SetUpTestCase(void)
37 {
38 }
39
TearDownTestCase(void)40 void PowerStateMachineTest::TearDownTestCase(void)
41 {
42 }
43
SetUp(void)44 void PowerStateMachineTest::SetUp(void)
45 {
46 auto& powerMgrClient = PowerMgrClient::GetInstance();
47 powerMgrClient.OverrideScreenOffTime(SCREEN_OFF_WAIT_TIME_MS);
48 }
49
TearDown(void)50 void PowerStateMachineTest::TearDown(void)
51 {
52 auto& powerMgrClient = PowerMgrClient::GetInstance();
53 powerMgrClient.RestoreScreenOffTime();
54 }
55
56 namespace {
57 /**
58 * @tc.name: PowerStateMachine001
59 * @tc.desc: test Suspend Device in proxy
60 * @tc.type: FUNC
61 */
62 HWTEST_F (PowerStateMachineTest, PowerStateMachine001, TestSize.Level0)
63 {
64 POWER_HILOGI(LABEL_TEST, "PowerStateMachine001 function start!");
65 sleep(SLEEP_WAIT_TIME_S);
66 GTEST_LOG_(INFO) << "PowerStateMachine001: Suspend Device start.";
67 auto& powerMgrClient = PowerMgrClient::GetInstance();
68
69 // Wakeup Device before test
70 GTEST_LOG_(INFO) << "PowerStateMachine001: Wakeup Device before test.";
71 powerMgrClient.WakeupDevice();
72 usleep(SLEEP_WAIT_TIME_MS);
73 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine001: Prepare Fail, Screen is OFF.";
74 GTEST_LOG_(INFO) << "PowerStateMachine001: Screen is On, Begin to Suspend Device!";
75
76 powerMgrClient.SuspendDevice();
77
78 sleep(REFRESHACTIVITY_WAIT_TIME_S);
79 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine001: Suspend Device Fail, Screen is On";
80
81 POWER_HILOGI(LABEL_TEST, "PowerStateMachine001 function end!");
82 GTEST_LOG_(INFO) << "PowerStateMachine001: Suspend Device end.";
83 }
84
85 /**
86 * @tc.name: PowerStateMachine002
87 * @tc.desc: test WakeupDevice(int64_t timeMs) in proxy
88 * @tc.type: FUNC
89 */
90 HWTEST_F (PowerStateMachineTest, PowerStateMachine002, TestSize.Level0)
91 {
92 POWER_HILOGI(LABEL_TEST, "PowerStateMachine002 function start!");
93 sleep(SLEEP_WAIT_TIME_S);
94 GTEST_LOG_(INFO) << "PowerStateMachine002: Wakeup Device start.";
95 auto& powerMgrClient = PowerMgrClient::GetInstance();
96
97 // Suspend Device before test
98 GTEST_LOG_(INFO) << "PowerStateMachine002: Suspend Device before test.";
99 powerMgrClient.SuspendDevice();
100 usleep(SLEEP_WAIT_TIME_MS);
101 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine002: Prepare Fail, Screen is On.";
102 GTEST_LOG_(INFO) << "PowerStateMachine002: Screen is Off, Begin to Wakeup Device!";
103
104 powerMgrClient.WakeupDevice();
105
106 usleep(SLEEP_WAIT_TIME_MS);
107 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine002: Wakeup Device Fail, Screen is Off";
108
109 POWER_HILOGI(LABEL_TEST, "PowerStateMachine002 function end!");
110 GTEST_LOG_(INFO) << "PowerStateMachine002: Wakeup Device end.";
111 }
112
113 /**
114 * @tc.name: PowerStateMachine003
115 * @tc.desc: test IsScreenOn in proxy
116 * @tc.type: FUNC
117 */
118 HWTEST_F (PowerStateMachineTest, PowerStateMachine003, TestSize.Level0)
119 {
120 POWER_HILOGI(LABEL_TEST, "PowerStateMachine003 function start!");
121 GTEST_LOG_(INFO) << "PowerStateMachine003: IsScreenOn start.";
122 sleep(SLEEP_WAIT_TIME_S);
123 auto& powerMgrClient = PowerMgrClient::GetInstance();
124
125 for (int i = 0; i < 3; i++) {
126 if (powerMgrClient.IsScreenOn()) {
127 powerMgrClient.SuspendDevice();
128 usleep(SLEEP_WAIT_TIME_MS);
129 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine003" << i
130 << ": Suspend Device Fail, Screen is On";
131 } else {
132 powerMgrClient.WakeupDevice();
133 usleep(SLEEP_WAIT_TIME_MS);
134 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine003" << i
135 << ": Wakeup Device Fail, Screen is Off";
136 }
137 }
138 POWER_HILOGI(LABEL_TEST, "PowerStateMachine003 function end!");
139 GTEST_LOG_(INFO) << "PowerStateMachine003: IsScreenOn end.";
140 }
141
142 /**
143 * @tc.name: PowerStateMachine004
144 * @tc.desc: test WakeupDevice(int64_t timeMs, const WakeupDeviceType reason, const std::string& details) in proxy
145 * @tc.type: FUNC
146 */
147 HWTEST_F (PowerStateMachineTest, PowerStateMachine004, TestSize.Level0)
148 {
149 POWER_HILOGI(LABEL_TEST, "PowerStateMachine004 function start!");
150 GTEST_LOG_(INFO) << "PowerStateMachine004: Wakeup Device start.";
151 sleep(SLEEP_WAIT_TIME_S);
152 auto& powerMgrClient = PowerMgrClient::GetInstance();
153
154 // Suspend Device before test
155 GTEST_LOG_(INFO) << "PowerStateMachine004: Suspend Device before test.";
156 powerMgrClient.SuspendDevice();
157 usleep(SLEEP_WAIT_TIME_MS);
158 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine004: Prepare Fail, Screen is On.";
159 GTEST_LOG_(INFO) << "PowerStateMachine004: Screen is Off, Begin to Wakeup Device!";
160
161 // Check illegal para details
162 GTEST_LOG_(INFO) << "PowerStateMachine004: Check illegal para details Begin!";
163 powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
164 usleep(SLEEP_WAIT_TIME_MS);
165 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine004: Check details test Fail, Screen is Off.";
166
167 // Suspend Device before test
168 GTEST_LOG_(INFO) << "PowerStateMachine004: Suspend Device before real test.";
169 powerMgrClient.SuspendDevice();
170 usleep(SLEEP_WAIT_TIME_MS);
171 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine004: Real test tprepare Fail, Screen is On.";
172 GTEST_LOG_(INFO) << "PowerStateMachine004: Screen is Off, Begin to Real Wakeup Device!";
173
174 powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
175
176 usleep(SLEEP_WAIT_TIME_MS);
177 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine004: Real Wakeup Device Fail, Screen is Off";
178 POWER_HILOGI(LABEL_TEST, "PowerStateMachine004 function end!");
179 GTEST_LOG_(INFO) << "PowerStateMachine004: Wakeup Device end.";
180 }
181
182 /**
183 * @tc.name: PowerStateMachine005
184 * @tc.desc: test Suspend Device in proxy
185 * @tc.type: FUNC
186 * @tc.require: issueI6MZ3M
187 */
188 HWTEST_F (PowerStateMachineTest, PowerStateMachine005, TestSize.Level0)
189 {
190 POWER_HILOGI(LABEL_TEST, "PowerStateMachine005 function start!");
191 sleep(SLEEP_WAIT_TIME_S);
192 auto& powerMgrClient = PowerMgrClient::GetInstance();
193 EXPECT_EQ(powerMgrClient.RestoreScreenOffTime(), PowerErrors::ERR_OK);
194 POWER_HILOGI(LABEL_TEST, "PowerStateMachine005 function end!");
195 }
196 }
197
OnPowerStateChanged(PowerState state)198 void PowerStateMachineTest::PowerStateTest1Callback::OnPowerStateChanged(PowerState state)
199 {
200 POWER_HILOGI(
201 LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
202 }
203
OnAsyncPowerStateChanged(PowerState state)204 void PowerStateMachineTest::PowerStateTest1Callback::OnAsyncPowerStateChanged(PowerState state)
205 {
206 POWER_HILOGI(LABEL_TEST, "PowerStateTest1Callback::OnAsyncPowerStateChanged state = %{public}u.",
207 static_cast<uint32_t>(state));
208 }
209
OnPowerStateChanged(PowerState state)210 void PowerStateMachineTest::PowerStateTest2Callback::OnPowerStateChanged(PowerState state)
211 {
212 POWER_HILOGI(
213 LABEL_TEST, "PowerStateTest2Callback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
214 }
215
OnAsyncPowerStateChanged(PowerState state)216 void PowerStateMachineTest::PowerStateTest2Callback::OnAsyncPowerStateChanged(PowerState state)
217 {
218 POWER_HILOGI(LABEL_TEST, "PowerStateTest2Callback::OnAsyncPowerStateChanged state = %{public}u.",
219 static_cast<uint32_t>(state));
220 }
221
222 namespace {
223 /**
224 * @tc.name: PowerStateCallback001
225 * @tc.desc: test PowerStateCallback
226 * @tc.type: FUNC
227 */
228 HWTEST_F (PowerStateMachineTest, PowerStateCallback001, TestSize.Level0)
229 {
230 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback001 function start!");
231 auto& powerMgrClient = PowerMgrClient::GetInstance();
232 sptr<IPowerStateCallback> callBackFirst = new PowerStateTest1Callback();
233 powerMgrClient.RegisterPowerStateCallback(callBackFirst);
234 POWER_HILOGI(LABEL_TEST, "PowerStateCallback001 1.");
235 {
236 sptr<IPowerStateCallback> callBackSecond = new PowerStateTest2Callback();
237 powerMgrClient.UnRegisterPowerStateCallback(callBackSecond);
238 POWER_HILOGI(LABEL_TEST, "PowerStateCallback001 2.");
239 powerMgrClient.RegisterPowerStateCallback(callBackSecond);
240 POWER_HILOGI(LABEL_TEST, "PowerStateCallback001 3.");
241 powerMgrClient.RegisterPowerStateCallback(callBackSecond);
242 POWER_HILOGI(LABEL_TEST, "PowerStateCallback001 4.");
243 }
244 EXPECT_TRUE(powerMgrClient.UnRegisterPowerStateCallback(callBackFirst));
245 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback001 function end!");
246 }
247
248 /**
249 * @tc.name: PowerStateCallback002
250 * @tc.desc: test PowerStateCallback
251 * @tc.type: FUNC
252 */
253 HWTEST_F (PowerStateMachineTest, PowerStateCallback002, TestSize.Level0)
254 {
255 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback002 function start!");
256 auto& powerMgrClient = PowerMgrClient::GetInstance();
257 sptr<IPowerStateCallback> callBackFirst = new PowerStateTest1Callback();
258 powerMgrClient.RegisterPowerStateCallback(callBackFirst, false);
259 POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 1.");
260 {
261 sptr<IPowerStateCallback> callBackSecond = new PowerStateTest2Callback();
262 powerMgrClient.UnRegisterPowerStateCallback(callBackSecond);
263 POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 2.");
264 powerMgrClient.RegisterPowerStateCallback(callBackSecond, false);
265 POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 3.");
266 powerMgrClient.RegisterPowerStateCallback(callBackSecond, false);
267 POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 4.");
268 }
269 EXPECT_TRUE(powerMgrClient.UnRegisterPowerStateCallback(callBackFirst));
270 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback002 function end!");
271 }
272 }
273
WakeUpthread()274 void PowerStateMachineTest::WakeUpthread()
275 {
276 auto& powerMgrClient = PowerMgrClient::GetInstance();
277 powerMgrClient.WakeupDevice();
278 }
279
Suspendthread()280 void PowerStateMachineTest::Suspendthread()
281 {
282 auto& powerMgrClient = PowerMgrClient::GetInstance();
283 powerMgrClient.SuspendDevice();
284 }
285
Rebootthread()286 void PowerStateMachineTest::Rebootthread()
287 {
288 auto& powerMgrClient = PowerMgrClient::GetInstance();
289 if (false) {
290 powerMgrClient.RebootDevice(string("RebootDeviceTestThread"));
291 }
292 }
293
Shutdownthread()294 void PowerStateMachineTest::Shutdownthread()
295 {
296 auto& powerMgrClient = PowerMgrClient::GetInstance();
297 if (false) {
298 powerMgrClient.ShutDownDevice(string("ShutDownDeviceTestThread"));
299 }
300 }
301