[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-style-monitor.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 // HEADER
19 #include "toolkit-style-monitor.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/signals/dali-signal.h>
26
27 namespace
28 {
29 const char* DEFAULT_THEME =
30   "{\n"
31   "  \"config\":\n"
32   "  {\n"
33   "    \"brokenImageUrl\":\"{DALI_IMAGE_DIR}broken.png\"\n"
34   "  },\n"
35   "  \"styles\":\n"
36   "  {\n"
37   "  \"textlabel\":\n"
38   "    {\n"
39   "      \"fontStyle\":{\"weight\":\"normal\"},\n"
40   "      \"pointSize\":18\n"
41   "    }\n"
42   "  }\n"
43   "}\n";
44
45 struct NamedTheme
46 {
47   NamedTheme(const std::string& name, const std::string& theme)
48   : name(name),
49     theme(theme)
50   {
51   }
52
53   std::string name;
54   std::string theme;
55 };
56 typedef std::vector<NamedTheme> NamedThemes;
57 NamedThemes                     gThemes;
58
59 std::string gTheme;
60 std::string gFontFamily("LucidaSans");
61 std::string gFontStyle("Regular");
62 int         gFontSize(1);
63
64 constexpr std::string_view THROW_EXCEPTION_STYLE_FILE_NAME = "throwException";
65 class DummyException : public std::exception
66 {
67 public:
68   DummyException()
69   {
70   }
71
72   const char* what() const throw()
73   {
74     return "DummyException";
75   }
76 };
77 } // namespace
78
79 namespace Dali
80 {
81 namespace Internal
82 {
83 namespace Adaptor
84 {
85 /**
86  * Stub for the StyleMonitor
87  */
88 class StyleMonitor : public BaseObject
89 {
90 public: // Creation & Destruction
91   static Dali::StyleMonitor Get();
92   StyleMonitor();
93   ~StyleMonitor();
94
95 public: // Style Information
96   std::string        GetDefaultFontFamily() const;
97   std::string        GetDefaultFontStyle() const;
98   float              GetDefaultFontSize() const;
99   const std::string& GetTheme() const;
100   void               SetTheme(std::string theme);
101   bool               LoadThemeFile(const std::string& filename, std::string& output);
102
103 public: // Signals
104   Dali::StyleMonitor::StyleChangeSignalType& StyleChangeSignal();
105
106   void EmitStyleChangeSignal(StyleChange::Type styleChange)
107   {
108     mStyleChangeSignal.Emit(Dali::StyleMonitor(this), styleChange);
109   }
110
111 private:
112   Dali::StyleMonitor::StyleChangeSignalType mStyleChangeSignal;
113   static Dali::StyleMonitor                 mToolkitStyleMonitor;
114
115   std::string mTheme; ///<< Current theme name
116 };
117
118 Dali::StyleMonitor StyleMonitor::mToolkitStyleMonitor;
119
120 Dali::StyleMonitor StyleMonitor::Get()
121 {
122   if(!mToolkitStyleMonitor)
123   {
124     mToolkitStyleMonitor = Dali::StyleMonitor(new Dali::Internal::Adaptor::StyleMonitor());
125   }
126   return mToolkitStyleMonitor;
127 }
128
129 StyleMonitor::StyleMonitor()
130 : mTheme("default")
131 {
132 }
133
134 StyleMonitor::~StyleMonitor()
135 {
136 }
137
138 std::string StyleMonitor::GetDefaultFontFamily() const
139 {
140   return gFontFamily;
141 }
142
143 std::string StyleMonitor::GetDefaultFontStyle() const
144 {
145   return gFontStyle;
146 }
147
148 float StyleMonitor::GetDefaultFontSize() const
149 {
150   return gFontSize;
151 }
152
153 const std::string& StyleMonitor::GetTheme() const
154 {
155   return mTheme;
156 }
157
158 void StyleMonitor::SetTheme(std::string path)
159 {
160   mTheme = path;
161   EmitStyleChangeSignal(StyleChange::THEME_CHANGE);
162 }
163
164 bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
165 {
166   // Throw something exceptions during load file
167   if(filename == THROW_EXCEPTION_STYLE_FILE_NAME)
168   {
169     throw DummyException();
170   }
171   for(NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter)
172   {
173     NamedTheme& theme = *iter;
174     if(theme.name == filename)
175     {
176       output = theme.theme;
177       return true;
178     }
179   }
180
181   if(!gTheme.empty())
182   {
183     output = gTheme;
184   }
185   else
186   {
187     output = DEFAULT_THEME;
188   }
189   return true;
190 }
191
192 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
193 {
194   return mStyleChangeSignal;
195 }
196
197 } // namespace Adaptor
198 } // namespace Internal
199
200 ////////////////////////////////////////////////////////////////////////////////////////////////////
201
202 Internal::Adaptor::StyleMonitor& GetImplementation(Dali::StyleMonitor& monitor)
203 {
204   BaseObject& object = monitor.GetBaseObject();
205   return static_cast<Internal::Adaptor::StyleMonitor&>(object);
206 }
207 const Internal::Adaptor::StyleMonitor& GetImplementation(const Dali::StyleMonitor& monitor)
208 {
209   const BaseObject& object = monitor.GetBaseObject();
210   return static_cast<const Internal::Adaptor::StyleMonitor&>(object);
211 }
212
213 StyleMonitor::StyleMonitor()
214 {
215 }
216
217 StyleMonitor::StyleMonitor(const StyleMonitor& monitor)
218 : BaseHandle(monitor)
219 {
220 }
221
222 StyleMonitor StyleMonitor::StyleMonitor::Get()
223 {
224   return Internal::Adaptor::StyleMonitor::Get();
225 }
226
227 StyleMonitor::~StyleMonitor()
228 {
229 }
230
231 std::string StyleMonitor::GetDefaultFontFamily() const
232 {
233   return GetImplementation(*this).GetDefaultFontFamily();
234 }
235
236 std::string StyleMonitor::GetDefaultFontStyle() const
237 {
238   return GetImplementation(*this).GetDefaultFontStyle();
239 }
240
241 int StyleMonitor::GetDefaultFontSize() const
242 {
243   return GetImplementation(*this).GetDefaultFontSize();
244 }
245
246 const std::string& StyleMonitor::GetTheme() const
247 {
248   return GetImplementation(*this).GetTheme();
249 }
250
251 void StyleMonitor::SetTheme(const std::string& themeFilePath)
252 {
253   GetImplementation(*this).SetTheme(themeFilePath);
254 }
255
256 StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
257 {
258   return GetImplementation(*this).StyleChangeSignal();
259 }
260
261 bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
262 {
263   return GetImplementation(*this).LoadThemeFile(filename, output);
264 }
265
266 StyleMonitor& StyleMonitor::operator=(const StyleMonitor& monitor)
267 {
268   if(*this != monitor)
269   {
270     BaseHandle::operator=(monitor);
271   }
272   return *this;
273 }
274
275 StyleMonitor::StyleMonitor(Internal::Adaptor::StyleMonitor* internal)
276 : BaseHandle(internal)
277 {
278 }
279
280 } // namespace Dali
281
282 namespace Test
283 {
284 namespace StyleMonitor
285 {
286 void SetThemeFileOutput(const std::string& name, const std::string& output)
287 {
288   for(NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter)
289   {
290     NamedTheme& theme = *iter;
291     if(theme.name == name)
292     {
293       theme.theme = output;
294       return;
295     }
296   }
297
298   gThemes.push_back(NamedTheme(name, output));
299 }
300
301 void SetDefaultFontFamily(const std::string& family)
302 {
303   gFontFamily = family;
304 }
305
306 void SetDefaultFontStyle(const std::string& style)
307 {
308   gFontStyle = style;
309 }
310
311 void SetDefaultFontSize(float size)
312 {
313   gFontSize = size;
314 }
315
316 } // namespace StyleMonitor
317 } // namespace Test