1 /*
2 * Copyright (c) 2022-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 "thermal_level_event_system_test.h"
17
18 #include <common_event_data.h>
19 #include <common_event_manager.h>
20 #include <common_event_publish_info.h>
21 #include <common_event_subscriber.h>
22 #include <common_event_support.h>
23 #include <condition_variable>
24 #include <mutex>
25
26 #include "mock_thermal_mgr_client.h"
27 #include "thermal_level_info.h"
28 #include "thermal_log.h"
29 #include "thermal_mgr_client.h"
30 #include "thermal_mgr_listener.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS;
35 using namespace std;
36 using namespace OHOS::AAFwk;
37 using namespace OHOS::EventFwk;
38
39 namespace {
40 std::condition_variable g_callbackCV;
41 std::mutex g_mutex;
42 constexpr int64_t TIME_OUT = 1;
43 bool g_callbackTriggered = false;
44 } // namespace
45
Notify()46 void Notify()
47 {
48 std::unique_lock<std::mutex> lock(g_mutex);
49 g_callbackTriggered = true;
50 lock.unlock();
51 g_callbackCV.notify_one();
52 }
53
Wait()54 void Wait()
55 {
56 std::unique_lock<std::mutex> lock(g_mutex);
57 g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] { return g_callbackTriggered; });
58 EXPECT_TRUE(g_callbackTriggered);
59 g_callbackTriggered = false;
60 }
61
62 class CommonEventThermalLevel1Test : public CommonEventSubscriber {
63 public:
64 CommonEventThermalLevel1Test() = default;
65 explicit CommonEventThermalLevel1Test(const CommonEventSubscribeInfo& subscriberInfo);
66 virtual ~CommonEventThermalLevel1Test() = default;
67 virtual void OnReceiveEvent(const CommonEventData& data);
68 static shared_ptr<CommonEventThermalLevel1Test> RegisterEvent();
69 };
70
CommonEventThermalLevel1Test(const CommonEventSubscribeInfo & subscriberInfo)71 CommonEventThermalLevel1Test::CommonEventThermalLevel1Test(const CommonEventSubscribeInfo& subscriberInfo)
72 : CommonEventSubscriber(subscriberInfo)
73 {
74 }
75
76 class CommonEventThermalLevel2Test : public CommonEventSubscriber {
77 public:
78 CommonEventThermalLevel2Test() = default;
79 explicit CommonEventThermalLevel2Test(const CommonEventSubscribeInfo& subscriberInfo);
80 virtual ~CommonEventThermalLevel2Test() = default;
81 virtual void OnReceiveEvent(const CommonEventData& data);
82 static shared_ptr<CommonEventThermalLevel2Test> RegisterEvent();
83 };
84
CommonEventThermalLevel2Test(const CommonEventSubscribeInfo & subscriberInfo)85 CommonEventThermalLevel2Test::CommonEventThermalLevel2Test(const CommonEventSubscribeInfo& subscriberInfo)
86 : CommonEventSubscriber(subscriberInfo)
87 {
88 }
89
90 class CommonEventThermalLevel3Test : public CommonEventSubscriber {
91 public:
92 CommonEventThermalLevel3Test() = default;
93 explicit CommonEventThermalLevel3Test(const CommonEventSubscribeInfo& subscriberInfo);
94 virtual ~CommonEventThermalLevel3Test() = default;
95 virtual void OnReceiveEvent(const CommonEventData& data);
96 static shared_ptr<CommonEventThermalLevel3Test> RegisterEvent();
97 };
98
CommonEventThermalLevel3Test(const CommonEventSubscribeInfo & subscriberInfo)99 CommonEventThermalLevel3Test::CommonEventThermalLevel3Test(const CommonEventSubscribeInfo& subscriberInfo)
100 : CommonEventSubscriber(subscriberInfo)
101 {
102 }
103
104 class CommonEventThermalIdleTrueTest : public CommonEventSubscriber {
105 public:
106 CommonEventThermalIdleTrueTest() = default;
107 explicit CommonEventThermalIdleTrueTest(const CommonEventSubscribeInfo& subscriberInfo);
108 virtual ~CommonEventThermalIdleTrueTest() = default;
109 virtual void OnReceiveEvent(const CommonEventData& data);
110 static shared_ptr<CommonEventThermalIdleTrueTest> RegisterEvent();
111 };
112
CommonEventThermalIdleTrueTest(const CommonEventSubscribeInfo & subscriberInfo)113 CommonEventThermalIdleTrueTest::CommonEventThermalIdleTrueTest(const CommonEventSubscribeInfo& subscriberInfo)
114 : CommonEventSubscriber(subscriberInfo)
115 {
116 }
117
118 class CommonEventThermalIdleFalseTest : public CommonEventSubscriber {
119 public:
120 CommonEventThermalIdleFalseTest() = default;
121 explicit CommonEventThermalIdleFalseTest(const CommonEventSubscribeInfo& subscriberInfo);
122 virtual ~CommonEventThermalIdleFalseTest() = default;
123 virtual void OnReceiveEvent(const CommonEventData& data);
124 static shared_ptr<CommonEventThermalIdleFalseTest> RegisterEvent();
125 };
126
CommonEventThermalIdleFalseTest(const CommonEventSubscribeInfo & subscriberInfo)127 CommonEventThermalIdleFalseTest::CommonEventThermalIdleFalseTest(const CommonEventSubscribeInfo& subscriberInfo)
128 : CommonEventSubscriber(subscriberInfo)
129 {
130 }
131
OnReceiveEvent(const CommonEventData & data)132 void CommonEventThermalLevel1Test::OnReceiveEvent(const CommonEventData& data)
133 {
134 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevel1Test: OnReceiveEvent Enter");
135 int32_t invalidLevel = -1;
136 std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
137 int32_t level = data.GetWant().GetIntParam(key, invalidLevel);
138 GTEST_LOG_(INFO) << "thermal level: " << level;
139 Notify();
140 EXPECT_EQ(level, static_cast<int32_t>(ThermalLevel::NORMAL)) << "get thermal level failed";
141 }
142
OnReceiveEvent(const CommonEventData & data)143 void CommonEventThermalLevel2Test::OnReceiveEvent(const CommonEventData& data)
144 {
145 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevel2Test: OnReceiveEvent Enter");
146 int32_t invalidLevel = -1;
147 std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
148 int32_t level = data.GetWant().GetIntParam(key, invalidLevel);
149 GTEST_LOG_(INFO) << "thermal level: " << level;
150 Notify();
151 EXPECT_EQ(level, static_cast<int32_t>(ThermalLevel::WARM)) << "get thermal level failed";
152 }
153
OnReceiveEvent(const CommonEventData & data)154 void CommonEventThermalLevel3Test::OnReceiveEvent(const CommonEventData& data)
155 {
156 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevel3Test: OnReceiveEvent Enter");
157 int32_t invalidLevel = -1;
158 std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
159 int32_t level = data.GetWant().GetIntParam(key, invalidLevel);
160 GTEST_LOG_(INFO) << "thermal level: " << level;
161 Notify();
162 EXPECT_EQ(level, static_cast<int32_t>(ThermalLevel::HOT)) << "get thermal level failed";
163 }
164
OnReceiveEvent(const CommonEventData & data)165 void CommonEventThermalIdleTrueTest::OnReceiveEvent(const CommonEventData& data)
166 {
167 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalIdleTrueTest: OnReceiveEvent Enter");
168 bool invalidState = false;
169 std::string key = ToString(static_cast<uint32_t>(ChargeIdleEventCode::EVENT_CODE_CHARGE_IDLE_STATE));
170 bool getState = data.GetWant().GetBoolParam(key, invalidState);
171 GTEST_LOG_(INFO) << "charger state: " << getState;
172 Notify();
173 EXPECT_EQ(getState, true) << "get charger state failed";
174 }
175
OnReceiveEvent(const CommonEventData & data)176 void CommonEventThermalIdleFalseTest::OnReceiveEvent(const CommonEventData& data)
177 {
178 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalIdleFalseTest: OnReceiveEvent Enter");
179 bool invalidState = true;
180 std::string key = ToString(static_cast<uint32_t>(ChargeIdleEventCode::EVENT_CODE_CHARGE_IDLE_STATE));
181 bool getState = data.GetWant().GetBoolParam(key, invalidState);
182 GTEST_LOG_(INFO) << "charger state: " << getState;
183 Notify();
184 EXPECT_EQ(getState, false) << "get charger state failed";
185 }
186
RegisterEvent()187 shared_ptr<CommonEventThermalLevel1Test> CommonEventThermalLevel1Test::RegisterEvent()
188 {
189 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
190 int32_t retryTimes = 2;
191 bool succeed = false;
192 MatchingSkills matchingSkills;
193 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
194 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
195 auto subscriberPtr = std::make_shared<CommonEventThermalLevel1Test>(subscribeInfo);
196 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
197 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
198 }
199 if (!succeed) {
200 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
201 return nullptr;
202 }
203 return subscriberPtr;
204 }
205
RegisterEvent()206 shared_ptr<CommonEventThermalLevel2Test> CommonEventThermalLevel2Test::RegisterEvent()
207 {
208 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
209 int32_t retryTimes = 2;
210 bool succeed = false;
211 MatchingSkills matchingSkills;
212 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
213 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
214 auto subscriberPtr = std::make_shared<CommonEventThermalLevel2Test>(subscribeInfo);
215 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
216 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
217 }
218 if (!succeed) {
219 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
220 return nullptr;
221 }
222 return subscriberPtr;
223 }
224
RegisterEvent()225 shared_ptr<CommonEventThermalLevel3Test> CommonEventThermalLevel3Test::RegisterEvent()
226 {
227 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
228 int32_t retryTimes = 2;
229 bool succeed = false;
230 MatchingSkills matchingSkills;
231 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
232 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
233 auto subscriberPtr = std::make_shared<CommonEventThermalLevel3Test>(subscribeInfo);
234 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
235 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
236 }
237 if (!succeed) {
238 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
239 return nullptr;
240 }
241 return subscriberPtr;
242 }
243
RegisterEvent()244 shared_ptr<CommonEventThermalIdleTrueTest> CommonEventThermalIdleTrueTest::RegisterEvent()
245 {
246 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
247 int32_t retryTimes = 2;
248 bool succeed = false;
249 MatchingSkills matchingSkills;
250 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGE_IDLE_MODE_CHANGED);
251 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
252 auto subscriberPtr = std::make_shared<CommonEventThermalIdleTrueTest>(subscribeInfo);
253 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
254 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
255 }
256 if (!succeed) {
257 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
258 return nullptr;
259 }
260 return subscriberPtr;
261 }
262
RegisterEvent()263 shared_ptr<CommonEventThermalIdleFalseTest> CommonEventThermalIdleFalseTest::RegisterEvent()
264 {
265 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
266 int32_t retryTimes = 2;
267 bool succeed = false;
268 MatchingSkills matchingSkills;
269 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGE_IDLE_MODE_CHANGED);
270 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
271 auto subscriberPtr = std::make_shared<CommonEventThermalIdleFalseTest>(subscribeInfo);
272 for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
273 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
274 }
275 if (!succeed) {
276 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
277 return nullptr;
278 }
279 return subscriberPtr;
280 }
281
TearDown()282 void ThermalLevelEventSystemTest::TearDown()
283 {
284 InitNode();
285 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
286 thermalMgrClient.SetScene("");
287 MockThermalMgrClient::GetInstance().GetThermalInfo();
288 g_callbackTriggered = false;
289 }
290
291 namespace {
292 /*
293 * @tc.number: ThermalLevelEventSystemTest001
294 * @tc.name: ThermalLevelEventSystemTest
295 * @tc.desc: Verify the receive the level common event
296 */
297 HWTEST_F (ThermalLevelEventSystemTest, ThermalLevelEventSystemTest001, TestSize.Level0)
298 {
299 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest001: Start");
300 if (!IsMock(BATTERY_PATH) || IsVendor()) {
301 return;
302 }
303 shared_ptr<CommonEventThermalLevel1Test> subscriber = CommonEventThermalLevel1Test::RegisterEvent();
304 int32_t batteryTemp = 40600;
305 int32_t ret = SetNodeValue(batteryTemp, BATTERY_PATH);
306 EXPECT_EQ(true, ret == ERR_OK);
307 MockThermalMgrClient::GetInstance().GetThermalInfo();
308 Wait();
309 CommonEventManager::UnSubscribeCommonEvent(subscriber);
310 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest001: End");
311 }
312
313 /*
314 * @tc.number: ThermalLevelEventSystemTest002
315 * @tc.name: ThermalLevelEventSystemTest
316 * @tc.desc: Verify the receive the level common event
317 */
318 HWTEST_F (ThermalLevelEventSystemTest, ThermalLevelEventSystemTest002, TestSize.Level0)
319 {
320 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest002: Start");
321 if (!IsMock(BATTERY_PATH) || IsVendor()) {
322 return;
323 }
324 shared_ptr<CommonEventThermalLevel2Test> subscriber = CommonEventThermalLevel2Test::RegisterEvent();
325 int32_t batteryTemp = 43600;
326 int32_t ret = SetNodeValue(batteryTemp, BATTERY_PATH);
327 EXPECT_EQ(true, ret == ERR_OK);
328 MockThermalMgrClient::GetInstance().GetThermalInfo();
329 Wait();
330 CommonEventManager::UnSubscribeCommonEvent(subscriber);
331 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest002: End");
332 }
333
334 /*
335 * @tc.number: ThermalLevelEventSystemTest003
336 * @tc.name: ThermalLevelEventSystemTest
337 * @tc.desc: Verify the receive the level common event
338 */
339 HWTEST_F (ThermalLevelEventSystemTest, ThermalLevelEventSystemTest003, TestSize.Level0)
340 {
341 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest003: Start");
342 if (!IsMock(BATTERY_PATH) || IsVendor()) {
343 return;
344 }
345 shared_ptr<CommonEventThermalLevel3Test> subscriber = CommonEventThermalLevel3Test::RegisterEvent();
346 int32_t batteryTemp = 46600;
347 int32_t ret = SetNodeValue(batteryTemp, BATTERY_PATH);
348 EXPECT_EQ(true, ret == ERR_OK);
349 MockThermalMgrClient::GetInstance().GetThermalInfo();
350 Wait();
351 CommonEventManager::UnSubscribeCommonEvent(subscriber);
352 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest003: End");
353 }
354
355 /*
356 * @tc.number: ThermalLevelEventSystemTest004
357 * @tc.name: ThermalLevelEventSystemTest
358 * @tc.desc: Verify the receive the idle common event
359 */
360 HWTEST_F (ThermalLevelEventSystemTest, ThermalLevelEventSystemTest004, TestSize.Level0)
361 {
362 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest004: Start");
363 if (!IsMock(BATTERY_CAPACITY_PATH) || !IsMock(CHARGER_CURRENT_PATH) || IsVendor()) {
364 return;
365 }
366 shared_ptr<CommonEventThermalIdleTrueTest> subscriber = CommonEventThermalIdleTrueTest::RegisterEvent();
367 int32_t batteryCapacity = 90;
368 int32_t ret = SetNodeValue(batteryCapacity, BATTERY_CAPACITY_PATH);
369 EXPECT_EQ(true, ret == ERR_OK);
370
371 int32_t chargerCurrent = 1100;
372 ret = SetNodeValue(chargerCurrent, CHARGER_CURRENT_PATH);
373 EXPECT_EQ(true, ret == ERR_OK);
374
375 system("hidumper -s 3302 -a -r");
376
377 Wait();
378 CommonEventManager::UnSubscribeCommonEvent(subscriber);
379 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest004: End");
380 }
381
382 /*
383 * @tc.number: ThermalLevelEventSystemTest005
384 * @tc.name: ThermalLevelEventSystemTest
385 * @tc.desc: Verify the receive the idle common event
386 */
387 HWTEST_F (ThermalLevelEventSystemTest, ThermalLevelEventSystemTest005, TestSize.Level0)
388 {
389 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest005: Start");
390 if (!IsMock(BATTERY_CAPACITY_PATH) || !IsMock(CHARGER_CURRENT_PATH) || IsVendor()) {
391 return;
392 }
393 shared_ptr<CommonEventThermalIdleFalseTest> subscriber = CommonEventThermalIdleFalseTest::RegisterEvent();
394
395 int32_t batteryCapacity = 70;
396 int32_t ret = SetNodeValue(batteryCapacity, BATTERY_CAPACITY_PATH);
397 EXPECT_EQ(true, ret == ERR_OK);
398
399 int32_t chargerCurrent = 900;
400 ret = SetNodeValue(chargerCurrent, CHARGER_CURRENT_PATH);
401 EXPECT_EQ(true, ret == ERR_OK);
402
403 system("hidumper -s 3302 -a -u");
404
405 Wait();
406 CommonEventManager::UnSubscribeCommonEvent(subscriber);
407 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventSystemTest005: End");
408 }
409 } // namespace
410