(Automated Tests) Added more tests to increase TEM score
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / utc-Dali-Timer.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <adaptor-test-application.h>
19 #include <dali-test-suite-utils.h>
20 #include <dali/dali.h>
21 #include <dali/internal/system/linux/dali-ecore.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <iostream>
25
26 using namespace Dali;
27
28 void utc_dali_timer_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_timer_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 namespace
39 {
40 bool          ecore_timer_running = false;
41 Ecore_Task_Cb timer_callback_func = NULL;
42 const void*   timer_callback_data = NULL;
43 bool          main_loop_can_run   = false;
44 intptr_t      timerId             = 0; // intptr_t has the same size as a pointer and is platform independent so this can be returned as a pointer in ecore_timer_add below without compilation warnings
45 } // namespace
46
47 extern "C"
48 {
49   Ecore_Timer* ecore_timer_add(double        in,
50                                Ecore_Task_Cb func,
51                                const void*   data)
52   {
53     ecore_timer_running = true;
54     timer_callback_func = func;
55     timer_callback_data = data;
56     timerId += 8;
57     return (Ecore_Timer*)timerId;
58   }
59
60   void* ecore_timer_del(Ecore_Timer* timer)
61   {
62     ecore_timer_running = false;
63     timer_callback_func = NULL;
64     return NULL;
65   }
66 }
67
68 namespace
69 {
70 void test_ecore_main_loop_begin()
71 {
72   if(timer_callback_func != NULL)
73   {
74     main_loop_can_run = true;
75     while(main_loop_can_run)
76     {
77       if(!timer_callback_func(const_cast<void*>(timer_callback_data)))
78         break;
79     }
80   }
81 }
82
83 void test_ecore_main_loop_quit()
84 {
85   timer_callback_func = NULL;
86   main_loop_can_run   = false;
87 }
88
89 /**
90  * small class to test timer signal
91  */
92 class TimerTestClass : public ConnectionTracker
93 {
94 public:
95   TimerTestClass(bool repeat)
96   : mTimerCalled(false),
97     mReturnContiune(repeat)
98   {
99   }
100
101   bool Tick()
102   {
103     tet_printf("timer ticked\n");
104     mTimerCalled = true;
105     // quit the main loop otherwise we'll never return to tet
106     test_ecore_main_loop_quit();
107     return mReturnContiune;
108   }
109   bool mTimerCalled;    // whether tick has been called or not
110   bool mReturnContiune; // whether to return true / false to continue
111 };
112
113 } // namespace
114
115 // Positive test case for a method
116 int UtcDaliTimerCreation(void)
117 {
118   AdaptorTestApplication application;
119
120   tet_printf("timer creation \n");
121   Timer timer = Timer::New(300);
122
123   DALI_TEST_CHECK(timer);
124
125   DALI_TEST_CHECK(timer.GetInterval() == 300);
126
127   END_TEST;
128 }
129
130 int UtcDaliTimerUnitializedStart(void)
131 {
132   AdaptorTestApplication application;
133
134   tet_printf("unintialized timer start \n");
135
136   Timer* timer = new Timer;
137   DALI_TEST_CHECK(timer != NULL);
138
139   try
140   {
141     timer->Start();
142   }
143   catch(Dali::DaliException& e)
144   {
145     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
146   }
147   END_TEST;
148 }
149
150 int UtcDaliTimerUnitializedStop(void)
151 {
152   AdaptorTestApplication application;
153
154   tet_printf("unintialized timer stop \n");
155
156   Timer* timer = new Timer;
157   DALI_TEST_CHECK(timer != NULL);
158
159   try
160   {
161     timer->Stop();
162   }
163   catch(Dali::DaliException& e)
164   {
165     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
166   }
167   END_TEST;
168 }
169
170 int UtcDaliTimerUnitializedGetInterval(void)
171 {
172   AdaptorTestApplication application;
173
174   tet_printf("unintialized get interval \n");
175
176   Timer* timer = new Timer;
177   DALI_TEST_CHECK(timer != NULL);
178
179   try
180   {
181     timer->GetInterval();
182   }
183   catch(Dali::DaliException& e)
184   {
185     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
186   }
187   END_TEST;
188 }
189
190 int UtcDaliTimerUnitializedSetInterval(void)
191 {
192   AdaptorTestApplication application;
193
194   tet_printf("unintialized set interval \n");
195
196   Timer* timer = new Timer;
197   DALI_TEST_CHECK(timer != NULL);
198
199   try
200   {
201     timer->SetInterval(10);
202   }
203   catch(Dali::DaliException& e)
204   {
205     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
206   }
207   END_TEST;
208 }
209
210 int UtcDaliTimerUnitializedIsRunning(void)
211 {
212   AdaptorTestApplication application;
213
214   tet_printf("unintialized is running \n");
215
216   Timer* timer = new Timer;
217   DALI_TEST_CHECK(timer != NULL);
218
219   try
220   {
221     timer->IsRunning();
222   }
223   catch(Dali::DaliException& e)
224   {
225     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
226   }
227   END_TEST;
228 }
229
230 int UtcDaliTimerUnitializedSignalTick(void)
231 {
232   AdaptorTestApplication application;
233
234   tet_printf("unintialized SignalTick \n");
235
236   Timer* timer = new Timer;
237   DALI_TEST_CHECK(timer != NULL);
238
239   try
240   {
241     TimerTestClass testClass(true); // = new TimerTestClass(true);
242
243     timer->TickSignal().Connect(&testClass, &TimerTestClass::Tick);
244   }
245   catch(Dali::DaliException& e)
246   {
247     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
248   }
249   END_TEST;
250 }
251
252 int UtcDaliTimerSetInterval(void)
253 {
254   AdaptorTestApplication application;
255
256   tet_printf("timer set interval \n");
257   Timer timer = Timer::New(10);
258
259   DALI_TEST_CHECK(timer.GetInterval() == 10);
260
261   timer.SetInterval(5000);
262
263   DALI_TEST_CHECK(timer.GetInterval() == 5000);
264
265   END_TEST;
266 }
267
268 int UtcDaliTimerSetInterval02(void)
269 {
270   AdaptorTestApplication application;
271
272   tet_printf("timer set interval 02 \n");
273   Timer timer = Timer::New(10);
274   timer.SetInterval(20);
275
276   DALI_TEST_CHECK(timer.GetInterval() == 20);
277   DALI_TEST_CHECK(timer.IsRunning() == true);
278
279   timer.SetInterval(5000, false);
280
281   DALI_TEST_CHECK(timer.GetInterval() == 5000);
282   DALI_TEST_CHECK(timer.IsRunning() == false);
283
284   END_TEST;
285 }
286
287 int UtcDaliTimerSetInterval03(void)
288 {
289   AdaptorTestApplication application;
290
291   tet_printf("UtcDaliTimerSetInterval03 SetInterval and ensure timer restarts \n");
292   Timer timer = Timer::New(10);
293   timer.SetInterval(20);
294
295   DALI_TEST_CHECK(timer.GetInterval() == 20);
296   DALI_TEST_CHECK(timer.IsRunning() == true);
297
298   timer.SetInterval(5000, true);
299
300   DALI_TEST_CHECK(timer.GetInterval() == 5000);
301   DALI_TEST_CHECK(timer.IsRunning() == true);
302
303   END_TEST;
304 }
305
306 int UtcDaliTimerCopyConstructor(void)
307 {
308   AdaptorTestApplication application;
309
310   tet_printf("timer copy constructor \n");
311   Timer timer = Timer::New(10);
312
313   Timer anotherTimer(timer);
314
315   DALI_TEST_CHECK(anotherTimer.GetInterval() == 10);
316   END_TEST;
317 }
318
319 int UtcDaliTimerAssignmentOperator(void)
320 {
321   AdaptorTestApplication application;
322
323   tet_printf("assignmnet constructor \n");
324
325   Timer timer = Timer::New(10);
326
327   DALI_TEST_CHECK(timer);
328
329   Timer anotherTimer = Timer::New(40);
330
331   DALI_TEST_CHECK(anotherTimer.GetInterval() == 40);
332
333   tet_printf("timer 1 interval %d, \n", anotherTimer.GetInterval());
334   tet_printf("timer 2 interval %d, \n", timer.GetInterval());
335
336   DALI_TEST_CHECK(timer != anotherTimer);
337
338   timer = anotherTimer;
339
340   DALI_TEST_CHECK(timer == anotherTimer);
341
342   tet_printf("timer 1 interval %d, \n", timer.GetInterval());
343   tet_printf("timer 2 interval %d, \n", anotherTimer.GetInterval());
344
345   DALI_TEST_CHECK(timer.GetInterval() == 40);
346
347   END_TEST;
348 }
349
350 int UtcDaliTimerMoveConstructor(void)
351 {
352   AdaptorTestApplication application;
353
354   Timer timer = Timer::New(40);
355   DALI_TEST_CHECK(timer);
356   DALI_TEST_EQUALS(1, timer.GetBaseObject().ReferenceCount(), TEST_LOCATION);
357   DALI_TEST_CHECK(timer.GetInterval() == 40);
358
359   Timer moved = std::move(timer);
360   DALI_TEST_CHECK(moved);
361   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
362   DALI_TEST_CHECK(moved.GetInterval() == 40);
363   DALI_TEST_CHECK(!timer);
364
365   END_TEST;
366 }
367
368 int UtcDaliTimerMoveAssignmentr(void)
369 {
370   AdaptorTestApplication application;
371
372   Timer timer = Timer::New(40);
373   DALI_TEST_CHECK(timer);
374   DALI_TEST_EQUALS(1, timer.GetBaseObject().ReferenceCount(), TEST_LOCATION);
375   DALI_TEST_CHECK(timer.GetInterval() == 40);
376
377   Timer moved;
378   moved = std::move(timer);
379   DALI_TEST_CHECK(moved);
380   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
381   DALI_TEST_CHECK(moved.GetInterval() == 40);
382   DALI_TEST_CHECK(!timer);
383
384   END_TEST;
385 }
386
387 int UtcDaliTimerIsRunning(void)
388 {
389   AdaptorTestApplication application;
390
391   tet_printf("timer is running \n");
392
393   Timer timer = Timer::New(100);
394
395   timer.Start();
396
397   DALI_TEST_CHECK(timer.IsRunning());
398
399   timer.Stop();
400
401   DALI_TEST_CHECK(timer.IsRunning() == false);
402
403   END_TEST;
404 }
405
406 int UtcDaliTimerSignalTickContinue(void)
407 {
408   AdaptorTestApplication application;
409
410   tet_printf("timer call back\n");
411
412   Timer          timer = Timer::New(100);
413   TimerTestClass testClass(true);
414
415   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
416
417   timer.Start();
418
419   test_ecore_main_loop_begin();
420
421   DALI_TEST_CHECK(testClass.mTimerCalled);
422
423   END_TEST;
424 }
425
426 int UtcDaliTimerSignalTickStop(void)
427 {
428   AdaptorTestApplication application;
429
430   Timer          timer = Timer::New(100);
431   TimerTestClass testClass(false);
432
433   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
434
435   timer.Start();
436
437   test_ecore_main_loop_begin();
438
439   DALI_TEST_CHECK(testClass.mTimerCalled);
440
441   END_TEST;
442 }
443
444 int UtcDaliTimerReset(void)
445 {
446   AdaptorTestApplication application;
447
448   Timer timer = Timer::New(100);
449
450   DALI_TEST_CHECK(timer);
451
452   timer.Reset();
453
454   DALI_TEST_CHECK(!timer);
455
456   END_TEST;
457 }
458
459 int UtcDaliTimerDownCastP(void)
460 {
461   AdaptorTestApplication application;
462
463   Timer timer = Timer::New(100);
464   Timer cast  = Timer::DownCast(timer);
465
466   DALI_TEST_CHECK(cast);
467
468   END_TEST;
469 }
470
471 int UtcDaliTimerDownCastN(void)
472 {
473   AdaptorTestApplication application;
474
475   Timer timer;
476   Timer cast = Timer::DownCast(timer);
477
478   DALI_TEST_CHECK(!cast);
479
480   END_TEST;
481 }
482
483 int UtcDaliTimerPauseN(void)
484 {
485   Timer timer;
486
487   try
488   {
489     timer.Pause();
490     DALI_TEST_CHECK(false); // Should not get here
491   }
492   catch(...)
493   {
494     DALI_TEST_CHECK(true);
495   }
496   END_TEST;
497 }
498
499 int UtcDaliTimerResumeN(void)
500 {
501   Timer timer;
502
503   try
504   {
505     timer.Resume();
506     DALI_TEST_CHECK(false); // Should not get here
507   }
508   catch(...)
509   {
510     DALI_TEST_CHECK(true);
511   }
512   END_TEST;
513 }