Merge "Changed signal order for StyleManager" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-styling / utc-Dali-StyleManager.cpp
1 /*
2  * Copyright (c) 2014 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 #include <iostream>
18 #include <stdlib.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali/integration-api/events/touch-event-integ.h>
22 #include <dali-toolkit/devel-api/styling/style-manager.h>
23 #include <dali-toolkit/devel-api/builder/builder.h>
24 #include <test-button.h>
25 #include <test-animation-data.h>
26 #include <toolkit-style-monitor.h>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 namespace
32 {
33 const char* defaultTheme =
34 "{\n"
35 "  \"styles\":\n"
36 "  {\n"
37 "    \"textlabel\":\n"
38 "    {\n"
39 "      \"pointSize\":18\n"
40 "    },\n"
41 "\n"
42 "    \"textlabelFontSize0\":\n"
43 "    {\n"
44 "      \"pointSize\":8\n"
45 "    },\n"
46 "    \"textlabelFontSize1\":\n"
47 "    {\n"
48 "      \"pointSize\":10\n"
49 "    },\n"
50 "    \"textlabelFontSize2\":\n"
51 "    {\n"
52 "      \"pointSize\":15\n"
53 "    },\n"
54 "    \"textlabelFontSize3\":\n"
55 "    {\n"
56 "      \"pointSize\":19\n"
57 "    },\n"
58 "    \"textlabelFontSize4\":\n"
59 "    {\n"
60 "      \"pointSize\":25\n"
61 "    },\n"
62 "\n"
63 "    \"textfield\":\n"
64 "    {\n"
65 "      \"pointSize\":18,\n"
66 "      \"primaryCursorColor\":[0.0,0.72,0.9,1.0],\n"
67 "      \"secondaryCursorColor\":[0.0,0.72,0.9,1.0],\n"
68 "      \"cursorWidth\":3,\n"
69 "      \"selectionHighlightColor\":[0.75,0.96,1.0,1.0],\n"
70 "      \"grabHandleImage\" : \"{DALI_STYLE_IMAGE_DIR}cursor_handler_drop_center.png\",\n"
71 "      \"selectionHandleImageLeft\" : {\"filename\":\"{DALI_STYLE_IMAGE_DIR}selection_handle_drop_left.png\" },\n"
72 "      \"selectionHandleImageRight\": {\"filename\":\"{DALI_STYLE_IMAGE_DIR}selection_handle_drop_right.png\" }\n"
73 "    },\n"
74 "\n"
75 "    \"textfieldFontSize0\":\n"
76 "    {\n"
77 "      \"pointSize\":10\n"
78 "    },\n"
79 "    \"textfieldFontSize1\":\n"
80 "    {\n"
81 "      \"pointSize\":10\n"
82 "    },\n"
83 "    \"textfieldFontSize2\":\n"
84 "    {\n"
85 "      \"pointSize\":10\n"
86 "    },\n"
87 "    \"textfieldFontSize3\":\n"
88 "    {\n"
89 "      \"pointSize\":10\n"
90 "    },\n"
91 "    \"textfieldFontSize4\":\n"
92 "    {\n"
93 "      \"pointSize\":10\n"
94 "    },\n"
95 "    \"textselectionpopup\":\n"
96 "    {\n"
97 "      \"popupMaxSize\":[656,72],\n"
98 "      \"optionDividerSize\":[2,0],\n"
99 "      \"popupDividerColor\":[0.23,0.72,0.8,0.11],\n"
100 "      \"popupIconColor\":[1.0,1.0,1.0,1.0],\n"
101 "      \"popupPressedColor\":[0.24,0.72,0.8,0.11],\n"
102 "      \"background\": {\n"
103 "        \"rendererType\": \"nPatch\",\n"
104 "        \"imageUrl\": \"{DALI_IMAGE_DIR}selection-popup-bg.9.png\"\n"
105 "        },\n"
106 "      \"popupFadeInDuration\":0.25,\n"
107 "      \"popupFadeOutDuration\":0.25\n"
108 "    },\n"
109 "    \"textselectionpopupbutton\":\n"
110 "    {\n"
111 "      \"label\":\n"
112 "      {\n"
113 "        \"pointSize\":8,\n"
114 "        \"fontStyle\":\"{\\\\""weight\\\\"":\\\\""light\\\\""}\"\n"
115 "      }\n"
116 "    },\n"
117 "    \"textselectiontoolbar\":\n"
118 "    {\n"
119 "      \"enableOvershoot\":true,\n"
120 "      \"scrollView\":\n"
121 "      {\n"
122 "        \"overshootAnimationSpeed\":360.0,\n"
123 "        \"overshootSize\":[720.0,130.0]\n"
124 "      }\n"
125 "    },\n"
126 "    \"scrollview\":\n"
127 "    {\n"
128 "      \"overshootEffectColor\":\"B018\",\n"
129 "      \"overshootAnimationSpeed\":360.0,\n"
130 "      \"overshootSize\":[720.0,130.0]\n"
131 "    },\n"
132 "    \"itemview\":\n"
133 "    {\n"
134 "      \"overshootEffectColor\":\"B018\",\n"
135 "      \"overshootAnimationSpeed\":360.0,\n"
136 "      \"overshootSize\":[720.0,130.0]\n"
137 "    },\n"
138 "    \"texteditor\":\n"
139 "    {\n"
140 "      \"pointSize\":18,\n"
141 "      \"primaryCursorColor\":[0.0,0.72,0.9,1.0],\n"
142 "      \"secondaryCursorColor\":[0.0,0.72,0.9,1.0],\n"
143 "      \"cursorWidth\":3,\n"
144 "      \"selectionHighlightColor\":[0.75,0.96,1.0,1.0],\n"
145 "      \"grabHandleImage\" : \"{DALI_STYLE_IMAGE_DIR}cursor_handler_drop_center.png\",\n"
146 "      \"selectionHandleImageLeft\" : {\"filename\":\"{DALI_STYLE_IMAGE_DIR}selection_handle_drop_left.png\" },\n"
147 "      \"selectionHandleImageRight\": {\"filename\":\"{DALI_STYLE_IMAGE_DIR}selection_handle_drop_right.png\" }\n"
148 "    }\n"
149 "  }\n"
150 "}\n";
151
152 } // anonymous namespace
153
154
155
156 void dali_style_manager_startup(void)
157 {
158   test_return_value = TET_UNDEF;
159 }
160
161 void dali_style_manager_cleanup(void)
162 {
163   test_return_value = TET_PASS;
164 }
165
166 int UtcDaliStyleManagerGet(void)
167 {
168   ToolkitTestApplication application;
169
170   tet_infoline(" UtcDaliStyleManagerGet");
171
172   // Register Type
173   TypeInfo type;
174   type = TypeRegistry::Get().GetTypeInfo( "StyleManager" );
175   DALI_TEST_CHECK( type );
176   BaseHandle handle = type.CreateInstance();
177   DALI_TEST_CHECK( handle );
178
179   StyleManager manager;
180
181   manager = StyleManager::Get();
182   DALI_TEST_CHECK(manager);
183
184   StyleManager newManager = StyleManager::Get();
185   DALI_TEST_CHECK(newManager);
186
187   // Check that focus manager is a singleton
188   DALI_TEST_CHECK(manager == newManager);
189   END_TEST;
190 }
191
192 int UtcDaliStyleManagerSetOrientationValue(void)
193 {
194   ToolkitTestApplication application;
195
196   tet_infoline( " UtcDaliStyleManagerSetOrientationValue" );
197
198   StyleManager manager = StyleManager::Get();
199
200   int orientation1 = 0;
201   manager.SetOrientationValue( orientation1 );
202   DALI_TEST_CHECK( manager.GetOrientationValue() == orientation1 );
203
204   int orientation2 = 180;
205   manager.SetOrientationValue( orientation2 );
206   DALI_TEST_CHECK( manager.GetOrientationValue() == orientation2 );
207
208   END_TEST;
209 }
210
211 int UtcDaliStyleManagerSetOrientation(void)
212 {
213   ToolkitTestApplication application;
214
215   tet_infoline( " UtcDaliStyleManagerSetOrientation" );
216
217   StyleManager manager = StyleManager::Get();
218
219   Orientation orientation;
220
221   manager.SetOrientation( orientation );
222
223   DALI_TEST_CHECK( manager.GetOrientation() == orientation );
224
225   END_TEST;
226 }
227
228 int UtcDaliStyleManagerSetStyleConstant(void)
229 {
230   ToolkitTestApplication application;
231
232   tet_infoline( " UtcDaliStyleManagerSetStyleConstant" );
233
234   StyleManager manager = StyleManager::Get();
235
236   std::string key( "key" );
237   Property::Value value( 100 );
238
239   manager.SetStyleConstant( key, value );
240
241   Property::Value returnedValue;
242   manager.GetStyleConstant( key, returnedValue );
243
244   DALI_TEST_CHECK( value.Get<int>() == returnedValue.Get<int>() );
245
246   std::string key2( "key2" );
247   Property::Value returnedValue2;
248   DALI_TEST_CHECK( !manager.GetStyleConstant( key2, returnedValue2 ) );
249
250   END_TEST;
251 }
252
253 namespace
254 {
255 class StyleChangedSignalChecker : public ConnectionTracker
256 {
257 public:
258   StyleChangedSignalChecker()
259   : signalCount(0)
260   {
261   }
262
263   void OnStyleChanged(StyleManager styleManager, StyleChange::Type type)
264   {
265     signalCount++;
266   }
267
268   void Reset()
269   {
270     signalCount =0;
271   }
272
273 public:
274   int signalCount;
275 };
276
277 } // anonymous namespace
278
279 int UtcDaliStyleManagerPropertyOverride(void)
280 {
281   ToolkitTestApplication application;
282
283   tet_infoline( "Testing StyleManager property overrides" );
284
285   const char* json1 =
286     "{\n"
287     "  \"styles\":\n"
288     "  {\n"
289     "    \"testbutton\":\n"
290     "    {\n"
291     "      \"backgroundColor\":[1.0,1.0,0.0,1.0],\n"
292     "      \"foregroundColor\":[0.0,0.0,1.0,1.0]\n"
293     "    }\n"
294     "  }\n"
295     "}\n";
296
297   const char* json2 =
298     "{\n"
299     "  \"styles\":\n"
300     "  {\n"
301     "    \"testbutton\":\n"
302     "    {\n"
303     "      \"backgroundColor\":[1.0,0.0,0.0,1.0],\n"
304     "      \"foregroundColor\":[0.0,1.0,1.0,1.0]\n"
305     "    }\n"
306     "  }\n"
307     "}\n";
308
309   // Add 2 buttons to test how many times the signal is sent
310   Test::TestButton testButton = Test::TestButton::New();
311   Test::TestButton testButton2 = Test::TestButton::New();
312   Stage::GetCurrent().Add( testButton );
313   Stage::GetCurrent().Add( testButton2 );
314   StyleChangedSignalChecker styleChangedSignalHandler;
315   StyleChangedSignalChecker styleChangedSignalHandler2;
316   StyleManager styleManager = StyleManager::Get();
317
318   styleManager.StyleChangedSignal().Connect(&styleChangedSignalHandler, &StyleChangedSignalChecker::OnStyleChanged);
319
320   // Render and notify
321   application.SendNotification();
322   application.Render();
323
324   tet_infoline("Apply the style");
325
326   Test::StyleMonitor::SetThemeFileOutput(json1);
327   std::string themeFile("ThemeOne");
328   StyleManager::Get().RequestThemeChange(themeFile);
329
330   Property::Value bgColor( testButton.GetProperty(Test::TestButton::Property::BACKGROUND_COLOR) );
331   Property::Value fgColor( testButton.GetProperty(Test::TestButton::Property::FOREGROUND_COLOR) );
332
333   DALI_TEST_EQUALS( bgColor, Property::Value(Color::YELLOW), 0.001, TEST_LOCATION );
334   DALI_TEST_EQUALS( fgColor, Property::Value(Color::BLUE), 0.001, TEST_LOCATION );
335
336   tet_infoline("Testing that the signal handler is called only once");
337   DALI_TEST_EQUALS( styleChangedSignalHandler.signalCount, 1, TEST_LOCATION );
338
339   tet_infoline("Override the background property");
340   testButton.SetProperty( Test::TestButton::Property::BACKGROUND_COLOR, Color::GREEN );
341   bgColor = testButton.GetProperty(Test::TestButton::Property::BACKGROUND_COLOR);
342   fgColor = testButton.GetProperty(Test::TestButton::Property::FOREGROUND_COLOR);
343   DALI_TEST_EQUALS( bgColor, Property::Value(Color::GREEN), 0.001, TEST_LOCATION );
344   DALI_TEST_EQUALS( fgColor, Property::Value(Color::BLUE), 0.001, TEST_LOCATION );
345
346   // Render and notify
347   application.SendNotification();
348   application.Render();
349
350   tet_infoline("Apply the style again");
351
352   styleChangedSignalHandler.signalCount = 0;
353   StyleManager::Get().RequestThemeChange(themeFile);
354
355   bgColor = testButton.GetProperty(Test::TestButton::Property::BACKGROUND_COLOR);
356   fgColor = testButton.GetProperty(Test::TestButton::Property::FOREGROUND_COLOR);
357
358   tet_infoline("Check that the property is changed");
359   DALI_TEST_EQUALS( bgColor, Property::Value(Color::YELLOW), 0.001, TEST_LOCATION );
360   DALI_TEST_EQUALS( fgColor, Property::Value(Color::BLUE), 0.001, TEST_LOCATION );
361   tet_infoline("Testing that the signal handler is called only once");
362   DALI_TEST_EQUALS( styleChangedSignalHandler.signalCount, 1, TEST_LOCATION );
363
364   tet_infoline( "Load a different stylesheet");
365
366   tet_infoline("Apply the new style");
367   Test::StyleMonitor::SetThemeFileOutput(json2);
368
369   styleChangedSignalHandler.signalCount = 0;
370   std::string themeFile2("ThemeTwo");
371   StyleManager::Get().RequestThemeChange(themeFile2);
372
373   bgColor = testButton.GetProperty(Test::TestButton::Property::BACKGROUND_COLOR);
374   fgColor = testButton.GetProperty(Test::TestButton::Property::FOREGROUND_COLOR);
375
376   tet_infoline("Check that the properties change, but the signal gets sent only once");
377   DALI_TEST_EQUALS( bgColor, Property::Value(Color::RED), 0.001, TEST_LOCATION );
378   DALI_TEST_EQUALS( fgColor, Property::Value(Color::CYAN), 0.001, TEST_LOCATION );
379   DALI_TEST_EQUALS( styleChangedSignalHandler.signalCount, 1, TEST_LOCATION );
380
381   END_TEST;
382 }
383
384 int UtcDaliStyleManagerFontSizeChange(void)
385 {
386   tet_infoline("Test that the StyleChange signal is fired when the font size is altered" );
387   Test::StyleMonitor::SetThemeFileOutput(defaultTheme);
388
389   ToolkitTestApplication application;
390
391   std::string labelStr("Label");
392   Toolkit::TextLabel label = Toolkit::TextLabel::New(labelStr);
393   Stage::GetCurrent().Add( label );
394
395   Toolkit::TextLabel label2 = Toolkit::TextLabel::New(labelStr);
396   Stage::GetCurrent().Add( label2 );
397
398   StyleChangedSignalChecker styleChangedSignalHandler;
399   StyleMonitor styleMonitor = StyleMonitor::Get();
400   StyleManager styleManager = StyleManager::Get();
401
402   styleManager.StyleChangedSignal().Connect(&styleChangedSignalHandler, &StyleChangedSignalChecker::OnStyleChanged);
403
404   Test::StyleMonitor::SetDefaultFontFamily("Times New Roman");
405
406   styleMonitor.StyleChangeSignal().Emit( styleMonitor,  StyleChange::DEFAULT_FONT_CHANGE);
407
408   tet_infoline("Test that the StyleChanged signal is received only once");
409
410   DALI_TEST_EQUALS( styleChangedSignalHandler.signalCount, 1, TEST_LOCATION );
411
412   // Check that the label's font style has been altered
413   Property::Value family = label.GetProperty(TextLabel::Property::FONT_FAMILY);
414   std::string familyStr;
415   family.Get( familyStr );
416
417   DALI_TEST_EQUALS( familyStr, "Times New Roman", TEST_LOCATION);
418
419   END_TEST;
420 }