[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-style-monitor.cpp
index dc2d39f..e0000ec 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
  *
  */
 
+// HEADER
 #include "toolkit-style-monitor.h"
 
-#include <dali/public-api/common/dali-common.h>
+// EXTERNAL INCLUDES
+#include <dali-toolkit/public-api/dali-toolkit-common.h>
+#include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/object/base-object.h>
 #include <dali/public-api/signals/dali-signal.h>
 
-namespace Dali
+namespace
+{
+const char* DEFAULT_THEME =
+  "{\n"
+  "  \"config\":\n"
+  "  {\n"
+  "    \"brokenImageUrl\":\"{DALI_IMAGE_DIR}broken.png\"\n"
+  "  },\n"
+  "  \"styles\":\n"
+  "  {\n"
+  "  \"textlabel\":\n"
+  "    {\n"
+  "      \"fontStyle\":{\"weight\":\"normal\"},\n"
+  "      \"pointSize\":18\n"
+  "    }\n"
+  "  }\n"
+  "}\n";
+
+struct NamedTheme
+{
+  NamedTheme(const std::string& name, const std::string& theme)
+  : name(name),
+    theme(theme)
+  {
+  }
+
+  std::string name;
+  std::string theme;
+};
+typedef std::vector<NamedTheme> NamedThemes;
+NamedThemes                     gThemes;
+
+std::string gTheme;
+std::string gFontFamily("LucidaSans");
+std::string gFontStyle("Regular");
+int         gFontSize(1);
+
+constexpr std::string_view THROW_EXCEPTION_STYLE_FILE_NAME = "throwException";
+class DummyException : public std::exception
 {
+public:
+  DummyException()
+  {
+  }
 
+  const char* what() const throw()
+  {
+    return "DummyException";
+  }
+};
+} // namespace
 
+namespace Dali
+{
 namespace Internal
 {
 namespace Adaptor
@@ -40,32 +93,35 @@ public: // Creation & Destruction
   ~StyleMonitor();
 
 public: // Style Information
-  std::string GetDefaultFontFamily() const;
-  float GetDefaultFontSize() const;
+  std::string        GetDefaultFontFamily() const;
+  std::string        GetDefaultFontStyle() const;
+  float              GetDefaultFontSize() const;
   const std::string& GetTheme() const;
-  void SetTheme(std::string theme);
+  void               SetTheme(std::string theme);
+  bool               LoadThemeFile(const std::string& filename, std::string& output);
 
 public: // Signals
   Dali::StyleMonitor::StyleChangeSignalType& StyleChangeSignal();
 
-  void EmitStyleChangeSignal(StyleChange styleChange)
+  void EmitStyleChangeSignal(StyleChange::Type styleChange)
   {
     mStyleChangeSignal.Emit(Dali::StyleMonitor(this), styleChange);
   }
 
 private:
   Dali::StyleMonitor::StyleChangeSignalType mStyleChangeSignal;
-  static Dali::StyleMonitor mToolkitStyleMonitor;
-  std::string mTheme;
+  static Dali::StyleMonitor                 mToolkitStyleMonitor;
+
+  std::string mTheme; ///<< Current theme name
 };
 
 Dali::StyleMonitor StyleMonitor::mToolkitStyleMonitor;
 
 Dali::StyleMonitor StyleMonitor::Get()
 {
-  if( ! mToolkitStyleMonitor )
+  if(!mToolkitStyleMonitor)
   {
-    mToolkitStyleMonitor = Dali::StyleMonitor( new Dali::Internal::Adaptor::StyleMonitor() );
+    mToolkitStyleMonitor = Dali::StyleMonitor(new Dali::Internal::Adaptor::StyleMonitor());
   }
   return mToolkitStyleMonitor;
 }
@@ -81,12 +137,17 @@ StyleMonitor::~StyleMonitor()
 
 std::string StyleMonitor::GetDefaultFontFamily() const
 {
-  return Dali::StyleMonitor::DEFAULT_FONT_FAMILY;
+  return gFontFamily;
+}
+
+std::string StyleMonitor::GetDefaultFontStyle() const
+{
+  return gFontStyle;
 }
 
 float StyleMonitor::GetDefaultFontSize() const
 {
-  return Dali::StyleMonitor::DEFAULT_FONT_SIZE;
+  return gFontSize;
 }
 
 const std::string& StyleMonitor::GetTheme() const
@@ -96,11 +157,36 @@ const std::string& StyleMonitor::GetTheme() const
 
 void StyleMonitor::SetTheme(std::string path)
 {
-  StyleChange styleChange;
-  styleChange.themeChange = true;
-  styleChange.themeFilePath = path;
   mTheme = path;
-  EmitStyleChangeSignal(styleChange);
+  EmitStyleChangeSignal(StyleChange::THEME_CHANGE);
+}
+
+bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
+{
+  // Throw something exceptions during load file
+  if(filename == THROW_EXCEPTION_STYLE_FILE_NAME)
+  {
+    throw DummyException();
+  }
+  for(NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter)
+  {
+    NamedTheme& theme = *iter;
+    if(theme.name == filename)
+    {
+      output = theme.theme;
+      return true;
+    }
+  }
+
+  if(!gTheme.empty())
+  {
+    output = gTheme;
+  }
+  else
+  {
+    output = DEFAULT_THEME;
+  }
+  return true;
 }
 
 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
@@ -124,9 +210,6 @@ const Internal::Adaptor::StyleMonitor& GetImplementation(const Dali::StyleMonito
   return static_cast<const Internal::Adaptor::StyleMonitor&>(object);
 }
 
-const std::string Dali::StyleMonitor::DEFAULT_FONT_FAMILY("DefaultFont");
-const float       Dali::StyleMonitor::DEFAULT_FONT_SIZE(1.0f);
-
 StyleMonitor::StyleMonitor()
 {
 }
@@ -150,7 +233,12 @@ std::string StyleMonitor::GetDefaultFontFamily() const
   return GetImplementation(*this).GetDefaultFontFamily();
 }
 
-float StyleMonitor::GetDefaultFontSize() const
+std::string StyleMonitor::GetDefaultFontStyle() const
+{
+  return GetImplementation(*this).GetDefaultFontStyle();
+}
+
+int StyleMonitor::GetDefaultFontSize() const
 {
   return GetImplementation(*this).GetDefaultFontSize();
 }
@@ -160,7 +248,7 @@ const std::string& StyleMonitor::GetTheme() const
   return GetImplementation(*this).GetTheme();
 }
 
-void StyleMonitor::SetTheme(std::string themeFilePath)
+void StyleMonitor::SetTheme(const std::string& themeFilePath)
 {
   GetImplementation(*this).SetTheme(themeFilePath);
 }
@@ -170,14 +258,14 @@ StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
   return GetImplementation(*this).StyleChangeSignal();
 }
 
-void StyleMonitor::EmitStyleChangeSignal(StyleChange styleChange)
+bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
 {
-  GetImplementation(*this).EmitStyleChangeSignal(styleChange);
+  return GetImplementation(*this).LoadThemeFile(filename, output);
 }
 
 StyleMonitor& StyleMonitor::operator=(const StyleMonitor& monitor)
 {
-  if( *this != monitor )
+  if(*this != monitor)
   {
     BaseHandle::operator=(monitor);
   }
@@ -190,3 +278,40 @@ StyleMonitor::StyleMonitor(Internal::Adaptor::StyleMonitor* internal)
 }
 
 } // namespace Dali
+
+namespace Test
+{
+namespace StyleMonitor
+{
+void SetThemeFileOutput(const std::string& name, const std::string& output)
+{
+  for(NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter)
+  {
+    NamedTheme& theme = *iter;
+    if(theme.name == name)
+    {
+      theme.theme = output;
+      return;
+    }
+  }
+
+  gThemes.push_back(NamedTheme(name, output));
+}
+
+void SetDefaultFontFamily(const std::string& family)
+{
+  gFontFamily = family;
+}
+
+void SetDefaultFontStyle(const std::string& style)
+{
+  gFontStyle = style;
+}
+
+void SetDefaultFontSize(float size)
+{
+  gFontSize = size;
+}
+
+} // namespace StyleMonitor
+} // namespace Test