[dali_1.4.56] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-Visual.cpp
index 9c817b6..d1df2ac 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <stdlib.h>
 #include <unistd.h>
 
+#include <toolkit-event-thread-callback.h>
 #include <dali-toolkit-test-suite-utils.h>
 #include <dali/devel-api/object/handle-devel.h>
+#include <dali/devel-api/text-abstraction/font-client.h>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
@@ -275,7 +277,7 @@ int UtcDaliVisualSetGetDepthIndex(void)
 int UtcDaliVisualSize(void)
 {
   ToolkitTestApplication application;
-  tet_infoline( "UtcDaliVisualGetNaturalSize" );
+  tet_infoline( "UtcDaliVisualSize" );
 
   VisualFactory factory = VisualFactory::Get();
   Vector2 controlSize( 20.f, 30.f );
@@ -344,7 +346,6 @@ int UtcDaliVisualSize(void)
 
   // svg visual
   Visual::Base svgVisual = factory.CreateVisual( TEST_SVG_FILE_NAME, ImageDimensions() );
-  svgVisual.SetTransformAndSize(DefaultTransform(), controlSize );
   svgVisual.GetNaturalSize(naturalSize);
   // TEST_SVG_FILE:
   //  <svg width="100" height="100">
@@ -369,18 +370,29 @@ int UtcDaliVisualSize(void)
 
   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf" );
 
+  // Create a TextVisual with a font size of 12 first
   propertyMap.Clear();
   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::TEXT );
   propertyMap.Insert( TextVisual::Property::ENABLE_MARKUP, true );
   propertyMap.Insert( TextVisual::Property::TEXT, "<font family='TizenSans' size='12'>Hello world</font>" );
   propertyMap.Insert( TextVisual::Property::MULTI_LINE, true );
 
-  Visual::Base textVisual = factory.CreateVisual( propertyMap );
-  textVisual.GetNaturalSize( naturalSize );
-  DALI_TEST_EQUALS( naturalSize, Size( 80.f, 20.f ), TEST_LOCATION );
+  Visual::Base smallTextVisual = factory.CreateVisual( propertyMap );
+  Vector2 smallTextVisualNaturalSize;
+  smallTextVisual.GetNaturalSize( smallTextVisualNaturalSize );
+
+  // Then create a TextVisual with a font size of 20
+  propertyMap[ TextVisual::Property::TEXT ] = "<font family='TizenSans' size='20'>Hello world</font>";
+  Visual::Base largeTextVisual = factory.CreateVisual( propertyMap );
+  Vector2 largeTextVisualNaturalSize;
+  largeTextVisual.GetNaturalSize( largeTextVisualNaturalSize );
+
+  // Compare the sizes of the two text visuals, the second one should be bigger as it has a larger point size in the markup.
+  DALI_TEST_CHECK( smallTextVisualNaturalSize.width < largeTextVisualNaturalSize.width &&
+                   smallTextVisualNaturalSize.height < largeTextVisualNaturalSize.height );
 
-  const float height = textVisual.GetHeightForWidth( 40.f );
-  DALI_TEST_EQUALS( height, 40.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+  // The height returned for a particular width should also be greater for the large text visual
+  DALI_TEST_CHECK( smallTextVisual.GetHeightForWidth( 40.f ) < largeTextVisual.GetHeightForWidth( 40.f ) );
 
   //AnimatedImageVisual
   Visual::Base animatedImageVisual = factory.CreateVisual( TEST_GIF_FILE_NAME, ImageDimensions() );
@@ -396,7 +408,7 @@ int UtcDaliVisualSize(void)
 int UtcDaliVisualSetOnOffStage(void)
 {
   ToolkitTestApplication application;
-  tet_infoline( "UtcDaliVisualSetDepthIndex" );
+  tet_infoline( "UtcDaliVisualSetOnOffStage" );
 
   VisualFactory factory = VisualFactory::Get();
   Property::Map propertyMap;
@@ -429,6 +441,64 @@ int UtcDaliVisualSetOnOffStage(void)
   END_TEST;
 }
 
+int UtcDaliVisualSetOnOffStage2(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline( "UtcDaliVisualSetOnOffStage2" );
+
+  VisualFactory factory = VisualFactory::Get();
+  Property::Map propertyMap;
+  propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::SVG );
+  propertyMap.Insert( ImageVisual::Property::URL,  TEST_SVG_FILE_NAME );
+  Visual::Base visual = factory.CreateVisual( propertyMap );
+
+  DummyControl actor = DummyControl::New(true);
+  Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
+  dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
+
+  actor.SetSize(200.f, 200.f);
+
+  application.SendNotification();
+  application.Render(0);
+  DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
+
+  // First on/off
+  Stage::GetCurrent().Add( actor );
+
+  application.SendNotification();
+  application.Render(0);
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
+  DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
+  Renderer renderer = actor.GetRendererAt( 0 );
+  auto textures = renderer.GetTextures();
+  DALI_TEST_CHECK( textures.GetTextureCount() != 0u );
+
+  Stage::GetCurrent().Remove( actor );
+
+  application.SendNotification();
+  application.Render(0);
+  DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
+
+  // Second on/off
+  Stage::GetCurrent().Add( actor );
+
+  application.SendNotification();
+  application.Render(0);
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
+  DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
+  renderer = actor.GetRendererAt( 0 );
+  textures = renderer.GetTextures();
+  DALI_TEST_CHECK( textures.GetTextureCount() != 0u );
+
+  Stage::GetCurrent().Remove( actor );
+
+  application.SendNotification();
+  application.Render(0);
+  DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
+
+  END_TEST;
+}
+
 int UtcDaliVisualGetPropertyMap1(void)
 {
   ToolkitTestApplication application;
@@ -1123,7 +1193,7 @@ int UtcDaliVisualGetPropertyMap10(void)
   propertyMap.Insert( "shadow", shadowMapSet.Add("color", Color::RED).Add("offset", Vector2(2.0f, 2.0f)).Add("blurRadius", 3.0f) );
 
   Property::Map underlineMapSet;
-  propertyMap.Insert( "underline", underlineMapSet.Add("enable", "true").Add("color", "green").Add("height", "1") );
+  propertyMap.Insert( "underline", underlineMapSet.Add("enable", true).Add("color", Color::GREEN).Add("height", 1) );
 
   Property::Map outlineMapSet;
   propertyMap.Insert( "outline", outlineMapSet.Add("color", Color::YELLOW).Add("width", 1) );
@@ -3224,7 +3294,7 @@ int UtcDaliVisualPremultipliedAlpha(void)
 
   VisualFactory factory = VisualFactory::Get();
 
-  // image visual, test default value ( false )
+  // image visual, test default value ( true )
   {
     Visual::Base imageVisual = factory.CreateVisual(
           Property::Map()
@@ -3237,7 +3307,7 @@ int UtcDaliVisualPremultipliedAlpha(void)
 
     // test values
     DALI_TEST_CHECK( value );
-    DALI_TEST_EQUALS( value->Get<bool>(), false, TEST_LOCATION );
+    DALI_TEST_EQUALS( value->Get<bool>(), true, TEST_LOCATION );
   }
 
   // image visual, override premultiplied
@@ -3246,7 +3316,7 @@ int UtcDaliVisualPremultipliedAlpha(void)
           Property::Map()
           .Add( Toolkit::Visual::Property::TYPE, Visual::IMAGE )
           .Add( ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME )
-          .Add( Visual::Property::PREMULTIPLIED_ALPHA, true ) );
+          .Add( Visual::Property::PREMULTIPLIED_ALPHA, false ) );
 
     Dali::Property::Map visualMap;
     imageVisual.CreatePropertyMap( visualMap );
@@ -3254,7 +3324,7 @@ int UtcDaliVisualPremultipliedAlpha(void)
 
     // test values
     DALI_TEST_CHECK( value );
-    DALI_TEST_EQUALS( value->Get<bool>(), true, TEST_LOCATION);
+    DALI_TEST_EQUALS( value->Get<bool>(), false, TEST_LOCATION);
   }
 
   // svg visual ( premultiplied alpha by default is true )
@@ -3455,3 +3525,51 @@ int UtcDaliColorVisualRenderIfTransparentProperty(void)
 
   END_TEST;
 }
+
+int UtcDaliSvgVisualCustomShader(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline( "SvgVisual with custom shader" );
+
+  VisualFactory factory = VisualFactory::Get();
+  Property::Map properties;
+  Property::Map shader;
+  const std::string vertexShader = "Foobar";
+  const std::string fragmentShader = "Foobar";
+  shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+  shader[Dali::Toolkit::Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
+
+  properties[Visual::Property::TYPE] = Visual::IMAGE;
+  properties[Visual::Property::SHADER] = shader;
+  properties[ImageVisual::Property::URL] = TEST_SVG_FILE_NAME;
+
+  Visual::Base visual = factory.CreateVisual( properties );
+
+  // trigger creation through setting on stage
+  DummyControl dummy = DummyControl::New( true );
+  Impl::DummyControl& dummyImpl = static_cast< Impl::DummyControl& >( dummy.GetImplementation() );
+  dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
+
+  dummy.SetSize( 200.f, 200.f );
+  dummy.SetParentOrigin( ParentOrigin::CENTER );
+  Stage::GetCurrent().Add( dummy );
+
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
+
+  Renderer renderer = dummy.GetRendererAt( 0 );
+  Shader shader2 = renderer.GetShader();
+  Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+  Property::Map* map = value.GetMap();
+  DALI_TEST_CHECK( map );
+
+  Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+  DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
+
+  Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
+  DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
+
+  END_TEST;
+}