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 "thermal_level_info.h"
27 #include "thermal_log.h"
28
29 #define private public
30 #define protected public
31 #include "thermal_config_file_parser.h"
32 #include "thermal_service.h"
33 #include "thermal_srv_config_parser.h"
34 #include "v1_1/ithermal_interface.h"
35 #include "v1_1/thermal_types.h"
36 #undef private
37 #undef protected
38
39 using namespace testing::ext;
40 using namespace OHOS::PowerMgr;
41 using namespace OHOS;
42 using namespace std;
43 using namespace OHOS::AAFwk;
44 using namespace OHOS::EventFwk;
45 using namespace OHOS::HDI::Thermal::V1_1;
46 using namespace Security::AccessToken;
47 using Security::AccessToken::AccessTokenID;
48
49 AccessTokenID ThermalLevelEventTest::tokenID_ = 0;
50 static constexpr int32_t DEFAULT_API_VERSION = 8;
51 namespace {
52 std::condition_variable g_callbackCV;
53 std::mutex g_mutex;
54 constexpr int64_t TIME_OUT = 3;
55 bool g_callbackTriggered = false;
56 int32_t g_thermalLevel = -1;
57 const std::string SYSTEM_THERMAL_SERVICE_CONFIG_PATH = "/system/etc/thermal_config/thermal_service_config.xml";
58 sptr<ThermalService> g_service = nullptr;
59 ThermalConfigFileParser g_parser;
60
61 uint64_t g_token;
62
63 PermissionStateFull g_infoManagerTestState = {
64 .grantFlags = {1},
65 .grantStatus = {PermissionState::PERMISSION_GRANTED},
66 .isGeneral = true,
67 .permissionName = "ohos.permission.PUBLISH_SYSTEM_COMMON_EVENT",
68 .resDeviceID = {"local"}
69 };
70
71 HapPolicyParams g_infoManagerTestPolicyPrams = {
72 .apl = APL_NORMAL,
73 .domain = "test.domain.ThermalLevelEventTest",
74 .permList = {},
75 .permStateList = {g_infoManagerTestState}
76 };
77
78 HapInfoParams g_infoManagerTestInfoParms = {
79 .bundleName = "com.ohos.thermal_level_event_test",
80 .userID = 100,
81 .instIndex = 0,
82 .appIDDesc = "ThermalLevelEventTest",
83 .apiVersion = DEFAULT_API_VERSION,
84 .isSystemApp = true
85 };
86
Notify()87 void Notify()
88 {
89 std::unique_lock<std::mutex> lock(g_mutex);
90 g_callbackTriggered = true;
91 lock.unlock();
92 g_callbackCV.notify_one();
93 }
94
Wait()95 void Wait()
96 {
97 std::unique_lock<std::mutex> lock(g_mutex);
98 g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] {
99 return g_callbackTriggered;
100 });
101 EXPECT_TRUE(g_callbackTriggered);
102 g_callbackTriggered = false;
103 }
104 } // namespace
105
106 class CommonEventThermalLevelTest : public CommonEventSubscriber {
107 public:
108 CommonEventThermalLevelTest() = default;
109 explicit CommonEventThermalLevelTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventThermalLevelTest()110 virtual ~CommonEventThermalLevelTest() {};
111 virtual void OnReceiveEvent(const CommonEventData& data);
112 static shared_ptr<CommonEventThermalLevelTest> RegisterEvent();
113 };
114
CommonEventThermalLevelTest(const CommonEventSubscribeInfo & subscriberInfo)115 CommonEventThermalLevelTest::CommonEventThermalLevelTest(const CommonEventSubscribeInfo& subscriberInfo)
116 : CommonEventSubscriber(subscriberInfo)
117 {
118 }
119
OnReceiveEvent(const CommonEventData & data)120 void CommonEventThermalLevelTest::OnReceiveEvent(const CommonEventData& data)
121 {
122 THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest: OnReceiveEvent Enter");
123 int invalidLevel = -1;
124 std::string action = data.GetWant().GetAction();
125 EXPECT_TRUE(action == CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
126 if (action == CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) {
127 std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
128 int32_t level = data.GetWant().GetIntParam(key, invalidLevel);
129 GTEST_LOG_(INFO) << "thermal level: " << level;
130 EXPECT_TRUE(g_thermalLevel == level);
131 }
132 Notify();
133 }
134
RegisterEvent()135 shared_ptr<CommonEventThermalLevelTest> CommonEventThermalLevelTest::RegisterEvent()
136 {
137 THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start");
138 static const int32_t MAX_RETRY_TIMES = 2;
139 auto succeed = false;
140 MatchingSkills matchingSkills;
141 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
142 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
143 auto subscriberPtr = std::make_shared<CommonEventThermalLevelTest>(subscribeInfo);
144 for (int32_t tryTimes = 0; tryTimes < MAX_RETRY_TIMES; tryTimes++) {
145 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
146 }
147 if (!succeed) {
148 THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
149 return nullptr;
150 }
151 return subscriberPtr;
152 }
153
TearDown()154 void ThermalLevelEventTest::TearDown()
155 {
156 g_thermalLevel = -1;
157 g_callbackTriggered = false;
158 }
159
SetUpTestCase()160 void ThermalLevelEventTest::SetUpTestCase()
161 {
162 AccessTokenIDEx tokenIdEx = {0};
163 tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams);
164 tokenID_ = tokenIdEx.tokenIdExStruct.tokenID;
165 ASSERT_NE(0, tokenID_);
166 g_token = GetSelfTokenID();
167 ASSERT_EQ(0, SetSelfTokenID(tokenID_));
168 g_service = ThermalService::GetInstance();
169 g_service->InitSystemTestModules();
170 g_service->OnStart();
171 g_parser.Init();
172 }
173
TearDownTestCase()174 void ThermalLevelEventTest::TearDownTestCase()
175 {
176 if (g_service != nullptr) {
177 g_service->OnStop();
178 }
179 AccessTokenKit::DeleteToken(tokenID_);
180 SetSelfTokenID(g_token);
181 }
182
183 namespace {
184 /*
185 * @tc.number: ThermalLevelEventTest001
186 * @tc.name: ThermalLevelEventTest
187 * @tc.desc: Verify the receive the level common event
188 */
189 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest001, TestSize.Level0)
190 {
191 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest001: start");
192 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
193 HdfThermalCallbackInfo event;
194 ThermalZoneInfo info1;
195 info1.type = "battery";
196 info1.temp = 46100;
197 event.info.push_back(info1);
198 g_thermalLevel = 3;
199 GTEST_LOG_(INFO) << "test thermal level: " << g_thermalLevel;
200 g_service->HandleThermalCallbackEvent(event);
201 event.info.clear();
202 Wait();
203
204 info1.type = "soc";
205 info1.temp = -10000;
206 event.info.push_back(info1);
207 g_thermalLevel = 0;
208 GTEST_LOG_(INFO) << "test thermal level: " << g_thermalLevel;
209 g_service->HandleThermalCallbackEvent(event);
210 Wait();
211 CommonEventManager::UnSubscribeCommonEvent(subscriber);
212 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest001: end");
213 }
214
215 /*
216 * @tc.number: ThermalLevelEventTest002
217 * @tc.name: ThermalLevelEventTest
218 * @tc.desc: Verify the receive the level common event
219 */
220 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest002, TestSize.Level0)
221 {
222 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest002: start");
223 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
224
225 auto levelItems = g_parser.GetLevelItems("base_safe", "battery");
226 for (auto item : levelItems) {
227 std::vector<PolicyAction> policy;
228 if (!g_parser.GetActionPolicy("base_safe", item.level, policy)) {
229 continue;
230 }
231 HdfThermalCallbackInfo event;
232 ThermalZoneInfo info;
233 info.type = "battery";
234 info.temp = item.threshold;
235 g_thermalLevel = item.level;
236 event.info.push_back(info);
237 GTEST_LOG_(INFO) << "test thermal level: " << g_thermalLevel << ", threshold: " << item.threshold;
238 g_service->HandleThermalCallbackEvent(event);
239 Wait();
240 }
241 CommonEventManager::UnSubscribeCommonEvent(subscriber);
242 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest002: end");
243 }
244
245 /*
246 * @tc.number: ThermalLevelEventTest003
247 * @tc.name: ThermalLevelEventTest
248 * @tc.desc: Verify the receive the level common event
249 */
250 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest003, TestSize.Level0)
251 {
252 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest003: start");
253 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
254
255 HdfThermalCallbackInfo event;
256 ThermalZoneInfo info1;
257 info1.type = "pa";
258 info1.temp = 41000;
259 event.info.push_back(info1);
260
261 ThermalZoneInfo info2;
262 info2.type = "ambient";
263 info2.temp = 10000;
264 event.info.push_back(info2);
265
266 g_thermalLevel = 4;
267 GTEST_LOG_(INFO) << "test thermal level: " << g_thermalLevel;
268 g_service->HandleThermalCallbackEvent(event);
269 Wait();
270 CommonEventManager::UnSubscribeCommonEvent(subscriber);
271 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest003: end");
272 }
273
274 /*
275 * @tc.number: ThermalLevelEventTest004
276 * @tc.name: ThermalLevelEventTest
277 * @tc.desc: Verify the receive the level common event
278 */
279 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest004, TestSize.Level0)
280 {
281 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest004: start");
282 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
283
284 HdfThermalCallbackInfo event;
285 ThermalZoneInfo info1;
286 info1.type = "pa";
287 info1.temp = 44000;
288 event.info.push_back(info1);
289
290 ThermalZoneInfo info2;
291 info2.type = "ambient";
292 info2.temp = 10000;
293 event.info.push_back(info2);
294
295 g_thermalLevel = 5;
296 GTEST_LOG_(INFO) << "test thermal level: " << g_thermalLevel;
297 g_service->HandleThermalCallbackEvent(event);
298 Wait();
299 CommonEventManager::UnSubscribeCommonEvent(subscriber);
300 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest004: end");
301 }
302
303 /*
304 * @tc.number: ThermalLevelEventTest005
305 * @tc.name: ThermalLevelEventTest
306 * @tc.desc: Verify the receive the level common event
307 */
308 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest005, TestSize.Level0)
309 {
310 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest005: start");
311 shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
312 HdfThermalCallbackInfo event;
313 ThermalZoneInfo info1;
314 info1.type = "ap";
315 info1.temp = 78000;
316 event.info.push_back(info1);
317
318 ThermalZoneInfo info2;
319 info2.type = "ambient";
320 info2.temp = 1000;
321 event.info.push_back(info2);
322
323 ThermalZoneInfo info3;
324 info3.type = "shell";
325 info3.temp = 30000;
326 event.info.push_back(info3);
327
328 g_thermalLevel = 6;
329 GTEST_LOG_(INFO) << "test thermal level: " << g_thermalLevel;
330 g_service->HandleThermalCallbackEvent(event);
331 Wait();
332 CommonEventManager::UnSubscribeCommonEvent(subscriber);
333 THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest005: end");
334 }
335 } // namespace
336