• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ohos/aafwk/content/intent.h>
30 #include <string_ex.h>
31 
32 #include "power_common.h"
33 #include "power_mgr_client.h"
34 #include "power_mgr_service.h"
35 #include "power_state_machine.h"
36 
37 using namespace testing::ext;
38 using namespace OHOS::PowerMgr;
39 using namespace OHOS;
40 using namespace std;
41 using namespace OHOS::AAFwk;
42 using namespace OHOS::Notification;
43 
44 using NeedWaitFunc = std::function<bool()>;
45 using Clock = std::chrono::steady_clock;
46 using TimePoint = std::chrono::time_point<Clock>;
47 
48 namespace {
49 constexpr int MAX_RETRY_TIME = 2;
50 constexpr int WAIT_EVENT_TIME_S = 1;
51 constexpr int RETRY_WAIT_TIME_US = 50000;
52 constexpr int SLEEP_WAIT_TIME_S = 3;
53 
54 class TestCommonEventSubscriber : public CommonEventSubscriber {
55 public:
56     std::mutex onReceivedLock_;
57     bool received_ = false;
58     TimePoint receivedTime_;
59     std::string action_;
60     shared_ptr<OHOS::AppExecFwk::EventHandler> receiveHandler_ = nullptr;
61 
62     bool receivedScreenOFF = false;
63     bool receivedScreenOn = false;
64 
TestCommonEventSubscriber(const sptr<CommonEventSubscribeInfo> & subscribeInfo)65     explicit TestCommonEventSubscriber(const sptr<CommonEventSubscribeInfo>& subscribeInfo)
66         : CommonEventSubscriber(subscribeInfo) {}
67 
TestCommonEventSubscriber()68     TestCommonEventSubscriber() {}
69 
~TestCommonEventSubscriber()70     ~TestCommonEventSubscriber() override {}
OnReceive(const sptr<CommonEventData> & event)71     void OnReceive(const sptr<CommonEventData> &event) override
72     {
73         GTEST_LOG_(INFO) << "PowerMgrMonitor:: OnReceive!!";
74         receiveHandler_ = OHOS::AppExecFwk::EventHandler::Current();
75         receivedTime_ = Clock::now();
76         received_ = true;
77         action_ = event->GetIntent()->GetAction();
78         if (action_ == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
79             receivedScreenOFF = true;
80         }
81         if (action_ == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
82             receivedScreenOn = true;
83         }
84     }
85 };
86 
RegisterEvent()87 shared_ptr<TestCommonEventSubscriber> RegisterEvent()
88 {
89     GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Regist Subscriber Start!!";
90     sptr<AAFwk::Skills> skill = new AAFwk::Skills();
91     skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
92     skill->AddAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
93 
94     sptr<CommonEventSubscribeInfo> subscriberInfo = new CommonEventSubscribeInfo();
95     subscriberInfo->SetSkills(skill);
96     int tryTimes = 0;
97     shared_ptr<TestCommonEventSubscriber> subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
98     // Notice, rightnow AddAbilityListener is not ok, we use this method to make sure register success
99     while (tryTimes < MAX_RETRY_TIME) {
100         const auto result = CommonEventManager::GetInstance().SubscribeCommonEvent(subscriber);
101         if (ERR_OK == result) {
102             break;
103         } else {
104             GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber, Sleep 50ms and try again!!!";
105             usleep(RETRY_WAIT_TIME_US); // sleep 50ms
106             // Reset powerMgrMonitor_, otherwise we will register fail
107             subscriber = make_shared<TestCommonEventSubscriber>(subscriberInfo);
108         }
109         tryTimes++;
110     }
111     if (MAX_RETRY_TIME == tryTimes) {
112         GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: Fail to register Subscriber!!!";
113         return nullptr;
114     }
115     GTEST_LOG_(INFO) << "PowerMgrNotifyTest:: register Subscriber Success!!";
116     return subscriber;
117 }
118 }
119 
SetUpTestCase(void)120 void PowerMgrNotifyTest::SetUpTestCase(void)
121 {
122 }
123 
TearDownTestCase(void)124 void PowerMgrNotifyTest::TearDownTestCase(void)
125 {
126 }
127 
SetUp(void)128 void PowerMgrNotifyTest::SetUp(void)
129 {
130 }
131 
TearDown(void)132 void PowerMgrNotifyTest::TearDown(void)
133 {
134 }
135 
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