ecd4831edf014f76c346a1b0e93cbd45c0deab16
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / utc-Dali-Timer.cpp
1 /*
2  * Copyright (c) 2020 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 <iostream>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <dali/dali.h>
22 #include <dali/internal/system/linux/dali-ecore.h>
23 #include <dali-test-suite-utils.h>
24 #include <adaptor-test-application.h>
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 }// anon 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
69 namespace
70 {
71
72 void test_ecore_main_loop_begin()
73 {
74   if(timer_callback_func != NULL)
75   {
76     main_loop_can_run = true;
77     while( main_loop_can_run )
78     {
79       if( ! timer_callback_func(const_cast<void*>(timer_callback_data)) )
80         break;
81     }
82   }
83 }
84
85 void test_ecore_main_loop_quit()
86 {
87   timer_callback_func = NULL;
88   main_loop_can_run = false;
89 }
90
91
92 /**
93  * small class to test timer signal
94  */
95 class TimerTestClass : public ConnectionTracker
96 {
97 public:
98
99   TimerTestClass(bool repeat):mTimerCalled(false),mReturnContiune(repeat) {}
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
114 } // anon namespace
115
116
117 // Positive test case for a method
118 int UtcDaliTimerCreation(void)
119 {
120   AdaptorTestApplication application;
121
122   tet_printf("timer creation \n");
123   Timer timer = Timer::New(300);
124
125   DALI_TEST_CHECK( timer );
126
127   DALI_TEST_CHECK( timer.GetInterval() == 300);
128
129   END_TEST;
130 }
131
132 int UtcDaliTimerUnitializedStart(void)
133 {
134   AdaptorTestApplication application;
135
136   tet_printf("unintialized timer start \n");
137
138   Timer *timer = new Timer;
139   DALI_TEST_CHECK(timer != NULL);
140
141   try
142   {
143     timer->Start();
144   }
145   catch(Dali::DaliException& e)
146   {
147     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
148   }
149   END_TEST;
150 }
151
152 int UtcDaliTimerUnitializedStop(void)
153 {
154   AdaptorTestApplication application;
155
156   tet_printf("unintialized timer stop \n");
157
158   Timer *timer = new Timer;
159   DALI_TEST_CHECK(timer != NULL);
160
161   try
162   {
163     timer->Stop();
164   }
165   catch(Dali::DaliException& e)
166   {
167     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
168   }
169   END_TEST;
170 }
171
172 int UtcDaliTimerUnitializedGetInterval(void)
173 {
174   AdaptorTestApplication application;
175
176   tet_printf("unintialized get interval \n");
177
178   Timer *timer = new Timer;
179   DALI_TEST_CHECK(timer != NULL);
180
181   try
182   {
183     timer->GetInterval();
184   }
185   catch(Dali::DaliException& e)
186   {
187     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
188   }
189   END_TEST;
190 }
191
192 int UtcDaliTimerUnitializedSetInterval(void)
193 {
194   AdaptorTestApplication application;
195
196   tet_printf("unintialized set interval \n");
197
198   Timer *timer = new Timer;
199   DALI_TEST_CHECK(timer != NULL);
200
201   try
202   {
203     timer->SetInterval(10);
204   }
205   catch(Dali::DaliException& e)
206   {
207     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
208   }
209   END_TEST;
210 }
211
212 int UtcDaliTimerUnitializedIsRunning(void)
213 {
214   AdaptorTestApplication application;
215
216   tet_printf("unintialized is running \n");
217
218   Timer *timer = new Timer;
219   DALI_TEST_CHECK(timer != NULL);
220
221   try
222   {
223     timer->IsRunning();
224   }
225   catch(Dali::DaliException& e)
226   {
227     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
228   }
229   END_TEST;
230 }
231
232
233 int UtcDaliTimerUnitializedSignalTick(void)
234 {
235   AdaptorTestApplication application;
236
237   tet_printf("unintialized SignalTick \n");
238
239   Timer *timer = new Timer;
240   DALI_TEST_CHECK(timer != NULL);
241
242   try
243   {
244     TimerTestClass testClass(true);// = new TimerTestClass(true);
245
246     timer->TickSignal().Connect(&testClass, &TimerTestClass::Tick);
247   }
248   catch(Dali::DaliException& e)
249   {
250     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
251   }
252   END_TEST;
253 }
254
255 int UtcDaliTimerSetInterval(void)
256 {
257   AdaptorTestApplication application;
258
259   tet_printf("timer set interval \n");
260   Timer timer = Timer::New(10);
261
262   DALI_TEST_CHECK( timer.GetInterval() == 10);
263
264   timer.SetInterval(5000);
265
266   DALI_TEST_CHECK( timer.GetInterval() == 5000);
267
268   END_TEST;
269 }
270
271 int UtcDaliTimerSetInterval02(void)
272 {
273   AdaptorTestApplication application;
274
275   tet_printf("timer set interval 02 \n");
276   Timer timer = Timer::New(10);
277   timer.SetInterval(20);
278
279   DALI_TEST_CHECK( timer.GetInterval() == 20 );
280   DALI_TEST_CHECK( timer.IsRunning() == true );
281
282   timer.SetInterval(5000, false);
283
284   DALI_TEST_CHECK( timer.GetInterval() == 5000 );
285   DALI_TEST_CHECK( timer.IsRunning() == false );
286
287   END_TEST;
288 }
289
290 int UtcDaliTimerSetInterval03(void)
291 {
292   AdaptorTestApplication application;
293
294   tet_printf("UtcDaliTimerSetInterval03 SetInterval and ensure timer restarts \n");
295   Timer timer = Timer::New(10);
296   timer.SetInterval(20);
297
298   DALI_TEST_CHECK( timer.GetInterval() == 20 );
299   DALI_TEST_CHECK( timer.IsRunning() == true );
300
301   timer.SetInterval(5000, true);
302
303   DALI_TEST_CHECK( timer.GetInterval() == 5000 );
304   DALI_TEST_CHECK( timer.IsRunning() == true );
305
306   END_TEST;
307 }
308
309 int UtcDaliTimerCopyConstructor(void)
310 {
311   AdaptorTestApplication application;
312
313   tet_printf("timer copy constructor \n");
314   Timer timer = Timer::New(10);
315
316   Timer anotherTimer( timer );
317
318   DALI_TEST_CHECK( anotherTimer.GetInterval() == 10);
319   END_TEST;
320 }
321
322 int UtcDaliTimerAssignmentOperator(void)
323 {
324   AdaptorTestApplication application;
325
326   tet_printf("assignmnet constructor \n");
327
328   Timer timer = Timer::New(10);
329
330   DALI_TEST_CHECK( timer );
331
332   Timer anotherTimer = Timer::New(40);
333
334   DALI_TEST_CHECK(anotherTimer.GetInterval() == 40);
335
336   tet_printf("timer 1 interval %d, \n",anotherTimer.GetInterval());
337   tet_printf("timer 2 interval %d, \n",timer.GetInterval());
338
339   DALI_TEST_CHECK(timer != anotherTimer);
340
341   timer = anotherTimer;
342
343   DALI_TEST_CHECK(timer == anotherTimer);
344
345   tet_printf("timer 1 interval %d, \n",timer.GetInterval());
346   tet_printf("timer 2 interval %d, \n",anotherTimer.GetInterval());
347
348   DALI_TEST_CHECK(timer.GetInterval() == 40);
349
350   END_TEST;
351 }
352
353 int UtcDaliTimerMoveConstructor(void)
354 {
355   AdaptorTestApplication application;
356
357   Timer timer = Timer::New( 40 );
358   DALI_TEST_CHECK( timer );
359   DALI_TEST_EQUALS( 1, timer.GetBaseObject().ReferenceCount(), TEST_LOCATION );
360   DALI_TEST_CHECK( timer.GetInterval() == 40) ;
361
362   Timer moved = std::move( timer );
363   DALI_TEST_CHECK( moved );
364   DALI_TEST_EQUALS( 1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION );
365   DALI_TEST_CHECK( moved.GetInterval() == 40 );
366   DALI_TEST_CHECK( !timer );
367
368   END_TEST;
369 }
370
371 int UtcDaliTimerMoveAssignmentr(void)
372 {
373   AdaptorTestApplication application;
374
375   Timer timer = Timer::New( 40 );
376   DALI_TEST_CHECK( timer );
377   DALI_TEST_EQUALS( 1, timer.GetBaseObject().ReferenceCount(), TEST_LOCATION );
378   DALI_TEST_CHECK( timer.GetInterval() == 40) ;
379
380   Timer moved;
381   moved = std::move( timer );
382   DALI_TEST_CHECK( moved );
383   DALI_TEST_EQUALS( 1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION );
384   DALI_TEST_CHECK( moved.GetInterval() == 40 );
385   DALI_TEST_CHECK( !timer );
386
387   END_TEST;
388 }
389
390 int UtcDaliTimerIsRunning(void)
391 {
392   AdaptorTestApplication application;
393
394   tet_printf("timer is running \n");
395
396   Timer timer = Timer::New(100);
397
398   timer.Start();
399
400   DALI_TEST_CHECK( timer.IsRunning() );
401
402   timer.Stop();
403
404   DALI_TEST_CHECK( timer.IsRunning() == false );
405
406   END_TEST;
407 }
408
409 int UtcDaliTimerSignalTickContinue(void)
410 {
411   AdaptorTestApplication application;
412
413   tet_printf("timer call back\n");
414
415   Timer timer = Timer::New(100);
416   TimerTestClass testClass(true);
417
418   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
419
420   timer.Start();
421
422   test_ecore_main_loop_begin();
423
424   DALI_TEST_CHECK( testClass.mTimerCalled );
425
426   END_TEST;
427 }
428
429 int UtcDaliTimerSignalTickStop(void)
430 {
431   AdaptorTestApplication application;
432
433   Timer timer = Timer::New(100);
434   TimerTestClass testClass(false);
435
436   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
437
438   timer.Start();
439
440   test_ecore_main_loop_begin();
441
442   DALI_TEST_CHECK( testClass.mTimerCalled );
443
444   END_TEST;
445 }
446
447 int UtcDaliTimerReset(void)
448 {
449   AdaptorTestApplication application;
450
451   Timer timer = Timer::New(100);
452
453   DALI_TEST_CHECK(timer);
454
455   timer.Reset();
456
457   DALI_TEST_CHECK(!timer);
458
459   END_TEST;
460 }
461
462 int UtcDaliTimerDownCastP(void)
463 {
464   AdaptorTestApplication application;
465
466   Timer timer = Timer::New(100);
467   Timer cast = Timer::DownCast( timer );
468
469   DALI_TEST_CHECK( cast );
470
471   END_TEST;
472 }
473
474 int UtcDaliTimerDownCastN(void)
475 {
476   AdaptorTestApplication application;
477
478   Timer timer;
479   Timer cast = Timer::DownCast( timer );
480
481   DALI_TEST_CHECK( ! cast );
482
483   END_TEST;
484 }