Builder templated constant expansion
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / builder / utc-Dali-Builder.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23 #include <dali-toolkit/public-api/builder/builder.h>
24 #include <dali-toolkit-test-suite-utils.h>
25
26 using namespace Dali;
27 using namespace Toolkit;
28
29 namespace
30 {
31 //
32 // Note: To avoid escaping double quotes single quotes are used and then replaced
33 //       before parsing. JSON uses double quotes
34 //
35   std::string JSON_TEXTSTYLE_ONLY("\
36 {                                                                  \
37     'text-styles':                                                 \
38     {                                                              \
39         'title-text-style':{'font-name': 'Vera',                   \
40                             'font-style': 'Bold',                  \
41                             'point-size': 12.0,                    \
42                             'weight': 'light',                     \
43                             'text-color': [0.0,0.5,0.5,1],         \
44                             'italic': false,                       \
45                             'underline': false,                    \
46                             'shadow': true,                        \
47                             'glow': true,                          \
48                             'outline': true,                       \
49                             'shadow-color': [0.0,1.0,0.0,1.0],     \
50                             'shadow-offset': [3.0,2.0],            \
51                             'shadow-size': 2.0,                    \
52                             'glow-color': [0.9,0.6,0.3,1.0],       \
53                             'glow-intensity':0.1,                  \
54                             'smooth-edge': 0.45,                   \
55                             'outline-color': [1.0,0.5,0.0,1.0],    \
56                             'outline-thickness': [0.7,0.6]         \
57       }                                                            \
58     }                                                              \
59 }                                                                  \
60 ");
61
62   std::string JSON_TEXT_ACTOR("\
63 {                                                              \
64   'styles':                                                    \
65   {                                                            \
66     'basic-text':                                              \
67     {                                                          \
68       'type':'TextActor',                                      \
69       'text':'Hello',                                          \
70       'font':'',                                               \
71       'parent-origin':[0.0,0.0,0],                             \
72       'anchor-point' :[0.5,0.5,0],                             \
73       'size': [150,170,1],                                     \
74       'position':[-10,10,0]                                    \
75     }                                                          \
76   },                                                           \
77   'animations':                                                \
78   {                                                            \
79     'rotate':                                                  \
80     {                                                          \
81       'duration': 10,                                          \
82       'properties':                                            \
83       [                                                        \
84         {                                                      \
85           'actor':'text',                                      \
86           'property':'rotation',                               \
87           'value':[0, 3, 0, 0],                                \
88           'alpha-function': 'EASE_IN_OUT',                     \
89           'time-period': {'delay': 0, 'duration': 3 }          \
90         }                                                      \
91       ]                                                        \
92     }                                                          \
93   },                                                           \
94   'stage':                                                     \
95   [                                                            \
96     {                                                          \
97       'name':'text',                                           \
98       'type':'basic-text',                                     \
99       'text':'Hello'                                           \
100     },                                                         \
101     {                                                          \
102       'name':'text2',                                          \
103       'type':'basic-text',                                     \
104       'text':'Hello',                                          \
105       'signals':                                               \
106       [                                                        \
107         { 'name': 'on-stage', 'action':'set', 'actor':'text2', 'property':'text', 'value':'Jaylo' } \
108       ]                                                        \
109     }                                                          \
110   ],                                                           \
111   'other':                                                     \
112   [                                                            \
113     {                                                          \
114       'name':'other-text',                                     \
115       'type':'basic-text',                                     \
116       'text':'Hello'                                           \
117     }                                                          \
118   ]                                                            \
119 }                                                              \
120 ");
121
122
123   std::string JSON_CORE_ACTOR_TREE("\
124 {                                                                                         \
125     'styles':                                                                             \
126     {                                                                                     \
127         'my-camera': {                                                                    \
128                       'type':'CameraActor',                                               \
129                       'camera-type':'FreeLook',                                           \
130                       'field-of-view': 0.125,                                             \
131                       'aspect-ratio':5.0,                                                 \
132                       'near-plane-distance': 100,                                         \
133                       'far-plane-distance': 200                                           \
134                       },                                                                  \
135         'basic-text': {                                                                   \
136                     'type':'TextActor',                                                   \
137                     'text':'Hello',                                                       \
138                     'font':'Freesans',                                                    \
139                     'smooth-edge':0.2,                                                    \
140                     'position': [-10.0, 10.0, -1000.0],                                   \
141                     'size': [300.0, 250.0, 0.0]                                           \
142                    },                                                                     \
143         'theme2-text': {                                                                  \
144                     'type':'TextActor',                                                   \
145                     'text':'Hello',                                                       \
146                     'font':'Freesans',                                                    \
147                     'smooth-edge':0.8                                                     \
148                    }                                                                      \
149     },                                                                                    \
150     'stage':                                                                              \
151     [                                                                                     \
152         {'name':'txt1',                                                                   \
153          'type':'TextActor',                                                              \
154          'text':'Hello World',                                                            \
155          'font':'freesans',                                                               \
156          'parent-origin':'CENTER',                                                        \
157          'actors':                                                                        \
158          [                                                                                \
159            { 'type':'basic-text', 'text':'Hello', 'position-y':50 },                      \
160            { 'type':'basic-text', 'text':'Hello', 'position-y':100 },                     \
161            { 'type':'basic-text', 'text':'Hello', 'position-y':150 },                     \
162            { 'type':'basic-text', 'text':'Hello', 'position-y':200 },                     \
163            { 'type':'basic-text', 'text':'Hello', 'position-y':250 }                      \
164          ]                                                                                \
165         }                                                                                 \
166     ]                                                                                     \
167 }                                                                                         \
168 ");
169
170   std::string JSON_CONSTANTS("\
171 {                                                                                         \
172     'constants':                                                                          \
173     {                                                                                     \
174       'HELLO':'Hello World',                                                              \
175       'HELLO2':'Hello {WORLD}'                                                            \
176     },                                                                                    \
177     'styles':                                                                             \
178     {                                                                                     \
179         'basic-text': {                                                                   \
180                     'type':'TextActor',                                                   \
181                     'text':'{HELLO2}'                                                     \
182                    }                                                                      \
183     },                                                                                    \
184     'animations':                                                                         \
185     {                                                                                     \
186       'rotate':                                                                           \
187       {                                                                                   \
188         'duration': 10,                                                                   \
189         'properties':                                                                     \
190         [                                                                                 \
191           {                                                                               \
192             'actor':'{ACTOR}',                                                            \
193             'property':'rotation',                                                        \
194             'value':[0, 3, 0, 0],                                                         \
195             'alpha-function': 'EASE_IN_OUT',                                              \
196             'time-period': {'delay': 0, 'duration': 3 }                                   \
197           }                                                                               \
198         ]                                                                                 \
199       }                                                                                   \
200     },                                                                                    \
201     'stage':                                                                              \
202     [                                                                                     \
203         {'name':'txt1',                                                                   \
204          'type':'TextActor',                                                              \
205          'text':'{HELLO}'                                                                \
206         }                                                                                 \
207     ]                                                                                     \
208 }                                                                                         \
209 ");
210
211
212   std::string ReplaceQuotes(const std::string &in_s)
213   {
214     std::string s(in_s);
215     // wrong as no embedded quote but had regex link problems
216     std::replace(s.begin(), s.end(), '\'', '"');
217     return s;
218   }
219
220 } // namespace
221
222 static void Startup();
223 static void Cleanup();
224
225 extern "C" {
226   void (*tet_startup)() = Startup;
227   void (*tet_cleanup)() = Cleanup;
228 }
229
230 static void UtcDaliBuilderConstants();
231 static void UtcDaliBuilderTextActorCreateFromStyle();
232 static void UtcDaliBuilderTextActorCreateAnimation();
233 static void UtcDaliBuilderTextActorApplyFromStyle();
234 static void UtcDaliBuilderStyles();
235 static void UtcDaliBuilderAddActorsOther();
236 static void UtcDaliBuilderAddActors();
237 static void UtcDaliBuilderSetProperty();
238
239 enum {
240   POSITIVE_TC_IDX = 0x01,
241   NEGATIVE_TC_IDX,
242 };
243
244 #define MAX_NUMBER_OF_TESTS 10000
245 extern "C" {
246   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
247 }
248
249 TEST_FUNCTION( UtcDaliBuilderConstants                     , POSITIVE_TC_IDX );
250 TEST_FUNCTION( UtcDaliBuilderTextActorCreateFromStyle      , POSITIVE_TC_IDX );
251 TEST_FUNCTION( UtcDaliBuilderTextActorCreateAnimation      , POSITIVE_TC_IDX );
252 TEST_FUNCTION( UtcDaliBuilderTextActorApplyFromStyle       , POSITIVE_TC_IDX );
253 TEST_FUNCTION( UtcDaliBuilderStyles                        , POSITIVE_TC_IDX );
254 TEST_FUNCTION( UtcDaliBuilderAddActorsOther                , POSITIVE_TC_IDX );
255 TEST_FUNCTION( UtcDaliBuilderAddActors                     , POSITIVE_TC_IDX );
256 TEST_FUNCTION( UtcDaliBuilderSetProperty                   , POSITIVE_TC_IDX );
257
258 // Called only once before first test is run.
259 static void Startup()
260 {
261 }
262
263 // Called only once after last test is run
264 static void Cleanup()
265 {
266 }
267
268 static void UtcDaliBuilderConstants()
269 {
270   ToolkitTestApplication application;
271   Stage stage = Stage::GetCurrent();
272
273   tet_infoline(" UtcDaliBuilderConstants");
274
275   Builder builder = Builder::New();
276
277   PropertyValueMap userMap;
278   userMap["WORLD"] = "World";
279   builder.AddConstants(userMap);
280
281   builder.LoadFromString( ReplaceQuotes(JSON_CONSTANTS) );
282
283   // constants in json
284   Layer layer = stage.GetRootLayer();
285   size_t count = layer.GetChildCount();
286
287   builder.AddActors( layer );
288   DALI_TEST_CHECK( layer.GetChildCount() == count + 1 );
289
290   TextActor actor = TextActor::DownCast( layer.GetChildAt( count ) );
291   DALI_TEST_CHECK( actor );
292   DALI_TEST_CHECK( actor.GetText() == std::string("Hello World") );
293
294   // global constants
295   PropertyValueMap map;
296   map["HELLO2"] = "Hi";
297
298   builder.AddConstants( map );
299
300   actor = TextActor::DownCast( builder.CreateFromStyle("basic-text") );
301   DALI_TEST_CHECK( actor );
302   DALI_TEST_CHECK( actor.GetText() == std::string("Hi") );
303
304   // user overriding
305   userMap["HELLO2"] = "Hello Dali";
306   actor = TextActor::DownCast( builder.CreateFromStyle("basic-text", userMap) );
307   DALI_TEST_CHECK( actor );
308   DALI_TEST_CHECK( actor.GetText() == std::string("Hello Dali") );
309
310   // animation constants
311   actor = TextActor::DownCast( layer.GetChildAt( count ) );
312   actor.SetName("rotate-me");
313   userMap["ACTOR"] = actor.GetName();
314
315   Animation anim = builder.CreateAnimation("rotate", userMap);
316   DALI_TEST_CHECK( anim );
317   DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
318
319 }
320
321 static void UtcDaliBuilderTextActorCreateFromStyle()
322 {
323   ToolkitTestApplication application;
324   Stage stage = Stage::GetCurrent();
325
326   tet_infoline(" UtcDaliBuilderTextActorCreateFromStyle");
327
328   Builder builder = Builder::New();
329
330   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
331
332   TextActor actor( TextActor::DownCast( builder.CreateFromStyle("basic-text") ) );
333
334   DALI_TEST_CHECK( actor );
335
336   stage.GetRootLayer().Add( actor );
337
338   application.SendNotification();
339   application.Render();
340
341   Vector3 v;
342
343   v = actor.GetCurrentPosition();
344   DALI_TEST_CHECK(v.x == -10.0);
345   DALI_TEST_CHECK(v.y ==  10.0);
346   DALI_TEST_CHECK(v.z ==  0.0);
347
348   v = actor.GetCurrentSize();
349   DALI_TEST_CHECK(v.x == 150.0);
350   DALI_TEST_CHECK(v.y == 170.0);
351   DALI_TEST_CHECK(v.z == 1.0);
352
353   DALI_TEST_CHECK(actor.GetText() == "Hello");
354
355   actor = TextActor::DownCast( builder.CreateFromStyle("*(&^") );
356   DALI_TEST_CHECK(!actor);
357
358 }
359
360 static void UtcDaliBuilderTextActorCreateAnimation()
361 {
362   ToolkitTestApplication application;
363
364   tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
365
366   Builder builder = Builder::New();
367
368   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
369
370   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
371
372   Animation anim = builder.CreateAnimation("rotate");
373   DALI_TEST_CHECK( anim );
374
375   DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
376
377 }
378
379 static void UtcDaliBuilderTextActorApplyFromStyle()
380 {
381   ToolkitTestApplication application;
382
383   tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
384
385   Builder builder = Builder::New();
386
387   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
388
389   TextActor actor = TextActor::New("a");
390
391   builder.ApplyStyle("basic-text", actor);
392
393   DALI_TEST_CHECK( actor );
394
395   Stage::GetCurrent().GetRootLayer().Add( actor );
396
397   application.SendNotification();
398   application.Render();
399
400   Vector3 v;
401
402   v = actor.GetCurrentPosition();
403   DALI_TEST_CHECK(v.x == -10.0);
404   DALI_TEST_CHECK(v.y ==  10.0);
405   DALI_TEST_CHECK(v.z ==  0.0);
406
407   v = actor.GetCurrentSize();
408   DALI_TEST_CHECK(v.x == 150.0);
409   DALI_TEST_CHECK(v.y == 170.0);
410   DALI_TEST_CHECK(v.z == 1.0);
411
412   DALI_TEST_CHECK(actor.GetText() == "Hello");
413
414 }
415
416 static void UtcDaliBuilderAddActors()
417 {
418   ToolkitTestApplication application;
419
420   tet_infoline(" UtcDaliBuilderAddActors");
421
422   Builder builder = Builder::New();
423
424   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
425
426   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
427
428   application.SendNotification();
429   application.Render();
430
431   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
432
433   DALI_TEST_CHECK( actor );
434   DALI_TEST_CHECK(actor.GetText() == "Hello");
435
436 }
437
438 static void UtcDaliBuilderAddActorsOther()
439 {
440   ToolkitTestApplication application;
441
442   tet_infoline(" UtcDaliBuilderAddActorsOther");
443
444   Actor rootActor = Stage::GetCurrent().GetRootLayer();
445
446   Builder builder = Builder::New();
447
448   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
449
450   builder.AddActors( "other", rootActor  );
451
452   application.SendNotification();
453   application.Render();
454
455   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
456
457   DALI_TEST_CHECK( actor );
458   DALI_TEST_CHECK(actor.GetText() == "Hello");
459
460 }
461
462
463 static void UtcDaliBuilderStyles()
464 {
465   ToolkitTestApplication application;
466
467   tet_infoline(" UtcDaliBuilderStyles");
468
469   Builder builder = Builder::New();
470
471   builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
472
473   BaseHandle handle = builder.CreateFromStyle("my-camera");
474   CameraActor camera = CameraActor::DownCast(handle);
475
476   DALI_TEST_CHECK(camera);
477
478   Property::Value v;
479
480   v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
481   DALI_TEST_CHECK( 0.125f == v.Get<float>() );
482
483   v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
484   DALI_TEST_CHECK( 5.0f == v.Get<float>() );
485
486   handle   = builder.CreateFromStyle("basic-text");
487   TextActor textActor = TextActor::DownCast(handle);
488
489   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
490
491   DALI_TEST_CHECK( 0.2f == v.Get<float>() );
492
493   // test ApplyStyle another
494   builder.ApplyStyle("theme2-text", textActor);
495
496   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
497   DALI_TEST_CHECK( 0.8f == v.Get<float>() );
498
499 }
500
501 static void UtcDaliBuilderSetProperty()
502 {
503   ToolkitTestApplication application;
504
505   tet_infoline(" UtcDaliBuilderSetProperty");
506
507   Builder builder = Builder::New();
508
509   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
510
511   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
512
513   application.SendNotification();
514   application.Render();
515
516   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
517
518   DALI_TEST_CHECK( actor );
519   DALI_TEST_CHECK( actor.GetText() == "Jaylo" );
520
521 }