Merge "Added actor creation from json snippet" into tizen
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / 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 #include <stdlib.h>
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/public-api/builder/builder.h>
22
23 using namespace Dali;
24 using namespace Toolkit;
25
26 namespace
27 {
28 //
29 // Note: To avoid escaping double quotes single quotes are used and then replaced
30 //       before parsing. JSON uses double quotes
31 //
32
33   std::string JSON_TEXT_ACTOR("\
34 {                                                              \
35   'templates':                                                 \
36   {                                                            \
37     'basic-text':                                              \
38     {                                                          \
39       'type':'TextActor',                                      \
40       'text':'Template Hello',                                 \
41       'size': [150,170,1],                                     \
42       'position':[-10,10,0]                                    \
43     }                                                          \
44   },                                                           \
45   'styles':                                                    \
46   {                                                            \
47     'basic-text':                                              \
48     {                                                          \
49       'text':'Hello',                                          \
50       'font':'',                                               \
51       'parent-origin':[0.0,0.0,0],                             \
52       'anchor-point' :[0.5,0.5,0],                             \
53       'size': [150,170,1],                                     \
54       'position':[-10,10,0]                                    \
55     }                                                          \
56   },                                                           \
57   'animations':                                                \
58   {                                                            \
59     'rotate':                                                  \
60     {                                                          \
61       'duration': 10,                                          \
62       'properties':                                            \
63       [                                                        \
64         {                                                      \
65           'actor':'text',                                      \
66           'property':'rotation',                               \
67           'value':[0, 3, 0, 0],                                \
68           'alpha-function': 'EASE_IN_OUT',                     \
69           'time-period': {'delay': 0, 'duration': 3 }          \
70         }                                                      \
71       ]                                                        \
72     }                                                          \
73   },                                                           \
74   'stage':                                                     \
75   [                                                            \
76     {                                                          \
77       'name':'text',                                           \
78       'type':'basic-text',                                     \
79       'text':'Hello'                                           \
80     },                                                         \
81     {                                                          \
82       'name':'text2',                                          \
83       'type':'basic-text',                                     \
84       'text':'Hello',                                          \
85       'signals':                                               \
86       [                                                        \
87         { 'name': 'on-stage', 'action':'set', 'actor':'text2', 'property':'text', 'value':'Jaylo' } \
88       ]                                                        \
89     }                                                          \
90   ],                                                           \
91   'other':                                                     \
92   [                                                            \
93     {                                                          \
94       'name':'other-text',                                     \
95       'type':'basic-text',                                     \
96       'text':'Hello'                                           \
97     }                                                          \
98   ]                                                            \
99 }                                                              \
100 ");
101
102
103   std::string JSON_CORE_ACTOR_TREE("\
104 {                                                                                         \
105     'templates':                                                                          \
106     {                                                                                     \
107         'my-camera': {                                                                    \
108                       'type':'CameraActor',                                               \
109                       'camera-type':'FreeLook',                                           \
110                       'field-of-view': 0.125,                                             \
111                       'aspect-ratio':5.0,                                                 \
112                       'near-plane-distance': 100,                                         \
113                       'far-plane-distance': 200                                           \
114                       },                                                                  \
115         'basic-text': {                                                                   \
116                     'type':'TextActor',                                                   \
117                     'text':'Hello',                                                       \
118                     'font':'Freesans',                                                    \
119                     'smooth-edge':0.2,                                                    \
120                     'position': [-10.0, 10.0, -1000.0],                                   \
121                     'size': [300.0, 250.0, 0.0]                                           \
122                    }                                                                      \
123     },                                                                                    \
124     'styles':                                                                             \
125     {                                                                                     \
126        'theme2-text': {                                                                   \
127                    'type':'TextActor',                                                    \
128                    'text':'Hello',                                                        \
129                    'font':'Freesans',                                                     \
130                    'smooth-edge':0.8                                                      \
131                   }                                                                       \
132     },                                                                                    \
133     'stage':                                                                              \
134     [                                                                                     \
135         {'name':'txt1',                                                                   \
136          'type':'TextActor',                                                              \
137          'text':'Hello World',                                                            \
138          'font':'freesans',                                                               \
139          'parent-origin':'CENTER',                                                        \
140          'actors':                                                                        \
141          [                                                                                \
142            { 'type':'basic-text', 'text':'Hello', 'position-y':50 },                      \
143            { 'type':'basic-text', 'text':'Hello', 'position-y':100 },                     \
144            { 'type':'basic-text', 'text':'Hello', 'position-y':150 },                     \
145            { 'type':'basic-text', 'text':'Hello', 'position-y':200 },                     \
146            { 'type':'basic-text', 'text':'Hello', 'position-y':250 }                      \
147          ]                                                                                \
148         }                                                                                 \
149     ]                                                                                     \
150 }                                                                                         \
151 ");
152
153
154 std::string ReplaceQuotes(const std::string &in_s)
155 {
156   std::string s(in_s);
157   // wrong as no embedded quote but had regex link problems
158   std::replace(s.begin(), s.end(), '\'', '"');
159   return s;
160 }
161
162 } // namespace
163
164
165
166 void builder_startup(void)
167 {
168   test_return_value = TET_UNDEF;
169 }
170
171 void builder_cleanup(void)
172 {
173   test_return_value = TET_PASS;
174 }
175
176 int UtcDaliBuilderTextActorCreate(void)
177 {
178   ToolkitTestApplication application;
179   Stage stage = Stage::GetCurrent();
180
181   tet_infoline(" UtcDaliBuilderTextActorCreate");
182
183   Builder builder = Builder::New();
184
185   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
186
187   TextActor actor( TextActor::DownCast( builder.Create("basic-text") ) );
188
189   DALI_TEST_CHECK( actor );
190
191   stage.GetRootLayer().Add( actor );
192
193   application.SendNotification();
194   application.Render();
195
196   Vector3 v;
197
198   v = actor.GetCurrentPosition();
199   DALI_TEST_CHECK(v.x == -10.0);
200   DALI_TEST_CHECK(v.y ==  10.0);
201   DALI_TEST_CHECK(v.z ==  0.0);
202
203   v = actor.GetCurrentSize();
204   DALI_TEST_CHECK(v.x == 150.0);
205   DALI_TEST_CHECK(v.y == 170.0);
206   DALI_TEST_CHECK(v.z == 1.0);
207
208   DALI_TEST_CHECK(actor.GetText() == "Template Hello");
209
210   actor = TextActor::DownCast( builder.Create("*(&^") );
211   DALI_TEST_CHECK(!actor);
212
213   END_TEST;
214 }
215
216 int UtcDaliBuilderTextActorCreateAnimation(void)
217 {
218   ToolkitTestApplication application;
219
220   tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
221
222   Builder builder = Builder::New();
223
224   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
225
226   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
227
228   Animation anim = builder.CreateAnimation("rotate");
229   DALI_TEST_CHECK( anim );
230
231   DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
232
233   END_TEST;
234 }
235
236 int UtcDaliBuilderTextActorApplyFromStyle(void)
237 {
238   ToolkitTestApplication application;
239
240   tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
241
242   Builder builder = Builder::New();
243
244   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
245
246   TextActor actor = TextActor::New("a");
247
248   builder.ApplyStyle("basic-text", actor);
249
250   DALI_TEST_CHECK( actor );
251
252   Stage::GetCurrent().GetRootLayer().Add( actor );
253
254   application.SendNotification();
255   application.Render();
256
257   Vector3 v;
258
259   v = actor.GetCurrentPosition();
260   DALI_TEST_CHECK(v.x == -10.0);
261   DALI_TEST_CHECK(v.y ==  10.0);
262   DALI_TEST_CHECK(v.z ==  0.0);
263
264   v = actor.GetCurrentSize();
265   DALI_TEST_CHECK(v.x == 150.0);
266   DALI_TEST_CHECK(v.y == 170.0);
267   DALI_TEST_CHECK(v.z == 1.0);
268
269   DALI_TEST_CHECK(actor.GetText() == "Hello");
270
271   END_TEST;
272 }
273
274 int UtcDaliBuilderAddActors(void)
275 {
276   ToolkitTestApplication application;
277
278   tet_infoline(" UtcDaliBuilderAddActors");
279
280   Builder builder = Builder::New();
281
282   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
283
284   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
285
286   application.SendNotification();
287   application.Render();
288
289   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
290
291   DALI_TEST_CHECK( actor );
292   DALI_TEST_CHECK(actor.GetText() == "Hello");
293
294   END_TEST;
295 }
296
297 int UtcDaliBuilderAddActorsOther(void)
298 {
299   ToolkitTestApplication application;
300
301   tet_infoline(" UtcDaliBuilderAddActorsOther");
302
303   Actor rootActor = Stage::GetCurrent().GetRootLayer();
304
305   Builder builder = Builder::New();
306
307   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
308
309   builder.AddActors( "other", rootActor  );
310
311   application.SendNotification();
312   application.Render();
313
314   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
315
316   DALI_TEST_CHECK( actor );
317   DALI_TEST_CHECK(actor.GetText() == "Hello");
318
319   END_TEST;
320 }
321
322
323 int UtcDaliBuilderStyles(void)
324 {
325   ToolkitTestApplication application;
326
327   tet_infoline(" UtcDaliBuilderStyles");
328
329   Builder builder = Builder::New();
330
331   builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
332
333   BaseHandle handle = builder.Create("my-camera");
334   CameraActor camera = CameraActor::DownCast(handle);
335
336   DALI_TEST_CHECK(camera);
337
338   Property::Value v;
339
340   v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
341   DALI_TEST_CHECK( 0.125f == v.Get<float>() );
342
343   v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
344   DALI_TEST_CHECK( 5.0f == v.Get<float>() );
345
346   handle   = builder.Create("basic-text");
347   TextActor textActor = TextActor::DownCast(handle);
348
349   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
350
351   DALI_TEST_CHECK( 0.2f == v.Get<float>() );
352
353   // test ApplyStyle another
354   builder.ApplyStyle("theme2-text", textActor);
355
356   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
357   DALI_TEST_CHECK( 0.8f == v.Get<float>() );
358
359   END_TEST;
360 }
361
362 int UtcDaliBuilderSetProperty(void)
363 {
364   ToolkitTestApplication application;
365
366   tet_infoline(" UtcDaliBuilderSetProperty");
367
368   Builder builder = Builder::New();
369
370   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
371
372   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
373
374   application.SendNotification();
375   application.Render();
376
377   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
378
379   DALI_TEST_CHECK( actor );
380   DALI_TEST_CHECK( actor.GetText() == "Jaylo" );
381
382   END_TEST;
383 }
384
385 int UtcDaliBuilderCreateFromJson(void)
386 {
387   ToolkitTestApplication application;
388
389   tet_infoline(" UtcDaliBuilderCreateFromJson");
390
391   Builder builder = Builder::New();
392
393   TextActor actor = TextActor::DownCast( builder.CreateFromJson("foobar") );
394
395   DALI_TEST_CHECK( !actor );
396
397   actor = TextActor::DownCast(
398     builder.CreateFromJson(
399       ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
400
401   DALI_TEST_CHECK( actor );
402
403   DALI_TEST_CHECK( actor.GetText() == "Hi" );
404
405   END_TEST;
406 }
407
408 int UtcDaliBuilderApplyFromJson(void)
409 {
410   ToolkitTestApplication application;
411
412   tet_infoline(" UtcDaliBuilderApplyFromJson");
413
414   Builder builder = Builder::New();
415
416   TextActor actor = TextActor::DownCast(
417     builder.CreateFromJson(
418       ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
419
420   DALI_TEST_CHECK( actor );
421
422   DALI_TEST_CHECK( actor.GetText() == "Hi" );
423
424   DALI_TEST_CHECK( !builder.ApplyFromJson(actor, ReplaceQuotes("foobar") ) );
425
426   builder.ApplyFromJson(actor, ReplaceQuotes("{'text':'low'}") );
427
428   DALI_TEST_CHECK( actor.GetText() == "low" );
429
430   END_TEST;
431 }