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