License conversion from Flora to Apache 2.0
[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 <dali/dali.h>
21 #include <Ecore.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_timer_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_timer_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 namespace
37 {
38 bool ecore_timer_running = false;
39 Ecore_Task_Cb timer_callback_func=NULL;
40 const void* timer_callback_data=NULL;
41 bool main_loop_can_run = false;
42 int timerId = 0;
43 }// anon namespace
44
45 extern "C"
46 {
47 Ecore_Timer* ecore_timer_add(double in,
48                              Ecore_Task_Cb func,
49                              const void   *data)
50 {
51   ecore_timer_running = true;
52   timer_callback_func = func;
53   timer_callback_data = data;
54   timerId+=8;
55   return (Ecore_Timer*)timerId;
56 }
57
58 void* ecore_timer_del(Ecore_Timer *timer)
59 {
60   ecore_timer_running = false;
61   timer_callback_func = NULL;
62   return NULL;
63 }
64
65 }
66
67 namespace
68 {
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 /**
91  * small class to test timer signal
92  */
93 class TimerTestClass : public ConnectionTracker
94 {
95 public:
96
97   TimerTestClass(bool repeat):mTimerCalled(false),mReturnContiune(repeat) {}
98
99   bool Tick()
100   {
101     tet_printf("timer ticked\n");
102     mTimerCalled = true;
103     // quit the main loop otherwise we'll never return to tet
104     test_ecore_main_loop_quit();
105     return mReturnContiune;
106   }
107   bool mTimerCalled;    // whether tick has been called or not
108   bool mReturnContiune; // whether to return true / false to continue
109
110 };
111
112 } // anon namespace
113
114
115 // Positive test case for a method
116 int UtcDaliTimerCreation(void)
117 {
118  // TestApplication application;
119   tet_printf("timer creation \n");
120   Timer timer = Timer::New(300);
121
122   DALI_TEST_CHECK( timer );
123
124   DALI_TEST_CHECK( timer.GetInterval() == 300);
125
126   END_TEST;
127 }
128
129 int UtcDaliTimerUnitializedStart(void)
130 {
131   tet_printf("unintialized timer start \n");
132
133   Timer *timer = new Timer;
134   DALI_TEST_CHECK(timer != NULL);
135
136   try
137   {
138     timer->Start();
139   }
140   catch(Dali::DaliException& e)
141   {
142     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
143   }
144   END_TEST;
145 }
146
147 int UtcDaliTimerUnitializedStop(void)
148 {
149   tet_printf("unintialized timer stop \n");
150
151   Timer *timer = new Timer;
152   DALI_TEST_CHECK(timer != NULL);
153
154   try
155   {
156     timer->Stop();
157   }
158   catch(Dali::DaliException& e)
159   {
160     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
161   }
162   END_TEST;
163 }
164
165 int UtcDaliTimerUnitializedGetInterval(void)
166 {
167   tet_printf("unintialized get interval \n");
168
169   Timer *timer = new Timer;
170   DALI_TEST_CHECK(timer != NULL);
171
172   try
173   {
174     timer->GetInterval();
175   }
176   catch(Dali::DaliException& e)
177   {
178     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
179   }
180   END_TEST;
181 }
182
183 int UtcDaliTimerUnitializedSetInterval(void)
184 {
185   tet_printf("unintialized set interval \n");
186
187   Timer *timer = new Timer;
188   DALI_TEST_CHECK(timer != NULL);
189
190   try
191   {
192     timer->SetInterval(10);
193   }
194   catch(Dali::DaliException& e)
195   {
196     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
197   }
198   END_TEST;
199 }
200
201 int UtcDaliTimerUnitializedIsRunning(void)
202 {
203   tet_printf("unintialized is running \n");
204
205   Timer *timer = new Timer;
206   DALI_TEST_CHECK(timer != NULL);
207
208   try
209   {
210     timer->IsRunning();
211   }
212   catch(Dali::DaliException& e)
213   {
214     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
215   }
216   END_TEST;
217 }
218
219
220 int UtcDaliTimerUnitializedSignalTick(void)
221 {
222   tet_printf("unintialized SignalTick \n");
223
224   Timer *timer = new Timer;
225   DALI_TEST_CHECK(timer != NULL);
226
227   try
228   {
229     TimerTestClass testClass(true);// = new TimerTestClass(true);
230
231     timer->TickSignal().Connect(&testClass, &TimerTestClass::Tick);
232   }
233   catch(Dali::DaliException& e)
234   {
235     DALI_TEST_ASSERT(e, "timer", TEST_LOCATION);
236   }
237   END_TEST;
238 }
239
240 int UtcDaliTimerSetInterval(void)
241 {
242   tet_printf("timer set interval \n");
243   Timer timer = Timer::New(10);
244
245   DALI_TEST_CHECK( timer.GetInterval() == 10);
246
247   timer.SetInterval(5000);
248
249   DALI_TEST_CHECK( timer.GetInterval() == 5000);
250
251   END_TEST;
252 }
253
254 int UtcDaliTimerCopyConstructor(void)
255 {
256   tet_printf("timer copy constructor \n");
257   Timer timer = Timer::New(10);
258
259   Timer anotherTimer( timer );
260
261   DALI_TEST_CHECK( anotherTimer.GetInterval() == 10);
262   END_TEST;
263 }
264
265 int UtcDaliTimerAssignmentOperator(void)
266 {
267   tet_printf("assignmnet constructor \n");
268
269   Timer timer = Timer::New(10);
270
271   DALI_TEST_CHECK( timer );
272
273   Timer anotherTimer = Timer::New(40);
274
275   DALI_TEST_CHECK(anotherTimer.GetInterval() == 40);
276
277   tet_printf("timer 1 interval %d, \n",anotherTimer.GetInterval());
278   tet_printf("timer 2 interval %d, \n",timer.GetInterval());
279
280   DALI_TEST_CHECK(timer != anotherTimer);
281
282   timer = anotherTimer;
283
284   DALI_TEST_CHECK(timer == anotherTimer);
285
286   tet_printf("timer 1 interval %d, \n",timer.GetInterval());
287   tet_printf("timer 2 interval %d, \n",anotherTimer.GetInterval());
288
289   DALI_TEST_CHECK(timer.GetInterval() == 40);
290
291   END_TEST;
292 }
293
294 int UtcDaliTimerIsRunning(void)
295 {
296   tet_printf("timer is running \n");
297
298   Timer timer = Timer::New(100);
299
300   timer.Start();
301
302   DALI_TEST_CHECK( timer.IsRunning() );
303
304   timer.Stop();
305
306   DALI_TEST_CHECK( timer.IsRunning() == false );
307
308   END_TEST;
309 }
310
311 int UtcDaliTimerSignalTickContinue(void)
312 {
313   tet_printf("timer call back\n");
314
315   Timer timer = Timer::New(100);
316   TimerTestClass testClass(true);
317
318   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
319
320   timer.Start();
321
322   test_ecore_main_loop_begin();
323
324   DALI_TEST_CHECK( testClass.mTimerCalled );
325
326   END_TEST;
327 }
328
329 int UtcDaliTimerSignalTickStop(void)
330 {
331   Timer timer = Timer::New(100);
332   TimerTestClass testClass(false);
333
334   timer.TickSignal().Connect(&testClass, &TimerTestClass::Tick);
335
336   timer.Start();
337
338   test_ecore_main_loop_begin();
339
340   DALI_TEST_CHECK( testClass.mTimerCalled );
341
342   END_TEST;
343 }
344
345 int UtcDaliTimerReset(void)
346 {
347   Timer timer = Timer::New(100);
348
349   DALI_TEST_CHECK(timer);
350
351   timer.Reset();
352
353   DALI_TEST_CHECK(!timer);
354
355   END_TEST;
356 }