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_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 sptr<AAFwk::Skills> skill = new AAFwk::Skills();
88 skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
89 skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
90
91 sptr<CommonEventSubscribeInfo> subscriberInfo = new CommonEventSubscribeInfo();
92 subscriberInfo->SetSkills(skill);
93 int tryTimes = 0;
94 shared_ptr<TestCommonEventSubscriber> subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
95 // Notice, rightnow AddAbilityListener is not ok, we use this method to make sure register success
96 while (tryTimes < MAX_RETRY_TIME) {
97 const auto result = CommonEventManager::GetInstance().SubscribeCommonEvent(subscriber);
98 if (ERR_OK == result) {
99 break;
100 } else {
101 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber, Sleep 50ms and try again!!!";
102 usleep(RETRY_WAIT_TIME_US); // sleep 50ms
103 subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
104 }
105 tryTimes++;
106 }
107 if (MAX_RETRY_TIME == tryTimes) {
108 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber!!!";
109 return nullptr;
110 }
111 GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: register Subscriber Success!!";
112 return subscriber;
113 }
114 }
115
SetUpTestCase(void)116 void PowerMgrNotifyTest::SetUpTestCase(void)
117 {
118 }
119
TearDownTestCase(void)120 void PowerMgrNotifyTest::TearDownTestCase(void)
121 {
122 }
123
SetUp(void)124 void PowerMgrNotifyTest::SetUp(void)
125 {
126 }
127
TearDown(void)128 void PowerMgrNotifyTest::TearDown(void)
129 {
130 }
131
132 namespace {
133 /**
134 * @tc.name: PowerMgrNotifyTest001
135 * @tc.desc: test powermgr notify for screen Off
136 * @tc.type: FUNC
137 */
138 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest001, TestSize.Level0)
139 {
140 // We need wait for 15s, to preivent the last test interfere(screen is in keyguard scene and screen is ON).
141 int waitForStatusOk = 15;
142 sleep(waitForStatusOk);
143 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification start.";
144 auto& powerMgrClient = PowerMgrClient::GetInstance();
145
146 // Wakeup Device before test
147 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Wakeup Device before test.";
148 powerMgrClient.WakeupDevice();
149 sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
150 EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerMgrNotifyTest001: Prepare Fail, Screen is OFF.";
151 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Screen is On, Begin to Suspend Device!";
152
153 shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
154 EXPECT_FALSE(subscriber == nullptr);
155
156 powerMgrClient.SuspendDevice();
157
158 sleep(WAIT_EVENT_TIME_S);
159
160 auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
161 EXPECT_EQ(ERR_OK, err);
162
163 GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification end.";
164 }
165
166 /**
167 * @tc.name: PowerMgrNotifyTest002
168 * @tc.desc: test powermgr notify for screen On
169 * @tc.type: FUNC
170 */
171 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest002, TestSize.Level0)
172 {
173 sleep(SLEEP_WAIT_TIME_S);
174 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification start.";
175 auto& powerMgrClient = PowerMgrClient::GetInstance();
176
177 // Wakeup Device before test
178 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Suspend Device before test.";
179 powerMgrClient.SuspendDevice();
180 sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
181 EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerMgrNotifyTest002: Prepare Fail, Screen is On.";
182 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Screen is Off, Begin to Wakeup Device!";
183
184 shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
185 EXPECT_FALSE(subscriber == nullptr);
186
187 powerMgrClient.WakeupDevice();
188
189 sleep(WAIT_EVENT_TIME_S);
190
191 auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
192 EXPECT_EQ(ERR_OK, err);
193
194 GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification end.";
195 }
196 }
197