Dali 3D User Interface Engine
utc-Dali-ControlRenderer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 #include <iostream>
18 #include <stdlib.h>
24 
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27 
28 namespace
29 {
30 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
31 const char* TEST_NPATCH_FILE_NAME = "gallery_image_01.9.jpg";
32 }
33 
35 {
37 }
38 
40 {
42 }
43 
45 {
46  ToolkitTestApplication application;
47  tet_infoline( "UtcDaliControlRendererCopyAndAssignment" );
48 
50  Property::Map propertyMap;
51  propertyMap.Insert("rendererType", "color");
52  propertyMap.Insert("blendColor", Color::BLUE);
53  ControlRenderer controlRenderer = factory.GetControlRenderer( propertyMap );
54 
55  ControlRenderer controlRendererCopy( controlRenderer );
56  DALI_TEST_CHECK(controlRenderer == controlRendererCopy);
57 
58  ControlRenderer emptyControlRenderer;
59  ControlRenderer emptyControlRendererCopy( emptyControlRenderer );
60  DALI_TEST_CHECK(emptyControlRenderer == emptyControlRendererCopy);
61 
62  ControlRenderer controlRendererEquals;
63  controlRendererEquals = controlRenderer;
64  DALI_TEST_CHECK(controlRenderer == controlRendererEquals);
65 
66  ControlRenderer emptyControlRendererEquals;
67  emptyControlRendererEquals = emptyControlRenderer;
68  DALI_TEST_CHECK( emptyControlRenderer == emptyControlRendererEquals );
69 
70  //self assignment
71  controlRenderer = controlRenderer;
72  DALI_TEST_CHECK( controlRenderer = controlRendererCopy );
73 
74  END_TEST;
75 }
76 
78 {
79  ToolkitTestApplication application;
80  tet_infoline( "UtcDaliControlRendererSetDepthIndex" );
81 
83  Property::Map propertyMap;
84  propertyMap.Insert("rendererType", "color");
85  propertyMap.Insert("blendColor", Color::BLUE);
86  ControlRenderer controlRenderer = factory.GetControlRenderer( propertyMap );
87 
88  controlRenderer.SetDepthIndex( 1.f );
89 
90  Actor actor = Actor::New();
91  actor.SetSize(200.f, 200.f);
92  Stage::GetCurrent().Add( actor );
93  controlRenderer.SetOnStage( actor );
94 
95  int depthIndex = actor.GetRendererAt(0u).GetProperty<int>( Renderer::Property::DEPTH_INDEX );
96  DALI_TEST_EQUALS( depthIndex, 1, TEST_LOCATION );
97  DALI_TEST_EQUALS( controlRenderer.GetDepthIndex(), 1.f, TEST_LOCATION );
98 
99  controlRenderer.SetDepthIndex( -1.f );
100  depthIndex = actor.GetRendererAt(0u).GetProperty<int>( Renderer::Property::DEPTH_INDEX );
101  DALI_TEST_EQUALS( depthIndex, -1, TEST_LOCATION );
102  DALI_TEST_EQUALS( controlRenderer.GetDepthIndex(), -1.f, TEST_LOCATION );
103 
104  END_TEST;
105 }
106 
108 {
109  ToolkitTestApplication application;
110  tet_infoline( "UtcDaliControlRendererGetNaturalSize" );
111 
113  Vector2 rendererSize( 20.f, 30.f );
114  Vector2 naturalSize;
115 
116  // color renderer
117  ControlRenderer colorRenderer = factory.GetControlRenderer( Color::MAGENTA );
118  colorRenderer.SetSize( rendererSize );
119  DALI_TEST_EQUALS( colorRenderer.GetSize(), rendererSize, TEST_LOCATION );
120  colorRenderer.GetNaturalSize(naturalSize);
122 
123  // image renderer
125  ControlRenderer imageRenderer = factory.GetControlRenderer( image );
126  imageRenderer.SetSize( rendererSize );
127  DALI_TEST_EQUALS( imageRenderer.GetSize(), rendererSize, TEST_LOCATION );
128  imageRenderer.GetNaturalSize(naturalSize);
129  DALI_TEST_EQUALS( naturalSize, Vector2(100.f, 200.f), TEST_LOCATION );
130 
131  // n patch renderer
132  TestPlatformAbstraction& platform = application.GetPlatform();
133  Vector2 testSize(80.f, 160.f);
134  platform.SetClosestImageSize(testSize);
136  ControlRenderer nPatchRenderer = factory.GetControlRenderer( image );
137  nPatchRenderer.SetSize( rendererSize );
138  DALI_TEST_EQUALS( nPatchRenderer.GetSize(), rendererSize, TEST_LOCATION );
139  nPatchRenderer.GetNaturalSize(naturalSize);
140  DALI_TEST_EQUALS( naturalSize, testSize, TEST_LOCATION );
141 
142  // border renderer
143  float borderSize = 5.f;
144  ControlRenderer borderRenderer = factory.GetControlRenderer( borderSize, Color::RED );
145  borderRenderer.SetSize( rendererSize );
146  DALI_TEST_EQUALS( borderRenderer.GetSize(), rendererSize, TEST_LOCATION );
147  borderRenderer.GetNaturalSize(naturalSize);
149 
150  // gradient renderer
151  Property::Map propertyMap;
152  propertyMap.Insert("rendererType", "gradient");
153  Vector2 start(-1.f, -1.f);
154  Vector2 end(1.f, 1.f);
155  propertyMap.Insert("gradientStartPosition", start);
156  propertyMap.Insert("gradientEndPosition", end);
157  propertyMap.Insert("gradientStopOffset", Vector2(0.f, 1.f));
158  Property::Array stopColors;
159  stopColors.PushBack( Color::RED );
160  stopColors.PushBack( Color::GREEN );
161  propertyMap.Insert("gradientStopColor", stopColors);
162  ControlRenderer gradientRenderer = factory.GetControlRenderer(propertyMap);
163  gradientRenderer.SetSize( rendererSize );
164  DALI_TEST_EQUALS( gradientRenderer.GetSize(), rendererSize, TEST_LOCATION );
165  gradientRenderer.GetNaturalSize(naturalSize);
167 
168  END_TEST;
169 }
170 
172 {
173  ToolkitTestApplication application;
174  tet_infoline( "UtcDaliControlRendererSetDepthIndex" );
175 
177  Property::Map propertyMap;
178  propertyMap.Insert("rendererType", "color");
179  propertyMap.Insert("blendColor", Color::BLUE);
180  ControlRenderer controlRenderer = factory.GetControlRenderer( propertyMap );
181 
182  Actor actor = Actor::New();
183  actor.SetSize(200.f, 200.f);
184  Stage::GetCurrent().Add( actor );
185 
186  application.SendNotification();
187  application.Render(0);
188  DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
189 
190  controlRenderer.SetOnStage( actor );
191  application.SendNotification();
192  application.Render(0);
193  DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
194 
195  controlRenderer.SetOffStage( actor );
196  application.SendNotification();
197  application.Render(0);
198  DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
199 
200  END_TEST;
201 }
202 
204 {
205  ToolkitTestApplication application;
206  tet_infoline( "intUtcDaliControlRendererRemoveAndReset" );
207 
209 
210  Actor actor = Actor::New();
211  actor.SetSize(200.f, 200.f);
212  Stage::GetCurrent().Add( actor );
213 
214  ControlRenderer imageRenderer;
215  // test calling RemoveAndReset with an empty handle
216  try
217  {
218  imageRenderer.RemoveAndReset( actor );
220  }
221  catch (DaliException& exception)
222  {
224  }
225 
227  imageRenderer = factory.GetControlRenderer(image);
228  DALI_TEST_CHECK( imageRenderer );
229 
230  imageRenderer.SetOnStage( actor );
231  application.SendNotification();
232  application.Render(0);
233  DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
234 
235  imageRenderer.RemoveAndReset( actor );
236  application.SendNotification();
237  application.Render(0);
238  DALI_TEST_CHECK( actor.GetRendererCount() == 0u ); // renderer is removed from actor
239  DALI_TEST_CHECK( !imageRenderer ); // control renderer is reset
240 
241  END_TEST;
242 }
243 
245 {
246  ToolkitTestApplication application;
247  tet_infoline( "UtcDaliControlRendererGetPropertyMap1: ColorRenderer" );
248 
250  Property::Map propertyMap;
251  propertyMap.Insert("rendererType", "color");
252  propertyMap.Insert("blendColor", Color::BLUE);
253  ControlRenderer colorRenderer = factory.GetControlRenderer( propertyMap );
254 
255  Property::Map resultMap;
256  colorRenderer.CreatePropertyMap( resultMap );
257 
258  Property::Value* typeValue = resultMap.Find( "rendererType", Property::STRING );
259  DALI_TEST_CHECK( typeValue );
260  DALI_TEST_CHECK( typeValue->Get<std::string>() == "color" );
261 
262  Property::Value* colorValue = resultMap.Find( "blendColor", Property::VECTOR4 );
263  DALI_TEST_CHECK( colorValue );
264  DALI_TEST_CHECK( colorValue->Get<Vector4>() == Color::BLUE );
265 
266  // change the blend color
267  Actor actor;
268  factory.ResetRenderer( colorRenderer, actor, Color::CYAN );
269  colorRenderer.CreatePropertyMap( resultMap );
270 
271  colorValue = resultMap.Find( "blendColor", Property::VECTOR4 );
272  DALI_TEST_CHECK( colorValue );
273  DALI_TEST_CHECK( colorValue->Get<Vector4>() == Color::CYAN );
274 
275  END_TEST;
276 }
277 
279 {
280  ToolkitTestApplication application;
281  tet_infoline( "UtcDaliControlRendererGetPropertyMap2: BorderRenderer" );
282 
284  Property::Map propertyMap;
285  propertyMap.Insert("rendererType", "border");
286  propertyMap.Insert("borderColor", Color::BLUE);
287  propertyMap.Insert("borderSize", 5.f);
288  ControlRenderer borderRenderer = factory.GetControlRenderer( propertyMap );
289 
290  Property::Map resultMap;
291  borderRenderer.CreatePropertyMap( resultMap );
292 
293  // check the property values from the returned map from control renderer
294  Property::Value* typeValue = resultMap.Find( "rendererType", Property::STRING );
295  DALI_TEST_CHECK( typeValue );
296  DALI_TEST_CHECK( typeValue->Get<std::string>() == "border" );
297 
298  Property::Value* colorValue = resultMap.Find( "borderColor", Property::VECTOR4 );
299  DALI_TEST_CHECK( colorValue );
300  DALI_TEST_CHECK( colorValue->Get<Vector4>() == Color::BLUE );
301 
302  Property::Value* sizeValue = resultMap.Find( "borderSize", Property::FLOAT );
303  DALI_TEST_CHECK( sizeValue );
304  DALI_TEST_CHECK( sizeValue->Get<float>() == 5.f );
305 
306  borderRenderer = factory.GetControlRenderer( 10.f, Color::CYAN );
307  borderRenderer.CreatePropertyMap( resultMap );
308 
309  typeValue = resultMap.Find( "rendererType", Property::STRING );
310  DALI_TEST_CHECK( typeValue );
311  DALI_TEST_CHECK( typeValue->Get<std::string>() == "border" );
312 
313  colorValue = resultMap.Find( "borderColor", Property::VECTOR4 );
314  DALI_TEST_CHECK( colorValue );
315  DALI_TEST_CHECK( colorValue->Get<Vector4>() == Color::CYAN );
316 
317  colorValue = resultMap.Find( "borderSize", Property::FLOAT );
318  DALI_TEST_CHECK( colorValue );
319  DALI_TEST_CHECK( colorValue->Get<float>() == 10.f );
320 
321  END_TEST;
322 }
323 
325 {
326  ToolkitTestApplication application;
327  tet_infoline( "UtcDaliControlRendererGetPropertyMap3: linear GradientRenderer" );
328 
330  DALI_TEST_CHECK( factory );
331 
332  Property::Map propertyMap;
333  propertyMap.Insert("rendererType", "gradient");
334 
335  Vector2 start(-1.f, -1.f);
336  Vector2 end(1.f, 1.f);
337  propertyMap.Insert("gradientStartPosition", start);
338  propertyMap.Insert("gradientEndPosition", end);
339  propertyMap.Insert("gradientSpreadMethod", "repeat");
340 
341  propertyMap.Insert("gradientStopOffset", Vector2(0.2f, 0.8f));
342 
343  Property::Array stopColors;
344  stopColors.PushBack( Color::RED );
345  stopColors.PushBack( Color::GREEN );
346  propertyMap.Insert("gradientStopColor", stopColors);
347 
348  ControlRenderer gradientRenderer = factory.GetControlRenderer(propertyMap);
349 
350  Property::Map resultMap;
351  gradientRenderer.CreatePropertyMap( resultMap );
352 
353  // check the property values from the returned map from control renderer
354  Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
355  DALI_TEST_CHECK( value );
356  DALI_TEST_CHECK( value->Get<std::string>() == "gradient" );
357 
358  value = resultMap.Find( "gradientUnits", Property::STRING );
359  DALI_TEST_CHECK( value );
360  DALI_TEST_CHECK( value->Get<std::string>() == "objectBoundingBox" );
361 
362  value = resultMap.Find( "gradientSpreadMethod", Property::STRING );
363  DALI_TEST_CHECK( value );
364  DALI_TEST_CHECK( value->Get<std::string>() == "repeat" );
365 
366  value = resultMap.Find( "gradientStartPosition", Property::VECTOR2 );
367  DALI_TEST_CHECK( value );
369 
370  value = resultMap.Find( "gradientEndPosition", Property::VECTOR2 );
371  DALI_TEST_CHECK( value );
373 
374  value = resultMap.Find( "gradientStopOffset", Property::ARRAY );
375  DALI_TEST_CHECK( value );
376  Property::Array* offsetArray = value->GetArray();
377  DALI_TEST_CHECK( offsetArray->Count() == 2 );
378  DALI_TEST_EQUALS( offsetArray->GetElementAt(0).Get<float>(), 0.2f , Math::MACHINE_EPSILON_100, TEST_LOCATION );
379  DALI_TEST_EQUALS( offsetArray->GetElementAt(1).Get<float>(), 0.8f , Math::MACHINE_EPSILON_100, TEST_LOCATION );
380 
381  value = resultMap.Find( "gradientStopColor", Property::ARRAY );
382  DALI_TEST_CHECK( value );
383  Property::Array* colorArray = value->GetArray();
384  DALI_TEST_CHECK( colorArray->Count() == 2 );
385  DALI_TEST_EQUALS( colorArray->GetElementAt(0).Get<Vector4>(), Color::RED , Math::MACHINE_EPSILON_100, TEST_LOCATION );
386  DALI_TEST_EQUALS( colorArray->GetElementAt(1).Get<Vector4>(), Color::GREEN , Math::MACHINE_EPSILON_100, TEST_LOCATION );
387 
388  END_TEST;
389 }
390 
392 {
393  ToolkitTestApplication application;
394  tet_infoline( "UtcDaliControlRendererGetPropertyMap4: radial GradientRenderer" );
395 
397  DALI_TEST_CHECK( factory );
398 
399  Property::Map propertyMap;
400  propertyMap.Insert("rendererType", "gradient");
401 
402  Vector2 center(100.f, 100.f);
403  float radius = 100.f;
404  propertyMap.Insert("gradientUnits", "userSpace");
405  propertyMap.Insert("gradientCenter", center);
406  propertyMap.Insert("gradientRadius", radius);
407  propertyMap.Insert("gradientStopOffset", Vector3(0.1f, 0.3f, 1.1f));
408 
409  Property::Array stopColors;
410  stopColors.PushBack( Color::RED );
411  stopColors.PushBack( Color::BLACK );
412  stopColors.PushBack( Color::GREEN );
413  propertyMap.Insert("gradientStopColor", stopColors);
414 
415  ControlRenderer gradientRenderer = factory.GetControlRenderer(propertyMap);
416  DALI_TEST_CHECK( gradientRenderer );
417 
418  Property::Map resultMap;
419  gradientRenderer.CreatePropertyMap( resultMap );
420 
421  // check the property values from the returned map from control renderer
422  Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
423  DALI_TEST_CHECK( value );
424  DALI_TEST_CHECK( value->Get<std::string>() == "gradient" );
425 
426  value = resultMap.Find( "gradientUnits", Property::STRING );
427  DALI_TEST_CHECK( value );
428  DALI_TEST_CHECK( value->Get<std::string>() == "userSpace" );
429 
430  value = resultMap.Find( "gradientSpreadMethod", Property::STRING );
431  DALI_TEST_CHECK( value );
432  DALI_TEST_CHECK( value->Get<std::string>() == "pad" );
433 
434  value = resultMap.Find( "gradientCenter", Property::VECTOR2 );
435  DALI_TEST_CHECK( value );
437 
438  value = resultMap.Find( "gradientRadius", Property::FLOAT );
439  DALI_TEST_CHECK( value );
440  DALI_TEST_EQUALS( value->Get<float>(), radius , Math::MACHINE_EPSILON_100, TEST_LOCATION );
441 
442  value = resultMap.Find( "gradientStopOffset", Property::ARRAY );
443  DALI_TEST_CHECK( value );
444  Property::Array* offsetArray = value->GetArray();
445  DALI_TEST_CHECK( offsetArray->Count() == 3 );
446  DALI_TEST_EQUALS( offsetArray->GetElementAt(0).Get<float>(), 0.1f , Math::MACHINE_EPSILON_100, TEST_LOCATION );
447  DALI_TEST_EQUALS( offsetArray->GetElementAt(1).Get<float>(), 0.3f , Math::MACHINE_EPSILON_100, TEST_LOCATION );
448  // any stop value will be clamped to [0.0, 1.0];
449  DALI_TEST_EQUALS( offsetArray->GetElementAt(2).Get<float>(), 1.0f , Math::MACHINE_EPSILON_100, TEST_LOCATION );
450 
451  value = resultMap.Find( "gradientStopColor", Property::ARRAY );
452  DALI_TEST_CHECK( value );
453  Property::Array* colorArray = value->GetArray();
454  DALI_TEST_CHECK( colorArray->Count() == 3 );
455  DALI_TEST_EQUALS( colorArray->GetElementAt(0).Get<Vector4>(), Color::RED , Math::MACHINE_EPSILON_100, TEST_LOCATION );
456  DALI_TEST_EQUALS( colorArray->GetElementAt(1).Get<Vector4>(), Color::BLACK , Math::MACHINE_EPSILON_100, TEST_LOCATION );
457  DALI_TEST_EQUALS( colorArray->GetElementAt(2).Get<Vector4>(), Color::GREEN , Math::MACHINE_EPSILON_100, TEST_LOCATION );
458 
459  END_TEST;
460 }
461 
463 {
464  ToolkitTestApplication application;
465  tet_infoline( "UtcDaliControlRendererGetPropertyMap5: ImageRenderer" );
466 
468  Property::Map propertyMap;
469  propertyMap.Insert( "rendererType", "image" );
470  propertyMap.Insert( "imageUrl", TEST_IMAGE_FILE_NAME );
471  propertyMap.Insert( "imageDesiredWidth", 20 );
472  propertyMap.Insert( "imageDesiredHeight", 30 );
473  propertyMap.Insert( "imageFittingMode", "fitHeight" );
474  propertyMap.Insert( "imageSamplingMode", "boxThenNearest" );
475 
476  ControlRenderer imageRenderer = factory.GetControlRenderer(propertyMap);
477  DALI_TEST_CHECK( imageRenderer );
478 
479  Property::Map resultMap;
480  imageRenderer.CreatePropertyMap( resultMap );
481 
482  // check the property values from the returned map from control renderer
483  Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
484  DALI_TEST_CHECK( value );
485  DALI_TEST_CHECK( value->Get<std::string>() == "image" );
486 
487  value = resultMap.Find( "imageUrl", Property::STRING );
488  DALI_TEST_CHECK( value );
489  DALI_TEST_CHECK( value->Get<std::string>() == TEST_IMAGE_FILE_NAME );
490 
491  value = resultMap.Find( "imageFittingMode", Property::STRING );
492  DALI_TEST_CHECK( value );
493  DALI_TEST_CHECK( value->Get<std::string>() == "fitHeight" );
494 
495  value = resultMap.Find( "imageSamplingMode", Property::STRING );
496  DALI_TEST_CHECK( value );
497  DALI_TEST_CHECK( value->Get<std::string>() == "boxThenNearest" );
498 
499  value = resultMap.Find( "imageDesiredWidth", Property::INTEGER );
500  DALI_TEST_CHECK( value );
501  DALI_TEST_CHECK( value->Get<int>() == 20 );
502 
503  value = resultMap.Find( "imageDesiredHeight", Property::INTEGER );
504  DALI_TEST_CHECK( value );
505  DALI_TEST_CHECK( value->Get<int>() == 30 );
506 
507  // Get an image renderer with an image handle, and test the default property values
509  imageRenderer = factory.GetControlRenderer(image);
510  imageRenderer.CreatePropertyMap( resultMap );
511 
512  value = resultMap.Find( "rendererType", Property::STRING );
513  DALI_TEST_CHECK( value );
514  DALI_TEST_CHECK( value->Get<std::string>() == "image" );
515 
516  value = resultMap.Find( "imageUrl", Property::STRING );
517  DALI_TEST_CHECK( value );
518  DALI_TEST_CHECK( value->Get<std::string>() == TEST_IMAGE_FILE_NAME );
519 
520  value = resultMap.Find( "imageFittingMode", Property::STRING );
521  DALI_TEST_CHECK( value );
522  DALI_TEST_CHECK( value->Get<std::string>() == "shrinkToFit" );
523 
524  value = resultMap.Find( "imageSamplingMode", Property::STRING );
525  DALI_TEST_CHECK( value );
526  DALI_TEST_CHECK( value->Get<std::string>() == "box" );
527 
528  value = resultMap.Find( "imageDesiredWidth", Property::INTEGER );
529  DALI_TEST_CHECK( value );
530  DALI_TEST_CHECK( value->Get<int>() == 100 );
531 
532  value = resultMap.Find( "imageDesiredHeight", Property::INTEGER );
533  DALI_TEST_CHECK( value );
534  DALI_TEST_CHECK( value->Get<int>() == 200 );
535 
536  END_TEST;
537 }
538 
540 {
541  ToolkitTestApplication application;
542  tet_infoline( "UtcDaliControlRendererGetPropertyMap6: NPatchRenderer" );
543 
545  Property::Map propertyMap;
546  propertyMap.Insert( "rendererType", "nPatch" );
547  propertyMap.Insert( "imageUrl", TEST_NPATCH_FILE_NAME );
548  propertyMap.Insert( "borderOnly", true );
549  ControlRenderer nPatchRenderer = factory.GetControlRenderer( propertyMap );
550 
551  Property::Map resultMap;
552  nPatchRenderer.CreatePropertyMap( resultMap );
553 
554  // check the property values from the returned map from control renderer
555  Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
556  DALI_TEST_CHECK( value );
557  DALI_TEST_CHECK( value->Get<std::string>() == "nPatch" );
558 
559  value = resultMap.Find( "imageUrl", Property::STRING );
560  DALI_TEST_CHECK( value );
561  DALI_TEST_CHECK( value->Get<std::string>() == TEST_NPATCH_FILE_NAME );
562 
563  value = resultMap.Find( "borderOnly", Property::BOOLEAN );
564  DALI_TEST_CHECK( value );
565  DALI_TEST_CHECK( value->Get<bool>() );
566 
567  END_TEST;
568 }
Dali Docs Home
Read more about Dali