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>
24 using namespace Toolkit;
29 // Note: To avoid escaping double quotes single quotes are used and then replaced
30 // before parsing. JSON uses double quotes
33 std::string JSON_TEXT_ACTOR("\
40 'text':'Template Hello', \
41 'size': [150,170,1], \
42 'position':[-10,10,0] \
51 'parent-origin':[0.0,0.0,0], \
52 'anchor-point' :[0.5,0.5,0], \
53 'size': [150,170,1], \
54 'position':[-10,10,0] \
66 'property':'rotation', \
67 'value':[0, 3, 0, 0], \
68 'alpha-function': 'EASE_IN_OUT', \
69 'time-period': {'delay': 0, 'duration': 3 } \
78 'type':'basic-text', \
83 'type':'basic-text', \
87 { 'name': 'on-stage', 'action':'set', 'actor':'text2', 'property':'text', 'value':'Jaylo' } \
94 'name':'other-text', \
95 'type':'basic-text', \
103 std::string JSON_CORE_ACTOR_TREE("\
108 'type':'CameraActor', \
109 'camera-type':'FreeLook', \
110 'field-of-view': 0.125, \
111 'aspect-ratio':5.0, \
112 'near-plane-distance': 100, \
113 'far-plane-distance': 200 \
116 'type':'TextActor', \
120 'position': [-10.0, 10.0, -1000.0], \
121 'size': [300.0, 250.0, 0.0] \
127 'type':'TextActor', \
136 'type':'TextActor', \
137 'text':'Hello World', \
139 'parent-origin':'CENTER', \
142 { 'type':'basic-text', 'text':'Hello', 'position-y':50 }, \
143 { 'type':'basic-text', 'text':'Hello', 'position-y':100 }, \
144 { 'type':'basic-text', 'text':'Hello', 'position-y':150 }, \
145 { 'type':'basic-text', 'text':'Hello', 'position-y':200 }, \
146 { 'type':'basic-text', 'text':'Hello', 'position-y':250 } \
154 std::string ReplaceQuotes(const std::string &in_s)
157 // wrong as no embedded quote but had regex link problems
158 std::replace(s.begin(), s.end(), '\'', '"');
166 void builder_startup(void)
168 test_return_value = TET_UNDEF;
171 void builder_cleanup(void)
173 test_return_value = TET_PASS;
176 int UtcDaliBuilderTextActorCreate(void)
178 ToolkitTestApplication application;
179 Stage stage = Stage::GetCurrent();
181 tet_infoline(" UtcDaliBuilderTextActorCreate");
183 Builder builder = Builder::New();
185 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
187 TextActor actor( TextActor::DownCast( builder.Create("basic-text") ) );
189 DALI_TEST_CHECK( actor );
191 stage.GetRootLayer().Add( actor );
193 application.SendNotification();
194 application.Render();
198 v = actor.GetCurrentPosition();
199 DALI_TEST_CHECK(v.x == -10.0);
200 DALI_TEST_CHECK(v.y == 10.0);
201 DALI_TEST_CHECK(v.z == 0.0);
203 v = actor.GetCurrentSize();
204 DALI_TEST_CHECK(v.x == 150.0);
205 DALI_TEST_CHECK(v.y == 170.0);
206 DALI_TEST_CHECK(v.z == 1.0);
208 DALI_TEST_CHECK(actor.GetText() == "Template Hello");
210 actor = TextActor::DownCast( builder.Create("*(&^") );
211 DALI_TEST_CHECK(!actor);
216 int UtcDaliBuilderTextActorCreateAnimation(void)
218 ToolkitTestApplication application;
220 tet_infoline(" UtcDaliBuilderTextActorCreateAnimation");
222 Builder builder = Builder::New();
224 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
226 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
228 Animation anim = builder.CreateAnimation("rotate");
229 DALI_TEST_CHECK( anim );
231 DALI_TEST_CHECK( 10.0f == anim.GetDuration() );
236 int UtcDaliBuilderTextActorApplyFromStyle(void)
238 ToolkitTestApplication application;
240 tet_infoline(" UtcDaliBuilderTextActorApplyFromStyle");
242 Builder builder = Builder::New();
244 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
246 TextActor actor = TextActor::New("a");
248 builder.ApplyStyle("basic-text", actor);
250 DALI_TEST_CHECK( actor );
252 Stage::GetCurrent().GetRootLayer().Add( actor );
254 application.SendNotification();
255 application.Render();
259 v = actor.GetCurrentPosition();
260 DALI_TEST_CHECK(v.x == -10.0);
261 DALI_TEST_CHECK(v.y == 10.0);
262 DALI_TEST_CHECK(v.z == 0.0);
264 v = actor.GetCurrentSize();
265 DALI_TEST_CHECK(v.x == 150.0);
266 DALI_TEST_CHECK(v.y == 170.0);
267 DALI_TEST_CHECK(v.z == 1.0);
269 DALI_TEST_CHECK(actor.GetText() == "Hello");
274 int UtcDaliBuilderAddActors(void)
276 ToolkitTestApplication application;
278 tet_infoline(" UtcDaliBuilderAddActors");
280 Builder builder = Builder::New();
282 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
284 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
286 application.SendNotification();
287 application.Render();
289 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text") );
291 DALI_TEST_CHECK( actor );
292 DALI_TEST_CHECK(actor.GetText() == "Hello");
297 int UtcDaliBuilderAddActorsOther(void)
299 ToolkitTestApplication application;
301 tet_infoline(" UtcDaliBuilderAddActorsOther");
303 Actor rootActor = Stage::GetCurrent().GetRootLayer();
305 Builder builder = Builder::New();
307 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
309 builder.AddActors( "other", rootActor );
311 application.SendNotification();
312 application.Render();
314 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("other-text") );
316 DALI_TEST_CHECK( actor );
317 DALI_TEST_CHECK(actor.GetText() == "Hello");
323 int UtcDaliBuilderStyles(void)
325 ToolkitTestApplication application;
327 tet_infoline(" UtcDaliBuilderStyles");
329 Builder builder = Builder::New();
331 builder.LoadFromString(ReplaceQuotes(JSON_CORE_ACTOR_TREE));
333 BaseHandle handle = builder.Create("my-camera");
334 CameraActor camera = CameraActor::DownCast(handle);
336 DALI_TEST_CHECK(camera);
340 v = camera.GetProperty( camera.GetPropertyIndex("field-of-view") );
341 DALI_TEST_CHECK( 0.125f == v.Get<float>() );
343 v = camera.GetProperty( camera.GetPropertyIndex("aspect-ratio") );
344 DALI_TEST_CHECK( 5.0f == v.Get<float>() );
346 handle = builder.Create("basic-text");
347 TextActor textActor = TextActor::DownCast(handle);
349 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
351 DALI_TEST_CHECK( 0.2f == v.Get<float>() );
353 // test ApplyStyle another
354 builder.ApplyStyle("theme2-text", textActor);
356 v = textActor.GetProperty( textActor.GetPropertyIndex("smooth-edge") );
357 DALI_TEST_CHECK( 0.8f == v.Get<float>() );
362 int UtcDaliBuilderSetProperty(void)
364 ToolkitTestApplication application;
366 tet_infoline(" UtcDaliBuilderSetProperty");
368 Builder builder = Builder::New();
370 builder.LoadFromString(ReplaceQuotes(JSON_TEXT_ACTOR));
372 builder.AddActors( Stage::GetCurrent().GetRootLayer() );
374 application.SendNotification();
375 application.Render();
377 TextActor actor = TextActor::DownCast( Stage::GetCurrent().GetRootLayer().FindChildByName("text2") );
379 DALI_TEST_CHECK( actor );
380 DALI_TEST_CHECK( actor.GetText() == "Jaylo" );
385 int UtcDaliBuilderCreateFromJson(void)
387 ToolkitTestApplication application;
389 tet_infoline(" UtcDaliBuilderCreateFromJson");
391 Builder builder = Builder::New();
393 TextActor actor = TextActor::DownCast( builder.CreateFromJson("foobar") );
395 DALI_TEST_CHECK( !actor );
397 actor = TextActor::DownCast(
398 builder.CreateFromJson(
399 ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
401 DALI_TEST_CHECK( actor );
403 DALI_TEST_CHECK( actor.GetText() == "Hi" );
408 int UtcDaliBuilderApplyFromJson(void)
410 ToolkitTestApplication application;
412 tet_infoline(" UtcDaliBuilderApplyFromJson");
414 Builder builder = Builder::New();
416 TextActor actor = TextActor::DownCast(
417 builder.CreateFromJson(
418 ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
420 DALI_TEST_CHECK( actor );
422 DALI_TEST_CHECK( actor.GetText() == "Hi" );
424 DALI_TEST_CHECK( !builder.ApplyFromJson(actor, ReplaceQuotes("foobar") ) );
426 builder.ApplyFromJson(actor, ReplaceQuotes("{'text':'low'}") );
428 DALI_TEST_CHECK( actor.GetText() == "low" );