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