78a4669ae212ff98a6006696702265a04a35b042
[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   'other':                                                     \
103   [                                                            \
104     {                                                          \
105       'name':'other-text',                                     \
106       'type':'basic-text',                                     \
107       'text':'Hello'                                           \
108     }                                                          \
109   ]                                                            \
110 }                                                              \
111 ");
112
113
114   std::string JSON_CORE_ACTOR_TREE("\
115 {                                                                                         \
116     'styles':                                                                             \
117     {                                                                                     \
118         'my-camera': {                                                                    \
119                       'type':'CameraActor',                                               \
120                       'camera-type':'FreeLook',                                           \
121                       'field-of-view': 0.125,                                             \
122                       'aspect-ratio':5.0,                                                 \
123                       'near-plane-distance': 100,                                         \
124                       'far-plane-distance': 200                                           \
125                       },                                                                  \
126         'basic-text': {                                                                   \
127                     'type':'TextActor',                                                   \
128                     'text':'Hello',                                                       \
129                     'font':'Freesans',                                                    \
130                     'smooth-edge':0.2,                                                    \
131                     'position': [-10.0, 10.0, -1000.0],                                   \
132                     'size': [300.0, 250.0, 0.0]                                           \
133                    },                                                                     \
134         'theme2-text': {                                                                  \
135                     'type':'TextActor',                                                   \
136                     'text':'Hello',                                                       \
137                     'font':'Freesans',                                                    \
138                     'smooth-edge':0.8                                                     \
139                    }                                                                      \
140     },                                                                                    \
141     'stage':                                                                              \
142     [                                                                                     \
143         {'name':'txt1',                                                                   \
144          'type':'TextActor',                                                              \
145          'text':'Hello World',                                                            \
146          'font':'freesans',                                                               \
147          'parent-origin':'CENTER',                                                        \
148          'actors':                                                                        \
149          [                                                                                \
150            { 'type':'basic-text', 'text':'Hello', 'position-y':50 },                      \
151            { 'type':'basic-text', 'text':'Hello', 'position-y':100 },                     \
152            { 'type':'basic-text', 'text':'Hello', 'position-y':150 },                     \
153            { 'type':'basic-text', 'text':'Hello', 'position-y':200 },                     \
154            { 'type':'basic-text', 'text':'Hello', 'position-y':250 }                      \
155          ]                                                                                \
156         }                                                                                 \
157     ]                                                                                     \
158 }                                                                                         \
159 ");
160
161
162   std::string ReplaceQuotes(const std::string &in_s)
163   {
164     std::string s(in_s);
165     // wrong as no embedded quote but had regex link problems
166     std::replace(s.begin(), s.end(), '\'', '"');
167     return s;
168   }
169
170 } // namespace
171
172 static void Startup();
173 static void Cleanup();
174
175 extern "C" {
176   void (*tet_startup)() = Startup;
177   void (*tet_cleanup)() = Cleanup;
178 }
179
180 static void UtcDaliBuilderTextActorCreateFromStyle();
181 static void UtcDaliBuilderTextActorCreateAnimation();
182 static void UtcDaliBuilderTextActorApplyFromStyle();
183 static void UtcDaliBuilderStyles();
184 static void UtcDaliBuilderAddActorsOther();
185 static void UtcDaliBuilderAddActors();
186
187 enum {
188   POSITIVE_TC_IDX = 0x01,
189   NEGATIVE_TC_IDX,
190 };
191
192 // Add test functionality for all APIs in the class (Positive and Negative)
193 extern "C" {
194   struct tet_testlist tet_testlist[] = {
195     { UtcDaliBuilderTextActorCreateFromStyle, POSITIVE_TC_IDX },
196     { UtcDaliBuilderTextActorCreateAnimation, POSITIVE_TC_IDX },
197     { UtcDaliBuilderTextActorApplyFromStyle, POSITIVE_TC_IDX },
198     { UtcDaliBuilderStyles, POSITIVE_TC_IDX },
199     { UtcDaliBuilderAddActorsOther, POSITIVE_TC_IDX },
200     { UtcDaliBuilderAddActors, POSITIVE_TC_IDX },
201     { NULL, 0 }
202   };
203 }
204
205 // Called only once before first test is run.
206 static void Startup()
207 {
208 }
209
210 // Called only once after last test is run
211 static void Cleanup()
212 {
213 }
214
215 static void UtcDaliBuilderTextActorCreateFromStyle()
216 {
217   ToolkitTestApplication application;
218   Stage stage = Stage::GetCurrent();
219
220   tet_infoline(" UtcDaliBuilderTextActorCreateFromStyle");
221
222   Builder builder = Builder::New();
223
224   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
225
226   TextActor actor( TextActor::DownCast( builder.CreateFromStyle("basic-text") ) );
227
228   DALI_TEST_CHECK( actor );
229
230   stage.GetRootLayer().Add( actor );
231
232   application.SendNotification();
233   application.Render();
234
235   Vector3 v;
236
237   v = actor.GetCurrentPosition();
238   DALI_TEST_CHECK(v.x == -10.0);
239   DALI_TEST_CHECK(v.y ==  10.0);
240   DALI_TEST_CHECK(v.z ==  0.0);
241
242   v = actor.GetCurrentSize();
243   DALI_TEST_CHECK(v.x == 150.0);
244   DALI_TEST_CHECK(v.y == 170.0);
245   DALI_TEST_CHECK(v.z == 1.0);
246
247   DALI_TEST_CHECK(actor.GetText() == "Hello");
248
249   actor = TextActor::DownCast( builder.CreateFromStyle("*(&^") );
250   DALI_TEST_CHECK(!actor);
251
252 }
253
254 static void UtcDaliBuilderTextActorCreateAnimation()
255 {
256   ToolkitTestApplication application;
257
258   tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
259
260   Builder builder = Builder::New();
261
262   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
263
264   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
265
266   Animation anim = builder.CreateAnimation("rotate");
267   DALI_TEST_CHECK( anim );
268
269   DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
270
271 }
272
273 static void UtcDaliBuilderTextActorApplyFromStyle()
274 {
275   ToolkitTestApplication application;
276
277   tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
278
279   Builder builder = Builder::New();
280
281   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
282
283   TextActor actor = TextActor::New("a");
284
285   builder.ApplyStyle("basic-text", actor);
286
287   DALI_TEST_CHECK( actor );
288
289   Stage::GetCurrent().GetRootLayer().Add( actor );
290
291   application.SendNotification();
292   application.Render();
293
294   Vector3 v;
295
296   v = actor.GetCurrentPosition();
297   DALI_TEST_CHECK(v.x == -10.0);
298   DALI_TEST_CHECK(v.y ==  10.0);
299   DALI_TEST_CHECK(v.z ==  0.0);
300
301   v = actor.GetCurrentSize();
302   DALI_TEST_CHECK(v.x == 150.0);
303   DALI_TEST_CHECK(v.y == 170.0);
304   DALI_TEST_CHECK(v.z == 1.0);
305
306   DALI_TEST_CHECK(actor.GetText() == "Hello");
307
308 }
309
310 static void UtcDaliBuilderAddActors()
311 {
312   ToolkitTestApplication application;
313
314   tet_infoline(" UtcDaliBuilderAddActors");
315
316   Builder builder = Builder::New();
317
318   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
319
320   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
321
322   application.SendNotification();
323   application.Render();
324
325   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
326
327   DALI_TEST_CHECK( actor );
328   DALI_TEST_CHECK(actor.GetText() == "Hello");
329
330 }
331
332 static void UtcDaliBuilderAddActorsOther()
333 {
334   ToolkitTestApplication application;
335
336   tet_infoline(" UtcDaliBuilderAddActorsOther");
337
338   Actor rootActor = Stage::GetCurrent().GetRootLayer();
339
340   Builder builder = Builder::New();
341
342   builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
343
344   builder.AddActors( "other", rootActor  );
345
346   application.SendNotification();
347   application.Render();
348
349   TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
350
351   DALI_TEST_CHECK( actor );
352   DALI_TEST_CHECK(actor.GetText() == "Hello");
353
354 }
355
356
357 static void UtcDaliBuilderStyles()
358 {
359   ToolkitTestApplication application;
360
361   tet_infoline(" UtcDaliBuilderStyles");
362
363   Builder builder = Builder::New();
364
365   builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
366
367   BaseHandle handle = builder.CreateFromStyle("my-camera");
368   CameraActor camera = CameraActor::DownCast(handle);
369
370   DALI_TEST_CHECK(camera);
371
372   Property::Value v;
373
374   v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
375   DALI_TEST_CHECK( 0.125f == v.Get<float>() );
376
377   v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
378   DALI_TEST_CHECK( 5.0f == v.Get<float>() );
379
380   handle   = builder.CreateFromStyle("basic-text");
381   TextActor textActor = TextActor::DownCast(handle);
382
383   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
384
385   DALI_TEST_CHECK( 0.2f == v.Get<float>() );
386
387   // test ApplyStyle another
388   builder.ApplyStyle("theme2-text", textActor);
389
390   v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
391   DALI_TEST_CHECK( 0.8f == v.Get<float>() );
392
393 }