Updated visuals to add animatable mixColor property
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-VisualFactory.cpp
index 7d36ae6..0b1be30 100644 (file)
 #include <iostream>
 #include <stdlib.h>
 #include <dali-toolkit-test-suite-utils.h>
+#include <toolkit-timer.h>
 #include <toolkit-bitmap-loader.h>
 #include <toolkit-event-thread-callback.h>
 #include <dali/public-api/rendering/renderer.h>
 #include <dali/public-api/rendering/texture-set.h>
 #include <dali/public-api/rendering/shader.h>
 #include <dali/devel-api/images/nine-patch-image.h>
+#include <dali-toolkit/devel-api/align-enums.h>
+#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
 #include <dali-toolkit/dali-toolkit.h>
 #include "dummy-control.h"
@@ -42,11 +45,26 @@ const char* TEST_MTL_FILE_NAME = TEST_RESOURCE_DIR "/ToyRobot-Metal.mtl";
 const char* TEST_SIMPLE_OBJ_FILE_NAME = TEST_RESOURCE_DIR "/Cube-Points-Only.obj";
 const char* TEST_SIMPLE_MTL_FILE_NAME = TEST_RESOURCE_DIR "/ToyRobot-Metal-Simple.mtl";
 
+// resolution: 50*50, frame count: 4, frame delay: 0.2 second for each frame
+const char* TEST_GIF_FILE_NAME = TEST_RESOURCE_DIR "/anim.gif";
+
 // resolution: 34*34, pixel format: RGBA8888
 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
 // resolution: 600*600, pixel format: RGB888
 static const char* gImage_600_RGB = TEST_RESOURCE_DIR "/test-image-600.jpg";
 
+Property::Map DefaultTransform()
+{
+  Property::Map transformMap;
+  transformMap
+    .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
+    .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
+    .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
+    .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
+    .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
+  return transformMap;
+}
+
 Integration::Bitmap* CreateBitmap( unsigned int imageWidth, unsigned int imageHeight, unsigned int initialColor, Pixel::Format pixelFormat )
 {
   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
@@ -372,7 +390,7 @@ int UtcDaliVisualFactoryGetBorderVisual1(void)
   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
   actor.SetSize(200.f, 200.f);
   Stage::GetCurrent().Add( actor );
-  visual.SetSize(Vector2(200.f, 200.f));
+  visual.SetTransformAndSize(DefaultTransform(), Vector2(200.f, 200.f));
 
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
   int blendMode = actor.GetRendererAt(0u).GetProperty<int>( Renderer::Property::BLEND_MODE );
@@ -420,7 +438,7 @@ int UtcDaliVisualFactoryGetBorderVisual2(void)
   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
   actor.SetSize(200.f, 200.f);
   Stage::GetCurrent().Add( actor );
-  visual.SetSize(Vector2(200.f, 200.f));
+  visual.SetTransformAndSize(DefaultTransform(), Vector2(200.f, 200.f));
 
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
 
@@ -971,9 +989,8 @@ int UtcDaliVisualFactoryGetNPatchVisual3(void)
 
   ResourceImage image = ResourceImage::New(TEST_NPATCH_FILE_NAME);
   Visual::Base nPatchVisual = factory.CreateVisual( image );
-  Vector2 visualSize( 20.f, 30.f ), naturalSize(0,0);
-  nPatchVisual.SetSize( visualSize );
-  DALI_TEST_EQUALS( nPatchVisual.GetSize(), visualSize, TEST_LOCATION );
+  Vector2 controlSize( 20.f, 30.f ), naturalSize(0,0);
+  nPatchVisual.SetTransformAndSize(DefaultTransform(), controlSize );
   nPatchVisual.GetNaturalSize( naturalSize );
   DALI_TEST_EQUALS( naturalSize, Vector2( ninePatchImageWidth-2, ninePatchImageHeight-2 ), TEST_LOCATION );
 
@@ -1124,7 +1141,7 @@ int UtcDaliVisualFactoryGetSvgVisual(void)
   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
   actor.SetSize( 200.f, 200.f );
   Stage::GetCurrent().Add( actor );
-  visual.SetSize( Vector2(200.f, 200.f) );
+  visual.SetTransformAndSize(DefaultTransform(), Vector2(200.f, 200.f) );
 
   application.SendNotification();
   application.Render();
@@ -1132,11 +1149,7 @@ int UtcDaliVisualFactoryGetSvgVisual(void)
   // renderer is not added to actor until the rasterization is completed.
   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
 
-  EventThreadCallback* eventTrigger = EventThreadCallback::Get();
-  CallbackBase* callback = eventTrigger->GetCallback();
-
-  eventTrigger->WaitingForTrigger( 1 );// waiting until the svg image is rasterized.
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
 
   // renderer is added to actor
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
@@ -1163,8 +1176,9 @@ int UtcDaliVisualFactoryGetSvgVisualLarge(void)
   TraceCallStack& textureTrace = gl.GetTextureTrace();
   textureTrace.Enable(true);
 
-  DummyControl actor = DummyControl::New();
+  DummyControl actor = DummyControl::New(true);
   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
+  actor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); // Only rasterizes when it knows control size.
   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
   Stage::GetCurrent().Add( actor );
 
@@ -1174,11 +1188,7 @@ int UtcDaliVisualFactoryGetSvgVisualLarge(void)
   // renderer is not added to actor until the rasterization is completed.
   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
 
-  EventThreadCallback* eventTrigger = EventThreadCallback::Get();
-  CallbackBase* callback = eventTrigger->GetCallback();
-
-  eventTrigger->WaitingForTrigger( 1 );// waiting until the svg image is rasterized.
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
 
   // renderer is added to actor
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
@@ -1209,7 +1219,7 @@ void MeshVisualLoadsCorrectlyTest( Property::Map& propertyMap, ToolkitTestApplic
   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
   actor.SetSize( 200.f, 200.f );
   Stage::GetCurrent().Add( actor );
-  visual.SetSize( Vector2( 200.f, 200.f ) );
+  visual.SetTransformAndSize(DefaultTransform(), Vector2( 200.f, 200.f ) );
 
   //Ensure set on stage.
   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
@@ -1256,7 +1266,7 @@ void MeshVisualDoesNotLoadCorrectlyTest( Property::Map& propertyMap, ToolkitTest
   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
   actor.SetSize( 200.f, 200.f );
   Stage::GetCurrent().Add( actor );
-  visual.SetSize( Vector2( 200.f, 200.f ) );
+  visual.SetTransformAndSize(DefaultTransform(),  Vector2( 200.f, 200.f ) );
 
   //Ensure set on stage.
   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
@@ -1324,6 +1334,30 @@ int UtcDaliVisualFactoryGetMeshVisual2(void)
   END_TEST;
 }
 
+//Test if mesh loads correctly when supplied with all main parameters, an object file, a material file and a directory location, but duff optional parameters
+int UtcDaliVisualFactoryGetMeshVisual3b(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliVisualFactoryGetMeshVisual3:  Request mesh visual with all parameters correct" );
+
+  //Set up visual properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( Visual::Property::TYPE, Visual::MESH );
+  propertyMap.Insert( MeshVisual::Property::OBJECT_URL, TEST_OBJ_FILE_NAME );
+  propertyMap.Insert( MeshVisual::Property::MATERIAL_URL, TEST_MTL_FILE_NAME );
+  propertyMap.Insert( MeshVisual::Property::USE_MIPMAPPING, Color::GREEN ); // Test that wrong property types don't prevent the object load
+  propertyMap.Insert( MeshVisual::Property::USE_SOFT_NORMALS, 1.0f );
+  propertyMap.Insert( MeshVisual::Property::LIGHT_POSITION, 1.0f );
+  propertyMap.Insert( MeshVisual::Property::TEXTURES_PATH, TEST_RESOURCE_DIR "/" );
+
+  //Test to see if mesh loads correctly.
+  MeshVisualLoadsCorrectlyTest( propertyMap, application );
+
+  END_TEST;
+}
+
 //Test if mesh loads correctly when supplied with all main parameters, an object file, a material file and a directory location.
 int UtcDaliVisualFactoryGetMeshVisual3(void)
 {
@@ -1337,6 +1371,9 @@ int UtcDaliVisualFactoryGetMeshVisual3(void)
   propertyMap.Insert( Visual::Property::TYPE, Visual::MESH );
   propertyMap.Insert( MeshVisual::Property::OBJECT_URL, TEST_OBJ_FILE_NAME );
   propertyMap.Insert( MeshVisual::Property::MATERIAL_URL, TEST_MTL_FILE_NAME );
+  propertyMap.Insert( MeshVisual::Property::USE_MIPMAPPING, false );
+  propertyMap.Insert( MeshVisual::Property::USE_SOFT_NORMALS, false );
+  propertyMap.Insert( MeshVisual::Property::LIGHT_POSITION, Vector3::XAXIS );
   propertyMap.Insert( MeshVisual::Property::TEXTURES_PATH, TEST_RESOURCE_DIR "/" );
 
   //Test to see if mesh loads correctly.
@@ -1364,7 +1401,6 @@ int UtcDaliVisualFactoryGetMeshVisual4(void)
   //Test to see if mesh loads correctly.
   MeshVisualLoadsCorrectlyTest( propertyMap, application );
 
-
   END_TEST;
 }
 
@@ -1379,10 +1415,13 @@ int UtcDaliVisualFactoryGetMeshVisual5(void)
   //Set up visual properties.
   Property::Map propertyMap;
   propertyMap.Insert( Visual::Property::TYPE, Visual::MESH );
-  propertyMap.Insert( MeshVisual::Property::OBJECT_URL, TEST_OBJ_FILE_NAME );
-  propertyMap.Insert( MeshVisual::Property::MATERIAL_URL, TEST_MTL_FILE_NAME );
-  propertyMap.Insert( MeshVisual::Property::TEXTURES_PATH, TEST_RESOURCE_DIR "/" );
-  propertyMap.Insert( MeshVisual::Property::SHADING_MODE, MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING );
+  propertyMap.Insert( "objectUrl", TEST_OBJ_FILE_NAME );
+  propertyMap.Insert( "materialUrl", TEST_MTL_FILE_NAME );
+  propertyMap.Insert( "texturesPath", TEST_RESOURCE_DIR "/" );
+  propertyMap.Insert( "useMipmapping", false );
+  propertyMap.Insert( "useSoftNormals", false );
+  propertyMap.Insert( "lightPosition", Vector3::ZAXIS );
+  propertyMap.Insert( "shadingMode", MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING );
 
   //Test to see if mesh loads correctly.
   MeshVisualLoadsCorrectlyTest( propertyMap, application );
@@ -1538,7 +1577,7 @@ void TestPrimitiveVisualWithProperties( Property::Map& propertyMap, ToolkitTestA
 
   actor.SetSize( 200.f, 200.f );
   Stage::GetCurrent().Add( actor );
-  visual.SetSize( Vector2( 200.f, 200.f ) );
+  visual.SetTransformAndSize(DefaultTransform(),  Vector2( 200.f, 200.f ) );
 
   //Ensure set on stage.
   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
@@ -1959,7 +1998,7 @@ int UtcDaliVisualFactoryGetBatchImageVisual1(void)
 
   actor.SetSize( 200.0f, 200.0f );
   Stage::GetCurrent().Add( actor );
-  visual.SetSize( Vector2( 200.0f, 200.0f ) );
+  visual.SetTransformAndSize(DefaultTransform(),  Vector2( 200.0f, 200.0f ) );
 
   // Test SetOnStage().
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
@@ -2008,7 +2047,7 @@ int UtcDaliVisualFactoryGetBatchImageVisual2(void)
 
   actor.SetSize( 200.0f, 200.0f );
   Stage::GetCurrent().Add( actor );
-  visual.SetSize( Vector2( 200.0f, 200.0f ) );
+  visual.SetTransformAndSize(DefaultTransform(),  Vector2( 200.0f, 200.0f ) );
 
   // Test SetOnStage().
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
@@ -2033,8 +2072,35 @@ int UtcDaliVisualFactoryGetBatchImageVisual3(void)
 
   // Create a property-map that enables batching.
   Property::Map propertyMap;
-  propertyMap.Insert( Dali::Toolkit::ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
-  propertyMap.Insert( ImageVisual::Property::BATCHING_ENABLED, true );
+  propertyMap[ Dali::Toolkit::ImageVisual::Property::URL ] = TEST_IMAGE_FILE_NAME ;
+  propertyMap[ "desiredHeight" ] = 200;
+  propertyMap[ "desiredWidth" ] = 200;
+  propertyMap[ "batchingEnabled" ] = true;
+
+  // Create an ImageView, passing the property-map in to instruct it to use batching.
+  Toolkit::ImageView imageView = Toolkit::ImageView::New();
+  imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, propertyMap );
+
+  imageView.SetSize( 200.0f, 200.0f );
+  Stage::GetCurrent().Add( imageView );
+
+  END_TEST;
+}
+
+int UtcDaliVisualFactoryGetBatchImageVisual4N(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline( "UtcDaliVisualFactoryGetBatchImageVisual4: Create an ImageView that uses a batched visual, with desired properties of the wrong type" );
+
+  VisualFactory factory = VisualFactory::Get();
+  DALI_TEST_CHECK( factory );
+
+  // Create a property-map that enables batching.
+  Property::Map propertyMap;
+  propertyMap[ Dali::Toolkit::ImageVisual::Property::URL ] = TEST_IMAGE_FILE_NAME ;
+  propertyMap[ "desiredHeight" ] = Vector2(100, 100);
+  propertyMap[ "desiredWidth" ] = Vector3(1, 1, 1);
+  propertyMap[ "batchingEnabled" ] = true;
 
   // Create an ImageView, passing the property-map in to instruct it to use batching.
   Toolkit::ImageView imageView = Toolkit::ImageView::New();
@@ -2045,3 +2111,82 @@ int UtcDaliVisualFactoryGetBatchImageVisual3(void)
 
   END_TEST;
 }
+
+int UtcDaliVisualFactoryGetAnimatedImageVisual(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline( "UtcDaliVisualFactoryGetAnimatedImageVisual: Request animated image visual with a gif url" );
+
+  VisualFactory factory = VisualFactory::Get();
+  Visual::Base visual = factory.CreateVisual( TEST_GIF_FILE_NAME, ImageDimensions() );
+  DALI_TEST_CHECK( visual );
+
+  TestGlAbstraction& gl = application.GetGlAbstraction();
+  TraceCallStack& textureTrace = gl.GetTextureTrace();
+  textureTrace.Enable(true);
+
+  DummyControl actor = DummyControl::New();
+  DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
+  dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
+  Stage::GetCurrent().Add( actor );
+
+  application.SendNotification();
+  application.Render();
+
+  // renderer is added to actor
+  DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
+
+  // test the uniforms which used to handle the atlas rect
+  // the four frames should be located inside atlas as follows: atlas size 100*100
+  // -------------
+  // |     |     |
+  // |  0  |  1  |
+  // -------------
+  // |     |     |
+  // |  2  |  3  |
+  // -------------
+
+  Renderer renderer = actor.GetRendererAt( 0u );
+  DALI_TEST_CHECK( renderer );
+
+  Property::Value atlasRectValue = renderer.GetProperty( renderer.GetPropertyIndex( "uAtlasRect" ) );
+  // take into consideration the half pixel correction
+  DALI_TEST_EQUALS( atlasRectValue.Get<Vector4>(), Vector4(0.5f, 0.5f, 49.5f, 49.5f)/100.f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  // waiting for the resource uploading
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
+
+  // Force the timer used by the animatedImageVisual to tick,
+  Dali::Timer timer = Timer::New( 0 );
+  timer.MockEmitSignal();
+  application.SendNotification();
+  application.Render();
+  atlasRectValue = renderer.GetProperty( renderer.GetPropertyIndex( "uAtlasRect" ) );
+  // take into consideration the half pixel correction
+  DALI_TEST_EQUALS( atlasRectValue.Get<Vector4>(), Vector4(50.5f, 0.5f, 99.5f, 49.5f)/100.f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  // Force the timer used by the animatedImageVisual to tick,
+  timer.MockEmitSignal();
+  application.SendNotification();
+  application.Render();
+  atlasRectValue = renderer.GetProperty( renderer.GetPropertyIndex( "uAtlasRect" ) );
+  // take into consideration the half pixel correction
+  DALI_TEST_EQUALS( atlasRectValue.Get<Vector4>(), Vector4(0.5f, 50.5f, 49.5f, 99.5f)/100.f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  // Force the timer used by the animatedImageVisual to tick,
+  timer.MockEmitSignal();
+  application.SendNotification();
+  application.Render();
+  atlasRectValue = renderer.GetProperty( renderer.GetPropertyIndex( "uAtlasRect" ) );
+  // take into consideration the half pixel correction
+  DALI_TEST_EQUALS( atlasRectValue.Get<Vector4>(), Vector4(50.5f, 50.5f, 99.5f, 99.5f)/100.f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  // Test SetOffStage().
+  actor.Unparent();
+  DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
+
+  END_TEST;
+}