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