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