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_mgr_notify_test.h"
17
18 #include <iostream>
19
20 #include <common_event_data.h>
21 #include <common_event_manager.h>
22 #include <common_event_publish_info.h>
23 #include <common_event_subscriber.h>
24 #include <common_event_support.h>
25 #include <datetime_ex.h>
26 #include <gtest/gtest.h>
27 #include <if_system_ability_manager.h>
28 #include <ipc_skeleton.h>
29 #include <string_ex.h>
30
31 #include "power_common.h"
32 #include "power_mgr_client.h"
33 #include "power_mgr_service.h"
34 #include "power_state_machine.h"
35
36 using namespace testing::ext;
37 using namespace OHOS::PowerMgr;
38 using namespace OHOS;
39 using namespace std;
40 using namespace OHOS::AAFwk;
41 using namespace OHOS::Notification;
42
43 using NeedWaitFunc = std::function<bool()>;
44 using Clock = std::chrono::steady_clock;
45 using TimePoint = std::chrono::time_point<Clock>;
46
47 namespace {
48 constexpr int MAX_RETRY_TIME = 2;
49 constexpr int WAIT_EVENT_TIME_S = 1;
50 constexpr int RETRY_WAIT_TIME_US = 50000;
51 constexpr int SLEEP_WAIT_TIME_S = 3;
52
53 class TestCommonEventSubscriber : public CommonEventSubscriber {
54 public:
55 std::mutex onReceivedLock_;
56 bool received_ = false;
57 TimePoint receivedTime_;
58 std::string action_;
59
60 bool receivedScreenOFF = false;
61 bool receivedScreenOn = false;
62
TestCommonEventSubscriber(const sptr<CommonEventSubscribeInfo> & subscribeInfo)63 explicit TestCommonEventSubscriber(const sptr<CommonEventSubscribeInfo>& subscribeInfo)
64 : CommonEventSubscriber(subscribeInfo) {}
65
TestCommonEventSubscriber()66 TestCommonEventSubscriber() {}
67
~TestCommonEventSubscriber()68 ~TestCommonEventSubscriber() override {}
OnReceive(const sptr<CommonEventData> & event)69 void OnReceive(const sptr<CommonEventData> &event) override
70 {
71 GTEST_LOG_(INFO) << "PowerMgrMonitor:: OnReceive!!";
72 receivedTime_ = Clock::now();
73 received_ = true;
74 action_ = event->GetIntent()->GetAction();
75 if (action_ == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
76 receivedScreenOFF = true;
77 }
78 if (action_ == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
79 receivedScreenOn = true;
80 }
81 }
82 };
83
RegisterEvent()84 shared_ptr<TestCommonEventSubscriber> RegisterEvent()
85 {
86 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Regist Subscriber Start!!";
87 POWER_HILOGI(LABEL_TEST, "PowerMgrNotifyTest:: Regist Subscriber start!");
88 sptr<AAFwk::Skills> skill = new AAFwk::Skills();
89 skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
90 skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
91
92 sptr<CommonEventSubscribeInfo> subscriberInfo = new CommonEventSubscribeInfo();
93 subscriberInfo->SetSkills(skill);
94 int tryTimes = 0;
95 shared_ptr<TestCommonEventSubscriber> subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
96 // Notice, rightnow AddAbilityListener is not ok, we use this method to make sure register success
97 while (tryTimes < MAX_RETRY_TIME) {
98 const auto result = CommonEventManager::GetInstance().SubscribeCommonEvent(subscriber);
99 if (ERR_OK == result) {
100 break;
101 } else {
102 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber, Sleep 50ms and try again!!!";
103 usleep(RETRY_WAIT_TIME_US); // sleep 50ms
104 subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
105 }
106 tryTimes++;
107 }
108 if (MAX_RETRY_TIME == tryTimes) {
109 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber!!!";
110 return nullptr;
111 }
112 POWER_HILOGI(LABEL_TEST, "PowerMgrNotifyTest:: Regist Subscriber Success!");
113 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: register Subscriber Success!!";
114 return subscriber;
115 }
116 }
117
SetUpTestCase(void)118 void PowerMgrNotifyTest::SetUpTestCase(void)
119 {
120 }
121
TearDownTestCase(void)122 void PowerMgrNotifyTest::TearDownTestCase(void)
123 {
124 }
125
SetUp(void)126 void PowerMgrNotifyTest::SetUp(void)
127 {
128 }
129
TearDown(void)130 void PowerMgrNotifyTest::TearDown(void)
131 {
132 }
133
134 namespace {
135 /**
136 * @tc.name: PowerMgrNotifyTest001
137 * @tc.desc: test powermgr notify for screen Off
138 * @tc.type: FUNC
139 */
140 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest001, TestSize.Level1)
141 {
142 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification start.";
143 POWER_HILOGI(LABEL_TEST, "PowerMgrNotifyTest001 function start!");
144 // We need wait for 15s, to preivent the last test interfere(screen is in keyguard scene and screen is ON).
145 int waitForStatusOk = 15;
146 sleep(waitForStatusOk);
147 auto& powerMgrClient = PowerMgrClient::GetInstance();
148
149 // Wakeup Device before test
150 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Wakeup Device before test.";
151 powerMgrClient.WakeupDevice();
152 sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
153 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerMgrNotifyTest001: Prepare Fail, Screen is OFF.";
154 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Screen is On, Begin to Suspend Device!";
155
156 shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
157 EXPECT_FALSE(subscriber == nullptr);
158
159 powerMgrClient.SuspendDevice();
160
161 sleep(WAIT_EVENT_TIME_S);
162
163 auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
164 EXPECT_EQ(ERR_OK, err);
165 POWER_HILOGI(LABEL_TEST, "PowerMgrNotifyTest001 function end!");
166 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification end.";
167 }
168
169 /**
170 * @tc.name: PowerMgrNotifyTest002
171 * @tc.desc: test powermgr notify for screen On
172 * @tc.type: FUNC
173 */
174 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest002, TestSize.Level1)
175 {
176 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification start.";
177 POWER_HILOGI(LABEL_TEST, "PowerMgrNotifyTest002 function start!");
178 sleep(SLEEP_WAIT_TIME_S);
179 auto& powerMgrClient = PowerMgrClient::GetInstance();
180
181 // Wakeup Device before test
182 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Suspend Device before test.";
183 powerMgrClient.SuspendDevice();
184 sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
185 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerMgrNotifyTest002: Prepare Fail, Screen is On.";
186 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Screen is Off, Begin to Wakeup Device!";
187
188 shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
189 EXPECT_FALSE(subscriber == nullptr);
190
191 powerMgrClient.WakeupDevice();
192
193 sleep(WAIT_EVENT_TIME_S);
194
195 auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
196 EXPECT_EQ(ERR_OK, err);
197 POWER_HILOGI(LABEL_TEST, "PowerMgrNotifyTest002 function end!");
198 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification end.";
199 }
200 }
201