1 /*
2 * Copyright (c) 2021-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_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: PowerStateMachine003
59 * @tc.desc: test Suspend Device in proxy
60 * @tc.type: FUNC
61 */
62 HWTEST_F (PowerStateMachineTest, PowerStateMachine003, TestSize.Level0)
63 {
64 POWER_HILOGI(LABEL_TEST, "PowerStateMachine003::fun is start!");
65 sleep(SLEEP_WAIT_TIME_S);
66 GTEST_LOG_(INFO) << "PowerStateMachine003: Suspend Device start.";
67 auto& powerMgrClient = PowerMgrClient::GetInstance();
68
69 // Wakeup Device before test
70 GTEST_LOG_(INFO) << "PowerStateMachine003: Wakeup Device before test.";
71 powerMgrClient.WakeupDevice();
72 usleep(SLEEP_WAIT_TIME_MS);
73 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine003: Prepare Fail, Screen is OFF.";
74 GTEST_LOG_(INFO) << "PowerStateMachine003: Screen is On, Begin to Suspend Device!";
75
76 powerMgrClient.SuspendDevice();
77
78 sleep(REFRESHACTIVITY_WAIT_TIME_S);
79 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine003: Suspend Device Fail, Screen is On";
80
81 POWER_HILOGI(LABEL_TEST, "PowerStateMachine003::fun is end!");
82 GTEST_LOG_(INFO) << "PowerStateMachine003: Suspend Device end.";
83 }
84
85 /**
86 * @tc.name: PowerStateMachine004
87 * @tc.desc: test WakeupDevice(int64_t timeMs) in proxy
88 * @tc.type: FUNC
89 */
90 HWTEST_F (PowerStateMachineTest, PowerStateMachine004, TestSize.Level0)
91 {
92 POWER_HILOGI(LABEL_TEST, "PowerStateMachine004::fun is start!");
93 sleep(SLEEP_WAIT_TIME_S);
94 GTEST_LOG_(INFO) << "PowerStateMachine004: Wakeup Device start.";
95 auto& powerMgrClient = PowerMgrClient::GetInstance();
96
97 // Suspend Device before test
98 GTEST_LOG_(INFO) << "PowerStateMachine004: Suspend Device before test.";
99 powerMgrClient.SuspendDevice();
100 usleep(SLEEP_WAIT_TIME_MS);
101 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine004: Prepare Fail, Screen is On.";
102 GTEST_LOG_(INFO) << "PowerStateMachine004: Screen is Off, Begin to Wakeup Device!";
103
104 powerMgrClient.WakeupDevice();
105
106 usleep(SLEEP_WAIT_TIME_MS);
107 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine004: Wakeup Device Fail, Screen is Off";
108
109 POWER_HILOGI(LABEL_TEST, "PowerStateMachine004::fun is end!");
110 GTEST_LOG_(INFO) << "PowerStateMachine004: Wakeup Device end.";
111 }
112
113 /**
114 * @tc.name: PowerStateMachine005
115 * @tc.desc: test IsScreenOn in proxy
116 * @tc.type: FUNC
117 */
118 HWTEST_F (PowerStateMachineTest, PowerStateMachine005, TestSize.Level0)
119 {
120 sleep(SLEEP_WAIT_TIME_S);
121 GTEST_LOG_(INFO) << "PowerStateMachine005: IsScreenOn start.";
122 auto& powerMgrClient = PowerMgrClient::GetInstance();
123
124 for (int i = 0; i < 3; i++) {
125 if (powerMgrClient.IsScreenOn()) {
126 powerMgrClient.SuspendDevice();
127 usleep(SLEEP_WAIT_TIME_MS);
128 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine005_" << i
129 << ": Suspend Device Fail, Screen is On";
130 } else {
131 powerMgrClient.WakeupDevice();
132 usleep(SLEEP_WAIT_TIME_MS);
133 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine005_" << i
134 << ": Wakeup Device Fail, Screen is Off";
135 }
136 }
137
138 GTEST_LOG_(INFO) << "PowerStateMachine05: IsScreenOn end.";
139 }
140
141 /**
142 * @tc.name: PowerStateMachine006
143 * @tc.desc: test WakeupDevice(int64_t timeMs, const WakeupDeviceType reason, const std::string& details) in proxy
144 * @tc.type: FUNC
145 */
146 HWTEST_F (PowerStateMachineTest, PowerStateMachine006, TestSize.Level0)
147 {
148 sleep(SLEEP_WAIT_TIME_S);
149 GTEST_LOG_(INFO) << "PowerStateMachine006: Wakeup Device start.";
150 auto& powerMgrClient = PowerMgrClient::GetInstance();
151
152 // Suspend Device before test
153 GTEST_LOG_(INFO) << "PowerStateMachine006: Suspend Device before test.";
154 powerMgrClient.SuspendDevice();
155 usleep(SLEEP_WAIT_TIME_MS);
156 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine006: Prepare Fail, Screen is On.";
157 GTEST_LOG_(INFO) << "PowerStateMachine006: Screen is Off, Begin to Wakeup Device!";
158
159 // Check illegal para details
160 GTEST_LOG_(INFO) << "PowerStateMachine006: Check illegal para details Begin!";
161 powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
162 usleep(SLEEP_WAIT_TIME_MS);
163 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine006: Check details test Fail, Screen is Off.";
164
165 // Suspend Device before test
166 GTEST_LOG_(INFO) << "PowerStateMachine006: Suspend Device before real test.";
167 powerMgrClient.SuspendDevice();
168 usleep(SLEEP_WAIT_TIME_MS);
169 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine006: Real test tprepare Fail, Screen is On.";
170 GTEST_LOG_(INFO) << "PowerStateMachine006: Screen is Off, Begin to Real Wakeup Device!";
171
172 powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION);
173
174 usleep(SLEEP_WAIT_TIME_MS);
175 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine006: Real Wakeup Device Fail, Screen is Off";
176
177 GTEST_LOG_(INFO) << "PowerStateMachine006: Wakeup Device end.";
178 }
179
180 /**
181 * @tc.name: PowerStateMachine007
182 * @tc.desc: test Suspend Device in proxy
183 * @tc.type: FUNC
184 * @tc.require: issueI6MZ3M
185 */
186 HWTEST_F (PowerStateMachineTest, PowerStateMachine007, TestSize.Level0)
187 {
188 POWER_HILOGD(LABEL_TEST, "PowerStateMachine007::fun is start!");
189 sleep(SLEEP_WAIT_TIME_S);
190 auto& powerMgrClient = PowerMgrClient::GetInstance();
191 EXPECT_TRUE(powerMgrClient.RestoreScreenOffTime());
192 POWER_HILOGD(LABEL_TEST, "PowerStateMachine007::fun is end!");
193 }
194 }
195
OnPowerStateChanged(PowerState state)196 void PowerStateMachineTest::PowerStateTest1Callback::OnPowerStateChanged(PowerState state)
197 {
198 POWER_HILOGD(LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %u.",
199 static_cast<uint32_t>(state));
200 }
201
OnPowerStateChanged(PowerState state)202 void PowerStateMachineTest::PowerStateTest2Callback::OnPowerStateChanged(PowerState state)
203 {
204 POWER_HILOGD(LABEL_TEST, "PowerStateTest2Callback::OnPowerStateChanged state = %u.",
205 static_cast<uint32_t>(state));
206 }
207
208 namespace {
209 /**
210 * @tc.name: PowerStateCallback001
211 * @tc.desc: test PowerStateCallback
212 * @tc.type: FUNC
213 */
214 HWTEST_F (PowerStateMachineTest, PowerStateCallback001, TestSize.Level0)
215 {
216 auto& powerMgrClient = PowerMgrClient::GetInstance();
217 sptr<IPowerStateCallback> cb1 = new PowerStateTest1Callback();
218 powerMgrClient.RegisterPowerStateCallback(cb1);
219 POWER_HILOGD(LABEL_TEST, "PowerStateCallback001 1.");
220 {
221 sptr<IPowerStateCallback> cb2 = new PowerStateTest2Callback();
222 powerMgrClient.UnRegisterPowerStateCallback(cb2);
223 POWER_HILOGD(LABEL_TEST, "PowerStateCallback001 2.");
224 powerMgrClient.RegisterPowerStateCallback(cb2);
225 POWER_HILOGD(LABEL_TEST, "PowerStateCallback001 3.");
226 powerMgrClient.RegisterPowerStateCallback(cb2);
227 POWER_HILOGD(LABEL_TEST, "PowerStateCallback001 4.");
228 }
229 EXPECT_TRUE(powerMgrClient.UnRegisterPowerStateCallback(cb1));
230 POWER_HILOGD(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback001 end.");
231 }
232 }
233
WakeUpthread()234 void PowerStateMachineTest::WakeUpthread()
235 {
236 auto& powerMgrClient = PowerMgrClient::GetInstance();
237 powerMgrClient.WakeupDevice();
238 }
239
Suspendthread()240 void PowerStateMachineTest::Suspendthread()
241 {
242 auto& powerMgrClient = PowerMgrClient::GetInstance();
243 powerMgrClient.SuspendDevice();
244 }
245
Rebootthread()246 void PowerStateMachineTest::Rebootthread()
247 {
248 auto& powerMgrClient = PowerMgrClient::GetInstance();
249 if (false) {
250 powerMgrClient.RebootDevice(string("RebootDeviceTestThread"));
251 }
252 }
253
Shutdownthread()254 void PowerStateMachineTest::Shutdownthread()
255 {
256 auto& powerMgrClient = PowerMgrClient::GetInstance();
257 if (false) {
258 powerMgrClient.ShutDownDevice(string("ShutDownDeviceTestThread"));
259 }
260 }
261