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 "thermal_level_event_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
31 using namespace testing::ext;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS;
34 using namespace std;
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::EventFwk;
37
38 namespace {
39 std::condition_variable g_callbackCV;
40 std::mutex g_mutex;
41 constexpr int64_t TIME_OUT = 1;
42 bool g_callbackTriggered = false;
43 int32_t g_thermalLevel = -1;
44
Notify()45 void Notify()
46 {
47 std::unique_lock<std::mutex> lock(g_mutex);
48 g_callbackTriggered = true;
49 lock.unlock();
50 g_callbackCV.notify_one();
51 }
52
Wait()53 void Wait()
54 {
55 std::unique_lock<std::mutex> lock(g_mutex);
56 g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] { return g_callbackTriggered; });
57 EXPECT_TRUE(g_callbackTriggered);
58 g_callbackTriggered = false;
59 }
60 } // namespace
61
62 class CommonEventThermalLevelTest : public CommonEventSubscriber {
63 public:
64 CommonEventThermalLevelTest() = default;
65 explicit CommonEventThermalLevelTest(const CommonEventSubscribeInfo &subscriberInfo);
~CommonEventThermalLevelTest()66 virtual ~CommonEventThermalLevelTest() {};
67 virtual void OnReceiveEvent(const CommonEventData &data);
68 static shared_ptr<CommonEventThermalLevelTest> RegisterEvent();
69 };
70
CommonEventThermalLevelTest(const CommonEventSubscribeInfo & subscriberInfo)71 CommonEventThermalLevelTest::CommonEventThermalLevelTest
72 (const CommonEventSubscribeInfo &subscriberInfo) : CommonEventSubscriber(subscriberInfo) {}
73
74
OnReceiveEvent(const CommonEventData & data)75 void CommonEventThermalLevelTest::OnReceiveEvent(const CommonEventData &data)
76 {
77 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest: OnReceiveEvent Enter");
78 int invalidLevel = -1;
79 std::string action = data.GetWant().GetAction();
80 EXPECT_TRUE(action == CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
81 if (action == CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) {
82 std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
83 int32_t level = data.GetWant().GetIntParam(key, invalidLevel);
84 GTEST_LOG_(INFO) << "thermal level: " << level;
85 EXPECT_TRUE(g_thermalLevel == level);
86 }
87 Notify();
88 }
89
RegisterEvent()90 shared_ptr<CommonEventThermalLevelTest> CommonEventThermalLevelTest::RegisterEvent()
91 {
92 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
93 static const int32_t MAX_RETRY_TIMES = 2;
94 auto succeed = false;
95 MatchingSkills matchingSkills;
96 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
97 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
98 auto subscriberPtr = std::make_shared<CommonEventThermalLevelTest>(subscribeInfo);
99 for (int32_t tryTimes = 0; tryTimes < MAX_RETRY_TIMES; tryTimes++) {
100 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
101 }
102 if (!succeed) {
103 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
104 return nullptr;
105 }
106 return subscriberPtr;
107 }
108
TearDown()109 void ThermalLevelEventTest::TearDown()
110 {
111 InitNode();
112 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
113 thermalMgrClient.SetScene("");
114 MockThermalMgrClient::GetInstance().GetThermalInfo();
115 g_thermalLevel = -1;
116 g_callbackTriggered = false;
117 }
118
119 namespace {
120 /*
121 * @tc.number: ThermalLevelEventTest001
122 * @tc.name: ThermalLevelEventTest
123 * @tc.desc: Verify the receive the level common event
124 */
125 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest001, TestSize.Level0)
126 {
127 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest001: start");
128 if (!IsMock(BATTERY_PATH) || IsVendor()) {
129 return;
130 }
131 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
132 int32_t batteryTemp = 41000;
133 g_thermalLevel = 1;
134 SetNodeValue(batteryTemp, BATTERY_PATH);
135 MockThermalMgrClient::GetInstance().GetThermalInfo();
136 Wait();
137 CommonEventManager::UnSubscribeCommonEvent(subscriber);
138 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest001: end");
139 }
140
141 /*
142 * @tc.number: ThermalLevelEventTest002
143 * @tc.name: ThermalLevelEventTest
144 * @tc.desc: Verify the receive the level common event
145 */
146 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest002, TestSize.Level0)
147 {
148 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest002: start");
149 if (!IsMock(BATTERY_PATH) || IsVendor()) {
150 return;
151 }
152 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
153 int32_t batteryTemp = 43100;
154 g_thermalLevel = 2;
155 SetNodeValue(batteryTemp, BATTERY_PATH);
156 MockThermalMgrClient::GetInstance().GetThermalInfo();
157 Wait();
158 CommonEventManager::UnSubscribeCommonEvent(subscriber);
159 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest002: end");
160 }
161
162 /*
163 * @tc.number: ThermalLevelEventTest003
164 * @tc.name: ThermalLevelEventTest
165 * @tc.desc: Verify the receive the level common event
166 */
167 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest003, TestSize.Level0)
168 {
169 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest003: start");
170 if (!IsMock(BATTERY_PATH) || IsVendor()) {
171 return;
172 }
173 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
174 int32_t batteryTemp = 46100;
175 g_thermalLevel = 3;
176 SetNodeValue(batteryTemp, BATTERY_PATH);
177 MockThermalMgrClient::GetInstance().GetThermalInfo();
178 Wait();
179 CommonEventManager::UnSubscribeCommonEvent(subscriber);
180 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest003: end");
181 }
182
183 /*
184 * @tc.number: ThermalLevelEventTest004
185 * @tc.name: ThermalLevelEventTest
186 * @tc.desc: Verify the receive the level common event
187 */
188 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest004, TestSize.Level0)
189 {
190 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest004: start");
191 if (!IsMock(PA_PATH) || !IsMock(AMBIENT_PATH) || IsVendor()) {
192 return;
193 }
194 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
195 int32_t paTemp = 41000;
196 int32_t amTemp = 10000;
197 g_thermalLevel = 4;
198 SetNodeValue(paTemp, PA_PATH);
199 SetNodeValue(amTemp, AMBIENT_PATH);
200 MockThermalMgrClient::GetInstance().GetThermalInfo();
201 Wait();
202 CommonEventManager::UnSubscribeCommonEvent(subscriber);
203 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest004: end");
204 }
205
206 /*
207 * @tc.number: ThermalLevelEventTest005
208 * @tc.name: ThermalLevelEventTest
209 * @tc.desc: Verify the receive the level common event
210 */
211 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest005, TestSize.Level0)
212 {
213 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest005: start");
214 if (!IsMock(PA_PATH) || !IsMock(AMBIENT_PATH) || IsVendor()) {
215 return;
216 }
217 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
218 int32_t paTemp = 44000;
219 int32_t amTemp = 10000;
220 g_thermalLevel = 5;
221 SetNodeValue(paTemp, PA_PATH);
222 SetNodeValue(amTemp, AMBIENT_PATH);
223 MockThermalMgrClient::GetInstance().GetThermalInfo();
224 Wait();
225 CommonEventManager::UnSubscribeCommonEvent(subscriber);
226 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest005: end");
227 }
228
229 /*
230 * @tc.number: ThermalLevelEventTest006
231 * @tc.name: ThermalLevelEventTest
232 * @tc.desc: Verify the receive the level common event
233 */
234 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest006, TestSize.Level0)
235 {
236 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest006: start");
237 if (!IsMock(AP_PATH) || !IsMock(AMBIENT_PATH) || !IsMock(SHELL_PATH) || IsVendor()) {
238 return;
239 }
240 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
241 int32_t apTemp = 78000;
242 int32_t amTemp = 1000;
243 int32_t shellTemp = 30000;
244 g_thermalLevel = 6;
245 SetNodeValue(apTemp, AP_PATH);
246 SetNodeValue(amTemp, AMBIENT_PATH);
247 SetNodeValue(shellTemp, SHELL_PATH);
248 MockThermalMgrClient::GetInstance().GetThermalInfo();
249 Wait();
250 CommonEventManager::UnSubscribeCommonEvent(subscriber);
251 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest006: end");
252 }
253
254 /*
255 * @tc.number: ThermalLevelEventTest007
256 * @tc.name: ThermalLevelEventTest
257 * @tc.desc: Verify the receive the level common event
258 */
259 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest007, TestSize.Level0)
260 {
261 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest007: start");
262 if (!IsMock(BATTERY_PATH) || !IsMock(SOC_PATH) || IsVendor()) {
263 return;
264 }
265 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
266 int32_t batteryTemp = 46100;
267 g_thermalLevel = 3;
268 SetNodeValue(batteryTemp, BATTERY_PATH);
269 MockThermalMgrClient::GetInstance().GetThermalInfo();
270
271 int32_t socTemp = -10000;
272 g_thermalLevel = 0;
273 SetNodeValue(socTemp, SOC_PATH);
274 MockThermalMgrClient::GetInstance().GetThermalInfo();
275 Wait();
276 CommonEventManager::UnSubscribeCommonEvent(subscriber);
277 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest007: end");
278 }
279
280 /*
281 * @tc.number: ThermalLevelEventTest008
282 * @tc.name: ThermalLevelEventTest
283 * @tc.desc: Verify the receive the level common event
284 */
285 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest008, TestSize.Level0)
286 {
287 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest008: start");
288 if (!IsMock(BATTERY_PATH) || IsVendor()) {
289 return;
290 }
291 bool result = false;
292 int32_t batteryTemp = 40100;
293 g_thermalLevel = 1;
294 SetNodeValue(batteryTemp, BATTERY_PATH);
295 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
296 EXPECT_TRUE(MockThermalMgrClient::GetInstance().GetThermalInfo());
297 Wait();
298
299 CommonEventData stickyData;
300 CommonEventManager::GetStickyCommonEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED, stickyData);
301 if (stickyData.GetWant().GetAction() != CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) {
302 result = false;
303 } else {
304 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest008: sticky event successfully");
305 result = true;
306 }
307 CommonEventManager::UnSubscribeCommonEvent(subscriber);
308 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest008: end");
309 }
310 }
311