Cleanup of Visual::Base::SetSize
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-event-thread-callback.cpp
1 /*
2  * Copyright (c) 2015 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 // CLASS HEADER
18 #include "toolkit-event-thread-callback.h"
19
20 // EXTERNAL INCLUDES
21 #include <cstddef>
22 #include <semaphore.h>
23 #include <math.h>
24 #include <ctime>
25 #include <climits>
26 #include <cstdio>
27 #include <unistd.h>
28
29 namespace Dali
30 {
31
32 namespace
33 {
34 EventThreadCallback* gEventThreadCallback = NULL;
35 }
36
37 struct EventThreadCallback::Impl
38 {
39   CallbackBase* callback;
40   unsigned int triggeredCount;
41   unsigned int expectedCount;
42   sem_t mySemaphore;
43 };
44
45 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
46 : mImpl( new Impl() )
47 {
48   mImpl->callback = callback;
49   mImpl->triggeredCount = 0u;
50   mImpl->expectedCount = UINT_MAX;
51   sem_init( &(mImpl->mySemaphore), 0, 0 );
52   gEventThreadCallback = this;
53 }
54
55 EventThreadCallback::~EventThreadCallback()
56 {
57   delete mImpl;
58 }
59
60 void EventThreadCallback::Trigger()
61 {
62   mImpl->triggeredCount++;
63   if(  mImpl->triggeredCount >= mImpl->expectedCount )
64   {
65     sem_post( &(mImpl->mySemaphore) );
66   }
67 }
68
69 bool EventThreadCallback::WaitingForTrigger(unsigned int count, unsigned int seconds)
70 {
71   if(  mImpl->triggeredCount >= count )
72   {
73     return true;
74   }
75   struct timespec now;
76   clock_gettime( CLOCK_REALTIME, &now );
77   now.tv_sec += seconds;
78   mImpl->expectedCount = count;
79   int error = sem_timedwait( &(mImpl->mySemaphore), &now );
80   return error != 0;
81 }
82
83 CallbackBase* EventThreadCallback::GetCallback()
84 {
85   return mImpl->callback;
86 }
87
88 EventThreadCallback* EventThreadCallback::Get()
89 {
90   return gEventThreadCallback;
91 }
92
93 }
94
95 namespace Test
96 {
97
98 bool WaitForEventThreadTrigger( int triggerCount )
99 {
100   bool success = true;
101   const int TEST_TIMEOUT(30);
102
103   struct timespec startTime;
104   struct timespec now;
105   clock_gettime( CLOCK_REALTIME, &startTime );
106   now.tv_sec = startTime.tv_sec;
107   now.tv_nsec = startTime.tv_nsec;
108
109   Dali::EventThreadCallback* eventTrigger = NULL;
110   while( eventTrigger == NULL )
111   {
112     eventTrigger = Dali::EventThreadCallback::Get();
113     clock_gettime( CLOCK_REALTIME, &now );
114     if( now.tv_sec - startTime.tv_sec > TEST_TIMEOUT )
115     {
116       success = false;
117       break;
118     }
119     usleep(10);
120   }
121   if( eventTrigger != NULL )
122   {
123     Dali::CallbackBase* callback = eventTrigger->GetCallback();
124     eventTrigger->WaitingForTrigger( triggerCount, TEST_TIMEOUT - (now.tv_sec - startTime.tv_sec) );
125     Dali::CallbackBase::Execute( *callback );
126   }
127
128   clock_gettime( CLOCK_REALTIME, &now );
129   if( now.tv_sec > startTime.tv_sec + 1 )
130   {
131     fprintf(stderr, "WaitForEventThreadTrigger took %ld seconds\n", now.tv_sec - startTime.tv_sec );
132   }
133   return success;
134 }
135
136 }