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 
68     ~TestCommonEventSubscriber() override {}
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  */
HWTEST_F(PowerMgrNotifyTest, PowerMgrNotifyTest001, TestSize.Level0)138 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest001, TestSize.Level0)
139 {
140     GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification start.";
141     POWER_HILOGD(LABEL_TEST, "PowerMgrNotifyTest001 start.");
142     // We need wait for 15s, to preivent the last test interfere(screen is in keyguard scene and screen is ON).
143     int waitForStatusOk = 15;
144     sleep(waitForStatusOk);
145     auto& powerMgrClient = PowerMgrClient::GetInstance();
146 
147     // Wakeup Device before test
148     GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Wakeup Device before test.";
149     powerMgrClient.WakeupDevice();
150     sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
151     EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerMgrNotifyTest001: Prepare Fail, Screen is OFF.";
152     GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Screen is On, Begin to Suspend Device!";
153 
154     shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
155     EXPECT_FALSE(subscriber == nullptr);
156 
157     powerMgrClient.SuspendDevice();
158 
159     sleep(WAIT_EVENT_TIME_S);
160 
161     auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
162     EXPECT_EQ(ERR_OK, err);
163     POWER_HILOGD(LABEL_TEST, "PowerMgrNotifyTest001 end.");
164     GTEST_LOG_(INFO) << "PowerMgrNotifyTest001: Test ScreenOFF Notification end.";
165 }
166 
167 /**
168  * @tc.name: PowerMgrNotifyTest002
169  * @tc.desc: test powermgr notify for screen On
170  * @tc.type: FUNC
171  */
HWTEST_F(PowerMgrNotifyTest, PowerMgrNotifyTest002, TestSize.Level0)172 HWTEST_F (PowerMgrNotifyTest, PowerMgrNotifyTest002, TestSize.Level0)
173 {
174     GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification start.";
175     POWER_HILOGD(LABEL_TEST, "PowerMgrNotifyTest002 start.");
176     sleep(SLEEP_WAIT_TIME_S);
177     auto& powerMgrClient = PowerMgrClient::GetInstance();
178 
179     // Wakeup Device before test
180     GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Suspend Device before test.";
181     powerMgrClient.SuspendDevice();
182     sleep(SLEEP_WAIT_TIME_S); // wait for 3 second
183     EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerMgrNotifyTest002: Prepare Fail, Screen is On.";
184     GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Screen is Off, Begin to Wakeup Device!";
185 
186     shared_ptr<TestCommonEventSubscriber> subscriber = RegisterEvent();
187     EXPECT_FALSE(subscriber == nullptr);
188 
189     powerMgrClient.WakeupDevice();
190 
191     sleep(WAIT_EVENT_TIME_S);
192 
193     auto err = CommonEventManager::GetInstance().UnsubscribeCommonEvent(subscriber);
194     EXPECT_EQ(ERR_OK, err);
195     POWER_HILOGD(LABEL_TEST, "PowerMgrNotifyTest002 end.");
196     GTEST_LOG_(INFO) << "PowerMgrNotifyTest002: Test ScreenOn Notification end.";
197 }
198 }
199