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