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