1 /*
2 * Copyright (c) 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 <gtest/gtest.h>
17 #include "intention_event/service/timer_manager/include/timer_manager.h"
18 #include "intention_event/service/anr_manager/include/anr_manager.h"
19 #include <algorithm>
20 #include <cinttypes>
21 #include "window_manager_hilog.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace Rosen {
27 constexpr int32_t MAX_INTERVAL_MS = 10000;
28 constexpr int32_t MIN_DELAY = 5000;
29 class TimerManagerTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 };
36
SetUpTestCase()37 void TimerManagerTest::SetUpTestCase()
38 {
39 }
40
TearDownTestCase()41 void TimerManagerTest::TearDownTestCase()
42 {
43 }
44
SetUp()45 void TimerManagerTest::SetUp()
46 {
47 }
48
TearDown()49 void TimerManagerTest::TearDown()
50 {
51 }
52
GetMillisTime()53 int64_t GetMillisTime()
54 {
55 auto timeNow = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now());
56 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow.time_since_epoch());
57 return tmp.count();
58 }
59
60 namespace {
61 /**
62 * @tc.name: Init
63 * @tc.desc: normal function
64 * @tc.type: FUNC
65 */
66 HWTEST_F(TimerManagerTest, Init, Function | SmallTest | Level2)
67 {
68 GTEST_LOG_(INFO) << "TimerManagerTest::Init start";
69 TimerManager* timermanager = new TimerManager();
70 ASSERT_NE(timermanager, nullptr);
71 timermanager->Init();
72 timermanager->Init();
73 int32_t res = timermanager->RemoveTimer(0);
74 ASSERT_EQ(-1, res);
75 delete timermanager;
76 GTEST_LOG_(INFO) << "TimerManagerTest::Init end";
77 }
78
79 /**
80 * @tc.name: RemoveTimer
81 * @tc.desc: normal function
82 * @tc.type: FUNC
83 */
84 HWTEST_F(TimerManagerTest, RemoveTimer, Function | SmallTest | Level2)
85 {
86 GTEST_LOG_(INFO) << "TimerManagerTest::RemoveTimer start";
87 TimerManager* timermanager = new TimerManager();
88 ASSERT_NE(timermanager, nullptr);
89 int32_t res = timermanager->RemoveTimer(0);
90 ASSERT_EQ(-1, res);
91 delete timermanager;
92 GTEST_LOG_(INFO) << "TimerManagerTest::RemoveTimer end";
93 }
94
95 /**
96 * @tc.name: OnThread
97 * @tc.desc: normal function
98 * @tc.type: FUNC
99 */
100 HWTEST_F(TimerManagerTest, OnThread, Function | SmallTest | Level2)
101 {
102 GTEST_LOG_(INFO) << "TimerManagerTest::OnThread start";
103 TimerManager* timermanager = new TimerManager();
104 ASSERT_NE(timermanager, nullptr);
105 timermanager->OnThread();
106 ASSERT_EQ(-1, timermanager->RemoveTimerInternal(0));
107 delete timermanager;
108 GTEST_LOG_(INFO) << "TimerManagerTest::OnThread start";
109 }
110
111 /**
112 * @tc.name: TakeNextTimerId
113 * @tc.desc: normal function
114 * @tc.type: FUNC
115 */
116 HWTEST_F(TimerManagerTest, TakeNextTimerId, Function | SmallTest | Level2)
117 {
118 GTEST_LOG_(INFO) << "TimerManagerTest::TakeNextTimerId start";
119 TimerManager* timermanager = new TimerManager();
120 ASSERT_NE(timermanager, nullptr);
121 int32_t res = timermanager->TakeNextTimerId();
122 ASSERT_EQ(res, 0);
123 delete timermanager;
124 GTEST_LOG_(INFO) << "TimerManagerTest::TakeNextTimerId start";
125 }
126
127 /**
128 * @tc.name: RemoveTimerInternal
129 * @tc.desc: normal function
130 * @tc.type: FUNC
131 */
132 HWTEST_F(TimerManagerTest, RemoveTimerInternal, Function | SmallTest | Level2)
133 {
134 GTEST_LOG_(INFO) << "TimerManagerTest::RemoveTimerInternal start";
135 TimerManager* timermanager = new TimerManager();
136 ASSERT_NE(timermanager, nullptr);
137 int32_t res = timermanager->RemoveTimerInternal(0);
138 ASSERT_EQ(res, -1);
139
140 std::unique_ptr<TimerManager::TimerItem> timer = std::make_unique<TimerManager::TimerItem>();
141 ASSERT_NE(timer, nullptr);
142 timer->id = 0;
143 timermanager->timers_.push_back(std::move(timer));
144 res = timermanager->RemoveTimerInternal(0);
145 ASSERT_EQ(res, 0);
146 delete timermanager;
147 GTEST_LOG_(INFO) << "TimerManagerTest::RemoveTimerInternal start";
148 }
149
150 /**
151 * @tc.name: CalcNextDelayInternal
152 * @tc.desc: normal function
153 * @tc.type: FUNC
154 */
155 HWTEST_F(TimerManagerTest, CalcNextDelayInternal, Function | SmallTest | Level2)
156 {
157 GTEST_LOG_(INFO) << "TimerManagerTest::CalcNextDelayInternal start";
158 TimerManager* timermanager = new TimerManager();
159 ASSERT_NE(timermanager, nullptr);
160 int32_t res = timermanager->CalcNextDelayInternal();
161 ASSERT_EQ(res, MIN_DELAY);
162
163 std::unique_ptr<TimerManager::TimerItem> timer = std::make_unique<TimerManager::TimerItem>();
164 ASSERT_NE(timer, nullptr);
165 timermanager->timers_.push_back(std::move(timer));
166 res = timermanager->CalcNextDelayInternal();
167 ASSERT_EQ(res, 0);
168
169 auto nowTime = GetMillisTime();
170 if (!timermanager->timers_.empty()) {
171 auto& firstTimer = timermanager->timers_.front();
172 firstTimer->nextCallTime = nowTime + 10000;
173 }
174 res = timermanager->CalcNextDelayInternal();
175 ASSERT_EQ(res, 10000);
176
177 delete timermanager;
178 GTEST_LOG_(INFO) << "TimerManagerTest::CalcNextDelayInternal start";
179 }
180
181 /**
182 * @tc.name: ProcessTimersInternal
183 * @tc.desc: normal function
184 * @tc.type: FUNC
185 */
186 HWTEST_F(TimerManagerTest, ProcessTimersInternal, Function | SmallTest | Level2)
187 {
188 GTEST_LOG_(INFO) << "TimerManagerTest::ProcessTimersInternal start";
189 TimerManager* timermanager = new TimerManager();
190 ASSERT_NE(timermanager, nullptr);
191 timermanager->ProcessTimersInternal();
192 ASSERT_EQ(timermanager->timers_.size(), 0);
193
194 std::unique_ptr<TimerManager::TimerItem> timer = std::make_unique<TimerManager::TimerItem>();
195 ASSERT_NE(timer, nullptr);
196 timer->nextCallTime = GetMillisTime() + 10000;
197 timermanager->timers_.push_back(std::move(timer));
198 timermanager->ProcessTimersInternal();
199
200 if (!timermanager->timers_.empty()) {
201 auto& firstTimer = timermanager->timers_.front();
202 ASSERT_NE(firstTimer, nullptr);
203 firstTimer->nextCallTime = 0;
204 ASSERT_EQ(firstTimer->callback, nullptr);
205 timermanager->ProcessTimersInternal();
206 }
207 timermanager->timers_.clear();
208 std::unique_ptr<TimerManager::TimerItem> timer2 = std::make_unique<TimerManager::TimerItem>();
209 ASSERT_NE(timer2, nullptr);
210 timer2->nextCallTime = 0;
__anon6a7eea4c0202() 211 timer2->callback = []() {
212 return;
213 };
214 ASSERT_NE(timer2->callback, nullptr);
215 timermanager->timers_.push_back(std::move(timer2));
216 timermanager->ProcessTimersInternal();
217
218 delete timermanager;
219 GTEST_LOG_(INFO) << "TimerManagerTest::ProcessTimersInternal start";
220 }
221
222 /**
223 * @tc.name: ANRManagerInit
224 * @tc.desc: normal function
225 * @tc.type: FUNC
226 */
227 HWTEST_F(TimerManagerTest, ANRManagerInit, Function | SmallTest | Level2)
228 {
229 GTEST_LOG_(INFO) << "ANRManager::Init start";
230 ANRManager* anrManager = new ANRManager();
231 ASSERT_NE(anrManager, nullptr);
232 anrManager->Init();
233 ASSERT_EQ(anrManager->switcher_, false);
234 delete anrManager;
235 GTEST_LOG_(INFO) << "ANRManager::Init end";
236 }
237
238 /**
239 * @tc.name: ANRManagerAddTimer
240 * @tc.desc: normal function
241 * @tc.type: FUNC
242 */
243 HWTEST_F(TimerManagerTest, ANRManagerAddTimer, Function | SmallTest | Level2)
244 {
245 GTEST_LOG_(INFO) << "ANRManager::AddTimer start";
246 ANRManager* anrManager = new ANRManager();
247 ASSERT_NE(anrManager, nullptr);
248 anrManager->AddTimer(0, 1);
249 anrManager->AddTimer(1, 1);
250 anrManager->AddTimer(1, 0);
251 ASSERT_EQ(anrManager->switcher_, true);
252 delete anrManager;
253 GTEST_LOG_(INFO) << "ANRManager::AddTimer end";
254 }
255
256 /**
257 * @tc.name: ANRManagerMarkProcessed
258 * @tc.desc: normal function
259 * @tc.type: FUNC
260 */
261 HWTEST_F(TimerManagerTest, ANRManagerMarkProcessed, Function | SmallTest | Level2)
262 {
263 GTEST_LOG_(INFO) << "ANRManager::MarkProcessed start";
264 ANRManager* anrManager = new ANRManager();
265 ASSERT_NE(anrManager, nullptr);
266 anrManager->MarkProcessed(0, 0);
267 anrManager->MarkProcessed(0, 1);
268 anrManager->MarkProcessed(1, 1);
269 anrManager->MarkProcessed(1, 0);
270 ASSERT_NE(anrManager->anrTimerCount_, 0);
271 delete anrManager;
272 GTEST_LOG_(INFO) << "ANRManager::MarkProcessed end";
273 }
274
275 /**
276 * @tc.name: ANRManagerRemoveTimers
277 * @tc.desc: normal function
278 * @tc.type: FUNC
279 */
280 HWTEST_F(TimerManagerTest, ANRManagerRemoveTimers, Function | SmallTest | Level2)
281 {
282 GTEST_LOG_(INFO) << "ANRManager::RemoveTimers start";
283 ANRManager* anrManager = new ANRManager();
284 ASSERT_NE(anrManager, nullptr);
285 anrManager->RemoveTimers(0);
286 ASSERT_EQ(anrManager->anrTimerCount_, 0);
287 delete anrManager;
288 GTEST_LOG_(INFO) << "ANRManager::RemoveTimers end";
289 }
290
291 /**
292 * @tc.name: ANRManagerSwitchAnr
293 * @tc.desc: normal function
294 * @tc.type: FUNC
295 */
296 HWTEST_F(TimerManagerTest, ANRManagerSwitchAnr, Function | SmallTest | Level2)
297 {
298 GTEST_LOG_(INFO) << "ANRManager::SwitchAnr start";
299 ANRManager* anrManager = new ANRManager();
300 ASSERT_NE(anrManager, nullptr);
301 anrManager->RemoveTimers(true);
302 ASSERT_EQ(anrManager->switcher_, true);
303 delete anrManager;
304 GTEST_LOG_(INFO) << "ANRManager::SwitchAnr end";
305 }
306
307 /**
308 * @tc.name: ANRManagerSetAppInfoGetter
309 * @tc.desc: normal function
310 * @tc.type: FUNC
311 */
312 HWTEST_F(TimerManagerTest, ANRManagerSetAppInfoGetter, Function | SmallTest | Level2)
313 {
314 GTEST_LOG_(INFO) << "ANRManager::SetAppInfoGetter start";
315 ANRManager* anrManager = new ANRManager();
316 ASSERT_NE(anrManager, nullptr);
317 std::function<void(int32_t, std::string&, int32_t)> callback;
318 anrManager->SetAppInfoGetter(callback);
319 ASSERT_EQ(anrManager->appInfoGetter_, nullptr);
320 delete anrManager;
321 GTEST_LOG_(INFO) << "ANRManager::SetAppInfoGetter end";
322 }
323
324 /**
325 * @tc.name: ANRManagerGetBundleName
326 * @tc.desc: normal function
327 * @tc.type: FUNC
328 */
329 HWTEST_F(TimerManagerTest, ANRManagerGetBundleName, Function | SmallTest | Level2)
330 {
331 GTEST_LOG_(INFO) << "ANRManager::GetBundleName start";
332 ANRManager* anrManager = new ANRManager();
333 ASSERT_NE(anrManager, nullptr);
334 auto res = anrManager->GetBundleName(0, 0);
335 anrManager->GetBundleName(0, 1);
336 anrManager->GetBundleName(1, 0);
337 anrManager->GetBundleName(1, 1);
338 ASSERT_EQ(res, "unknown");
339 delete anrManager;
340 GTEST_LOG_(INFO) << "ANRManager::GetBundleName end";
341 }
342
343 /**
344 * @tc.name: AddTimer001
345 * @tc.desc: normal function
346 * @tc.type: FUNC
347 */
348 HWTEST_F(TimerManagerTest, AddTimer001, Function | SmallTest | Level2)
349 {
350 GTEST_LOG_(INFO) << "AddTimer001::ProcessTimersInternal start";
351 TimerManager* timermanager = new TimerManager();
352 ASSERT_NE(timermanager, nullptr);
353 int32_t intervalMs = 1;
354 std::function<void()> callback;
355 int32_t res = timermanager->AddTimer(intervalMs, callback);
356 ASSERT_EQ(res, -1);
357 timermanager->state_ = TimerMgrState::STATE_RUNNING;
358 res = timermanager->AddTimer(intervalMs, callback);
359 ASSERT_EQ(res, timermanager->AddTimerInternal(intervalMs, callback));
360 delete(timermanager);
361 GTEST_LOG_(INFO) << "AddTimer001::ProcessTimersInternal start";
362 }
363
364 /**
365 * @tc.name: ProcessTimers002
366 * @tc.desc: normal function
367 * @tc.type: FUNC
368 */
369 HWTEST_F(TimerManagerTest, ProcessTimers002, Function | SmallTest | Level2)
370 {
371 GTEST_LOG_(INFO) << "ProcessTimers002::ProcessTimersInternal start";
372 TimerManager* timermanager = new TimerManager();
373 ASSERT_NE(timermanager, nullptr);
374 int res = 0;
__anon6a7eea4c0302() 375 std::function<void()> func = [&]() {
376 timermanager->ProcessTimers();
377 res = 1;
378 };
379 func();
380 ASSERT_EQ(res, 1);
381 delete(timermanager);
382 GTEST_LOG_(INFO) << "ProcessTimers002::ProcessTimersInternal start";
383 }
384
385 /**
386 * @tc.name: OnStop003
387 * @tc.desc: normal function
388 * @tc.type: FUNC
389 */
390 HWTEST_F(TimerManagerTest, OnStop003, Function | SmallTest | Level2)
391 {
392 GTEST_LOG_(INFO) << "OnStop003::ProcessTimersInternal start";
393 TimerManager* timermanager = new TimerManager();
394 ASSERT_NE(timermanager, nullptr);
395 int res = 0;
__anon6a7eea4c0402() 396 std::function<void()> func = [&]() {
397 timermanager->OnStop();
398 res = 1;
399 };
400 func();
401 ASSERT_EQ(res, 1);
402 delete(timermanager);
403 GTEST_LOG_(INFO) << "OnStop003::ProcessTimersInternal start";
404 }
405
406 /**
407 * @tc.name: AddTimerInternal004
408 * @tc.desc: normal function
409 * @tc.type: FUNC
410 */
411 HWTEST_F(TimerManagerTest, AddTimerInternal004, Function | SmallTest | Level2)
412 {
413 GTEST_LOG_(INFO) << "AddTimerInternal004::ProcessTimersInternal start";
414 TimerManager* timermanager = new TimerManager();
415 ASSERT_NE(timermanager, nullptr);
416 int32_t intervalMs = 1;
417 std::function<void()> callback;
418 int32_t res = timermanager->AddTimerInternal(intervalMs, callback);
419 ASSERT_EQ(res, -1);
420
421 res = timermanager->AddTimerInternal(MAX_INTERVAL_MS + 1, callback);
422 ASSERT_EQ(res, -1);
423
424 delete(timermanager);
425 GTEST_LOG_(INFO) << "AddTimerInternal004::ProcessTimersInternal start";
426 }
427
428 /**
429 * @tc.name: CalcNextDelay
430 * @tc.desc: normal function
431 * @tc.type: FUNC
432 */
433 HWTEST_F(TimerManagerTest, CalcNextDelay, Function | SmallTest | Level2)
434 {
435 TimerManager* timermanager = new TimerManager();
436 ASSERT_NE(timermanager, nullptr);
437 timermanager->state_ = TimerMgrState::STATE_NOT_START;
438 int32_t res = timermanager->CalcNextDelay();
439 ASSERT_EQ(res, -1);
440
441 timermanager->state_ = TimerMgrState::STATE_RUNNING;
442 res = timermanager->CalcNextDelay();
443 auto res2 = timermanager->CalcNextDelayInternal();
444 ASSERT_EQ(res, res2);
445 delete timermanager;
446 }
447
448 /**
449 * @tc.name: ProcessTimers
450 * @tc.desc: normal function
451 * @tc.type: FUNC
452 */
453 HWTEST_F(TimerManagerTest, ProcessTimers, Function | SmallTest | Level2)
454 {
455 int res = 0;
456 TimerManager* timermanager = new TimerManager();
457 ASSERT_NE(timermanager, nullptr);
458 timermanager->state_ = TimerMgrState::STATE_NOT_START;
459 timermanager->ProcessTimers();
460 timermanager->state_ = TimerMgrState::STATE_RUNNING;
461 timermanager->ProcessTimers();
462 ASSERT_EQ(res, 0);
463 delete timermanager;
464 }
465
466 /**
467 * @tc.name: InsertTimerInternal
468 * @tc.desc: normal function
469 * @tc.type: FUNC
470 */
471 HWTEST_F(TimerManagerTest, InsertTimerInternal, Function | SmallTest | Level2)
472 {
473 int res = 0;
474 TimerManager* timermanager = new TimerManager();
475 ASSERT_NE(timermanager, nullptr);
476 std::unique_ptr<TimerManager::TimerItem> timer = std::make_unique<TimerManager::TimerItem>();
477 ASSERT_NE(timer, nullptr);
478 timer->id = 0;
479 timer->nextCallTime = 1;
480 timermanager->timers_.push_back(std::move(timer));
481 std::unique_ptr<TimerManager::TimerItem> timer2 = std::make_unique<TimerManager::TimerItem>();
482 ASSERT_NE(timer2, nullptr);
483 timer2->nextCallTime = 0;
484 timermanager->InsertTimerInternal(timer2);
485 ASSERT_EQ(res, 0);
486 delete timermanager;
487 }
488 }
489 }
490 }
491