[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 void Adaptor::RequestUpdateOnce()
143 {
144   if(mTestApplication)
145   {
146     auto scene = mTestApplication->GetScene();
147     if(scene)
148     {
149       tet_printf("Adaptor::RequestUpdateOnce()\n");
150       scene.KeepRendering(0.0f);
151     }
152   }
153 }
154
155 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
156 {
157   DALI_ASSERT_ALWAYS(!mWindows.empty());
158
159   return reinterpret_cast<Dali::RenderSurfaceInterface&>(mWindows.front()->GetRenderSurface());
160 }
161
162 Dali::WindowContainer Adaptor::GetWindows()
163 {
164   Dali::WindowContainer windows;
165
166   for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
167   {
168     // Downcast to Dali::Window
169     Dali::Window window(dynamic_cast<Dali::Internal::Adaptor::Window*>(*iter));
170     if(window)
171     {
172       windows.push_back(window);
173     }
174   }
175
176   return windows;
177 }
178
179 Dali::SceneHolderList Adaptor::GetSceneHolders()
180 {
181   Dali::SceneHolderList sceneHolderList;
182
183   for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
184   {
185     sceneHolderList.push_back(Dali::Integration::SceneHolder(*iter));
186   }
187
188   return sceneHolderList;
189 }
190
191 Dali::Internal::Adaptor::SceneHolder* Adaptor::GetWindow(Dali::Actor& actor)
192 {
193   Dali::Integration::Scene scene = Dali::Integration::Scene::Get(actor);
194
195   for(auto window : mWindows)
196   {
197     if(scene == window->GetScene())
198     {
199       return window;
200     }
201   }
202
203   return nullptr;
204 }
205
206 void Adaptor::AddWindow(Internal::Adaptor::SceneHolder* window)
207 {
208   if(window)
209   {
210     mWindows.push_back(window);
211
212     Dali::Integration::SceneHolder newWindow(window);
213     mWindowCreatedSignal.Emit(newWindow);
214   }
215 }
216
217 void Adaptor::RemoveWindow(Internal::Adaptor::SceneHolder* window)
218 {
219   auto iter = std::find(mWindows.begin(), mWindows.end(), window);
220   if(iter != mWindows.end())
221   {
222     mWindows.erase(iter);
223   }
224 }
225
226 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
227 {
228   Integration::Core& core = mTestApplication->GetCore();
229   tet_printf("Adaptor::RegisterProcessor : %s\n", processor.GetProcessorName().data());
230   core.RegisterProcessor(processor, postProcessor);
231 }
232
233 void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
234 {
235   Integration::Core& core = mTestApplication->GetCore();
236   tet_printf("Adaptor::UnregisterProcessor : %s\n", processor.GetProcessorName().data());
237   core.UnregisterProcessor(processor, postProcessor);
238 }
239
240 void Adaptor::SetApplication(Dali::TestApplication& testApplication)
241 {
242   mTestApplication = &testApplication;
243 }
244
245 Dali::Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
246 {
247   return mResizedSignal;
248 }
249
250 Dali::Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
251 {
252   return mLanguageChangedSignal;
253 }
254
255 Dali::Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
256 {
257   return mWindowCreatedSignal;
258 }
259
260 } // namespace Adaptor
261 } // namespace Internal
262
263 ///////////////////////////////////////////////////////////////////////////////
264 //
265 // Dali::Adaptor Stub
266 //
267 ///////////////////////////////////////////////////////////////////////////////
268
269 Adaptor::Adaptor()
270 : mImpl(new Internal::Adaptor::Adaptor)
271 {
272 }
273
274 Adaptor::~Adaptor()
275 {
276   Internal::Adaptor::gAdaptor = nullptr;
277   delete mImpl;
278 }
279
280 void Adaptor::Start()
281 {
282 }
283
284 void Adaptor::Pause()
285 {
286 }
287
288 void Adaptor::Resume()
289 {
290 }
291
292 void Adaptor::Stop()
293 {
294   mImpl->Stop();
295 }
296
297 bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
298 {
299   return mImpl->AddIdle(callback, hasReturnValue);
300 }
301
302 void Adaptor::RemoveIdle(CallbackBase* callback)
303 {
304   mImpl->RemoveIdle(callback);
305 }
306
307 void Adaptor::ReplaceSurface(Window window, Dali::RenderSurfaceInterface& surface)
308 {
309 }
310
311 void Adaptor::ReplaceSurface(Dali::Integration::SceneHolder window, Dali::RenderSurfaceInterface& surface)
312 {
313 }
314
315 Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
316 {
317   return mImpl->ResizedSignal();
318 }
319
320 Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
321 {
322   return mImpl->LanguageChangedSignal();
323 }
324
325 Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
326 {
327   return mImpl->WindowCreatedSignal();
328 }
329
330 Dali::RenderSurfaceInterface& Adaptor::GetSurface()
331 {
332   return mImpl->GetSurface();
333 }
334
335 Dali::WindowContainer Adaptor::GetWindows() const
336 {
337   return mImpl->GetWindows();
338 }
339
340 Dali::SceneHolderList Adaptor::GetSceneHolders() const
341 {
342   return mImpl->GetSceneHolders();
343 }
344
345 Any Adaptor::GetNativeWindowHandle()
346 {
347   Any window;
348   return window;
349 }
350
351 Any Adaptor::GetNativeWindowHandle(Actor actor)
352 {
353   return GetNativeWindowHandle();
354 }
355
356 void Adaptor::ReleaseSurfaceLock()
357 {
358 }
359
360 void Adaptor::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender)
361 {
362 }
363
364 Adaptor& Adaptor::Get()
365 {
366   return Internal::Adaptor::Adaptor::Get();
367 }
368
369 bool Adaptor::IsAvailable()
370 {
371   return Internal::Adaptor::gAdaptor && (!Internal::Adaptor::Adaptor::GetImpl(*Internal::Adaptor::gAdaptor).IsStopped());
372 }
373
374 void Adaptor::NotifySceneCreated()
375 {
376 }
377
378 void Adaptor::NotifyLanguageChanged()
379 {
380 }
381
382 void Adaptor::FeedTouchPoint(TouchPoint& point, int timeStamp)
383 {
384 }
385
386 void Adaptor::FeedWheelEvent(WheelEvent& wheelEvent)
387 {
388 }
389
390 void Adaptor::FeedKeyEvent(KeyEvent& keyEvent)
391 {
392 }
393
394 void Adaptor::SceneCreated()
395 {
396 }
397
398 class LogFactory : public LogFactoryInterface
399 {
400 public:
401   virtual void InstallLogFunction() const
402   {
403     Dali::Integration::Log::LogFunction logFunction(&ToolkitTestApplication::LogMessage);
404     Dali::Integration::Log::InstallLogFunction(logFunction);
405   }
406
407   LogFactory()
408   {
409   }
410   virtual ~LogFactory()
411   {
412   }
413 };
414
415 LogFactory*                gLogFactory = NULL;
416 const LogFactoryInterface& Adaptor::GetLogFactory()
417 {
418   if(gLogFactory == NULL)
419   {
420     gLogFactory = new LogFactory;
421   }
422   return *gLogFactory;
423 }
424
425 class TraceFactory : public TraceFactoryInterface
426 {
427 public:
428   virtual void InstallTraceFunction() const
429   {
430     Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
431     Dali::Integration::Trace::InstallLogContextFunction(logContextFunction);
432   }
433
434   TraceFactory()
435   {
436   }
437   virtual ~TraceFactory()
438   {
439   }
440 };
441
442 TraceFactory*                gTraceFactory = NULL;
443 const TraceFactoryInterface& Adaptor::GetTraceFactory()
444 {
445   if(gTraceFactory == NULL)
446   {
447     gTraceFactory = new TraceFactory;
448   }
449   return *gTraceFactory;
450 }
451
452 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
453 {
454   mImpl->RegisterProcessor(processor, postProcessor);
455 }
456
457 void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
458 {
459   mImpl->UnregisterProcessor(processor, postProcessor);
460 }
461
462 } // namespace Dali