support string_view in DALI_TEST_EQUALS
[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 #include <vector>
29 #include <algorithm>
30
31 namespace
32 {
33 // Note, this is not thread safe - however, should not be using
34 // triggers from multiple threads - they should all be created on
35 // event thread.
36 std::vector<Dali::EventThreadCallback*> gEventThreadCallbacks;
37 }
38
39
40 namespace Dali
41 {
42
43 struct EventThreadCallback::Impl
44 {
45   CallbackBase* callback;
46   sem_t mySemaphore;
47 };
48
49 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
50 : mImpl( new Impl() )
51 {
52   mImpl->callback = callback;
53   sem_init( &(mImpl->mySemaphore), 0, 0 );
54
55   gEventThreadCallbacks.push_back(this);
56 }
57
58 EventThreadCallback::~EventThreadCallback()
59 {
60   std::vector<EventThreadCallback*>::iterator iter =
61     std::find(gEventThreadCallbacks.begin(), gEventThreadCallbacks.end(), this);
62   if( iter != gEventThreadCallbacks.end() )
63   {
64     gEventThreadCallbacks.erase(iter);
65   }
66   delete mImpl;
67 }
68
69 void EventThreadCallback::Trigger()
70 {
71   sem_post( &(mImpl->mySemaphore) );
72 }
73
74 // returns true if timed out rather than triggered
75 bool EventThreadCallback::WaitingForTrigger()
76 {
77   struct timespec now;
78   clock_gettime( CLOCK_REALTIME, &now );
79   if( now.tv_nsec < 999900000 ) // 999, 900, 000
80     now.tv_nsec += 1000;
81   else
82   {
83     now.tv_sec += 1;
84     now.tv_nsec = 0;
85   }
86
87   int error = sem_timedwait( &(mImpl->mySemaphore), &now );
88   return error != 0; // true if timeout
89 }
90
91 CallbackBase* EventThreadCallback::GetCallback()
92 {
93   return mImpl->callback;
94 }
95
96 }
97
98
99 namespace Test
100 {
101
102 bool WaitForEventThreadTrigger( int triggerCount, int timeoutInSeconds )
103 {
104   struct timespec startTime;
105   struct timespec now;
106   clock_gettime( CLOCK_REALTIME, &startTime );
107   now.tv_sec = startTime.tv_sec;
108   now.tv_nsec = startTime.tv_nsec;
109
110   // Round robin poll of each semaphore:
111   while ( triggerCount > 0 )
112   {
113     if( gEventThreadCallbacks.size() > 0 )
114     {
115       for( std::vector<Dali::EventThreadCallback*>::iterator iter = gEventThreadCallbacks.begin();
116            iter != gEventThreadCallbacks.end(); ++iter )
117       {
118         Dali::EventThreadCallback* eventTrigger = (*iter);
119         Dali::CallbackBase* callback = eventTrigger->GetCallback();
120         bool timedout = eventTrigger->WaitingForTrigger();
121         if( ! timedout )
122         {
123           // Semaphore was unlocked - execute the trigger
124           Dali::CallbackBase::Execute( *callback );
125           triggerCount--;
126         }
127         if( triggerCount <= 0 )
128         {
129           break;
130         }
131       }
132     }
133     clock_gettime( CLOCK_REALTIME, &now );
134     if( now.tv_sec - startTime.tv_sec > timeoutInSeconds )
135     {
136       // Ensure we break out of the loop if elapsed time has passed
137       break;
138     }
139   }
140
141   clock_gettime( CLOCK_REALTIME, &now );
142   if( now.tv_sec > startTime.tv_sec + 1 )
143   {
144     fprintf(stderr, "WaitForEventThreadTrigger took %ld seconds\n", now.tv_sec - startTime.tv_sec );
145   }
146   return triggerCount == 0;
147 }
148
149 }