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