• 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 "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 <datetime_ex.h>
24 #include <fcntl.h>
25 #include <gtest/gtest.h>
26 #include <if_system_ability_manager.h>
27 #include <ipc_skeleton.h>
28 #include <string_ex.h>
29 #include "action_thermal_level.h"
30 #include "file_operation.h"
31 #include "mock_thermal_mgr_client.h"
32 #include "securec.h"
33 #include "thermal_common.h"
34 #include "thermal_service.h"
35 #include "thermal_mgr_client.h"
36 #include "thermal_level_info.h"
37 
38 using namespace testing::ext;
39 using namespace OHOS::PowerMgr;
40 using namespace OHOS;
41 using namespace std;
42 using namespace OHOS::AAFwk;
43 using namespace OHOS::EventFwk;
44 
45 class CommonEventThermalLevelTest : public CommonEventSubscriber {
46 public:
47     CommonEventThermalLevelTest() = default;
48     explicit CommonEventThermalLevelTest(const CommonEventSubscribeInfo &subscriberInfo);
~CommonEventThermalLevelTest()49     virtual ~CommonEventThermalLevelTest() {};
50     virtual void OnReceiveEvent(const CommonEventData &data);
51     static shared_ptr<CommonEventThermalLevelTest> RegisterEvent();
52 };
53 
CommonEventThermalLevelTest(const CommonEventSubscribeInfo & subscriberInfo)54 CommonEventThermalLevelTest::CommonEventThermalLevelTest
55     (const CommonEventSubscribeInfo &subscriberInfo) : CommonEventSubscriber(subscriberInfo) {}
56 
57 
OnReceiveEvent(const CommonEventData & data)58 void CommonEventThermalLevelTest::OnReceiveEvent(const CommonEventData &data)
59 {
60     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest: OnReceiveEvent Enter \n");
61     int invalidLevel = -1;
62     std::string action = data.GetWant().GetAction();
63     if (action == CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) {
64         std::string key = ToString(static_cast<int32_t>(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED));
65         int32_t level = data.GetWant().GetIntParam(key, invalidLevel);
66         GTEST_LOG_(INFO) << "thermal level: " << level;
67     }
68 }
69 
RegisterEvent()70 shared_ptr<CommonEventThermalLevelTest> CommonEventThermalLevelTest::RegisterEvent()
71 {
72     THERMAL_HILOGD(LABEL_TEST, "RegisterEvent: Regist Subscriber Start!");
73     static const int32_t MAX_RETRY_TIMES = 2;
74     auto succeed = false;
75     MatchingSkills matchingSkills;
76     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
77     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
78     auto subscriberPtr = std::make_shared<CommonEventThermalLevelTest>(subscribeInfo);
79     for (int32_t tryTimes = 0; tryTimes < MAX_RETRY_TIMES; tryTimes++) {
80         succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
81     }
82     if (!succeed) {
83         THERMAL_HILOGD(COMP_SVC, "Failed to register subscriber");
84         return nullptr;
85     }
86     return subscriberPtr;
87 }
88 
InitNode()89 int32_t ThermalLevelEventTest::InitNode()
90 {
91     char bufTemp[MAX_PATH] = {0};
92     int32_t ret = -1;
93     std::map<std::string, int32_t> sensor;
94     sensor["battery"] = 0;
95     sensor["charger"] = 0;
96     sensor["pa"] = 0;
97     sensor["ap"] = 0;
98     sensor["ambient"] = 0;
99     sensor["cpu"] = 0;
100     sensor["soc"] = 0;
101     sensor["shell"] = 0;
102     for (auto iter : sensor) {
103         ret = snprintf_s(bufTemp, MAX_PATH, sizeof(bufTemp) - 1, SIMULATION_TEMP_DIR, iter.first.c_str());
104         if (ret < EOK) {
105             return ret;
106         }
107         std::string temp = std::to_string(iter.second);
108         FileOperation::WriteFile(bufTemp, temp, temp.length());
109     }
110     return ERR_OK;
111 }
112 
SetUpTestCase()113 void ThermalLevelEventTest::SetUpTestCase()
114 {
115 }
116 
TearDownTestCase()117 void ThermalLevelEventTest::TearDownTestCase()
118 {
119 }
120 
SetUp()121 void ThermalLevelEventTest::SetUp()
122 {
123 }
124 
TearDown()125 void ThermalLevelEventTest::TearDown()
126 {
127     InitNode();
128 }
129 
130 namespace {
131 /*
132  * @tc.number: ThermalLevelEventTest001
133  * @tc.name: ThermalLevelEventTest
134  * @tc.desc: Verify the receive the level common event
135  */
136 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest001, TestSize.Level0)
137 {
138     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest:: Test Start!!");
139     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
140     char batteryTempBuf[MAX_PATH] = {0};
141     int32_t temp = 41000;
142     int32_t ret = -1;
143     std::string sTemp = to_string(temp) + "\n";
144     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
145     EXPECT_EQ(true, ret >= EOK);
146     ret = FileOperation::WriteFile(batteryTempBuf, sTemp, sTemp.length());
147     EXPECT_EQ(true, ret == ERR_OK);
148     MockThermalMgrClient::GetInstance().GetThermalInfo();
149     CommonEventManager::UnSubscribeCommonEvent(subscriber);
150 }
151 
152 /*
153  * @tc.number: ThermalLevelEventTest002
154  * @tc.name: ThermalLevelEventTest
155  * @tc.desc: Verify the receive the level common event
156  */
157 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest002, TestSize.Level0)
158 {
159     THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest002:: Test Start!!");
160     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
161     char batteryTempBuf[MAX_PATH] = {0};
162     int32_t temp = 43100;
163     int32_t ret = -1;
164     std::string sTemp = to_string(temp) + "\n";
165     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
166     EXPECT_EQ(true, ret >= EOK);
167     ret = FileOperation::WriteFile(batteryTempBuf, sTemp, sTemp.length());
168     EXPECT_EQ(true, ret == ERR_OK);
169     MockThermalMgrClient::GetInstance().GetThermalInfo();
170     CommonEventManager::UnSubscribeCommonEvent(subscriber);
171 }
172 
173 /*
174  * @tc.number: ThermalLevelEventTest003
175  * @tc.name: ThermalLevelEventTest
176  * @tc.desc: Verify the receive the level common event
177  */
178 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest003, TestSize.Level0)
179 {
180     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest:: Test Start!!");
181     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
182     char batteryTempBuf[MAX_PATH] = {0};
183     int32_t temp = 46100;
184     int32_t ret = -1;
185     std::string sTemp = to_string(temp) + "\n";
186     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
187     EXPECT_EQ(true, ret >= EOK);
188     ret = FileOperation::WriteFile(batteryTempBuf, sTemp, sTemp.length());
189     EXPECT_EQ(true, ret == ERR_OK);
190     MockThermalMgrClient::GetInstance().GetThermalInfo();
191     CommonEventManager::UnSubscribeCommonEvent(subscriber);
192 }
193 
194 /*
195  * @tc.number: ThermalLevelEventTest004
196  * @tc.name: ThermalLevelEventTest
197  * @tc.desc: Verify the receive the level common event
198  */
199 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest004, TestSize.Level0)
200 {
201     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest:: Test Start!!");
202     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
203     int32_t ret = -1;
204     char paTempBuf[MAX_PATH] = {0};
205     char amTempBuf[MAX_PATH] = {0};
206     ret = snprintf_s(paTempBuf, MAX_PATH, sizeof(paTempBuf) - 1, PA_PATH.c_str());
207     EXPECT_EQ(true, ret >= EOK);
208     ret = snprintf_s(amTempBuf, MAX_PATH, sizeof(amTempBuf) - 1, AMBIENT_PATH.c_str());
209     EXPECT_EQ(true, ret >= EOK);
210 
211     int32_t paTemp = 41000;
212     std::string sTemp = to_string(paTemp) + "\n";
213     ret = FileOperation::WriteFile(paTempBuf, sTemp, sTemp.length());
214     EXPECT_EQ(true, ret == ERR_OK);
215 
216     int32_t amTemp = 10000;
217     sTemp = to_string(amTemp) + "\n";
218     ret = FileOperation::WriteFile(amTempBuf, sTemp, sTemp.length());
219     EXPECT_EQ(true, ret == ERR_OK);
220     MockThermalMgrClient::GetInstance().GetThermalInfo();
221     CommonEventManager::UnSubscribeCommonEvent(subscriber);
222 }
223 
224 /*
225  * @tc.number: ThermalLevelEventTest005
226  * @tc.name: ThermalLevelEventTest
227  * @tc.desc: Verify the receive the level common event
228  */
229 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest005, TestSize.Level0)
230 {
231     auto test = std::make_shared<CommonEventThermalLevelTest>();
232     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
233     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest:: Test Start!!");
234     int32_t ret = -1;
235     char paTempBuf[MAX_PATH] = {0};
236     char amTempBuf[MAX_PATH] = {0};
237     ret = snprintf_s(paTempBuf, MAX_PATH, sizeof(paTempBuf) - 1, PA_PATH.c_str());
238     EXPECT_EQ(true, ret >= EOK);
239     ret = snprintf_s(amTempBuf, MAX_PATH, sizeof(amTempBuf) - 1, AMBIENT_PATH.c_str());
240     EXPECT_EQ(true, ret >= EOK);
241 
242     int32_t paTemp = 44000;
243     std::string sTemp = to_string(paTemp) + "\n";
244     ret = FileOperation::WriteFile(paTempBuf, sTemp, sTemp.length());
245     EXPECT_EQ(true, ret == ERR_OK);
246 
247     int32_t amTemp = 10000;
248     sTemp = to_string(amTemp) + "\n";
249     ret = FileOperation::WriteFile(amTempBuf, sTemp, sTemp.length());
250     EXPECT_EQ(true, ret == ERR_OK);
251     MockThermalMgrClient::GetInstance().GetThermalInfo();
252     CommonEventManager::UnSubscribeCommonEvent(subscriber);
253 }
254 
255 /*
256  * @tc.number: ThermalLevelEventTest006
257  * @tc.name: ThermalLevelEventTest
258  * @tc.desc: Verify the receive the level common event
259  */
260 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest006, TestSize.Level0)
261 {
262     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest:: Test Start!!");
263     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
264     int32_t ret = -1;
265     char apTempBuf[MAX_PATH] = {0};
266     char amTempBuf[MAX_PATH] = {0};
267     char shellTempBuf[MAX_PATH] = {0};
268     ret = snprintf_s(apTempBuf, MAX_PATH, sizeof(apTempBuf) - 1, AP_PATH.c_str());
269     EXPECT_EQ(true, ret >= EOK);
270     ret = snprintf_s(amTempBuf, MAX_PATH, sizeof(amTempBuf) - 1, AMBIENT_PATH.c_str());
271     EXPECT_EQ(true, ret >= EOK);
272     ret = snprintf_s(shellTempBuf, MAX_PATH, sizeof(shellTempBuf) - 1, SHELL_PATH.c_str());
273     EXPECT_EQ(true, ret >= EOK);
274 
275     int32_t apTemp = 78000;
276     std::string sTemp = to_string(apTemp) + "\n";
277     ret = FileOperation::WriteFile(apTempBuf, sTemp, sTemp.length());
278     EXPECT_EQ(true, ret == ERR_OK);
279 
280     int32_t amTemp = 1000;
281     sTemp = to_string(amTemp) + "\n";
282     ret = FileOperation::WriteFile(amTempBuf, sTemp, sTemp.length());
283     EXPECT_EQ(true, ret == ERR_OK);
284 
285     int32_t shellTemp = 50000;
286     sTemp = to_string(shellTemp) + "\n";
287     ret = FileOperation::WriteFile(shellTempBuf, sTemp, sTemp.length());
288     EXPECT_EQ(true, ret == ERR_OK);
289     MockThermalMgrClient::GetInstance().GetThermalInfo();
290     CommonEventManager::UnSubscribeCommonEvent(subscriber);
291 }
292 
293 /*
294  * @tc.number: ThermalLevelEventTest007
295  * @tc.name: ThermalLevelEventTest
296  * @tc.desc: Verify the receive the level common event
297  */
298 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest007, TestSize.Level0)
299 {
300     THERMAL_HILOGD(LABEL_TEST, "CommonEventThermalLevelTest:: Test Start!!");
301     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
302     int32_t ret = -1;
303     char socTempBuf[MAX_PATH] = {0};
304     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
305     EXPECT_EQ(true, ret >= EOK);
306     int32_t socTemp = -10000;
307     std::string sTemp = to_string(socTemp);
308     ret = FileOperation::WriteFile(socTempBuf, sTemp, sTemp.length());
309     EXPECT_EQ(true, ret == ERR_OK);
310     MockThermalMgrClient::GetInstance().GetThermalInfo();
311     CommonEventManager::UnSubscribeCommonEvent(subscriber);
312 }
313 
314 /*
315  * @tc.number: ThermalLevelEventTest008
316  * @tc.name: ThermalLevelEventTest
317  * @tc.desc: Verify the receive the level common event
318  */
319 HWTEST_F(ThermalLevelEventTest, ThermalLevelEventTest008, TestSize.Level0)
320 {
321     THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest008:: Test Start!!");
322     int32_t ret = -1;
323     bool result = false;
324     char batteryTempBuf[MAX_PATH] = {0};
325     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
326     EXPECT_EQ(true, ret >= EOK);
327     int32_t batteryTemp = 40100;
328     std::string sTemp = to_string(batteryTemp);
329     ret = FileOperation::WriteFile(batteryTempBuf, sTemp, sTemp.length());
330     EXPECT_EQ(true, ret == ERR_OK);
331     shared_ptr<CommonEventThermalLevelTest> subscriber = CommonEventThermalLevelTest::RegisterEvent();
332     MockThermalMgrClient::GetInstance().GetThermalInfo();
333 
334     CommonEventData stickyData;
335     CommonEventManager::GetStickyCommonEvent(CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED, stickyData);
336     if (stickyData.GetWant().GetAction() != CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED) {
337         result = false;
338     } else {
339         THERMAL_HILOGD(LABEL_TEST, "ThermalLevelEventTest008:: sticky event successfully!!");
340         result = true;
341     }
342     CommonEventManager::UnSubscribeCommonEvent(subscriber);
343 }
344 }