2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/public-api/builder/builder.h>
25 #include <dali-toolkit-test-suite-utils.h>
28 using namespace Toolkit;
33 // Note: To avoid escaping double quotes single quotes are used and then replaced
34 // before parsing. JSON uses double quotes
36 std::string JSON_TEXTSTYLE_ONLY("\
40 'title-text-style':{'font-name': 'Vera', \
41 'font-style': 'Bold', \
44 'text-color': [0.0,0.5,0.5,1], \
50 'shadow-color': [0.0,1.0,0.0,1.0], \
51 'shadow-offset': [3.0,2.0], \
53 'glow-color': [0.9,0.6,0.3,1.0], \
54 'glow-intensity':0.1, \
55 'smooth-edge': 0.45, \
56 'outline-color': [1.0,0.5,0.0,1.0], \
57 'outline-thickness': [0.7,0.6] \
63 std::string JSON_TEXT_ACTOR("\
72 'parent-origin':[0.0,0.0,0], \
73 'anchor-point' :[0.5,0.5,0], \
74 'size': [150,170,1], \
75 'position':[-10,10,0] \
87 'property':'rotation', \
88 'value':[0, 3, 0, 0], \
89 'alpha-function': 'EASE_IN_OUT', \
90 'time-period': {'delay': 0, 'duration': 3 } \
99 'type':'basic-text', \
104 'type':'basic-text', \
108 { 'name': 'on-stage', 'action':'set', 'actor':'text2', 'property':'text', 'value':'Jaylo' } \
115 'name':'other-text', \
116 'type':'basic-text', \
124 std::string JSON_CORE_ACTOR_TREE("\
129 'type':'CameraActor', \
130 'camera-type':'FreeLook', \
131 'field-of-view': 0.125, \
132 'aspect-ratio':5.0, \
133 'near-plane-distance': 100, \
134 'far-plane-distance': 200 \
137 'type':'TextActor', \
141 'position': [-10.0, 10.0, -1000.0], \
142 'size': [300.0, 250.0, 0.0] \
145 'type':'TextActor', \
154 'type':'TextActor', \
155 'text':'Hello World', \
157 'parent-origin':'CENTER', \
160 { 'type':'basic-text', 'text':'Hello', 'position-y':50 }, \
161 { 'type':'basic-text', 'text':'Hello', 'position-y':100 }, \
162 { 'type':'basic-text', 'text':'Hello', 'position-y':150 }, \
163 { 'type':'basic-text', 'text':'Hello', 'position-y':200 }, \
164 { 'type':'basic-text', 'text':'Hello', 'position-y':250 } \
171 std::string JSON_CONSTANTS("\
175 'HELLO':'Hello World', \
176 'HELLO2':'Hello {WORLD}' \
181 'type':'TextActor', \
194 'property':'rotation', \
195 'value':[0, 3, 0, 0], \
196 'alpha-function': 'EASE_IN_OUT', \
197 'time-period': {'delay': 0, 'duration': 3 } \
205 'type':'TextActor', \
213 std::string ReplaceQuotes(const std::string &in_s)
216 // wrong as no embedded quote but had regex link problems
217 std::replace(s.begin(), s.end(), '\'', '"');
223 static void Startup();
224 static void Cleanup();
227 void (*tet_startup)() = Startup;
228 void (*tet_cleanup)() = Cleanup;
231 static void UtcDaliBuilderConstants();
232 static void UtcDaliBuilderTextActorCreateFromStyle();
233 static void UtcDaliBuilderTextActorCreateAnimation();
234 static void UtcDaliBuilderTextActorApplyFromStyle();
235 static void UtcDaliBuilderStyles();
236 static void UtcDaliBuilderAddActorsOther();
237 static void UtcDaliBuilderAddActors();
238 static void UtcDaliBuilderSetProperty();
241 POSITIVE_TC_IDX = 0x01,
245 #define MAX_NUMBER_OF_TESTS 10000
247 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
250 TEST_FUNCTION( UtcDaliBuilderConstants , POSITIVE_TC_IDX );
251 TEST_FUNCTION( UtcDaliBuilderTextActorCreateFromStyle , POSITIVE_TC_IDX );
252 TEST_FUNCTION( UtcDaliBuilderTextActorCreateAnimation , POSITIVE_TC_IDX );
253 TEST_FUNCTION( UtcDaliBuilderTextActorApplyFromStyle , POSITIVE_TC_IDX );
254 TEST_FUNCTION( UtcDaliBuilderStyles , POSITIVE_TC_IDX );
255 TEST_FUNCTION( UtcDaliBuilderAddActorsOther , POSITIVE_TC_IDX );
256 TEST_FUNCTION( UtcDaliBuilderAddActors , POSITIVE_TC_IDX );
257 TEST_FUNCTION( UtcDaliBuilderSetProperty , POSITIVE_TC_IDX );
259 // Called only once before first test is run.
260 static void Startup()
264 // Called only once after last test is run
265 static void Cleanup()
269 static void UtcDaliBuilderConstants()
271 ToolkitTestApplication application;
272 Stage stage = Stage::GetCurrent();
274 tet_infoline(" UtcDaliBuilderConstants");
276 Builder builder = Builder::New();
278 PropertyValueMap userMap;
279 userMap["WORLD"] = "World";
280 builder.AddConstants(userMap);
282 builder.LoadFromString( ReplaceQuotes(JSON_CONSTANTS) );
285 Layer layer = stage.GetRootLayer();
286 size_t count = layer.GetChildCount();
288 builder.AddActors( layer );
289 DALI_TEST_CHECK( layer.GetChildCount() == count + 1 );
291 TextActor actor = TextActor::DownCast( layer.GetChildAt( count ) );
292 DALI_TEST_CHECK( actor );
293 DALI_TEST_CHECK( actor.GetText() == std::string("Hello World") );
296 PropertyValueMap map;
297 map["HELLO2"] = "Hi";
299 builder.AddConstants( map );
301 actor = TextActor::DownCast( builder.CreateFromStyle("basic-text") );
302 DALI_TEST_CHECK( actor );
303 DALI_TEST_CHECK( actor.GetText() == std::string("Hi") );
306 userMap["HELLO2"] = "Hello Dali";
307 actor = TextActor::DownCast( builder.CreateFromStyle("basic-text", userMap) );
308 DALI_TEST_CHECK( actor );
309 DALI_TEST_CHECK( actor.GetText() == std::string("Hello Dali") );
311 // animation constants
312 actor = TextActor::DownCast( layer.GetChildAt( count ) );
313 actor.SetName("rotate-me");
314 userMap["ACTOR"] = actor.GetName();
316 Animation anim = builder.CreateAnimation("rotate", userMap);
317 DALI_TEST_CHECK( anim );
318 DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
322 static void UtcDaliBuilderTextActorCreateFromStyle()
324 ToolkitTestApplication application;
325 Stage stage = Stage::GetCurrent();
327 tet_infoline(" UtcDaliBuilderTextActorCreateFromStyle");
329 Builder builder = Builder::New();
331 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
333 TextActor actor( TextActor::DownCast( builder.CreateFromStyle("basic-text") ) );
335 DALI_TEST_CHECK( actor );
337 stage.GetRootLayer().Add( actor );
339 application.SendNotification();
340 application.Render();
344 v = actor.GetCurrentPosition();
345 DALI_TEST_CHECK(v.x == -10.0);
346 DALI_TEST_CHECK(v.y == 10.0);
347 DALI_TEST_CHECK(v.z == 0.0);
349 v = actor.GetCurrentSize();
350 DALI_TEST_CHECK(v.x == 150.0);
351 DALI_TEST_CHECK(v.y == 170.0);
352 DALI_TEST_CHECK(v.z == 1.0);
354 DALI_TEST_CHECK(actor.GetText() == "Hello");
356 actor = TextActor::DownCast( builder.CreateFromStyle("*(&^") );
357 DALI_TEST_CHECK(!actor);
361 static void UtcDaliBuilderTextActorCreateAnimation()
363 ToolkitTestApplication application;
365 tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
367 Builder builder = Builder::New();
369 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
371 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
373 Animation anim = builder.CreateAnimation("rotate");
374 DALI_TEST_CHECK( anim );
376 DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
380 static void UtcDaliBuilderTextActorApplyFromStyle()
382 ToolkitTestApplication application;
384 tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
386 Builder builder = Builder::New();
388 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
390 TextActor actor = TextActor::New("a");
392 builder.ApplyStyle("basic-text", actor);
394 DALI_TEST_CHECK( actor );
396 Stage::GetCurrent().GetRootLayer().Add( actor );
398 application.SendNotification();
399 application.Render();
403 v = actor.GetCurrentPosition();
404 DALI_TEST_CHECK(v.x == -10.0);
405 DALI_TEST_CHECK(v.y == 10.0);
406 DALI_TEST_CHECK(v.z == 0.0);
408 v = actor.GetCurrentSize();
409 DALI_TEST_CHECK(v.x == 150.0);
410 DALI_TEST_CHECK(v.y == 170.0);
411 DALI_TEST_CHECK(v.z == 1.0);
413 DALI_TEST_CHECK(actor.GetText() == "Hello");
417 static void UtcDaliBuilderAddActors()
419 ToolkitTestApplication application;
421 tet_infoline(" UtcDaliBuilderAddActors");
423 Builder builder = Builder::New();
425 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
427 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
429 application.SendNotification();
430 application.Render();
432 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
434 DALI_TEST_CHECK( actor );
435 DALI_TEST_CHECK(actor.GetText() == "Hello");
439 static void UtcDaliBuilderAddActorsOther()
441 ToolkitTestApplication application;
443 tet_infoline(" UtcDaliBuilderAddActorsOther");
445 Actor rootActor = Stage::GetCurrent().GetRootLayer();
447 Builder builder = Builder::New();
449 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
451 builder.AddActors( "other", rootActor );
453 application.SendNotification();
454 application.Render();
456 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
458 DALI_TEST_CHECK( actor );
459 DALI_TEST_CHECK(actor.GetText() == "Hello");
464 static void UtcDaliBuilderStyles()
466 ToolkitTestApplication application;
468 tet_infoline(" UtcDaliBuilderStyles");
470 Builder builder = Builder::New();
472 builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
474 BaseHandle handle = builder.CreateFromStyle("my-camera");
475 CameraActor camera = CameraActor::DownCast(handle);
477 DALI_TEST_CHECK(camera);
481 v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
482 DALI_TEST_CHECK( 0.125f == v.Get<float>() );
484 v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
485 DALI_TEST_CHECK( 5.0f == v.Get<float>() );
487 handle = builder.CreateFromStyle("basic-text");
488 TextActor textActor = TextActor::DownCast(handle);
490 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
492 DALI_TEST_CHECK( 0.2f == v.Get<float>() );
494 // test ApplyStyle another
495 builder.ApplyStyle("theme2-text", textActor);
497 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
498 DALI_TEST_CHECK( 0.8f == v.Get<float>() );
502 static void UtcDaliBuilderSetProperty()
504 ToolkitTestApplication application;
506 tet_infoline(" UtcDaliBuilderSetProperty");
508 Builder builder = Builder::New();
510 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
512 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
514 application.SendNotification();
515 application.Render();
517 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
519 DALI_TEST_CHECK( actor );
520 DALI_TEST_CHECK( actor.GetText() == "Jaylo" );