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.
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>
25 using namespace Toolkit;
30 // Note: To avoid escaping double quotes single quotes are used and then replaced
31 // before parsing. JSON uses double quotes
34 std::string JSON_TEXT_ACTOR("\
41 'text':'Template Hello', \
42 'size': [150,170,1], \
43 'position':[-10,10,0] \
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] \
67 'property':'orientation', \
68 'value':[0, 3, 0, 0], \
69 'alpha-function': 'EASE_IN_OUT', \
70 'time-period': {'delay': 0, 'duration': 3 } \
79 'type':'basic-text', \
84 'type':'basic-text', \
88 { 'name': 'on-stage', 'action':'set', 'actor':'text2', 'property':'text', 'value':'Jaylo' } \
95 'name':'other-text', \
96 'type':'basic-text', \
104 std::string JSON_CORE_ACTOR_TREE("\
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 \
117 'type':'TextActor', \
121 'position': [-10.0, 10.0, -1000.0], \
122 'size': [300.0, 250.0, 0.0] \
128 'type':'TextActor', \
137 'type':'TextActor', \
138 'text':'Hello World', \
140 'parent-origin':'CENTER', \
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 } \
155 std::string ReplaceQuotes(const std::string &in_s)
158 // wrong as no embedded quote but had regex link problems
159 std::replace(s.begin(), s.end(), '\'', '"');
163 struct BuilderFunctor
165 BuilderFunctor( bool& called ) : mCalled( called )
182 void builder_startup(void)
184 test_return_value = TET_UNDEF;
187 void builder_cleanup(void)
189 test_return_value = TET_PASS;
192 int UtcDaliBuilderTextActorCreate(void)
194 ToolkitTestApplication application;
195 Stage stage = Stage::GetCurrent();
197 tet_infoline(" UtcDaliBuilderTextActorCreate");
199 Builder builder = Builder::New();
201 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
203 TextActor actor( TextActor::DownCast( builder.Create("basic-text") ) );
205 DALI_TEST_CHECK( actor );
207 stage.GetRootLayer().Add( actor );
209 application.SendNotification();
210 application.Render();
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);
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);
224 DALI_TEST_CHECK(actor.GetText() == "Template Hello");
226 actor = TextActor::DownCast( builder.Create("*(&^") );
227 DALI_TEST_CHECK(!actor);
232 int UtcDaliBuilderTextActorCreateAnimation(void)
234 ToolkitTestApplication application;
236 tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
238 Builder builder = Builder::New();
240 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
242 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
244 Animation anim = builder.CreateAnimation("rotate");
245 DALI_TEST_CHECK( anim );
247 DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
252 int UtcDaliBuilderTextActorApplyFromStyle(void)
254 ToolkitTestApplication application;
256 tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
258 Builder builder = Builder::New();
260 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
262 TextActor actor = TextActor::New("a");
264 builder.ApplyStyle("basic-text", actor);
266 DALI_TEST_CHECK( actor );
268 Stage::GetCurrent().GetRootLayer().Add( actor );
270 application.SendNotification();
271 application.Render();
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);
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);
285 DALI_TEST_CHECK(actor.GetText() == "Hello");
290 int UtcDaliBuilderAddActors(void)
292 ToolkitTestApplication application;
294 tet_infoline(" UtcDaliBuilderAddActors");
296 Builder builder = Builder::New();
298 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
300 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
302 application.SendNotification();
303 application.Render();
305 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
307 DALI_TEST_CHECK( actor );
308 DALI_TEST_CHECK(actor.GetText() == "Hello");
313 int UtcDaliBuilderAddActorsOther(void)
315 ToolkitTestApplication application;
317 tet_infoline(" UtcDaliBuilderAddActorsOther");
319 Actor rootActor = Stage::GetCurrent().GetRootLayer();
321 Builder builder = Builder::New();
323 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
325 builder.AddActors( "other", rootActor );
327 application.SendNotification();
328 application.Render();
330 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
332 DALI_TEST_CHECK( actor );
333 DALI_TEST_CHECK(actor.GetText() == "Hello");
339 int UtcDaliBuilderStyles(void)
341 ToolkitTestApplication application;
343 tet_infoline(" UtcDaliBuilderStyles");
345 Builder builder = Builder::New();
347 builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
349 BaseHandle handle = builder.Create("my-camera");
350 CameraActor camera = CameraActor::DownCast(handle);
352 DALI_TEST_CHECK(camera);
356 v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
357 DALI_TEST_CHECK( 0.125f == v.Get<float>() );
359 v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
360 DALI_TEST_CHECK( 5.0f == v.Get<float>() );
362 handle = builder.Create("basic-text");
363 TextActor textActor = TextActor::DownCast(handle);
365 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
367 DALI_TEST_CHECK( 0.2f == v.Get<float>() );
369 // test ApplyStyle another
370 builder.ApplyStyle("theme2-text", textActor);
372 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
373 DALI_TEST_CHECK( 0.8f == v.Get<float>() );
378 int UtcDaliBuilderSetProperty(void)
380 ToolkitTestApplication application;
382 tet_infoline(" UtcDaliBuilderSetProperty");
384 Builder builder = Builder::New();
386 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
388 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
390 application.SendNotification();
391 application.Render();
393 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
395 DALI_TEST_CHECK( actor );
396 DALI_TEST_CHECK( actor.GetText() == "Jaylo" );
401 int UtcDaliBuilderCreateFromJson(void)
403 ToolkitTestApplication application;
405 tet_infoline(" UtcDaliBuilderCreateFromJson");
407 Builder builder = Builder::New();
409 TextActor actor = TextActor::DownCast( builder.CreateFromJson("foobar") );
411 DALI_TEST_CHECK( !actor );
413 actor = TextActor::DownCast(
414 builder.CreateFromJson(
415 ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
417 DALI_TEST_CHECK( actor );
419 DALI_TEST_CHECK( actor.GetText() == "Hi" );
424 int UtcDaliBuilderApplyFromJson(void)
426 ToolkitTestApplication application;
428 tet_infoline(" UtcDaliBuilderApplyFromJson");
430 Builder builder = Builder::New();
432 TextActor actor = TextActor::DownCast(
433 builder.CreateFromJson(
434 ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
436 DALI_TEST_CHECK( actor );
438 DALI_TEST_CHECK( actor.GetText() == "Hi" );
440 DALI_TEST_CHECK( !builder.ApplyFromJson(actor, ReplaceQuotes("foobar") ) );
442 builder.ApplyFromJson(actor, ReplaceQuotes("{'text':'low'}") );
444 DALI_TEST_CHECK( actor.GetText() == "low" );
449 int UtcDaliBuilderQuitSignal(void)
451 ToolkitTestApplication application;
453 // JSON with a quit event when the actor is touched
458 "\"type\": \"Actor\","
459 "\"size\": [100,100,1],"
460 "\"parent-origin\": \"TOP_LEFT\","
461 "\"anchor-point\": \"TOP_LEFT\","
463 "\"name\": \"touched\","
464 "\"action\": \"quit\""
469 Builder builder = Builder::New();
470 builder.LoadFromString( json );
471 builder.AddActors ( Stage::GetCurrent().GetRootLayer() );
473 // Connect to builder's quit signal
474 bool functorCalled( false );
475 builder.QuitSignal().Connect( &application, BuilderFunctor( functorCalled ) );
478 application.SendNotification();
479 application.Render();
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 );