Merge "Add an assertion for thread-satfy check to Timer" into devel/master
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / utc-Dali-Timer.cpp
1 /*
2  * Copyright (c) 2014 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 <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 UtcDaliTimerCopyConstructor(void)
272 {
273   AdaptorTestApplication application;
274
275   tet_printf("timer copy constructor \n");
276   Timer timer = Timer::New(10);
277
278   Timer anotherTimer( timer );
279
280   DALI_TEST_CHECK( anotherTimer.GetInterval() == 10);
281   END_TEST;
282 }
283
284 int UtcDaliTimerAssignmentOperator(void)
285 {
286   AdaptorTestApplication application;
287
288   tet_printf("assignmnet constructor \n");
289
290   Timer timer = Timer::New(10);
291
292   DALI_TEST_CHECK( timer );
293
294   Timer anotherTimer = Timer::New(40);
295
296   DALI_TEST_CHECK(anotherTimer.GetInterval() == 40);
297
298   tet_printf("timer 1 interval %d, \n",anotherTimer.GetInterval());
299   tet_printf("timer 2 interval %d, \n",timer.GetInterval());
300
301   DALI_TEST_CHECK(timer != anotherTimer);
302
303   timer = anotherTimer;
304
305   DALI_TEST_CHECK(timer == anotherTimer);
306
307   tet_printf("timer 1 interval %d, \n",timer.GetInterval());
308   tet_printf("timer 2 interval %d, \n",anotherTimer.GetInterval());
309
310   DALI_TEST_CHECK(timer.GetInterval() == 40);
311
312   END_TEST;
313 }
314
315 int UtcDaliTimerIsRunning(void)
316 {
317   AdaptorTestApplication application;
318
319   tet_printf("timer is running \n");
320
321   Timer timer = Timer::New(100);
322
323   timer.Start();
324
325   DALI_TEST_CHECK( timer.IsRunning() );
326
327   timer.Stop();
328
329   DALI_TEST_CHECK( timer.IsRunning() == false );
330
331   END_TEST;
332 }
333
334 int UtcDaliTimerSignalTickContinue(void)
335 {
336   AdaptorTestApplication application;
337
338   tet_printf("timer call back\n");
339
340   Timer timer = Timer::New(100);
341   TimerTestClass testClass(true);
342
343   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
344
345   timer.Start();
346
347   test_ecore_main_loop_begin();
348
349   DALI_TEST_CHECK( testClass.mTimerCalled );
350
351   END_TEST;
352 }
353
354 int UtcDaliTimerSignalTickStop(void)
355 {
356   AdaptorTestApplication application;
357
358   Timer timer = Timer::New(100);
359   TimerTestClass testClass(false);
360
361   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
362
363   timer.Start();
364
365   test_ecore_main_loop_begin();
366
367   DALI_TEST_CHECK( testClass.mTimerCalled );
368
369   END_TEST;
370 }
371
372 int UtcDaliTimerReset(void)
373 {
374   AdaptorTestApplication application;
375
376   Timer timer = Timer::New(100);
377
378   DALI_TEST_CHECK(timer);
379
380   timer.Reset();
381
382   DALI_TEST_CHECK(!timer);
383
384   END_TEST;
385 }
386
387 int UtcDaliTimerDownCastP(void)
388 {
389   AdaptorTestApplication application;
390
391   Timer timer = Timer::New(100);
392   Timer cast = Timer::DownCast( timer );
393
394   DALI_TEST_CHECK( cast );
395
396   END_TEST;
397 }
398
399 int UtcDaliTimerDownCastN(void)
400 {
401   AdaptorTestApplication application;
402
403   Timer timer;
404   Timer cast = Timer::DownCast( timer );
405
406   DALI_TEST_CHECK( ! cast );
407
408   END_TEST;
409 }