1 /*
2  * Copyright (c) 2024 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 #define private public
17 #define protected public
18 #undef private
19 #undef protected
20 
21 #include <gtest/gtest.h>
22 #include <gtest/hwext/gtest-multithread.h>
23 #include "socperf_client.h"
24 #include "socperf.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 
28 using namespace testing::ext;
29 using namespace testing::mt;
30 
31 namespace OHOS {
32 namespace SOCPERF {
33 class SocPerfSubTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase(void)41 void SocPerfSubTest::SetUpTestCase(void)
42 {
43 }
44 
TearDownTestCase(void)45 void SocPerfSubTest::TearDownTestCase(void)
46 {
47 }
48 
SetUp(void)49 void SocPerfSubTest::SetUp(void)
50 {
51 }
52 
TearDown(void)53 void SocPerfSubTest::TearDown(void)
54 {
55 }
56 
57 /*
58  * @tc.name: SocPerfSubTest_ActionType_001
59  * @tc.desc: action type perf
60  * @tc.type FUNC
61  * @tc.require: issueI78T3V
62  */
63 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ActionType_001, Function | MediumTest | Level0)
64 {
65     EXPECT_EQ(OHOS::SOCPERF::ActionType::ACTION_TYPE_PERF, 0);
66 }
67 
68 /*
69  * @tc.name: SocPerfSubTest_ActionType_002
70  * @tc.desc: action type power
71  * @tc.type FUNC
72  * @tc.require: issueI78T3V
73  */
74 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ActionType_002, Function | MediumTest | Level0)
75 {
76     EXPECT_EQ(OHOS::SOCPERF::ActionType::ACTION_TYPE_POWER, 1);
77 }
78 
79 /*
80  * @tc.name: SocPerfSubTest_ActionType_003
81  * @tc.desc: action type THERMAL
82  * @tc.type FUNC
83  * @tc.require: issueI78T3V
84  */
85 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ActionType_003, Function | MediumTest | Level0)
86 {
87     EXPECT_EQ(OHOS::SOCPERF::ActionType::ACTION_TYPE_THERMAL, 2);
88 }
89 
90 /*
91  * @tc.name: SocPerfSubTest_ActionType_004
92  * @tc.desc: action type max
93  * @tc.type FUNC
94  * @tc.require: issueI78T3V
95  */
96 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ActionType_004, Function | MediumTest | Level0)
97 {
98     EXPECT_EQ(OHOS::SOCPERF::ActionType::ACTION_TYPE_MAX, 4);
99 }
100 
101 /*
102  * @tc.name: SocPerfSubTest_EventType_001
103  * @tc.desc: event type off
104  * @tc.type FUNC
105  * @tc.require: issueI78T3V
106  */
107 HWTEST_F(SocPerfSubTest, SocPerfSubTest_EventType_001, Function | MediumTest | Level0)
108 {
109     EXPECT_EQ(OHOS::SOCPERF::EventType::EVENT_OFF, 0);
110 }
111 
112 /*
113  * @tc.name: SocPerfSubTest_EventType_002
114  * @tc.desc: event type on
115  * @tc.type FUNC
116  * @tc.require: issueI78T3V
117  */
118 HWTEST_F(SocPerfSubTest, SocPerfSubTest_EventType_002, Function | MediumTest | Level0)
119 {
120     EXPECT_EQ(OHOS::SOCPERF::EventType::EVENT_ON, 1);
121 }
122 
123 /*
124  * @tc.name: SocPerfSubTest_EventType_003
125  * @tc.desc: event type invalid
126  * @tc.type FUNC
127  * @tc.require: issueI78T3V
128  */
129 HWTEST_F(SocPerfSubTest, SocPerfSubTest_EventType_003, Function | MediumTest | Level0)
130 {
131     EXPECT_EQ(OHOS::SOCPERF::EventType::EVENT_INVALID, -1);
132 }
133 
134 /*
135  * @tc.name: SocPerfSubTest_InnerEventId_001
136  * @tc.desc: init res node info
137  * @tc.type FUNC
138  * @tc.require: issueI78T3V
139  */
140 HWTEST_F(SocPerfSubTest, SocPerfSubTest_InnerEventId_001, Function | MediumTest | Level0)
141 {
142     EXPECT_EQ(INNER_EVENT_ID_INIT_RESOURCE_NODE_INFO, 0);
143 }
144 
145 /*
146  * @tc.name: SocPerfSubTest_InnerEventId_002
147  * @tc.desc: do freq action
148  * @tc.type FUNC
149  * @tc.require: issueI78T3V
150  */
151 HWTEST_F(SocPerfSubTest, SocPerfSubTest_InnerEventId_003, Function | MediumTest | Level0)
152 {
153     EXPECT_EQ(INNER_EVENT_ID_DO_FREQ_ACTION, 1);
154 }
155 
156 /*
157  * @tc.name: SocPerfSubTest_InnerEventId_003
158  * @tc.desc: do freq action delayed
159  * @tc.type FUNC
160  * @tc.require: issueI78T3V
161  */
162 HWTEST_F(SocPerfSubTest, SocPerfSubTest_InnerEventId_004, Function | MediumTest | Level0)
163 {
164     EXPECT_EQ(INNER_EVENT_ID_DO_FREQ_ACTION_DELAYED, 3);
165 }
166 
167 /*
168  * @tc.name: SocPerfSubTest_InnerEventId_004
169  * @tc.desc: power limit boost freq
170  * @tc.type FUNC
171  * @tc.require: issueI78T3V
172  */
173 HWTEST_F(SocPerfSubTest, SocPerfSubTest_InnerEventId_005, Function | MediumTest | Level0)
174 {
175     EXPECT_EQ(INNER_EVENT_ID_POWER_LIMIT_BOOST_FREQ, 4);
176 }
177 
178 /*
179  * @tc.name: SocPerfSubTest_InnerEventId_005
180  * @tc.desc: thermal limit boost freq
181  * @tc.type FUNC
182  * @tc.require: issueI78T3V
183  */
184 HWTEST_F(SocPerfSubTest, SocPerfSubTest_InnerEventId_006, Function | MediumTest | Level0)
185 {
186     EXPECT_EQ(INNER_EVENT_ID_THERMAL_LIMIT_BOOST_FREQ, 5);
187 }
188 
189 /*
190  * @tc.name: SocPerfSubTest_GetService_001
191  * @tc.desc: get socperf service
192  * @tc.type FUNC
193  * @tc.require: issueI78T3V
194  */
195 HWTEST_F(SocPerfSubTest, SocPerfSubTest_GetService_001, Function | MediumTest | Level0)
196 {
197     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
198     EXPECT_NE(samgr, nullptr);
199 
200     sptr<IRemoteObject> object = samgr->GetSystemAbility(SOC_PERF_SERVICE_SA_ID);
201     EXPECT_NE(object, nullptr);
202 }
203 
204 /*
205  * @tc.name: SocPerfSubTest_GetService_002
206  * @tc.desc: get socperf service
207  * @tc.type FUNC
208  * @tc.require: issueI78T3V
209  */
210 HWTEST_F(SocPerfSubTest, SocPerfSubTest_GetService_002, Function | MediumTest | Level0)
211 {
212     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
213     EXPECT_NE(samgr, nullptr);
214 }
215 
SocPerfSubTestGetServiceTask()216 static void SocPerfSubTestGetServiceTask()
217 {
218     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
219     EXPECT_NE(samgr, nullptr);
220 
221     sptr<IRemoteObject> object = samgr->GetSystemAbility(SOC_PERF_SERVICE_SA_ID);
222     EXPECT_NE(object, nullptr);
223 }
224 
225 /*
226  * @tc.name: SocPerfSubTest_GetService_003
227  * @tc.desc: Test get socperf service in multithreading
228  * @tc.type FUNC
229  * @tc.require: issueI7G8VT
230  */
231 HWTEST_F(SocPerfSubTest, SocPerfSubTest_GetService_003, Function | MediumTest | Level0)
232 {
233     SET_THREAD_NUM(10);
234     GTEST_RUN_TASK(SocPerfSubTestGetServiceTask);
235 }
236 
237 /*
238  * @tc.name: SocPerfSubTest_PerfRequest_001
239  * @tc.desc: PerfRequest
240  * @tc.type FUNC
241  * @tc.require: issueI78T3V
242  */
243 HWTEST_F(SocPerfSubTest, SocPerfSubTest_PerfRequest_001, Function | MediumTest | Level0)
244 {
245     std::string msg = "";
246     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(10000, msg);
247     EXPECT_EQ(msg, "");
248 }
249 
250 /*
251  * @tc.name: SocPerfSubTest_PerfRequest_002
252  * @tc.desc: PerfRequestEx ON
253  * @tc.type FUNC
254  * @tc.require: issueI78T3V
255  */
256 HWTEST_F(SocPerfSubTest, SocPerfSubTest_PerfRequest_002, Function | MediumTest | Level0)
257 {
258     std::string msg = "";
259     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(10000, true, msg);
260     EXPECT_EQ(msg, "");
261 }
262 
263 /*
264  * @tc.name: SocPerfSubTest_PerfRequest_003
265  * @tc.desc: PerfRequestEx OFF
266  * @tc.type FUNC
267  * @tc.require: issueI78T3V
268  */
269 HWTEST_F(SocPerfSubTest, SocPerfSubTest_PerfRequest_003, Function | MediumTest | Level0)
270 {
271     std::string msg = "";
272     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(10000, false, msg);
273     EXPECT_EQ(msg, "");
274 }
275 
276 /*
277  * @tc.name: SocPerfSubTest_PowerLimitBoost_001
278  * @tc.desc: PowerLimitBoost ON
279  * @tc.type FUNC
280  * @tc.require: issueI78T3V
281  */
282 HWTEST_F(SocPerfSubTest, SocPerfSubTest_PowerLimitBoost_001, Function | MediumTest | Level0)
283 {
284     std::string msg = "";
285     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(true, msg);
286     EXPECT_EQ(msg, "");
287 }
288 
289 /*
290  * @tc.name: SocPerfSubTest_PowerLimitBoost_001
291  * @tc.desc: PowerLimitBoost OFF
292  * @tc.type FUNC
293  * @tc.require: issueI78T3V
294  */
295 HWTEST_F(SocPerfSubTest, SocPerfSubTest_PowerLimitBoost_002, Function | MediumTest | Level0)
296 {
297     std::string msg = "";
298     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(false, msg);
299     EXPECT_EQ(msg, "");
300 }
301 
302 /*
303  * @tc.name: SocPerfSubTest_ThermalLimitBoost_001
304  * @tc.desc: ThermalLimitBoost ON
305  * @tc.type FUNC
306  * @tc.require: issueI78T3V
307  */
308 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ThermalLimitBoost_001, Function | MediumTest | Level0)
309 {
310     std::string msg = "";
311     OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(true, msg);
312     EXPECT_EQ(msg, "");
313 }
314 
315 /*
316  * @tc.name: SocPerfSubTest_ThermalLimitBoost_002
317  * @tc.desc: ThermalLimitBoost OFF
318  * @tc.type FUNC
319  * @tc.require: issueI78T3V
320  */
321 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ThermalLimitBoost_002, Function | MediumTest | Level0)
322 {
323     std::string msg = "";
324     OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(false, msg);
325     EXPECT_EQ(msg, "");
326 }
327 
328 /*
329  * @tc.name: SocPerfSubTest_RequestCmdIdCount_001
330  * @tc.desc: RequestCmdIdCount
331  * @tc.type FUNC
332  * @tc.require: issueI9H4NS
333  */
334 HWTEST_F(SocPerfSubTest, SocPerfSubTest_RequestCmdIdCount_001, Function | MediumTest | Level0)
335 {
336     OHOS::SOCPERF::SocPerfClient::GetInstance().RequestCmdIdCount("");
337     SUCCEED();
338 }
339 
340 /*
341  * @tc.name: SocPerfSubTest_LimitRequest_001
342  * @tc.desc: LimitRequest thermal
343  * @tc.type FUNC
344  * @tc.require: issueI78T3V
345  */
346 HWTEST_F(SocPerfSubTest, SocPerfSubTest_LimitRequest_001, Function | MediumTest | Level0)
347 {
348     int32_t clientId_power = ACTION_TYPE_POWER;
349     std::vector<int32_t> tags;
350     tags.push_back(1001);
351     std::vector<int64_t> configs;
352     EXPECT_NE(tags.size(), configs.size());
353     configs.push_back(1608000);
354     EXPECT_EQ(tags.size(), configs.size());
355     OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(clientId_power, tags, configs, "");
356 }
357 
358 /*
359  * @tc.name: SocPerfSubTest_LimitRequest_002
360  * @tc.desc: LimitRequest thermal
361  * @tc.type FUNC
362  * @tc.require: issueI78T3V
363  */
364 HWTEST_F(SocPerfSubTest, SocPerfSubTest_LimitRequest_002, Function | MediumTest | Level0)
365 {
366     int32_t clientId_thermal = ACTION_TYPE_THERMAL;
367     std::vector<int32_t> tags;
368     tags.push_back(1000);
369     tags.push_back(1001);
370     std::vector<int64_t> configs;
371     EXPECT_NE(tags.size(), configs.size());
372     configs.push_back(1800000);
373     configs.push_back(1992000);
374     EXPECT_EQ(tags.size(), configs.size());
375     OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(clientId_thermal, tags, configs, "");
376 }
377 
SocPerfSubTestLimitRequestTask()378 static void SocPerfSubTestLimitRequestTask()
379 {
380     int32_t clientId_power = ACTION_TYPE_POWER;
381     std::vector<int32_t> tags;
382     constexpr int resId = 1001;
383     tags.push_back(resId);
384     std::vector<int64_t> configs;
385     EXPECT_NE(tags.size(), configs.size());
386     constexpr int freqValue = 1416000;
387     configs.push_back(freqValue);
388     EXPECT_EQ(tags.size(), configs.size());
389     OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(clientId_power, tags, configs, "");
390     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(true, "");
391     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(false, "");
392 }
393 
394 /*
395  * @tc.name: SocPerfSubTest_LimitRequest_003
396  * @tc.desc: Test LimitRequest thermal in multithreading
397  * @tc.type FUNC
398  * @tc.require: issueI7G8VT
399  */
400 HWTEST_F(SocPerfSubTest, SocPerfSubTest_LimitRequest_003, Function | MediumTest | Level0)
401 {
402     SET_THREAD_NUM(10);
403     GTEST_RUN_TASK(SocPerfSubTestLimitRequestTask);
404 }
405 
406 /*
407  * @tc.name: SocPerfSubTest_ResetClient_001
408  * @tc.desc: ResetClient
409  * @tc.type FUNC
410  * @tc.require: issueI78T3V
411  */
412 HWTEST_F(SocPerfSubTest, SocPerfSubTest_ResetClient_001, Function | MediumTest | Level0)
413 {
414     std::string msg = "";
415     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(10000, msg);
416     EXPECT_EQ(msg, "");
417     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(10000, true, msg);
418     EXPECT_EQ(msg, "");
419     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(10000, false, msg);
420     EXPECT_EQ(msg, "");
421     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(true, msg);
422     EXPECT_EQ(msg, "");
423     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(false, msg);
424     EXPECT_EQ(msg, "");
425     OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(true, msg);
426     EXPECT_EQ(msg, "");
427     OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(false, msg);
428     EXPECT_EQ(msg, "");
429     OHOS::SOCPERF::SocPerfClient::GetInstance().SetRequestStatus(false, msg);
430     EXPECT_EQ(msg, "");
431     OHOS::SOCPERF::SocPerfClient::GetInstance().SetRequestStatus(true, msg);
432     EXPECT_EQ(msg, "");
433     OHOS::SOCPERF::SocPerfClient::GetInstance().RequestDeviceMode("test", true);
434     EXPECT_EQ(msg, "");
435     OHOS::SOCPERF::SocPerfClient::GetInstance().RequestDeviceMode("test", false);
436     EXPECT_EQ(msg, "");
437     OHOS::SOCPERF::SocPerfClient::GetInstance().ResetClient();
438 }
439 
440 /*
441  * @tc.name: SocPerfSubTest_SetThermalLvl_001
442  * @tc.desc: perf request lvl client API
443  * @tc.type FUNC
444  * @tc.require: issue#I95U8S
445  */
446 HWTEST_F(SocPerfSubTest, SocPerfSubTest_SetThermalLevel_001, Function | MediumTest | Level0)
447 {
448     OHOS::SOCPERF::SocPerfClient::GetInstance().SetThermalLevel(3);
449     SUCCEED();
450 }
451 
SocPerfSubTestMultithreadingTask()452 static void SocPerfSubTestMultithreadingTask()
453 {
454     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(10000, "");
455     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(10000, true, "");
456     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(10000, false, "");
457     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(true, "");
458     OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(false, "");
459     OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(true, "");
460     OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(false, "");
461 }
462 
463 /*
464  * @tc.name: SocPerfSubTest_Multithreading_001
465  * @tc.desc: Test the service above in multithreading
466  * @tc.type FUNC
467  * @tc.require: issueI7G8VT
468  */
469 HWTEST_F(SocPerfSubTest, SocPerfSubTest_Multithreading_001, Function | MediumTest | Level0)
470 {
471     SET_THREAD_NUM(10);
472     GTEST_RUN_TASK(SocPerfSubTestMultithreadingTask);
473 }
474 } // namespace SOCPERF
475 } // namespace OHOS
476