[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-adaptor.cpp
1 /*
2  * Copyright (c) 2024 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 <dali/integration-api/debug.h>
28 #include <dali/integration-api/scene.h>
29 #include <test-application.h>
30 #include <toolkit-adaptor-impl.h>
31 #include <toolkit-application.h>
32 #include <toolkit-async-task-manager.h>
33 #include <toolkit-scene-holder-impl.h>
34 #include <toolkit-test-application.h>
35 #include "dali-test-suite-utils.h"
36
37 namespace Dali
38 {
39 namespace Internal
40 {
41 namespace Adaptor
42 {
43 ///////////////////////////////////////////////////////////////////////////////
44 //
45 // Dali::Internal::Adaptor::Adaptor Stub
46 //
47 ///////////////////////////////////////////////////////////////////////////////
48
49 Dali::Adaptor* gAdaptor = nullptr;
50
51 Dali::Adaptor& Adaptor::New()
52 {
53   DALI_ASSERT_ALWAYS(!gAdaptor);
54   gAdaptor = new Dali::Adaptor;
55   return *gAdaptor;
56 }
57
58 Dali::Adaptor& Adaptor::Get()
59 {
60   DALI_ASSERT_ALWAYS(gAdaptor);
61   return *gAdaptor;
62 }
63
64 Adaptor::Adaptor()
65 {
66 }
67
68 Adaptor::~Adaptor()
69 {
70   gAdaptor = nullptr;
71
72   // Ensure all threads and not-excuted tasks are destroyed.
73   // TODO : we'd better make some singletone service for toolkit UTC in future.
74   Test::AsyncTaskManager::DestroyAsyncTaskManager();
75 }
76
77 void Adaptor::Start(Dali::Window window)
78 {
79   AddWindow(&GetImplementation(window));
80 }
81
82 void Adaptor::Stop()
83 {
84   if(mTestApplication)
85   {
86     Integration::Core& core = mTestApplication->GetCore();
87     tet_printf("Adaptor::UnregisterProcessors\n");
88     core.UnregisterProcessors();
89   }
90
91   mStopped = true;
92 }
93
94 Integration::Scene Adaptor::GetScene(Dali::Window window)
95 {
96   return window.GetScene();
97 }
98
99 bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
100 {
101   if(ToolkitApplication::ADD_IDLE_SUCCESS)
102   {
103     if(hasReturnValue)
104     {
105       mReturnCallbacks.PushBack(callback);
106     }
107     else
108     {
109       mCallbacks.PushBack(callback);
110     }
111   }
112   return ToolkitApplication::ADD_IDLE_SUCCESS;
113 }
114
115 void Adaptor::RemoveIdle(CallbackBase* callback)
116 {
117   mCallbacks.Erase(std::remove_if(mCallbacks.Begin(), mCallbacks.End(), [&callback](CallbackBase* current) { return callback == current; }), mCallbacks.End());
118   mReturnCallbacks.Erase(std::remove_if(mReturnCallbacks.Begin(), mReturnCallbacks.End(), [&callback](CallbackBase* current) { return callback == current; }), mReturnCallbacks.End());
119 }
120
121 void Adaptor::RunIdles()
122 {
123   Dali::Vector<CallbackBase*> reusedCallbacks;
124   for(auto& callback : mReturnCallbacks)
125   {
126     bool retValue = CallbackBase::ExecuteReturn<bool>(*callback);
127     if(retValue)
128     {
129       reusedCallbacks.PushBack(callback);
130     }
131   }
132   for(auto& callback : mCallbacks)
133   {
134     CallbackBase::Execute(*callback);
135   }
136
137   mCallbacks.Clear();
138   mReturnCallbacks.Clear();
139   mReturnCallbacks.Swap(reusedCallbacks);
140 }
141
142 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
143 {
144   DALI_ASSERT_ALWAYS(!mWindows.empty());
145
146   return reinterpret_cast<Dali::RenderSurfaceInterface&>(mWindows.front()->GetRenderSurface());
147 }
148
149 Dali::WindowContainer Adaptor::GetWindows()
150 {
151   Dali::WindowContainer windows;
152
153   for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
154   {
155     // Downcast to Dali::Window
156     Dali::Window window(dynamic_cast<Dali::Internal::Adaptor::Window*>(*iter));
157     if(window)
158     {
159       windows.push_back(window);
160     }
161   }
162
163   return windows;
164 }
165
166 Dali::SceneHolderList Adaptor::GetSceneHolders()
167 {
168   Dali::SceneHolderList sceneHolderList;
169
170   for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
171   {
172     sceneHolderList.push_back(Dali::Integration::SceneHolder(*iter));
173   }
174
175   return sceneHolderList;
176 }
177
178 Dali::Internal::Adaptor::SceneHolder* Adaptor::GetWindow(Dali::Actor& actor)
179 {
180   Dali::Integration::Scene scene = Dali::Integration::Scene::Get(actor);
181
182   for(auto window : mWindows)
183   {
184     if(scene == window->GetScene())
185     {
186       return window;
187     }
188   }
189
190   return nullptr;
191 }
192
193 void Adaptor::AddWindow(Internal::Adaptor::SceneHolder* window)
194 {
195   if(window)
196   {
197     mWindows.push_back(window);
198
199     Dali::Integration::SceneHolder newWindow(window);
200     mWindowCreatedSignal.Emit(newWindow);
201   }
202 }
203
204 void Adaptor::RemoveWindow(Internal::Adaptor::SceneHolder* window)
205 {
206   auto iter = std::find(mWindows.begin(), mWindows.end(), window);
207   if(iter != mWindows.end())
208   {
209     mWindows.erase(iter);
210   }
211 }
212
213 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
214 {
215   Integration::Core& core = mTestApplication->GetCore();
216   tet_printf("Adaptor::RegisterProcessor : %s\n", processor.GetProcessorName().data());
217   core.RegisterProcessor(processor, postProcessor);
218 }
219
220 void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
221 {
222   Integration::Core& core = mTestApplication->GetCore();
223   tet_printf("Adaptor::UnregisterProcessor : %s\n", processor.GetProcessorName().data());
224   core.UnregisterProcessor(processor, postProcessor);
225 }
226
227 void Adaptor::SetApplication(Dali::TestApplication& testApplication)
228 {
229   mTestApplication = &testApplication;
230 }
231
232 Dali::Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
233 {
234   return mResizedSignal;
235 }
236
237 Dali::Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
238 {
239   return mLanguageChangedSignal;
240 }
241
242 Dali::Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
243 {
244   return mWindowCreatedSignal;
245 }
246
247 } // namespace Adaptor
248 } // namespace Internal
249
250 ///////////////////////////////////////////////////////////////////////////////
251 //
252 // Dali::Adaptor Stub
253 //
254 ///////////////////////////////////////////////////////////////////////////////
255
256 Adaptor::Adaptor()
257 : mImpl(new Internal::Adaptor::Adaptor)
258 {
259 }
260
261 Adaptor::~Adaptor()
262 {
263   Internal::Adaptor::gAdaptor = nullptr;
264   delete mImpl;
265 }
266
267 void Adaptor::Start()
268 {
269 }
270
271 void Adaptor::Pause()
272 {
273 }
274
275 void Adaptor::Resume()
276 {
277 }
278
279 void Adaptor::Stop()
280 {
281   mImpl->Stop();
282 }
283
284 bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
285 {
286   return mImpl->AddIdle(callback, hasReturnValue);
287 }
288
289 void Adaptor::RemoveIdle(CallbackBase* callback)
290 {
291   mImpl->RemoveIdle(callback);
292 }
293
294 void Adaptor::ReplaceSurface(Window window, Dali::RenderSurfaceInterface& surface)
295 {
296 }
297
298 void Adaptor::ReplaceSurface(Dali::Integration::SceneHolder window, Dali::RenderSurfaceInterface& surface)
299 {
300 }
301
302 Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
303 {
304   return mImpl->ResizedSignal();
305 }
306
307 Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
308 {
309   return mImpl->LanguageChangedSignal();
310 }
311
312 Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
313 {
314   return mImpl->WindowCreatedSignal();
315 }
316
317 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
318 {
319   return mImpl->GetSurface();
320 }
321
322 Dali::WindowContainer Adaptor::GetWindows() const
323 {
324   return mImpl->GetWindows();
325 }
326
327 Dali::SceneHolderList Adaptor::GetSceneHolders() const
328 {
329   return mImpl->GetSceneHolders();
330 }
331
332 Any Adaptor::GetNativeWindowHandle()
333 {
334   Any window;
335   return window;
336 }
337
338 Any Adaptor::GetNativeWindowHandle(Actor actor)
339 {
340   return GetNativeWindowHandle();
341 }
342
343 void Adaptor::ReleaseSurfaceLock()
344 {
345 }
346
347 void Adaptor::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender)
348 {
349 }
350
351 Adaptor& Adaptor::Get()
352 {
353   return Internal::Adaptor::Adaptor::Get();
354 }
355
356 bool Adaptor::IsAvailable()
357 {
358   return Internal::Adaptor::gAdaptor && (!Internal::Adaptor::Adaptor::GetImpl(*Internal::Adaptor::gAdaptor).IsStopped());
359 }
360
361 void Adaptor::NotifySceneCreated()
362 {
363 }
364
365 void Adaptor::NotifyLanguageChanged()
366 {
367 }
368
369 void Adaptor::FeedTouchPoint(TouchPoint& point, int timeStamp)
370 {
371 }
372
373 void Adaptor::FeedWheelEvent(WheelEvent& wheelEvent)
374 {
375 }
376
377 void Adaptor::FeedKeyEvent(KeyEvent& keyEvent)
378 {
379 }
380
381 void Adaptor::SceneCreated()
382 {
383 }
384
385 class LogFactory : public LogFactoryInterface
386 {
387 public:
388   virtual void InstallLogFunction() const
389   {
390     Dali::Integration::Log::LogFunction logFunction(&ToolkitTestApplication::LogMessage);
391     Dali::Integration::Log::InstallLogFunction(logFunction);
392   }
393
394   LogFactory()
395   {
396   }
397   virtual ~LogFactory()
398   {
399   }
400 };
401
402 LogFactory*                gLogFactory = NULL;
403 const LogFactoryInterface& Adaptor::GetLogFactory()
404 {
405   if(gLogFactory == NULL)
406   {
407     gLogFactory = new LogFactory;
408   }
409   return *gLogFactory;
410 }
411
412 class TraceFactory : public TraceFactoryInterface
413 {
414 public:
415   virtual void InstallTraceFunction() const
416   {
417     Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
418     Dali::Integration::Trace::InstallLogContextFunction(logContextFunction);
419   }
420
421   TraceFactory()
422   {
423   }
424   virtual ~TraceFactory()
425   {
426   }
427 };
428
429 TraceFactory*                gTraceFactory = NULL;
430 const TraceFactoryInterface& Adaptor::GetTraceFactory()
431 {
432   if(gTraceFactory == NULL)
433   {
434     gTraceFactory = new TraceFactory;
435   }
436   return *gTraceFactory;
437 }
438
439 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
440 {
441   mImpl->RegisterProcessor(processor, postProcessor);
442 }
443
444 void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
445 {
446   mImpl->UnregisterProcessor(processor, postProcessor);
447 }
448
449 } // namespace Dali