[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-adaptor.cpp
index c27157d..40c73ae 100644 (file)
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <algorithm>
+
+#include <toolkit-window-impl.h>
+
+// Don't want to include the actual window.h which otherwise will be indirectly included by adaptor.h.
+#define DALI_WINDOW_H
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali/integration-api/adaptor-framework/scene-holder.h>
+
+#include <dali/integration-api/debug.h>
+#include <dali/integration-api/scene.h>
+#include <test-application.h>
+#include <toolkit-adaptor-impl.h>
+#include <toolkit-application.h>
+#include <toolkit-async-task-manager.h>
+#include <toolkit-scene-holder-impl.h>
+#include <toolkit-test-application.h>
+#include "dali-test-suite-utils.h"
+
+namespace Dali
+{
+namespace Internal
+{
+namespace Adaptor
+{
+///////////////////////////////////////////////////////////////////////////////
 //
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+// Dali::Internal::Adaptor::Adaptor Stub
 //
+///////////////////////////////////////////////////////////////////////////////
 
-#define __DALI_ADAPTOR_H__
-#define __DALI_ACCESSIBILITY_MANAGER_H__
-#define __DALI_TIMER_H__
-#define __DALI_CLIPBOARD_H__
-#define IMFMANAGER_H
+Dali::Adaptor* gAdaptor = nullptr;
 
-#include "toolkit-adaptor.h"
-#include <map>
-#include <dali/public-api/common/dali-common.h>
-#include <dali/public-api/signals/dali-signal-v2.h>
+Dali::Adaptor& Adaptor::New()
+{
+  DALI_ASSERT_ALWAYS(!gAdaptor);
+  gAdaptor = new Dali::Adaptor;
+  return *gAdaptor;
+}
 
-namespace Dali
+Dali::Adaptor& Adaptor::Get()
 {
+  DALI_ASSERT_ALWAYS(gAdaptor);
+  return *gAdaptor;
+}
 
-////////////////////////////////////////////////////////////////////////////////////////////////////
+Adaptor::Adaptor()
+{
+}
 
-class TestRenderSurface : public RenderSurface
+Adaptor::~Adaptor()
 {
-public:
-  TestRenderSurface(){}
-  virtual ~TestRenderSurface(){}
-  virtual SurfaceType GetType() { return RenderSurface::WINDOW; }
-  virtual Dali::Any GetSurface() { return Dali::Any(); }
-  virtual Dali::Any GetDisplay() { return Dali::Any(); }
-  virtual PositionSize GetPositionSize() const { return PositionSize(0, 0, 640, 480);}
-  virtual void SetRenderMode(RenderMode mode){}
-  virtual RenderMode GetRenderMode() const { return RenderSurface::RENDER_60FPS; }
-};
+  gAdaptor = nullptr;
 
-typedef Dali::Rect<int> PositionSize;
+  // Ensure all threads and not-excuted tasks are destroyed.
+  // TODO : we'd better make some singletone service for toolkit UTC in future.
+  Test::AsyncTaskManager::DestroyAsyncTaskManager();
+}
 
-/**
- * Stub for the Adaptor
- */
-class Adaptor
+void Adaptor::Start(Dali::Window window)
 {
-public:
+  AddWindow(&GetImplementation(window));
+}
+
+void Adaptor::Stop()
+{
+  if(mTestApplication)
+  {
+    Integration::Core& core = mTestApplication->GetCore();
+    tet_printf("Adaptor::UnregisterProcessors\n");
+    core.UnregisterProcessors();
+  }
 
-  typedef SignalV2< void ( Adaptor& ) > AdaptorSignalV2;
+  mStopped = true;
+}
 
-  typedef std::pair<std::string, Dali::BaseHandle> SingletonPair;
-  typedef std::map<std::string, Dali::BaseHandle>  SingletonContainer;
-  typedef SingletonContainer::const_iterator       SingletonConstIter;
+Integration::Scene Adaptor::GetScene(Dali::Window window)
+{
+  return window.GetScene();
+}
 
-public:
+bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
+{
+  if(ToolkitApplication::ADD_IDLE_SUCCESS)
+  {
+    if(hasReturnValue)
+    {
+      mReturnCallbacks.PushBack(callback);
+    }
+    else
+    {
+      mCallbacks.PushBack(callback);
+    }
+  }
+  return ToolkitApplication::ADD_IDLE_SUCCESS;
+}
 
-  Adaptor(ToolkitAdaptor& toolkitAdaptor);
-  ~Adaptor();
+void Adaptor::RemoveIdle(CallbackBase* callback)
+{
+  mCallbacks.Erase(std::remove_if(mCallbacks.Begin(), mCallbacks.End(), [&callback](CallbackBase* current) { return callback == current; }), mCallbacks.End());
+  mReturnCallbacks.Erase(std::remove_if(mReturnCallbacks.Begin(), mReturnCallbacks.End(), [&callback](CallbackBase* current) { return callback == current; }), mReturnCallbacks.End());
+}
 
-public:
+void Adaptor::RunIdles()
+{
+  Dali::Vector<CallbackBase*> reusedCallbacks;
+  for(auto& callback : mReturnCallbacks)
+  {
+    bool retValue = CallbackBase::ExecuteReturn<bool>(*callback);
+    if(retValue)
+    {
+      reusedCallbacks.PushBack(callback);
+    }
+  }
+  for(auto& callback : mCallbacks)
+  {
+    CallbackBase::Execute(*callback);
+  }
 
-  void Start();
-  void Pause();
-  void Resume();
-  void Stop();
-  bool AddIdle(boost::function<void(void)> callBack);
-  void FeedEvent(TouchPoint& point, int timeStamp);
-  bool MoveResize(const PositionSize& positionSize);
-  void SurfaceResized(const PositionSize& positionSize);
-  void ReplaceSurface(RenderSurface& surface);
-  void RenderSync();
-  RenderSurface& GetSurface();
+  mCallbacks.Clear();
+  mReturnCallbacks.Clear();
+  mReturnCallbacks.Swap(reusedCallbacks);
+}
+
+void Adaptor::RequestUpdateOnce()
+{
+  if(mTestApplication)
+  {
+    auto scene = mTestApplication->GetScene();
+    if(scene)
+    {
+      tet_printf("Adaptor::RequestUpdateOnce()\n");
+      scene.KeepRendering(0.0f);
+    }
+  }
+}
+
+Dali::RenderSurfaceInterface& Adaptor::GetSurface()
+{
+  DALI_ASSERT_ALWAYS(!mWindows.empty());
+
+  return reinterpret_cast<Dali::RenderSurfaceInterface&>(mWindows.front()->GetRenderSurface());
+}
 
-  void RegisterSingleton(const std::type_info& info, Dali::BaseHandle singleton);
-  Dali::BaseHandle GetSingleton(const std::type_info& info) const;
+Dali::WindowContainer Adaptor::GetWindows()
+{
+  Dali::WindowContainer windows;
 
-public: // static methods
-  static Adaptor& Get();
-  static bool IsAvailable();
+  for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
+  {
+    // Downcast to Dali::Window
+    Dali::Window window(dynamic_cast<Dali::Internal::Adaptor::Window*>(*iter));
+    if(window)
+    {
+      windows.push_back(window);
+    }
+  }
 
-public:  // Signals
+  return windows;
+}
 
-  AdaptorSignalV2& SignalResize();
+Dali::SceneHolderList Adaptor::GetSceneHolders()
+{
+  Dali::SceneHolderList sceneHolderList;
 
-  void EmitSignalResize()
+  for(auto iter = mWindows.begin(); iter != mWindows.end(); ++iter)
   {
-    mResizeSignal.Emit( *this );
+    sceneHolderList.push_back(Dali::Integration::SceneHolder(*iter));
   }
 
-private:
+  return sceneHolderList;
+}
 
-  // Undefined
-  Adaptor(const Adaptor&);
-  Adaptor& operator=(Adaptor&);
+Dali::Internal::Adaptor::SceneHolder* Adaptor::GetWindow(Dali::Actor& actor)
+{
+  Dali::Integration::Scene scene = Dali::Integration::Scene::Get(actor);
 
-  AdaptorSignalV2 mResizeSignal;
-  TestRenderSurface mRenderSurface;
-  ToolkitAdaptor& mToolkitAdaptor;
+  for(auto window : mWindows)
+  {
+    if(scene == window->GetScene())
+    {
+      return window;
+    }
+  }
 
-  SingletonContainer mSingletonContainer;
-};
+  return nullptr;
+}
 
-namespace
+void Adaptor::AddWindow(Internal::Adaptor::SceneHolder* window)
 {
-Adaptor* gAdaptor = NULL;
+  if(window)
+  {
+    mWindows.push_back(window);
 
+    Dali::Integration::SceneHolder newWindow(window);
+    mWindowCreatedSignal.Emit(newWindow);
+  }
 }
 
-Adaptor::Adaptor(ToolkitAdaptor& toolkitAdaptor)
-: mToolkitAdaptor(toolkitAdaptor)
+void Adaptor::RemoveWindow(Internal::Adaptor::SceneHolder* window)
 {
+  auto iter = std::find(mWindows.begin(), mWindows.end(), window);
+  if(iter != mWindows.end())
+  {
+    mWindows.erase(iter);
+  }
 }
 
-Adaptor::~Adaptor()
+void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
 {
+  Integration::Core& core = mTestApplication->GetCore();
+  tet_printf("Adaptor::RegisterProcessor : %s\n", processor.GetProcessorName().data());
+  core.RegisterProcessor(processor, postProcessor);
+}
 
+void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
+{
+  Integration::Core& core = mTestApplication->GetCore();
+  tet_printf("Adaptor::UnregisterProcessor : %s\n", processor.GetProcessorName().data());
+  core.UnregisterProcessor(processor, postProcessor);
+}
+
+void Adaptor::SetApplication(Dali::TestApplication& testApplication)
+{
+  mTestApplication = &testApplication;
+}
+
+Dali::Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
+{
+  return mResizedSignal;
+}
+
+Dali::Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
+{
+  return mLanguageChangedSignal;
+}
+
+Dali::Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
+{
+  return mWindowCreatedSignal;
+}
+
+} // namespace Adaptor
+} // namespace Internal
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// Dali::Adaptor Stub
+//
+///////////////////////////////////////////////////////////////////////////////
+
+Adaptor::Adaptor()
+: mImpl(new Internal::Adaptor::Adaptor)
+{
+}
+
+Adaptor::~Adaptor()
+{
+  Internal::Adaptor::gAdaptor = nullptr;
+  delete mImpl;
 }
 
 void Adaptor::Start()
 {
-  mToolkitAdaptor.mFunctionsCalled.Start = true;
 }
 
 void Adaptor::Pause()
 {
-  mToolkitAdaptor.mFunctionsCalled.Pause = true;
 }
 
 void Adaptor::Resume()
 {
-  mToolkitAdaptor.mFunctionsCalled.Resume = true;
 }
 
 void Adaptor::Stop()
 {
-  mToolkitAdaptor.mFunctionsCalled.Stop = true;
+  mImpl->Stop();
+}
+
+bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue)
+{
+  return mImpl->AddIdle(callback, hasReturnValue);
+}
+
+void Adaptor::RemoveIdle(CallbackBase* callback)
+{
+  mImpl->RemoveIdle(callback);
+}
+
+void Adaptor::ReplaceSurface(Window window, Dali::RenderSurfaceInterface& surface)
+{
 }
 
-bool Adaptor::AddIdle(boost::function<void(void)> callBack)
+void Adaptor::ReplaceSurface(Dali::Integration::SceneHolder window, Dali::RenderSurfaceInterface& surface)
 {
-  mToolkitAdaptor.mFunctionsCalled.AddIdle = true;
-  mToolkitAdaptor.mLastIdleAdded = callBack;
-  return true;
 }
 
-void Adaptor::FeedEvent(TouchPoint& point, int timeStamp)
+Adaptor::AdaptorSignalType& Adaptor::ResizedSignal()
 {
-  mToolkitAdaptor.mFunctionsCalled.FeedEvent = true;
-  mToolkitAdaptor.mLastTouchPointFed = point;
-  mToolkitAdaptor.mLastTimeStampFed = timeStamp;
+  return mImpl->ResizedSignal();
 }
 
-bool Adaptor::MoveResize(const PositionSize& positionSize)
+Adaptor::AdaptorSignalType& Adaptor::LanguageChangedSignal()
 {
-  mToolkitAdaptor.mFunctionsCalled.MoveResize = true;
-  mToolkitAdaptor.mLastSizeSet = positionSize;
-  return true;
+  return mImpl->LanguageChangedSignal();
 }
 
-void Adaptor::SurfaceResized(const PositionSize& positionSize)
+Adaptor::WindowCreatedSignalType& Adaptor::WindowCreatedSignal()
 {
-  mToolkitAdaptor.mFunctionsCalled.SurfaceResized = true;
-  mToolkitAdaptor.mLastSizeSet = positionSize;
+  return mImpl->WindowCreatedSignal();
 }
 
-void Adaptor::ReplaceSurface(RenderSurface& surface)
+Dali::RenderSurfaceInterface& Adaptor::GetSurface()
 {
-  mToolkitAdaptor.mFunctionsCalled.ReplaceSurface = true;
+  return mImpl->GetSurface();
 }
 
-void Adaptor::RenderSync()
+Dali::WindowContainer Adaptor::GetWindows() const
 {
-  mToolkitAdaptor.mFunctionsCalled.RenderSync = true;
+  return mImpl->GetWindows();
 }
 
-RenderSurface& Adaptor::GetSurface()
+Dali::SceneHolderList Adaptor::GetSceneHolders() const
+{
+  return mImpl->GetSceneHolders();
+}
+
+Any Adaptor::GetNativeWindowHandle()
+{
+  Any window;
+  return window;
+}
+
+Any Adaptor::GetNativeWindowHandle(Actor actor)
+{
+  return GetNativeWindowHandle();
+}
+
+void Adaptor::ReleaseSurfaceLock()
+{
+}
+
+void Adaptor::SetRenderRefreshRate(unsigned int numberOfVSyncsPerRender)
 {
-  mToolkitAdaptor.mFunctionsCalled.GetSurface = true;
-  return mRenderSurface;
 }
 
 Adaptor& Adaptor::Get()
 {
-  DALI_ASSERT_ALWAYS(gAdaptor);
-  gAdaptor->mToolkitAdaptor.mFunctionsCalled.Get = true;
-  return *gAdaptor;
+  return Internal::Adaptor::Adaptor::Get();
 }
 
 bool Adaptor::IsAvailable()
 {
-  bool available(false);
+  return Internal::Adaptor::gAdaptor && (!Internal::Adaptor::Adaptor::GetImpl(*Internal::Adaptor::gAdaptor).IsStopped());
+}
 
-  if (gAdaptor)
-  {
-    gAdaptor->mToolkitAdaptor.mFunctionsCalled.IsAvailable = true;
-    available = true;
-  }
+void Adaptor::NotifySceneCreated()
+{
+}
 
-  return available;
+void Adaptor::NotifyLanguageChanged()
+{
 }
 
-void Adaptor::RegisterSingleton(const std::type_info& info, Dali::BaseHandle singleton)
+void Adaptor::FeedTouchPoint(TouchPoint& point, int timeStamp)
 {
-  mToolkitAdaptor.mFunctionsCalled.RegisterSingleton = true;
+}
 
-  if(singleton)
-  {
-    mSingletonContainer.insert(SingletonPair(info.name(), singleton));
-  }
+void Adaptor::FeedWheelEvent(WheelEvent& wheelEvent)
+{
 }
 
-Dali::BaseHandle Adaptor::GetSingleton(const std::type_info& info) const
+void Adaptor::FeedKeyEvent(KeyEvent& keyEvent)
 {
-  mToolkitAdaptor.mFunctionsCalled.GetSingleton = true;
+}
 
-  Dali::BaseHandle object = Dali::BaseHandle();
+void Adaptor::SceneCreated()
+{
+}
 
-  SingletonConstIter iter = mSingletonContainer.find(info.name());
-  if(iter != mSingletonContainer.end())
+class LogFactory : public LogFactoryInterface
+{
+public:
+  virtual void InstallLogFunction() const
   {
-    object = (*iter).second;
+    Dali::Integration::Log::LogFunction logFunction(&ToolkitTestApplication::LogMessage);
+    Dali::Integration::Log::InstallLogFunction(logFunction);
   }
 
-  return object;
-}
+  LogFactory()
+  {
+  }
+  virtual ~LogFactory()
+  {
+  }
+};
 
-Adaptor::AdaptorSignalV2& Adaptor::SignalResize()
+LogFactory*                gLogFactory = NULL;
+const LogFactoryInterface& Adaptor::GetLogFactory()
 {
-  mToolkitAdaptor.mFunctionsCalled.SignalResize = true;
-  return mResizeSignal;
+  if(gLogFactory == NULL)
+  {
+    gLogFactory = new LogFactory;
+  }
+  return *gLogFactory;
 }
 
-////////////////////////////////////////////////////////////////////////////////////////////////////
+class TraceFactory : public TraceFactoryInterface
+{
+public:
+  virtual void InstallTraceFunction() const
+  {
+    Dali::Integration::Trace::LogContextFunction logContextFunction(&TestApplication::LogContext);
+    Dali::Integration::Trace::InstallLogContextFunction(logContextFunction);
+  }
 
-ToolkitAdaptor::ToolkitAdaptor()
-: mLastTouchPointFed(0, TouchPoint::Down, 0.0f, 0.0f),
-  mLastTimeStampFed(0),
-  mStyleMonitor(StyleMonitor::Get()),
-  mAccessibilityManager(AccessibilityManager::Get()),
-  mImfManager(ImfManager::Get()),
-  mAdaptorStub(new Adaptor(*this))
+  TraceFactory()
+  {
+  }
+  virtual ~TraceFactory()
+  {
+  }
+};
+
+TraceFactory*                gTraceFactory = NULL;
+const TraceFactoryInterface& Adaptor::GetTraceFactory()
 {
-  gAdaptor = mAdaptorStub;
+  if(gTraceFactory == NULL)
+  {
+    gTraceFactory = new TraceFactory;
+  }
+  return *gTraceFactory;
 }
 
-ToolkitAdaptor::~ToolkitAdaptor()
+void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
 {
-  delete mAdaptorStub;
-  gAdaptor = NULL;
+  mImpl->RegisterProcessor(processor, postProcessor);
 }
 
-void ToolkitAdaptor::EmitSignalResize()
+void Adaptor::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
 {
-  mAdaptorStub->EmitSignalResize();
+  mImpl->UnregisterProcessor(processor, postProcessor);
 }
 
 } // namespace Dali