Revert "Remove dali-core dependency of GLES version."
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-timer.cpp
1 /*
2  * Copyright (c) 2018 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 "toolkit-timer.h"
19
20 // INTERNAL INCLUDES
21 #include <dali-toolkit/public-api/dali-toolkit-common.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/signals/dali-signal.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33 class Timer;
34
35 typedef IntrusivePtr<Timer> TimerPtr;
36
37 Dali::Timer::TimerSignalType gTickSignal;
38 int gTimerCount = 0;
39 bool gKeepTimersRunning = false;
40
41 /**
42  * Implementation of the timer
43  */
44 class Timer : public BaseObject
45 {
46 public:
47   void MockEmitSignal();
48
49 public:
50   static TimerPtr New( unsigned int milliSec );
51   Timer( unsigned int milliSec );
52   virtual ~Timer();
53
54   void Start();
55   void Stop();
56   void SetInterval( unsigned int interval );
57   unsigned int GetInterval() const;
58   bool IsRunning() const;
59   bool Tick();
60
61 public: // Signals
62
63   Dali::Timer::TimerSignalType& TickSignal();
64
65 private: // Implementation
66
67   // not implemented
68   Timer( const Timer& );
69   Timer& operator=( const Timer& );
70
71 private: // Data
72
73   unsigned int mInterval;
74   bool mRunning;
75 };
76
77 inline Timer& GetImplementation(Dali::Timer& timer)
78 {
79   DALI_ASSERT_ALWAYS(timer && "Timer handle is empty");
80
81   BaseObject& handle = timer.GetBaseObject();
82
83   return static_cast<Internal::Adaptor::Timer&>(handle);
84 }
85
86 inline const Timer& GetImplementation(const Dali::Timer& timer)
87 {
88   DALI_ASSERT_ALWAYS(timer && "Timer handle is empty");
89
90   const BaseObject& handle = timer.GetBaseObject();
91
92   return static_cast<const Internal::Adaptor::Timer&>(handle);
93 }
94
95 TimerPtr Timer::New( unsigned int milliSec )
96 {
97   TimerPtr timerImpl = new Timer( milliSec );
98   return timerImpl;
99 }
100
101 Timer::Timer( unsigned int milliSec )
102 : mInterval( milliSec ),
103   mRunning( false )
104 {
105   ++gTimerCount;
106 }
107
108 Timer::~Timer()
109 {
110   --gTimerCount;
111 }
112
113 void Timer::Start()
114 {
115   mRunning = true;
116 }
117
118 void Timer::Stop()
119 {
120   mRunning = false;
121 }
122
123 void Timer::SetInterval( unsigned int interval )
124 {
125   mInterval = interval;
126 }
127
128 unsigned int Timer::GetInterval() const
129 {
130   return mInterval;
131 }
132
133 bool Timer::IsRunning() const
134 {
135   return mRunning;
136 }
137
138 bool Timer::Tick()
139 {
140   return false;
141 }
142
143 Dali::Timer::TimerSignalType& Timer::TickSignal()
144 {
145   return gTickSignal;
146 }
147
148 // Mock setup functions:
149
150 void Timer::MockEmitSignal()
151 {
152   if( gTimerCount > 1 )
153   {
154     // Only emit the signal if we have more than just the timer created in the test function
155     gTickSignal.Emit();
156   }
157 }
158
159 } // namespace Adaptor
160
161 } // namespace Internal
162
163 /********************************************************************************/
164 /*********************************  PUBLIC CLASS  *******************************/
165 /********************************************************************************/
166
167 Timer::Timer()
168 {
169
170 }
171
172 Timer Timer::New( unsigned int milliSec )
173 {
174   Internal::Adaptor::TimerPtr internal = Internal::Adaptor::Timer::New( milliSec );
175   return Timer(internal.Get());
176 }
177
178 Timer::Timer( const Timer& timer )
179 :BaseHandle( timer )
180 {
181 }
182
183 Timer& Timer::operator=( const Timer& timer )
184 {
185   // check self assignment
186   if( *this != timer )
187   {
188     BaseHandle::operator=(timer);
189   }
190   return *this;
191 }
192
193 Timer::~Timer()
194 {
195 }
196
197 void Timer::Start()
198 {
199   Internal::Adaptor::GetImplementation( *this ).Start();
200   Dali::Internal::Adaptor::gKeepTimersRunning = true;
201 }
202
203 void Timer::Stop()
204 {
205   Internal::Adaptor::GetImplementation( *this ).Stop();
206 }
207
208 void Timer::SetInterval( unsigned int milliSec )
209 {
210   Internal::Adaptor::GetImplementation( *this ).SetInterval( milliSec );
211 }
212
213 unsigned int Timer::GetInterval() const
214 {
215   return Internal::Adaptor::GetImplementation( *this ).GetInterval();
216 }
217
218 bool Timer::IsRunning() const
219 {
220   return true;
221 }
222
223 Timer::TimerSignalType& Timer::TickSignal()
224 {
225   return Internal::Adaptor::GetImplementation( *this ).TickSignal();
226 }
227
228 Timer::Timer(Internal::Adaptor::Timer* timer)
229 : BaseHandle(timer)
230 {
231 }
232
233 // Mock setup functions:
234
235 void Timer::MockEmitSignal()
236 {
237   Internal::Adaptor::GetImplementation( *this ).MockEmitSignal();
238 }
239
240 } // namespace Dali
241
242
243 namespace Test
244 {
245
246 int GetTimerCount()
247 {
248   return Dali::Internal::Adaptor::gTimerCount;
249 }
250
251 void EmitGlobalTimerSignal()
252 {
253   // @todo Multiplex timers properly.
254   Dali::Internal::Adaptor::gKeepTimersRunning = Dali::Internal::Adaptor::gTickSignal.Emit();
255 }
256
257 bool AreTimersRunning()
258 {
259   return Dali::Internal::Adaptor::gKeepTimersRunning;
260 }
261
262 }