1 /*
2 * Copyright (c) 2022 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 <cinttypes>
17 #include <sys/resource.h>
18 #include <sched.h>
19 #include <linux/sched.h>
20 #include "gtest/gtest.h"
21 #define private public
22 #include "concurrent_task_controller.h"
23 #include "rtg_interface.h"
24 #include "ipc_skeleton.h"
25 #include "concurrent_task_log.h"
26 #undef private
27
28 namespace OHOS {
29 namespace FFRT_TEST {
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace OHOS::ConcurrentTask;
33 using namespace std;
34
35
36 class ConcurrentTaskControllerTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void ConcurrentTaskControllerTest::SetUpTestCase()
45 {
46 }
47
TearDownTestCase()48 void ConcurrentTaskControllerTest::TearDownTestCase()
49 {
50 }
51
SetUp()52 void ConcurrentTaskControllerTest::SetUp()
53 {
54 }
55
TearDown()56 void ConcurrentTaskControllerTest::TearDown()
57 {
58 }
59
60 /**
61 * @tc.name: ReportDataTest
62 * @tc.desc: Test whether the ReportDataTest interface are normal.
63 * @tc.type: FUNC
64 */
65 HWTEST_F(ConcurrentTaskControllerTest, ReportDataTest, TestSize.Level1)
66 {
67 uint32_t resType = 0;
68 int64_t value = 0;
69 const Json::Value payload;
70 TaskController repData;
71 repData.ReportData(resType, value, payload);
72 }
73
74 /**
75 * @tc.name: PushTaskTest
76 * @tc.desc: Test whether the PushTask interface are normal.
77 * @tc.type: FUNC
78 */
79 HWTEST_F(ConcurrentTaskControllerTest, QueryIntervalTest, TestSize.Level1)
80 {
81 TaskController queInt;
82 int queryItem = QUERY_UI;
83 IntervalReply queryRs = {87, 657, 357, 214};
84 queInt.QueryInterval(queryItem, queryRs);
85 queryItem = QUERY_RENDER;
86 queInt.QueryInterval(queryItem, queryRs);
87 queryItem = QUERY_RENDER_SERVICE;
88 queInt.QueryInterval(queryItem, queryRs);
89 queryItem = QUERY_COMPOSER;
90 queInt.QueryInterval(queryItem, queryRs);
91 queryItem = QURRY_TYPE_MAX;
92 queInt.QueryInterval(queryItem, queryRs);
93 }
94
95 /**
96 * @tc.name: PushTaskTest
97 * @tc.desc: Test whether the PushTask interface are normal.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(ConcurrentTaskControllerTest, InitTest, TestSize.Level1)
101 {
102 TaskController::GetInstance().Init();
103 }
104
105 /**
106 * @tc.name: PushTaskTest
107 * @tc.desc: Test whether the PushTask interface are normal.
108 * @tc.type: FUNC
109 */
110 HWTEST_F(ConcurrentTaskControllerTest, AuthHwcTest, TestSize.Level1)
111 {
112 TaskController::GetInstance().SetHwcAuth(true);
113 TaskController::GetInstance().SetHwcAuth(false);
114 TaskController::GetInstance().SetHwcAuth(true);
115 }
116
117 /**
118 * @tc.name: PushTaskTest
119 * @tc.desc: Test whether the PushTask interface are normal.
120 * @tc.type: FUNC
121 */
122 HWTEST_F(ConcurrentTaskControllerTest, CheckUidTest, TestSize.Level1)
123 {
124 int uid = SYSTEM_UID;
125 bool ret = TaskController::GetInstance().CheckUid(uid);
126 EXPECT_EQ(ret, true);
127 uid = 0;
128 ret = TaskController::GetInstance().CheckUid(uid);
129 EXPECT_EQ(ret, true);
130 uid = 100;
131 ret = TaskController::GetInstance().CheckUid(uid);
132 EXPECT_EQ(ret, false);
133 }
134
135 /**
136 * @tc.name: PushTaskTest
137 * @tc.desc: Test whether the PushTask interface are normal.
138 * @tc.type: FUNC
139 */
140 HWTEST_F(ConcurrentTaskControllerTest, TypeMapInitTest, TestSize.Level1)
141 {
142 TaskController::GetInstance().TypeMapInit();
143 }
144
145 /**
146 * @tc.name: PushTaskTest
147 * @tc.desc: Test whether the PushTask interface are normal.
148 * @tc.type: FUNC
149 */
150 HWTEST_F(ConcurrentTaskControllerTest, TryCreateRsGroupTest, TestSize.Level1)
151 {
152 TaskController::GetInstance().rtgEnabled_ = false;
153 TaskController::GetInstance().TryCreateRsGroup();
154 TaskController::GetInstance().rtgEnabled_ = true;
155 TaskController::GetInstance().TryCreateRsGroup();
156 }
157
158 /**
159 * @tc.name: PushTaskTest
160 * @tc.desc: Test whether the PushTask interface are normal.
161 * @tc.type: FUNC
162 */
163 HWTEST_F(ConcurrentTaskControllerTest, QueryRenderServiceTest, TestSize.Level1)
164 {
165 int uid = SYSTEM_UID;
166 IntervalReply queryRs = {87, 657, 357, 214};
167 TaskController::GetInstance().QueryRenderService(uid, queryRs);
168 int flag = TaskController::GetInstance().renderServiceGrpId_;
169 TaskController::GetInstance().renderServiceGrpId_ = 1;
170 TaskController::GetInstance().QueryRenderService(uid, queryRs);
171 TaskController::GetInstance().renderServiceGrpId_ = -1;
172 TaskController::GetInstance().QueryRenderService(uid, queryRs);
173 TaskController::GetInstance().renderServiceGrpId_ = flag;
174 TaskController::GetInstance().QueryRenderService(uid, queryRs);
175 }
176
177 /**
178 * @tc.name: PushTaskTest
179 * @tc.desc: Test whether the PushTask interface are normal.
180 * @tc.type: FUNC
181 */
182 HWTEST_F(ConcurrentTaskControllerTest, GetRequestTypeTest, TestSize.Level1)
183 {
184 std::string strRequstType = "test";
185 int ret = TaskController::GetInstance().GetRequestType(strRequstType);
186 EXPECT_EQ(ret, MSG_TYPE_MAX);
187 TaskController::GetInstance().msgType_["test"] = 8;
188 ret = TaskController::GetInstance().GetRequestType(strRequstType);
189 EXPECT_EQ(ret, 8);
190 TaskController::GetInstance().msgType_.erase("test");
191 ret = TaskController::GetInstance().GetRequestType(strRequstType);
192 EXPECT_EQ(ret, MSG_TYPE_MAX);
193 }
194
195 /**
196 * @tc.name: PushTaskTest
197 * @tc.desc: Test whether the PushTask interface are normal.
198 * @tc.type: FUNC
199 */
200 HWTEST_F(ConcurrentTaskControllerTest, NewForegroundTest, TestSize.Level1)
201 {
202 TaskController fore;
203 int uid = 0;
204 fore.NewForeground(uid);
205 fore.NewBackground(uid);
206 fore.NewAppStart(uid);
207 fore.NewForeground(uid);
208 fore.NewBackground(uid);
209 fore.AppKilled(uid);
210 uid = 574;
211 fore.foregroundApp_.push_back(ForegroundAppRecord(574));
212 fore.foregroundApp_.push_back(ForegroundAppRecord(1));
213 fore.foregroundApp_.push_back(ForegroundAppRecord(3));
214 auto iter = fore.foregroundApp_.begin();
215 EXPECT_EQ(iter->GetUid(), uid);
216 fore.NewForeground(uid);
217 fore.NewBackground(uid);
218 fore.NewAppStart(uid);
219 fore.NewForeground(uid);
220 fore.NewBackground(uid);
221 fore.AppKilled(uid);
222 }
223
224 /**
225 * @tc.name: PushTaskTest
226 * @tc.desc: Test whether the PushTask interface are normal.
227 * @tc.type: FUNC
228 */
229 HWTEST_F(ConcurrentTaskControllerTest, PrintInfoTest, TestSize.Level1)
230 {
231 TaskController print;
232 print.foregroundApp_.push_back(ForegroundAppRecord(1));
233 print.foregroundApp_.push_back(ForegroundAppRecord(3));
234 print.foregroundApp_.push_back(ForegroundAppRecord(5));
235 auto iter = print.foregroundApp_.begin();
236 EXPECT_NE(iter, print.foregroundApp_.end());
237 print.PrintInfo();
238 }
239
240 /**
241 * @tc.name: PushTaskTest
242 * @tc.desc: Test whether the PushTask interface are normal.
243 * @tc.type: FUNC
244 */
245 HWTEST_F(ConcurrentTaskControllerTest, AddKeyThreadTest, TestSize.Level1)
246 {
247 int uid = 758;
248 int tid = 45;
249 int tid2 = 46;
250 int tid3 = 47;
251 int tid4 = 48;
252 int tid5 = 49;
253 int prio = PRIO_NORMAL;
254 ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid);
255 foregroundapprecord.AddKeyThread(tid, prio);
256 foregroundapprecord.keyThreads_.insert(tid);
257 foregroundapprecord.AddKeyThread(tid, prio);
258 foregroundapprecord.grpId_ = -1;
259 foregroundapprecord.AddKeyThread(tid, prio);
260 foregroundapprecord.grpId_ = 1;
261 foregroundapprecord.AddKeyThread(tid, prio);
262 foregroundapprecord.keyThreads_.insert(tid2);
263 foregroundapprecord.keyThreads_.insert(tid3);
264 foregroundapprecord.keyThreads_.insert(tid4);
265 foregroundapprecord.keyThreads_.insert(tid5);
266 foregroundapprecord.keyThreads_.insert(tid5);
267 foregroundapprecord.AddKeyThread(tid, prio);
268 prio = RPIO_IN;
269 foregroundapprecord.keyThreads_.insert(tid);
270 prio = PRIO_RT;
271 foregroundapprecord.keyThreads_.insert(tid);
272 foregroundapprecord.AddKeyThread(tid, prio);
273 foregroundapprecord.keyThreads_.clear();
274 }
275
276 /**
277 * @tc.name: PushTaskTest
278 * @tc.desc: Test whether the PushTask interface are normal.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(ConcurrentTaskControllerTest, BeginSceneTest, TestSize.Level1)
282 {
283 int uid = 758;
284 ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid);
285 foregroundapprecord.BeginScene();
286 foregroundapprecord.EndScene();
287 foregroundapprecord.grpId_ = -1;
288 foregroundapprecord.BeginScene();
289 foregroundapprecord.EndScene();
290 foregroundapprecord.grpId_ = 1;
291 foregroundapprecord.BeginScene();
292 foregroundapprecord.EndScene();
293 }
294
295 /**
296 * @tc.name: PushTaskTest
297 * @tc.desc: Test whether the PushTask interface are normal.
298 * @tc.type: FUNC
299 */
300 HWTEST_F(ConcurrentTaskControllerTest, IsValidTest, TestSize.Level1)
301 {
302 int uid = 758;
303 ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid);
304 EXPECT_EQ(foregroundapprecord.GetUid(), foregroundapprecord.uid_);
305 EXPECT_EQ(foregroundapprecord.GetGrpId(), foregroundapprecord.grpId_);
306 foregroundapprecord.uid_ = -1;
307 foregroundapprecord.grpId_ = 1;
308 EXPECT_EQ(foregroundapprecord.IsValid(), false);
309 foregroundapprecord.uid_ = -1;
310 foregroundapprecord.grpId_ = -1;
311 EXPECT_EQ(foregroundapprecord.IsValid(), false);
312 foregroundapprecord.uid_ = 1;
313 foregroundapprecord.grpId_ = -1;
314 EXPECT_EQ(foregroundapprecord.IsValid(), false);
315 foregroundapprecord.uid_ = 1;
316 foregroundapprecord.grpId_ = 1;
317 EXPECT_EQ(foregroundapprecord.IsValid(), true);
318 }
319
320 /**
321 * @tc.name: PushTaskTest
322 * @tc.desc: Test whether the PushTask interface are normal.
323 * @tc.type: FUNC
324 */
325 HWTEST_F(ConcurrentTaskControllerTest, PrintKeyThreadsTest, TestSize.Level1)
326 {
327 int uid = 758;
328 ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid);
329 foregroundapprecord.keyThreads_.insert(1);
330 foregroundapprecord.keyThreads_.insert(3);
331 foregroundapprecord.keyThreads_.insert(5);
332 foregroundapprecord.keyThreads_.insert(7);
333 foregroundapprecord.keyThreads_.insert(9);
334 auto iter = foregroundapprecord.keyThreads_.begin();
335 EXPECT_NE(iter, foregroundapprecord.keyThreads_.end());
336 foregroundapprecord.PrintKeyThreads();
337 }
338 }
339 }