926e85f606d4de3dd27684323f891cf99cf21c1f
[platform/core/uifw/dali-toolkit.git] / automated-tests / 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
171   std::string ReplaceQuotes(const std::string &in_s)
172   {
173     std::string s(in_s);
174     // wrong as no embedded quote but had regex link problems
175     std::replace(s.begin(), s.end(), '\'', '"');
176     return s;
177   }
178
179 } // namespace
180
181 static void Startup();
182 static void Cleanup();
183
184 extern "C" {
185   void (*tet_startup)() = Startup;
186   void (*tet_cleanup)() = Cleanup;
187 }
188
189 static void UtcDaliBuilderTextActorCreateFromStyle();
190 static void UtcDaliBuilderTextActorCreateAnimation();
191 static void UtcDaliBuilderTextActorApplyFromStyle();
192 static void UtcDaliBuilderStyles();
193 static void UtcDaliBuilderAddActorsOther();
194 static void UtcDaliBuilderAddActors();
195 static void UtcDaliBuilderSetProperty();
196
197 enum {
198   POSITIVE_TC_IDX = 0x01,
199   NEGATIVE_TC_IDX,
200 };
201
202 // Add test functionality for all APIs in the class (Positive and Negative)
203 extern "C" {
204   struct tet_testlist tet_testlist[] = {
205     { UtcDaliBuilderTextActorCreateFromStyle, POSITIVE_TC_IDX },
206     { UtcDaliBuilderTextActorCreateAnimation, POSITIVE_TC_IDX },
207     { UtcDaliBuilderTextActorApplyFromStyle, POSITIVE_TC_IDX },
208     { UtcDaliBuilderStyles, POSITIVE_TC_IDX },
209     { UtcDaliBuilderAddActorsOther, POSITIVE_TC_IDX },
210     { UtcDaliBuilderAddActors, POSITIVE_TC_IDX },
211     { UtcDaliBuilderSetProperty, POSITIVE_TC_IDX },
212     { NULL, 0 }
213   };
214 }
215
216 // Called only once before first test is run.
217 static void Startup()
218 {
219 }
220
221 // Called only once after last test is run
222 static void Cleanup()
223 {
224 }
225
226 static void UtcDaliBuilderTextActorCreateFromStyle()
227 {
228   ToolkitTestApplication application;
229   Stage stage = Stage::GetCurrent();
230
231   tet_infoline(" UtcDaliBuilderTextActorCreateFromStyle");
232
233   Builder builder = Builder::New();
234
235   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
236
237   TextActor actor( TextActor::DownCast( builder.CreateFromStyle("basic-text") ) );
238
239   DALI_TEST_CHECK( actor );
240
241   stage.GetRootLayer().Add( actor );
242
243   application.SendNotification();
244   application.Render();
245
246   Vector3 v;
247
248   v = actor.GetCurrentPosition();
249   DALI_TEST_CHECK(v.x == -10.0);
250   DALI_TEST_CHECK(v.y ==  10.0);
251   DALI_TEST_CHECK(v.z ==  0.0);
252
253   v = actor.GetCurrentSize();
254   DALI_TEST_CHECK(v.x == 150.0);
255   DALI_TEST_CHECK(v.y == 170.0);
256   DALI_TEST_CHECK(v.z == 1.0);
257
258   DALI_TEST_CHECK(actor.GetText() == "Hello");
259
260   actor = TextActor::DownCast( builder.CreateFromStyle("*(&^") );
261   DALI_TEST_CHECK(!actor);
262
263 }
264
265 static void UtcDaliBuilderTextActorCreateAnimation()
266 {
267   ToolkitTestApplication application;
268
269   tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
270
271   Builder builder = Builder::New();
272
273   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
274
275   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
276
277   Animation anim = builder.CreateAnimation("rotate");
278   DALI_TEST_CHECK( anim );
279
280   DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
281
282 }
283
284 static void UtcDaliBuilderTextActorApplyFromStyle()
285 {
286   ToolkitTestApplication application;
287
288   tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
289
290   Builder builder = Builder::New();
291
292   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
293
294   TextActor actor = TextActor::New("a");
295
296   builder.ApplyStyle("basic-text", actor);
297
298   DALI_TEST_CHECK( actor );
299
300   Stage::GetCurrent().GetRootLayer().Add( actor );
301
302   application.SendNotification();
303   application.Render();
304
305   Vector3 v;
306
307   v = actor.GetCurrentPosition();
308   DALI_TEST_CHECK(v.x == -10.0);
309   DALI_TEST_CHECK(v.y ==  10.0);
310   DALI_TEST_CHECK(v.z ==  0.0);
311
312   v = actor.GetCurrentSize();
313   DALI_TEST_CHECK(v.x == 150.0);
314   DALI_TEST_CHECK(v.y == 170.0);
315   DALI_TEST_CHECK(v.z == 1.0);
316
317   DALI_TEST_CHECK(actor.GetText() == "Hello");
318
319 }
320
321 static void UtcDaliBuilderAddActors()
322 {
323   ToolkitTestApplication application;
324
325   tet_infoline(" UtcDaliBuilderAddActors");
326
327   Builder builder = Builder::New();
328
329   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
330
331   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
332
333   application.SendNotification();
334   application.Render();
335
336   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
337
338   DALI_TEST_CHECK( actor );
339   DALI_TEST_CHECK(actor.GetText() == "Hello");
340
341 }
342
343 static void UtcDaliBuilderAddActorsOther()
344 {
345   ToolkitTestApplication application;
346
347   tet_infoline(" UtcDaliBuilderAddActorsOther");
348
349   Actor rootActor = Stage::GetCurrent().GetRootLayer();
350
351   Builder builder = Builder::New();
352
353   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
354
355   builder.AddActors( "other", rootActor  );
356
357   application.SendNotification();
358   application.Render();
359
360   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
361
362   DALI_TEST_CHECK( actor );
363   DALI_TEST_CHECK(actor.GetText() == "Hello");
364
365 }
366
367
368 static void UtcDaliBuilderStyles()
369 {
370   ToolkitTestApplication application;
371
372   tet_infoline(" UtcDaliBuilderStyles");
373
374   Builder builder = Builder::New();
375
376   builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
377
378   BaseHandle handle = builder.CreateFromStyle("my-camera");
379   CameraActor camera = CameraActor::DownCast(handle);
380
381   DALI_TEST_CHECK(camera);
382
383   Property::Value v;
384
385   v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
386   DALI_TEST_CHECK( 0.125f == v.Get<float>() );
387
388   v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
389   DALI_TEST_CHECK( 5.0f == v.Get<float>() );
390
391   handle   = builder.CreateFromStyle("basic-text");
392   TextActor textActor = TextActor::DownCast(handle);
393
394   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
395
396   DALI_TEST_CHECK( 0.2f == v.Get<float>() );
397
398   // test ApplyStyle another
399   builder.ApplyStyle("theme2-text", textActor);
400
401   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
402   DALI_TEST_CHECK( 0.8f == v.Get<float>() );
403
404 }
405
406 static void UtcDaliBuilderSetProperty()
407 {
408   ToolkitTestApplication application;
409
410   tet_infoline(" UtcDaliBuilderSetProperty");
411
412   Builder builder = Builder::New();
413
414   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
415
416   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
417
418   application.SendNotification();
419   application.Render();
420
421   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
422
423   DALI_TEST_CHECK( actor );
424   DALI_TEST_CHECK( actor.GetText() == "Jaylo" );
425
426 }