1 /*
2 * Copyright (c) 2021 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 shared_ptr<OHOS::AppExecFwk::EventHandler> receiveHandler_ = nullptr;
60
61 bool receivedScreenOFF = false;
62 bool receivedScreenOn = false;
63
TestCommonEventSubscriber(const sptr<CommonEventSubscribeInfo> & subscribeInfo)64 explicit TestCommonEventSubscriber(const sptr<CommonEventSubscribeInfo>& subscribeInfo)
65 : CommonEventSubscriber(subscribeInfo) {}
66
TestCommonEventSubscriber()67 TestCommonEventSubscriber() {}
68
~TestCommonEventSubscriber()69 ~TestCommonEventSubscriber() override {}
OnReceive(const sptr<CommonEventData> & event)70 void OnReceive(const sptr<CommonEventData> &event) override
71 {
72 GTEST_LOG_(INFO) << "PowerMgrMonitor:: OnReceive!!";
73 receiveHandler_ = OHOS::AppExecFwk::EventHandler::Current();
74 receivedTime_ = Clock::now();
75 received_ = true;
76 action_ = event->GetIntent()->GetAction();
77 if (action_ == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
78 receivedScreenOFF = true;
79 }
80 if (action_ == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
81 receivedScreenOn = true;
82 }
83 }
84 };
85
RegisterEvent()86 shared_ptr<TestCommonEventSubscriber> RegisterEvent()
87 {
88 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Regist Subscriber Start!!";
89 sptr<AAFwk::Skills> skill = new AAFwk::Skills();
90 skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
91 skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
92
93 sptr<CommonEventSubscribeInfo> subscriberInfo = new CommonEventSubscribeInfo();
94 subscriberInfo->SetSkills(skill);
95 int tryTimes = 0;
96 shared_ptr<TestCommonEventSubscriber> subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
97 // Notice, rightnow AddAbilityListener is not ok, we use this method to make sure register success
98 while (tryTimes < MAX_RETRY_TIME) {
99 const auto result = CommonEventManager::GetInstance().SubscribeCommonEvent(subscriber);
100 if (ERR_OK == result) {
101 break;
102 } else {
103 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber, Sleep 50ms and try again!!!";
104 usleep(RETRY_WAIT_TIME_US); // sleep 50ms
105 // Reset powerMgrMonitor_, otherwise we will register fail
106 subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
107 }
108 tryTimes++;
109 }
110 if (MAX_RETRY_TIME == tryTimes) {
111 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber!!!";
112 return nullptr;
113 }
114 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: register Subscriber Success!!";
115 return subscriber;
116 }
117 }
118
SetUpTestCase(void)119 void PowerMgrNotifyTest::SetUpTestCase(void)
120 {
121 }
122
TearDownTestCase(void)123 void PowerMgrNotifyTest::TearDownTestCase(void)
124 {
125 }
126
SetUp(void)127 void PowerMgrNotifyTest::SetUp(void)
128 {
129 }
130
TearDown(void)131 void PowerMgrNotifyTest::TearDown(void)
132 {
133 }
134
135 namespace {
136 /**
137 * @tc.name: PowerMgrNotifyTest001
138 * @tc.desc: test powermgr notify for screen Off
139 * @tc.type: FUNC
140 */
141 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest001, TestSize.Level0)
142 {
143 // We need wait for 15s, to preivent the last test interfere(screen is in keyguard scene and screen is ON).
144 int waitForStatusOk = 15;
145 sleep(waitForStatusOk);
146 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification start.";
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
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.Level0)
175 {
176 sleep(SLEEP_WAIT_TIME_S);
177 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification start.";
178 auto& powerMgrClient = PowerMgrClient::GetInstance();
179
180 // Wakeup Device before test
181 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Suspend Device before test.";
182 powerMgrClient.SuspendDevice();
183 sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
184 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerMgrNotifyTest002: Prepare Fail, Screen is On.";
185 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Screen is Off, Begin to Wakeup Device!";
186
187 shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
188 EXPECT_FALSE(subscriber == nullptr);
189
190 powerMgrClient.WakeupDevice();
191
192 sleep(WAIT_EVENT_TIME_S);
193
194 auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
195 EXPECT_EQ(ERR_OK, err);
196
197 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification end.";
198 }
199 }
200