2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
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>
27 using namespace Toolkit;
32 // Note: To avoid escaping double quotes single quotes are used and then replaced
33 // before parsing. JSON uses double quotes
35 std::string JSON_TEXTSTYLE_ONLY("\
39 'title-text-style':{'font-name': 'Vera', \
40 'font-style': 'Bold', \
43 'text-color': [0.0,0.5,0.5,1], \
49 'shadow-color': [0.0,1.0,0.0,1.0], \
50 'shadow-offset': [3.0,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] \
62 std::string JSON_TEXT_ACTOR("\
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] \
86 'property':'rotation', \
87 'value':[0, 3, 0, 0], \
88 'alpha-function': 'EASE_IN_OUT', \
89 'time-period': {'delay': 0, 'duration': 3 } \
98 'type':'basic-text', \
103 'type':'basic-text', \
107 { 'name': 'on-stage', 'action':'set', 'actor':'text2', 'property':'text', 'value':'Jaylo' } \
114 'name':'other-text', \
115 'type':'basic-text', \
123 std::string JSON_CORE_ACTOR_TREE("\
128 'type':'CameraActor', \
129 'camera-type':'FreeLook', \
130 'field-of-view': 0.125, \
131 'aspect-ratio':5.0, \
132 'near-plane-distance': 100, \
133 'far-plane-distance': 200 \
136 'type':'TextActor', \
140 'position': [-10.0, 10.0, -1000.0], \
141 'size': [300.0, 250.0, 0.0] \
144 'type':'TextActor', \
153 'type':'TextActor', \
154 'text':'Hello World', \
156 'parent-origin':'CENTER', \
159 { 'type':'basic-text', 'text':'Hello', 'position-y':50 }, \
160 { 'type':'basic-text', 'text':'Hello', 'position-y':100 }, \
161 { 'type':'basic-text', 'text':'Hello', 'position-y':150 }, \
162 { 'type':'basic-text', 'text':'Hello', 'position-y':200 }, \
163 { 'type':'basic-text', 'text':'Hello', 'position-y':250 } \
170 std::string JSON_CONSTANTS("\
174 'HELLO':'Hello World', \
175 'HELLO2':'Hello {WORLD}' \
180 'type':'TextActor', \
193 'property':'rotation', \
194 'value':[0, 3, 0, 0], \
195 'alpha-function': 'EASE_IN_OUT', \
196 'time-period': {'delay': 0, 'duration': 3 } \
204 'type':'TextActor', \
212 std::string ReplaceQuotes(const std::string &in_s)
215 // wrong as no embedded quote but had regex link problems
216 std::replace(s.begin(), s.end(), '\'', '"');
222 static void Startup();
223 static void Cleanup();
226 void (*tet_startup)() = Startup;
227 void (*tet_cleanup)() = Cleanup;
230 static void UtcDaliBuilderConstants();
231 static void UtcDaliBuilderTextActorCreateFromStyle();
232 static void UtcDaliBuilderTextActorCreateAnimation();
233 static void UtcDaliBuilderTextActorApplyFromStyle();
234 static void UtcDaliBuilderStyles();
235 static void UtcDaliBuilderAddActorsOther();
236 static void UtcDaliBuilderAddActors();
237 static void UtcDaliBuilderSetProperty();
240 POSITIVE_TC_IDX = 0x01,
244 #define MAX_NUMBER_OF_TESTS 10000
246 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
249 TEST_FUNCTION( UtcDaliBuilderConstants , POSITIVE_TC_IDX );
250 TEST_FUNCTION( UtcDaliBuilderTextActorCreateFromStyle , POSITIVE_TC_IDX );
251 TEST_FUNCTION( UtcDaliBuilderTextActorCreateAnimation , POSITIVE_TC_IDX );
252 TEST_FUNCTION( UtcDaliBuilderTextActorApplyFromStyle , POSITIVE_TC_IDX );
253 TEST_FUNCTION( UtcDaliBuilderStyles , POSITIVE_TC_IDX );
254 TEST_FUNCTION( UtcDaliBuilderAddActorsOther , POSITIVE_TC_IDX );
255 TEST_FUNCTION( UtcDaliBuilderAddActors , POSITIVE_TC_IDX );
256 TEST_FUNCTION( UtcDaliBuilderSetProperty , POSITIVE_TC_IDX );
258 // Called only once before first test is run.
259 static void Startup()
263 // Called only once after last test is run
264 static void Cleanup()
268 static void UtcDaliBuilderConstants()
270 ToolkitTestApplication application;
271 Stage stage = Stage::GetCurrent();
273 tet_infoline(" UtcDaliBuilderConstants");
275 Builder builder = Builder::New();
277 PropertyValueMap userMap;
278 userMap["WORLD"] = "World";
279 builder.AddConstants(userMap);
281 builder.LoadFromString( ReplaceQuotes(JSON_CONSTANTS) );
284 Layer layer = stage.GetRootLayer();
285 size_t count = layer.GetChildCount();
287 builder.AddActors( layer );
288 DALI_TEST_CHECK( layer.GetChildCount() == count + 1 );
290 TextActor actor = TextActor::DownCast( layer.GetChildAt( count ) );
291 DALI_TEST_CHECK( actor );
292 DALI_TEST_CHECK( actor.GetText() == std::string("Hello World") );
295 PropertyValueMap map;
296 map["HELLO2"] = "Hi";
298 builder.AddConstants( map );
300 actor = TextActor::DownCast( builder.CreateFromStyle("basic-text") );
301 DALI_TEST_CHECK( actor );
302 DALI_TEST_CHECK( actor.GetText() == std::string("Hi") );
305 userMap["HELLO2"] = "Hello Dali";
306 actor = TextActor::DownCast( builder.CreateFromStyle("basic-text", userMap) );
307 DALI_TEST_CHECK( actor );
308 DALI_TEST_CHECK( actor.GetText() == std::string("Hello Dali") );
310 // animation constants
311 actor = TextActor::DownCast( layer.GetChildAt( count ) );
312 actor.SetName("rotate-me");
313 userMap["ACTOR"] = actor.GetName();
315 Animation anim = builder.CreateAnimation("rotate", userMap);
316 DALI_TEST_CHECK( anim );
317 DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
321 static void UtcDaliBuilderTextActorCreateFromStyle()
323 ToolkitTestApplication application;
324 Stage stage = Stage::GetCurrent();
326 tet_infoline(" UtcDaliBuilderTextActorCreateFromStyle");
328 Builder builder = Builder::New();
330 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
332 TextActor actor( TextActor::DownCast( builder.CreateFromStyle("basic-text") ) );
334 DALI_TEST_CHECK( actor );
336 stage.GetRootLayer().Add( actor );
338 application.SendNotification();
339 application.Render();
343 v = actor.GetCurrentPosition();
344 DALI_TEST_CHECK(v.x == -10.0);
345 DALI_TEST_CHECK(v.y == 10.0);
346 DALI_TEST_CHECK(v.z == 0.0);
348 v = actor.GetCurrentSize();
349 DALI_TEST_CHECK(v.x == 150.0);
350 DALI_TEST_CHECK(v.y == 170.0);
351 DALI_TEST_CHECK(v.z == 1.0);
353 DALI_TEST_CHECK(actor.GetText() == "Hello");
355 actor = TextActor::DownCast( builder.CreateFromStyle("*(&^") );
356 DALI_TEST_CHECK(!actor);
360 static void UtcDaliBuilderTextActorCreateAnimation()
362 ToolkitTestApplication application;
364 tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
366 Builder builder = Builder::New();
368 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
370 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
372 Animation anim = builder.CreateAnimation("rotate");
373 DALI_TEST_CHECK( anim );
375 DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
379 static void UtcDaliBuilderTextActorApplyFromStyle()
381 ToolkitTestApplication application;
383 tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
385 Builder builder = Builder::New();
387 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
389 TextActor actor = TextActor::New("a");
391 builder.ApplyStyle("basic-text", actor);
393 DALI_TEST_CHECK( actor );
395 Stage::GetCurrent().GetRootLayer().Add( actor );
397 application.SendNotification();
398 application.Render();
402 v = actor.GetCurrentPosition();
403 DALI_TEST_CHECK(v.x == -10.0);
404 DALI_TEST_CHECK(v.y == 10.0);
405 DALI_TEST_CHECK(v.z == 0.0);
407 v = actor.GetCurrentSize();
408 DALI_TEST_CHECK(v.x == 150.0);
409 DALI_TEST_CHECK(v.y == 170.0);
410 DALI_TEST_CHECK(v.z == 1.0);
412 DALI_TEST_CHECK(actor.GetText() == "Hello");
416 static void UtcDaliBuilderAddActors()
418 ToolkitTestApplication application;
420 tet_infoline(" UtcDaliBuilderAddActors");
422 Builder builder = Builder::New();
424 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
426 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
428 application.SendNotification();
429 application.Render();
431 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
433 DALI_TEST_CHECK( actor );
434 DALI_TEST_CHECK(actor.GetText() == "Hello");
438 static void UtcDaliBuilderAddActorsOther()
440 ToolkitTestApplication application;
442 tet_infoline(" UtcDaliBuilderAddActorsOther");
444 Actor rootActor = Stage::GetCurrent().GetRootLayer();
446 Builder builder = Builder::New();
448 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
450 builder.AddActors( "other", rootActor );
452 application.SendNotification();
453 application.Render();
455 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
457 DALI_TEST_CHECK( actor );
458 DALI_TEST_CHECK(actor.GetText() == "Hello");
463 static void UtcDaliBuilderStyles()
465 ToolkitTestApplication application;
467 tet_infoline(" UtcDaliBuilderStyles");
469 Builder builder = Builder::New();
471 builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
473 BaseHandle handle = builder.CreateFromStyle("my-camera");
474 CameraActor camera = CameraActor::DownCast(handle);
476 DALI_TEST_CHECK(camera);
480 v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
481 DALI_TEST_CHECK( 0.125f == v.Get<float>() );
483 v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
484 DALI_TEST_CHECK( 5.0f == v.Get<float>() );
486 handle = builder.CreateFromStyle("basic-text");
487 TextActor textActor = TextActor::DownCast(handle);
489 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
491 DALI_TEST_CHECK( 0.2f == v.Get<float>() );
493 // test ApplyStyle another
494 builder.ApplyStyle("theme2-text", textActor);
496 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
497 DALI_TEST_CHECK( 0.8f == v.Get<float>() );
501 static void UtcDaliBuilderSetProperty()
503 ToolkitTestApplication application;
505 tet_infoline(" UtcDaliBuilderSetProperty");
507 Builder builder = Builder::New();
509 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
511 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
513 application.SendNotification();
514 application.Render();
516 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
518 DALI_TEST_CHECK( actor );
519 DALI_TEST_CHECK( actor.GetText() == "Jaylo" );