UTC Increase builder line coverage
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / 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/devel-api/builder/builder.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
23
24 #define STRINGIFY(A)#A
25
26 using namespace Dali;
27 using namespace Toolkit;
28
29 namespace
30 {
31
32 std::string ReplaceQuotes(const std::string &in_s)
33 {
34   std::string s(in_s);
35   // wrong as no embedded quote but had regex link problems
36   std::replace(s.begin(), s.end(), '\'', '"');
37   return s;
38 }
39
40 struct BuilderFunctor
41 {
42   BuilderFunctor( bool& called ) : mCalled( called )
43   {
44     mCalled = false;
45   }
46
47   void operator()()
48   {
49     mCalled = true;
50   }
51
52   bool& mCalled;
53 };
54
55 } // namespace
56
57
58
59 void builder_startup(void)
60 {
61   test_return_value = TET_UNDEF;
62 }
63
64 void builder_cleanup(void)
65 {
66   test_return_value = TET_PASS;
67 }
68
69 int UtcDaliBuilderQuitSignal(void)
70 {
71   ToolkitTestApplication application;
72
73   // JSON with a quit event when the actor is touched
74   std::string json(
75       "{"
76          "\"stage\":"
77          "[{"
78            "\"type\": \"Actor\","
79            "\"size\": [100,100,1],"
80            "\"parent-origin\": \"TOP_LEFT\","
81            "\"anchor-point\": \"TOP_LEFT\","
82            "\"signals\": [{"
83              "\"name\": \"touched\","
84              "\"action\": \"quit\""
85            "}]"
86          "}]"
87       "}"
88   );
89   Builder builder = Builder::New();
90   builder.LoadFromString( json );
91   builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
92
93   // Connect to builder's quit signal
94   bool functorCalled( false );
95   builder.QuitSignal().Connect( &application, BuilderFunctor( functorCalled ) );
96
97   // Render and notify
98   application.SendNotification();
99   application.Render();
100
101   // Emit touch event and check that our quit method is called
102   Integration::TouchEvent touchEvent;
103   touchEvent.points.push_back( TouchPoint ( 0, TouchPoint::Down, 10.0f, 10.0f ) );
104   application.ProcessEvent( touchEvent );
105   DALI_TEST_CHECK( functorCalled );
106
107   END_TEST;
108 }
109
110
111 int UtcDaliBuilderAnimationP(void)
112 {
113   ToolkitTestApplication application;
114
115   // JSON with a quit event when the actor is touched
116   std::string json(
117         "{"
118         "   \"paths\":"
119         "   {"
120         "     \"path0\":"
121         "     {"
122         "       \"points\":[ [-150, -50, 0], [0.0,70.0,0.0], [190.0,-150.0,0.0] ],"
123         "       \"curvature\":0.35"
124         "     }"
125         "   },"
126         "  \"animations\": {"
127         "    \"animate\": {"
128         "      \"loop\": true,"
129         "      \"end-action\": \"BAKE\","
130         "      \"disconnect-action\": \"BAKE\","
131         "      \"properties\":"
132         "      [{"
133         "        \"actor\": \"greeting\","
134         "        \"property\": \"position\","
135         "        \"value\": [300, 300, -1000],"
136         "        \"alpha-function\": \"EASE_IN_OUT\","
137         "        \"relative\": true,"
138         "        \"time-period\": {"
139         "          \"delay\": 0,"
140         "          \"duration\": 3"
141         "        }"
142         "      },"
143         "       {"
144         "         \"actor\": \"greeting\","
145         "         \"property\": \"visible\","
146         "         \"alpha-function\": \"LINEAR\","
147         "         \"value\": true"
148         "       },"
149         "       {"
150         "         \"actor\": \"greeting\","
151         "         \"property\": \"size-width\","
152         "         \"alpha-function\": \"REVERSE\","
153         "         \"value\": 10.0"
154         "       },"
155         "       {"
156         "         \"actor\": \"greeting\","
157         "         \"property\": \"orientation\","
158         "         \"alpha-function\": \"EASE_IN\","
159         "         \"value\": [10.0,20.0,30.0]"
160         "       },"
161         "       {"
162         "         \"actor\": \"greeting\","
163         "         \"property\": \"orientation\","
164         "         \"alpha-function\": \"EASE_OUT\","
165         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
166         "       },"
167         "       {"
168         "         \"actor\": \"greeting\","
169         "         \"property\": \"orientation\","
170         "         \"alpha-function\": \"EASE_IN_OUT\","
171         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
172         "       },"
173         "       {"
174         "         \"actor\": \"greeting\","
175         "         \"property\": \"orientation\","
176         "         \"alpha-function\": \"EASE_IN_SINE\","
177         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
178         "       },"
179         "       {"
180         "         \"actor\": \"greeting\","
181         "         \"property\": \"orientation\","
182         "         \"alpha-function\": \"EASE_OUT_SINE\","
183         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
184         "       },"
185         "       {"
186         "         \"actor\": \"greeting\","
187         "         \"property\": \"orientation\","
188         "         \"alpha-function\": \"EASE_IN_OUT_SINE\","
189         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
190         "       },"
191         "       {"
192         "         \"actor\": \"greeting\","
193         "         \"property\": \"orientation\","
194         "         \"alpha-function\": \"BOUNCE\","
195         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
196         "       },"
197         "       {"
198         "         \"actor\": \"greeting\","
199         "         \"property\": \"orientation\","
200         "         \"alpha-function\": \"SIN\","
201         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
202         "       },"
203         "       {"
204         "         \"actor\": \"greeting\","
205         "         \"property\": \"orientation\","
206         "         \"alpha-function\": \"EASE_OUT_BACK\","
207         "         \"value\": [0.0, 0.0, 0.0, 1.0]"
208         "       }"
209         "      ]"
210         "    },"
211         "    \"path-animation\": {"
212         "      \"duration\": 3.0,"
213         "      \"end-action\": \"DISCARD\","
214         "      \"disconnect-action\": \"BAKE_FINAL\","
215         "      \"properties\": [{"
216         "        \"actor\": \"greeting\","
217         "        \"path\":\"path0\","
218         "        \"forward\":[1,0,0],"
219         "        \"alpha-function\": \"EASE_IN_OUT\","
220         "        \"time-period\": {"
221         "          \"delay\": 0,"
222         "          \"duration\": 3"
223         "        }"
224         "      }]"
225         "    }"
226         "  },"
227         "  \"stage\": [{"
228         "    \"name\": \"greeting\","
229         "    \"type\": \"TextLabel\","
230         "    \"text\": \"Touch me\","
231         "    \"styles\": [\"basic-text\"],"
232         "    \"position\": [0, -120, 0],"
233         "    \"size\": [200, 200, 1],"
234         "    \"orientation\": [0, 0, 30],"
235         "    \"signals\": [{"
236         "      \"name\": \"touched\","
237         "      \"action\": \"play\","
238         "      \"animation\": \"animate\""
239         "    }]"
240         "  }]"
241         "}");
242
243   Builder builder = Builder::New();
244   builder.LoadFromString( json );
245   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
246
247   Animation anim = builder.CreateAnimation("animate");
248
249   DALI_TEST_CHECK( anim );
250
251   anim = builder.CreateAnimation("path-animation");
252
253   DALI_TEST_CHECK( anim );
254
255   // trigger play
256   // Emit touch event and check that our quit method is called
257   Integration::TouchEvent touchEvent;
258   touchEvent.points.push_back( TouchPoint ( 0, TouchPoint::Down, 10.0f, 10.0f ) );
259   application.ProcessEvent( touchEvent );
260
261   // Render and notify
262   application.SendNotification();
263   application.Render();
264
265
266   END_TEST;
267 }
268
269 int UtcDaliBuilderAnimationN(void)
270 {
271   ToolkitTestApplication application;
272
273   // JSON with a quit event when the actor is touched
274   std::string json(
275         "{"
276         "   \"paths\":"
277         "   {"
278         "     \"path0\":"
279         "     {"
280         "       \"points\":[ [-150, -50, 0], [0.0,70.0,0.0], [190.0,-150.0,0.0] ],"
281         "       \"curvature\":0.35"
282         "     }"
283         "   },"
284         "  \"animations\": {"
285         "    \"animate\": {"
286         "      \"loop\": true,"
287         "      \"end-action\": \"BAKE\","
288         "      \"disconnect-action\": \"BAKE\","
289         "      \"properties\":"
290         "      [{"
291         "        \"actor\": \"greeting\","
292         "        \"property\": \"positioninvalid\","
293         "        \"value\": [300, 300, -1000],"
294         "        \"alpha-function\": \"EASE_IN_OUT\","
295         "        \"relative\": true,"
296         "        \"time-period\": {"
297         "          \"delay\": 0,"
298         "          \"duration\": 3"
299         "        }"
300         "      }"
301         "      ]"
302         "    },"
303         "    \"animate2\": {"
304         "      \"loop\": true,"
305         "      \"end-action\": \"BAKE\","
306         "      \"disconnect-action\": \"BAKE\","
307         "      \"properties\":"
308         "      [{"
309         "        \"actor\": \"greeting\","
310         "        \"property\": \"positioninvalid\","
311         "        \"value\": [300, 300, -1000],"
312         "        \"alpha-function\": \"EGGS_OVER_EASY\","
313         "        \"relative\": true,"
314         "        \"time-period\": {"
315         "          \"delay\": 0,"
316         "          \"duration\": 3"
317         "        }"
318         "      }"
319         "      ]"
320         "    },"
321         "    \"path-animation\": {"
322         "      \"duration\": 3.0,"
323         "      \"end-action\": \"DISCARD\","
324         "      \"disconnect-action\": \"BAKE_FINAL\","
325         "      \"properties\": [{"
326         "        \"actor\": \"greeting\","
327         "        \"path\":\"pathDoesntExist\","
328         "        \"forward\":[1,0,0],"
329         "        \"alpha-function\": \"EASE_IN_OUT\","
330         "        \"time-period\": {"
331         "          \"delay\": 0,"
332         "          \"duration\": 3"
333         "        }"
334         "      }]"
335         "    }"
336         "  },"
337         "  \"stage\": [{"
338         "    \"name\": \"greeting\","
339         "    \"type\": \"TextLabel\","
340         "    \"text\": \"Touch me\","
341         "    \"styles\": [\"basic-text\"],"
342         "    \"position\": [0, -120, 0],"
343         "    \"size\": [200, 200, 1],"
344         "    \"orientation\": [0, 0, 30],"
345         "    \"signals\": [{"
346         "      \"name\": \"touched\","
347         "      \"action\": \"play\","
348         "      \"animation\": \"animate\""
349         "    }]"
350         "  }]"
351         "}");
352
353
354   Builder builder = Builder::New();
355   builder.LoadFromString( json );
356   builder.AddActors( Stage::GetCurrent().GetRootLayer() );
357
358   Animation anim = builder.CreateAnimation("animate");
359
360   // log warning line coverage
361   anim = builder.CreateAnimation("path-animation");
362
363   anim = builder.CreateAnimation("animate");
364
365   anim = builder.CreateAnimation("animate2");
366
367   DALI_TEST_CHECK(true);
368
369   END_TEST;
370
371 }
372
373 int UtcDaliBuilderConstantsP(void)
374 {
375   ToolkitTestApplication application;
376
377   // JSON with a quit event when the actor is touched
378   std::string json(
379       "{"
380       "\"constants\":"
381       "{"
382       "  \"IMAGE_PATH\": \"apath\","
383       "  \"WIDTH\": 22.3,"
384       "  \"ANCHOR\": \"TOP_LEFT\","
385       "  \"PADDING\": [1,2,3,4]"
386       "},"
387       "\"stage\":"
388       "[{"
389       "  \"type\": \"ImageActor\","
390       "  \"size\": [100,100,1],"
391       "  \"parent-origin\": \"TOP_LEFT\","
392       "  \"anchor-point\": \"{ANCHOR}\","
393       "  \"padding\": \"{PADDING}\","
394       "  \"image\": { \"filename\": \"dir/{IMAGE_PATH}\" },"
395       "  \"size-width\": \"{WIDTH}\","
396       "  \"signals\": [{"
397       "    \"name\": \"touched\","
398       "    \"action\": \"quit\""
399       "  }]"
400       "}]"
401       "}"
402   );
403
404   Builder builder = Builder::New();
405   builder.LoadFromString( json );
406   builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
407   DALI_TEST_CHECK( builder );
408
409   END_TEST;
410 }
411
412 int UtcDaliBuilderTemplatesAndStylesP(void)
413 {
414   ToolkitTestApplication application;
415
416   // JSON with a quit event when the actor is touched
417   std::string json(
418       "{\n"
419       "\"styles\":\n"
420       "{\n"
421       "  \"image-style\": \n"
422       "  {\n"
423       "    \"color\": [1,0,0,1],\n"
424       "    \"actors\": {\n"
425       "      \"child-image\": {\n"
426       "        \"color\": [0,1,0,1]\n"
427       "      }\n"
428       "    }\n"
429       "  }\n"
430       "},\n"
431       "\"templates\":\n"
432       "{\n"
433       "  \"image-tree\": { \n"
434       "    \"type\": \"ImageActor\",\n"
435       "    \"styles\": [\"image-style\"],\n"
436       "    \"name\": \"image\",\n"
437       "    \"size\": [100,100,1],\n"
438       "    \"signals\": [{\n"
439       "      \"name\": \"touched\",\n"
440       "      \"action\": \"quit\"\n"
441       "    }],\n"
442       "    \"actors\": [\n"
443       "      {\n"
444       "        \"type\":\"ImageActor\",\n"
445       "        \"name\":\"child-image\" \n"
446       "      }\n"
447       "    ]\n"
448       "  }\n"
449       "}\n"
450       "}\n"
451   );
452
453   Builder builder = Builder::New();
454   builder.LoadFromString( json );
455
456   ImageActor actor = ImageActor::DownCast( builder.Create( "image-tree" ) );
457   DALI_TEST_CHECK( actor );
458
459   // NB: already applied in create
460   DALI_TEST_CHECK( builder.ApplyStyle( "image-style", actor ) );
461
462   END_TEST;
463 }
464
465 int UtcDaliBuilderRenderTasksP(void)
466 {
467   ToolkitTestApplication application;
468
469   // JSON with a quit event when the actor is touched
470   std::string json(
471       "{\n"
472       "\"render-tasks\":\n"
473       "{\n"
474       "  \"task0\": {\n"
475       "    \"source-actor\": \"image\",\n"
476       "    \"camera-actor\": \"camera\" \n"
477       "  }\n"
478       "},\n"
479       "\"stage\":\n"
480       "[\n"
481       "  { \n"
482       "    \"type\": \"CameraActor\",\n"
483       "    \"name\": \"image\"\n"
484       "  }, \n"
485       "  { \n"
486       "    \"type\": \"ImageActor\",\n"
487       "    \"name\": \"image\",\n"
488       "    \"size\": [100,100,1],\n"
489       "    \"signals\": [{\n"
490       "      \"name\": \"touched\",\n"
491       "      \"action\": \"quit\"\n"
492       "    }],\n"
493       "    \"actors\": [\n"
494       "      {\n"
495       "        \"type\":\"ImageActor\",\n"
496       "        \"name\":\"child-image\" \n"
497       "      }\n"
498       "    ]\n"
499       "  }\n"
500       "]\n"
501       "}\n"
502   );
503
504   Builder builder = Builder::New();
505   builder.LoadFromString( json );
506
507   unsigned int count = Stage::GetCurrent().GetRenderTaskList().GetTaskCount();
508
509   // coverage
510   builder.CreateRenderTask( "task0" );
511
512   DALI_TEST_CHECK( count <
513                    Stage::GetCurrent().GetRenderTaskList().GetTaskCount() );
514
515   END_TEST;
516 }
517
518 int UtcDaliBuilderChildActionP(void)
519 {
520   ToolkitTestApplication application;
521
522   // JSON with a quit event when the actor is touched
523   std::string json(
524       "{\n"
525       "  \"stage\":\n"
526       "  [{\n"
527       "    \"type\": \"Actor\",\n"
528       "    \"name\": \"actor\",\n"
529       "    \"size\": [100,100,1],\n"
530       "    \"parent-origin\": \"TOP_LEFT\",\n"
531       "    \"anchor-point\": \"TOP_LEFT\",\n"
532       "    \"actors\": [{\n"
533       "      \"type\": \"Actor\",\n"
534       "      \"name\": \"sub-actor\"\n"
535       "    }],\n"
536       "    \"signals\": [{\n"
537       "      \"name\": \"touched\",\n"
538       "      \"action\": \"hide\",\n"
539       "      \"actor\": \"actor\",\n"
540       "      \"child-actor\": \"sub-actor\"\n"
541       "    }]\n"
542       "  }]\n"
543       "}\n"
544   );
545
546   Builder builder = Builder::New();
547   builder.LoadFromString( json );
548   builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
549
550   // Render and notify
551   application.SendNotification();
552   application.Render();
553
554   // Emit touch event and check that our quit method is called
555   Integration::TouchEvent touchEvent;
556   touchEvent.points.push_back( TouchPoint ( 0, TouchPoint::Down, 10.0f, 10.0f ) );
557   application.ProcessEvent( touchEvent );
558
559   // Render and notify
560   application.SendNotification();
561   application.Render();
562
563   Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName("sub-actor");
564   DALI_TEST_CHECK( actor );
565
566   DALI_TEST_CHECK( !actor.IsVisible() );
567
568   END_TEST;
569 }
570
571 int UtcDaliBuilderSetPropertyActionP(void)
572 {
573   ToolkitTestApplication application;
574
575   // JSON with a quit event when the actor is touched
576   std::string json(
577       "{\n"
578       "  \"stage\":\n"
579       "  [{\n"
580       "    \"type\": \"Actor\",\n"
581       "    \"name\": \"actor\",\n"
582       "    \"size\": [100,100,1],\n"
583       "    \"parent-origin\": \"TOP_LEFT\",\n"
584       "    \"anchor-point\": \"TOP_LEFT\",\n"
585       "    \"actors\": [{\n"
586       "      \"type\": \"Actor\",\n"
587       "      \"name\": \"sub-actor\"\n"
588       "    }],\n"
589       "    \"signals\": [{\n"
590       "      \"name\": \"touched\",\n"
591       "      \"action\": \"set\",\n"
592       "      \"actor\": \"sub-actor\",\n"
593       "      \"property\": \"visible\",\n"
594       "      \"value\": false\n"
595       "    }]\n"
596       "  }]\n"
597       "}\n"
598   );
599
600   Builder builder = Builder::New();
601   builder.LoadFromString( json );
602   builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
603
604   // Render and notify
605   application.SendNotification();
606   application.Render();
607
608   // Emit touch event and check that our quit method is called
609   Integration::TouchEvent touchEvent;
610   touchEvent.points.push_back( TouchPoint ( 0, TouchPoint::Down, 10.0f, 10.0f ) );
611   application.ProcessEvent( touchEvent );
612
613   // Render and notify
614   application.SendNotification();
615   application.Render();
616
617   Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName("sub-actor");
618   DALI_TEST_CHECK( actor );
619
620   DALI_TEST_CHECK( !actor.IsVisible() );
621
622   END_TEST;
623 }
624
625 int UtcDaliBuilderGenericActionP(void)
626 {
627   ToolkitTestApplication application;
628
629   // JSON with a quit event when the actor is touched
630   std::string json(
631       "{\n"
632       "  \"stage\":\n"
633       "  [{\n"
634       "    \"type\": \"Actor\",\n"
635       "    \"name\": \"actor\",\n"
636       "    \"size\": [100,100,1],\n"
637       "    \"parent-origin\": \"TOP_LEFT\",\n"
638       "    \"anchor-point\": \"TOP_LEFT\",\n"
639       "    \"actors\": [{\n"
640       "      \"type\": \"Actor\",\n"
641       "      \"name\": \"sub-actor\"\n"
642       "    }],\n"
643       "    \"signals\": [{\n"
644       "      \"name\": \"touched\",\n"
645       "      \"action\": \"hide\"\n"
646       "    }]\n"
647       "  }]\n"
648       "}\n"
649   );
650
651   Builder builder = Builder::New();
652   builder.LoadFromString( json );
653   builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
654
655   // Render and notify
656   application.SendNotification();
657   application.Render();
658
659   // Emit touch event and check that our quit method is called
660   Integration::TouchEvent touchEvent;
661   touchEvent.points.push_back( TouchPoint ( 0, TouchPoint::Down, 10.0f, 10.0f ) );
662   application.ProcessEvent( touchEvent );
663
664   // Render and notify
665   application.SendNotification();
666   application.Render();
667
668   Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName("actor");
669   DALI_TEST_CHECK( actor );
670
671   DALI_TEST_CHECK( !actor.IsVisible() );
672
673   END_TEST;
674 }
675
676 int UtcDaliBuilderPropertyNotificationP(void)
677 {
678   ToolkitTestApplication application;
679
680   // JSON with a quit event when the actor is touched
681   std::string json(
682       "{\n"
683       "  \"stage\":\n"
684       "  [{\n"
685       "    \"type\": \"Actor\",\n"
686       "    \"name\": \"actor\",\n"
687       "    \"size\": [100,100,1],\n"
688       "    \"parent-origin\": \"TOP_LEFT\",\n"
689       "    \"anchor-point\": \"TOP_LEFT\",\n"
690       "    \"actors\": [{\n"
691       "      \"type\": \"Actor\",\n"
692       "      \"name\": \"sub-actor\"\n"
693       "    }],\n"
694       "    \"signals\": [{\n"
695       "      \"name\": \"touched\",\n"
696       "      \"action\": \"hide\"\n"
697       "    }],\n"
698       "    \"notifications\": [{\n"
699       "      \"property\": \"visible\",\n"
700       "      \"condition\": \"False\",\n"
701       "      \"action\": \"show\"\n"
702       "    }]\n"
703       "  }]\n"
704       "}\n"
705   );
706
707   Builder builder = Builder::New();
708   builder.LoadFromString( json );
709   builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
710
711   // Render and notify
712   application.SendNotification();
713   application.Render();
714
715   // Emit touch event and check that our quit method is called
716   Integration::TouchEvent touchEvent;
717   touchEvent.points.push_back( TouchPoint ( 0, TouchPoint::Down, 10.0f, 10.0f ) );
718   application.ProcessEvent( touchEvent );
719
720   // Render and notify
721   application.SendNotification();
722   application.Render();
723
724   // Render and notify
725   application.SendNotification();
726   application.Render();
727
728   Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName("actor");
729   DALI_TEST_CHECK( actor );
730
731   DALI_TEST_CHECK( actor.IsVisible() );
732
733   END_TEST;
734 }