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