bfc2478609447f0d40d3f5bc58b783ec0fcb7bac
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-adaptor.cpp
1 /*
2  * Copyright (c) 2019 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 <algorithm>
19
20 #include <toolkit-window-impl.h>
21
22 // Don't want to include the actual window.h which otherwise will be indirectly included by adaptor.h.
23 #define DALI_WINDOW_H
24 #include <dali/integration-api/adaptors/adaptor.h>
25
26 #include <dali/integration-api/adaptors/scene-holder.h>
27 #include <toolkit-scene-holder-impl.h>
28
29 #include <toolkit-adaptor-impl.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/integration-api/scene.h>
32 #include <test-application.h>
33 #include <test-render-surface.h>
34
35 namespace Dali
36 {
37
38 namespace
39 {
40
41 ///////////////////////////////////////////////////////////////////////////////
42 //
43 // LogFactoryStub
44 //
45 ///////////////////////////////////////////////////////////////////////////////
46
47 class LogFactory : public LogFactoryInterface
48 {
49 public:
50   LogFactory() = default;
51   virtual ~LogFactory() = default;
52
53 private:
54   void InstallLogFunction() const override
55   {
56     Dali::Integration::Log::InstallLogFunction( &TestApplication::LogMessage );
57   }
58 };
59 LogFactory* gLogFactory = NULL; // For some reason, destroying this when the Adaptor is destroyed causes a crash in some test cases when running all of them.
60 } //unnamed namespace
61
62 namespace Internal
63 {
64 namespace Adaptor
65 {
66
67 ///////////////////////////////////////////////////////////////////////////////
68 //
69 // Dali::Internal::Adaptor::Adaptor Stub
70 //
71 ///////////////////////////////////////////////////////////////////////////////
72
73 Dali::Adaptor* gAdaptor = nullptr;
74
75 Dali::Adaptor& Adaptor::New()
76 {
77   DALI_ASSERT_ALWAYS( ! gAdaptor );
78   gAdaptor = new Dali::Adaptor;
79   return *gAdaptor;
80 }
81
82 Dali::Adaptor& Adaptor::Get()
83 {
84   DALI_ASSERT_ALWAYS( gAdaptor );
85   return *gAdaptor;
86 }
87
88 Adaptor::Adaptor()
89 {
90 }
91
92 Adaptor::~Adaptor()
93 {
94   gAdaptor = nullptr;
95 }
96
97 void Adaptor::Start( Dali::Window window )
98 {
99   AddWindow( &GetImplementation( window ) );
100 }
101
102 Integration::Scene Adaptor::GetScene( Dali::Window window )
103 {
104   return window.GetScene();
105 }
106
107 bool Adaptor::AddIdle( CallbackBase* callback, bool hasReturnValue )
108 {
109   mCallbacks.PushBack( callback );
110   return true;
111 }
112
113 void Adaptor::RemoveIdle( CallbackBase* callback )
114 {
115   mCallbacks.Erase( std::find_if( mCallbacks.Begin(), mCallbacks.End(),
116                                   [ &callback ] ( CallbackBase* current ) { return callback == current; } ) );
117 }
118
119 void Adaptor::RunIdles()
120 {
121   for( auto& callback : mCallbacks )
122   {
123     CallbackBase::Execute( *callback );
124   }
125
126   mCallbacks.Clear();
127 }
128
129 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
130 {
131   DALI_ASSERT_ALWAYS( ! mWindows.empty() );
132
133   return reinterpret_cast < Dali::RenderSurfaceInterface& >( mWindows.front()->GetRenderSurface() );
134 }
135
136 Dali::WindowContainer Adaptor::GetWindows()
137 {
138   Dali::WindowContainer windows;
139
140   for ( auto iter = mWindows.begin(); iter != mWindows.end(); ++iter )
141   {
142     // Downcast to Dali::Window
143     Dali::Window window( dynamic_cast<Dali::Internal::Adaptor::Window*>( *iter ) );
144     if ( window )
145     {
146       windows.push_back( window );
147     }
148   }
149
150   return windows;
151 }
152
153 Dali::SceneHolderList Adaptor::GetSceneHolders()
154 {
155   Dali::SceneHolderList sceneHolderList;
156
157   for( auto iter = mWindows.begin(); iter != mWindows.end(); ++iter )
158   {
159     sceneHolderList.push_back( Dali::Integration::SceneHolder( *iter ) );
160   }
161
162   return sceneHolderList;
163 }
164
165 Dali::Internal::Adaptor::SceneHolder* Adaptor::GetWindow( Dali::Actor& actor )
166 {
167   Dali::Integration::Scene scene = Dali::Integration::Scene::Get( actor );
168
169   for( auto window : mWindows )
170   {
171     if ( scene == window->GetScene() )
172     {
173       return window;
174     }
175   }
176
177   return nullptr;
178 }
179
180 void Adaptor::AddWindow( Internal::Adaptor::SceneHolder* window )
181 {
182   if ( window )
183   {
184     mWindows.push_back( window );
185
186     Dali::Integration::SceneHolder newWindow( window );
187     mWindowCreatedSignal.Emit( newWindow );
188   }
189 }
190
191 void Adaptor::RemoveWindow( Internal::Adaptor::SceneHolder* window )
192 {
193   auto iter = std::find( mWindows.begin(), mWindows.end(), window );
194   if( iter != mWindows.end() )
195   {
196     mWindows.erase( iter );
197   }
198 }
199
200 Dali::Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
201 {
202   return mResizedSignal;
203 }
204
205 Dali::Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
206 {
207   return mLanguageChangedSignal;
208 }
209
210 Dali::Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
211 {
212   return mWindowCreatedSignal;
213 }
214
215 } // namespace Adaptor
216 } // namespace Internal
217
218 ///////////////////////////////////////////////////////////////////////////////
219 //
220 // Dali::Adaptor Stub
221 //
222 ///////////////////////////////////////////////////////////////////////////////
223
224 Adaptor::Adaptor()
225 : mImpl( new Internal::Adaptor::Adaptor )
226 {
227 }
228
229 Adaptor::~Adaptor()
230 {
231   Internal::Adaptor::gAdaptor = nullptr;
232   delete mImpl;
233 }
234
235 void Adaptor::Start()
236 {
237 }
238
239 void Adaptor::Pause()
240 {
241 }
242
243 void Adaptor::Resume()
244 {
245 }
246
247 void Adaptor::Stop()
248 {
249 }
250
251 bool Adaptor::AddIdle( CallbackBase* callback, bool hasReturnValue )
252 {
253   return mImpl->AddIdle( callback, hasReturnValue );
254 }
255
256 void Adaptor::RemoveIdle( CallbackBase* callback )
257 {
258   mImpl->RemoveIdle( callback );
259 }
260
261 void Adaptor::ReplaceSurface( Window window, Dali::RenderSurfaceInterface& surface )
262 {
263 }
264
265 void Adaptor::ReplaceSurface( Dali::Integration::SceneHolder window, Dali::RenderSurfaceInterface& surface )
266 {
267 }
268
269 Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
270 {
271   return mImpl->ResizedSignal();
272 }
273
274 Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
275 {
276   return mImpl->LanguageChangedSignal();
277 }
278
279 Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
280 {
281   return mImpl->WindowCreatedSignal();
282 }
283
284 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
285 {
286   return mImpl->GetSurface();
287 }
288
289 Dali::WindowContainer Adaptor::GetWindows() const
290 {
291   return mImpl->GetWindows();
292 }
293
294 Dali::SceneHolderList Adaptor::GetSceneHolders() const
295 {
296   return mImpl->GetSceneHolders();
297 }
298
299 Any Adaptor::GetNativeWindowHandle()
300 {
301   Any window;
302   return window;
303 }
304
305 Any Adaptor::GetNativeWindowHandle( Actor actor )
306 {
307   return GetNativeWindowHandle();
308 }
309
310 void Adaptor::ReleaseSurfaceLock()
311 {
312 }
313
314 void Adaptor::SetRenderRefreshRate( unsigned int numberOfVSyncsPerRender )
315 {
316 }
317
318 Adaptor& Adaptor::Get()
319 {
320   return Internal::Adaptor::Adaptor::Get();
321 }
322
323 bool Adaptor::IsAvailable()
324 {
325   return Internal::Adaptor::gAdaptor;
326 }
327
328 void Adaptor::NotifySceneCreated()
329 {
330 }
331
332 void Adaptor::NotifyLanguageChanged()
333 {
334 }
335
336 void Adaptor::FeedTouchPoint( TouchPoint& point, int timeStamp )
337 {
338 }
339
340 void Adaptor::FeedWheelEvent( WheelEvent& wheelEvent )
341 {
342 }
343
344 void Adaptor::FeedKeyEvent( KeyEvent& keyEvent )
345 {
346 }
347
348 void Adaptor::SceneCreated()
349 {
350 }
351
352 const LogFactoryInterface& Adaptor::GetLogFactory()
353 {
354   if( gLogFactory == NULL )
355   {
356     gLogFactory = new LogFactory;
357   }
358   return *gLogFactory;
359 }
360
361 } // namespace Dali