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 #include <fstream>
17 
18 #include <gtest/gtest.h>
19 
20 #include "mmi_log.h"
21 #include "uds_server.h"
22 #include "timer_manager.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 using namespace testing::ext;
28 } // namespace
29 
30 class TimerManagerTest : public testing::Test {
31 public:
SetUpTestCase(void)32     static void SetUpTestCase(void) {}
TearDownTestCase(void)33     static void TearDownTestCase(void) {}
34 };
35 
AddTimerCallback()36 void AddTimerCallback()
37 {
38     return;
39 }
40 
41 /**
42  * @tc.name: TimerManagerTest_ManagerTimer_001
43  * @tc.desc: Test the function AddTimer
44  * @tc.type: FUNC
45  * @tc.require:
46  */
47 HWTEST_F(TimerManagerTest, TimerManagerTest_ManagerTimer_001, TestSize.Level1)
48 {
49     CALL_TEST_DEBUG;
50     int32_t repeatCount = 3;
51     int32_t intervalMs = 1000;
52     int32_t timerld = TimerMgr->AddTimer(intervalMs, repeatCount, AddTimerCallback);
53     EXPECT_EQ(timerld, 0);
54 }
55 
56 /**
57  * @tc.name: TimerManagerTest_ManagerTimer_002
58  * @tc.desc: Test the function RemoveTimer
59  * @tc.type: FUNC
60  * @tc.require:
61  */
62 HWTEST_F(TimerManagerTest, TimerManagerTest_ManagerTimer_002, TestSize.Level1)
63 {
64     CALL_TEST_DEBUG;
65     int32_t repeatCount = 3;
66     int32_t intervalMs = 1000;
67     int32_t timerld = TimerMgr->AddTimer(intervalMs, repeatCount, AddTimerCallback);
68     ASSERT_EQ(TimerMgr->RemoveTimer(timerld), 0);
69 }
70 
71 /**
72  * @tc.name: TimerManagerTest_ManagerTimer_003
73  * @tc.desc: Test the function ResetTimer
74  * @tc.type: FUNC
75  * @tc.require:
76  */
77 HWTEST_F(TimerManagerTest, TimerManagerTest_ManagerTimer_003, TestSize.Level1)
78 {
79     CALL_TEST_DEBUG;
80     int32_t repeatCount = 3;
81     int32_t intervalMs = 1000;
82     int32_t timerld = TimerMgr->AddTimer(intervalMs, repeatCount, AddTimerCallback);
83     int32_t result = TimerMgr->ResetTimer(timerld);
84     EXPECT_EQ(result, 0);
85 }
86 
87 /**
88  * @tc.name: TimerManagerTest_ManagerTimer_004
89  * @tc.desc: Test the function IsExist
90  * @tc.type: FUNC
91  * @tc.require:
92  */
93 HWTEST_F(TimerManagerTest, TimerManagerTest_ManagerTimer_004, TestSize.Level1)
94 {
95     CALL_TEST_DEBUG;
96     int32_t repeatCount = 3;
97     int32_t intervalMs = 1000;
98     int32_t timerld = TimerMgr->AddTimer(intervalMs, repeatCount, AddTimerCallback);
99     ASSERT_TRUE(TimerMgr->IsExist(timerld));
100 }
101 
102 /**
103  * @tc.name: TimerManagerTest_ManagerTimer_005
104  * @tc.desc: Test the function AddTimer
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(TimerManagerTest, TimerManagerTest_ManagerTimer_005, TestSize.Level1)
109 {
110     CALL_TEST_DEBUG;
111     int32_t repeatCount = 3;
112     int32_t intervalMs = 1000;
113     int32_t timerld = TimerMgr->AddTimer(intervalMs, repeatCount, nullptr);
114     EXPECT_EQ(timerld, -1);
115 }
116 
117 /**
118  * @tc.name: TimerManagerTest_AddTimer_001
119  * @tc.desc: Test adding a timer to the TimerManager
120  * @tc.type: FUNC
121  * @tc.require:
122  */
123 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer_001, TestSize.Level1)
124 {
125     CALL_TEST_DEBUG;
126     TimerManager timermanager;
127     int32_t intervalMs = 0;
128     int32_t repeatCount = 1;
__anonaea6ac010202() 129     auto callback = []() {};
130     auto ret = timermanager.AddTimer(intervalMs, repeatCount, callback);
131     EXPECT_EQ(ret, 0);
132     intervalMs = -1;
133     repeatCount = 1;
134     ret = timermanager.AddTimer(intervalMs, repeatCount, callback);
135     EXPECT_EQ(ret, 1);
136 }
137 
138 /**
139  * @tc.name: TimerManagerTest_RemoveTimer_001
140  * @tc.desc: Test removing a timer from the TimerManager
141  * @tc.type: FUNC
142  * @tc.require:
143  */
144 HWTEST_F(TimerManagerTest, TimerManagerTest_RemoveTimer_001, TestSize.Level1)
145 {
146     CALL_TEST_DEBUG;
147     TimerManager timermanager;
148     int32_t timerId = 1;
149     auto ret = timermanager.RemoveTimer(timerId);
150     EXPECT_EQ(ret, -1);
151     timerId = -1;
152     ret = timermanager.RemoveTimer(timerId);
153     EXPECT_EQ(ret, -1);
154 }
155 
156 /**
157  * @tc.name: TimerManagerTest_ResetTimer_001
158  * @tc.desc: Test resetting a timer in the TimerManager
159  * @tc.type: FUNC
160  * @tc.require:
161  */
162 HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer_001, TestSize.Level1)
163 {
164     CALL_TEST_DEBUG;
165     TimerManager timermanager;
166     int32_t timerId = 1;
167     auto ret = timermanager.ResetTimer(timerId);
168     EXPECT_EQ(ret, -1);
169     timerId = -1;
170     ret = timermanager.ResetTimer(timerId);
171     EXPECT_EQ(ret, -1);
172     ASSERT_NO_FATAL_FAILURE(timermanager.ProcessTimers());
173 }
174 
175 /**
176  * @tc.name: TimerManagerTest_IsExist_001
177  * @tc.desc: Test checking if a timer exists in the TimerManager
178  * @tc.type: FUNC
179  * @tc.require:
180  */
181 HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist_001, TestSize.Level1)
182 {
183     CALL_TEST_DEBUG;
184     TimerManager timermanager;
185     int32_t timerId = 1;
186     auto ret = timermanager.IsExist(timerId);
187     EXPECT_FALSE(ret);
188     timerId = -1;
189     ret = timermanager.IsExist(timerId);
190     EXPECT_FALSE(ret);
191 }
192 
193 /**
194  * @tc.name: TimerManagerTest_CalcNextDelay_001
195  * @tc.desc: Test calculating the next delayed timer in the TimerManager
196  * @tc.type: FUNC
197  * @tc.require:
198  */
199 HWTEST_F(TimerManagerTest, TimerManagerTest_CalcNextDelay_001, TestSize.Level1)
200 {
201     CALL_TEST_DEBUG;
202     TimerManager timermanager;
203     auto ret = timermanager.CalcNextDelay();
204     EXPECT_EQ(ret, -1);;
205 }
206 
207 /**
208  * @tc.name: TimerManagerTest_TakeNextTimerId_001
209  * @tc.desc: Test obtaining the ID of the next timer in the TimerManager
210  * @tc.type: FUNC
211  * @tc.require:
212  */
213 HWTEST_F(TimerManagerTest, TimerManagerTest_TakeNextTimerId_001, TestSize.Level1)
214 {
215     CALL_TEST_DEBUG;
216     TimerManager timermanager;
217     auto ret = timermanager.TakeNextTimerId();
218     EXPECT_EQ(ret, 0);
219 }
220 
221 /**
222  * @tc.name: TimerManagerTest_AddTimerInternal_001
223  * @tc.desc: Test adding a timer internally within the TimerManager
224  * @tc.type: FUNC
225  * @tc.require:
226  */
227 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerInternal_001, TestSize.Level1)
228 {
229     CALL_TEST_DEBUG;
230     TimerManager timermanager;
231     int32_t intervalMs = 50;
232     int32_t repeatCount = 1;
__anonaea6ac010302() 233     auto callback = []() {};
234     auto ret = timermanager.AddTimerInternal(intervalMs, repeatCount, callback);
235     EXPECT_EQ(ret, 0);
236     intervalMs = 11000;
237     repeatCount = 1;
238     ret = timermanager.AddTimerInternal(intervalMs, repeatCount, callback);
239     EXPECT_EQ(ret, 1);
240     intervalMs = 500;
241     repeatCount = 1;
242     ret = timermanager.AddTimerInternal(intervalMs, repeatCount, callback);
243     EXPECT_EQ(ret, 2);
244 }
245 
246 /**
247  * @tc.name: TimerManagerTest_RemoveTimerInternal_001
248  * @tc.desc: Test removing a timer internally within the TimerManager
249  * @tc.type: FUNC
250  * @tc.require:
251  */
252 HWTEST_F(TimerManagerTest, TimerManagerTest_RemoveTimerInternal_001, TestSize.Level1)
253 {
254     CALL_TEST_DEBUG;
255     TimerManager timermanager;
256     int32_t timerId = 1;
257     auto ret = timermanager.RemoveTimerInternal(timerId);
258     EXPECT_EQ(ret, -1);
259     timerId = -1;
260     ret = timermanager.RemoveTimerInternal(timerId);
261     EXPECT_EQ(ret, -1);
262 }
263 
264 /**
265  * @tc.name: TimerManagerTest_ResetTimerInternal_001
266  * @tc.desc: Test resetting a timer internally within the TimerManager
267  * @tc.type: FUNC
268  * @tc.require:
269  */
270 HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimerInternal_001, TestSize.Level1)
271 {
272     CALL_TEST_DEBUG;
273     TimerManager timermanager;
274     int32_t timerId = 1;
__anonaea6ac010402() 275     timermanager.AddTimer(timerId, 1000, []() {});
276     auto ret = timermanager.ResetTimerInternal(timerId);
277     EXPECT_EQ(ret, -1);
278 }
279 
280 /**
281  * @tc.name: TimerManagerTest_IsExistInternal_001
282  * @tc.desc: Test checking if a timer exists internally within the TimerManager
283  * @tc.type: FUNC
284  * @tc.require:
285  */
286 HWTEST_F(TimerManagerTest, TimerManagerTest_IsExistInternal_001, TestSize.Level1)
287 {
288     CALL_TEST_DEBUG;
289     TimerManager timermanager;
290     int32_t timerId = 1;
291     auto ret = timermanager.IsExistInternal(timerId);
292     EXPECT_FALSE(ret);
293     timerId = 2;
294     ret = timermanager.IsExistInternal(timerId);
295     EXPECT_FALSE(ret);
296     timerId = -1;
297     ret = timermanager.IsExistInternal(timerId);
298     EXPECT_FALSE(ret);
299 }
300 
301 /**
302  * @tc.name: TimerManagerTest_InsertTimerInternal_001
303  * @tc.desc: Test inserting a timer internally within the TimerManager
304  * @tc.type: FUNC
305  * @tc.require:
306  */
307 HWTEST_F(TimerManagerTest, TimerManagerTest_InsertTimerInternal_001, TestSize.Level1)
308 {
309     CALL_TEST_DEBUG;
310     TimerManager timermanager;
311     auto timer = std::make_unique<TimerManager::TimerItem>();
312     timer->nextCallTime = 100;
313     timermanager.InsertTimerInternal(timer);
314     EXPECT_EQ(timermanager.timers_.front()->nextCallTime, 100);
315 }
316 
317 /**
318  * @tc.name: TimerManagerTest_InsertTimerInternal_002
319  * @tc.desc: Test inserting a timer internally within the TimerManager
320  * @tc.type: FUNC
321  * @tc.require:
322  */
323 HWTEST_F(TimerManagerTest, TimerManagerTest_InsertTimerInternal_002, TestSize.Level1)
324 {
325     CALL_TEST_DEBUG;
326     TimerManager timermanager;
327     auto timer1 = std::make_unique<TimerManager::TimerItem>();
328     timer1->nextCallTime = 100;
329     timermanager.InsertTimerInternal(timer1);
330     auto timer2 = std::make_unique<TimerManager::TimerItem>();
331     timer2->nextCallTime = 50;
332     timermanager.InsertTimerInternal(timer2);
333     EXPECT_EQ(timermanager.timers_.front()->nextCallTime, 50);
334 }
335 
336 /**
337  * @tc.name: TimerManagerTest_InsertTimerInternal_003
338  * @tc.desc: Test inserting a timer internally within the TimerManager
339  * @tc.type: FUNC
340  * @tc.require:
341  */
342 HWTEST_F(TimerManagerTest, TimerManagerTest_InsertTimerInternal_003, TestSize.Level1)
343 {
344     CALL_TEST_DEBUG;
345     TimerManager timermanager;
346     auto timer1 = std::make_unique<TimerManager::TimerItem>();
347     timer1->nextCallTime = 100;
348     timermanager.InsertTimerInternal(timer1);
349     auto timer2 = std::make_unique<TimerManager::TimerItem>();
350     timer2->nextCallTime = 200;
351     timermanager.InsertTimerInternal(timer2);
352     auto timer3 = std::make_unique<TimerManager::TimerItem>();
353     timer3->nextCallTime = 200;
354     timermanager.InsertTimerInternal(timer3);
355     EXPECT_EQ(timermanager.timers_.front()->nextCallTime, 100);
356 }
357 
358 /**
359  * @tc.name: TimerManagerTest_CalcNextDelayInternal_001
360  * @tc.desc: Test calculating the next delay internally within the TimerManager
361  * @tc.type: FUNC
362  * @tc.require:
363  */
364 HWTEST_F(TimerManagerTest, TimerManagerTest_CalcNextDelayInternal_001, TestSize.Level1)
365 {
366     CALL_TEST_DEBUG;
367     TimerManager timermanager;
368     int32_t timerId = 1;
__anonaea6ac010502() 369     timermanager.AddTimer(timerId, 1000, []() {});
370     int64_t millisTime = 36;
371     EXPECT_EQ(timermanager.CalcNextDelayInternal(), millisTime);
372 }
373 
374 /**
375  * @tc.name: TimerManagerTest_CalcNextDelayInternal
376  * @tc.desc: Test calculating the next delay internally within the TimerManager
377  * @tc.type: FUNC
378  * @tc.require:
379  */
380 HWTEST_F(TimerManagerTest, TimerManagerTest_CalcNextDelayInternal, TestSize.Level1)
381 {
382     TimerManager tMgr;
383     auto timer = std::make_unique<TimerManager::TimerItem>();
384     timer->nextCallTime = -1;
385     tMgr.InsertTimerInternal(timer);
386     EXPECT_EQ(tMgr.CalcNextDelayInternal(), 0);
387 }
388 
389 /**
390  * @tc.name: TimerManagerTest_ProcessTimersInternal_001
391  * @tc.desc: Test processing timers internally within the TimerManager
392  * @tc.type: FUNC
393  * @tc.require:
394  */
395 HWTEST_F(TimerManagerTest, TimerManagerTest_ProcessTimersInternal_001, TestSize.Level1)
396 {
397     CALL_TEST_DEBUG;
398     TimerManager timermanager;
399     ASSERT_NO_FATAL_FAILURE(timermanager.ProcessTimersInternal());
400 }
401 
402 /**
403  * @tc.name: TimerManagerTest_ProcessTimersInternal
404  * @tc.desc: Test processing timers internally within the TimerManager
405  * @tc.type: FUNC
406  * @tc.require:
407  */
408 HWTEST_F(TimerManagerTest, TimerManagerTest_ProcessTimersInternal, TestSize.Level1)
409 {
410     TimerManager tMgr;
411     auto timer = std::make_unique<TimerManager::TimerItem>();
412     timer->nextCallTime = 10000000000;
413     tMgr.InsertTimerInternal(timer);
414     ASSERT_NO_FATAL_FAILURE(tMgr.ProcessTimersInternal());
415 }
416 } // namespace MMI
417 } // namespace OHOS