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