Add new layouting support for TextLabel and ImageView. 35/183135/2
authorAnton Obzhirov <a.obzhirov@samsung.com>
Mon, 2 Jul 2018 17:13:56 +0000 (18:13 +0100)
committerAnton Obzhirov <a.obzhirov@samsung.com>
Wed, 4 Jul 2018 09:29:40 +0000 (10:29 +0100)
Change-Id: Ic4e72c3abc397277df72f14ce2af7057536ef4d0

automated-tests/src/dali-toolkit/dali-toolkit-test-utils/layout-utils.cpp
automated-tests/src/dali-toolkit/dali-toolkit-test-utils/layout-utils.h
automated-tests/src/dali-toolkit/utc-Dali-FlexLayout.cpp
automated-tests/src/dali-toolkit/utc-Dali-Layouting.cpp
dali-toolkit/devel-api/controls/control-devel.cpp
dali-toolkit/devel-api/controls/control-devel.h
dali-toolkit/internal/controls/image-view/image-view-impl.cpp
dali-toolkit/internal/controls/text-controls/text-label-impl.cpp

index 54ff302..81fd6bf 100755 (executable)
@@ -33,20 +33,24 @@ namespace Dali
 namespace Toolkit
 {
 
-Control CreateLeafControl( int width, int height )
+std::string CreateImageURL( Vector4 color, ImageDimensions size )
 {
-  auto control = Control::New();
-  control.SetName( "Leaf" );
-
-  auto pixelBuffer = Devel::PixelBuffer::New( 1, 1, Pixel::RGB888 );
+  auto pixelBuffer = Devel::PixelBuffer::New( size.GetWidth(), size.GetHeight(), Pixel::RGB888 );
   unsigned char* pixels = pixelBuffer.GetBuffer();
-  pixels[0] = 0xff;
-  pixels[1] = 0x00;
-  pixels[2] = 0x00;
-  auto texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGB888, 1, 1 );
+  pixels[0] = static_cast<unsigned char>( color.r );
+  pixels[1] = static_cast<unsigned char>( color.g );
+  pixels[2] = static_cast<unsigned char>( color.b );
+  auto texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGB888, size.GetWidth(), size.GetHeight() );
   auto pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
   texture.Upload( pixelData );
-  std::string url = TextureManager::AddTexture( texture );
+  return TextureManager::AddTexture( texture );
+}
+
+Control CreateLeafControl( int width, int height )
+{
+  auto control = Control::New();
+  control.SetName( "Leaf" );
+  std::string url = CreateImageURL( Vector4( 255, 0, 0, 255 ), ImageDimensions( 1, 1 ) );
 
   Property::Map map;
   map[ Visual::Property::TYPE ] = Visual::IMAGE;
@@ -57,6 +61,24 @@ Control CreateLeafControl( int width, int height )
   return control;
 }
 
+TextLabel CreateTextLabel( const char* text )
+{
+  TextLabel textLabel = TextLabel::New(text);
+  textLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
+  textLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
+  textLabel.SetName( "TextLabel" );
+  textLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  return textLabel;
+}
+
+ImageView CreateImageView( std::string& url, ImageDimensions size )
+{
+  ImageView imageView = ImageView::New( url, size );
+  imageView.SetName( "ImageView" );
+  imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  return imageView;
+}
+
 } // namespace Toolkit
 
 } // namespace Dali
index c0ef58f..6072140 100644 (file)
@@ -27,6 +27,15 @@ namespace Toolkit
 {
 
 /**
+ * @brief Creates an empty image buffer filled with the color for layout tests
+ *
+ * @param[in] color The color of the image buffer.
+ * @param[in] size The size of the image buffer.
+ * @param[out] string The url.
+ */
+std::string CreateImageURL( Vector4 color, ImageDimensions size );
+
+/**
  * @brief Creates an empty leaf control filled with red color for layout tests
  *
  * @param[in] width The width of the control.
@@ -35,6 +44,23 @@ namespace Toolkit
  */
 Control CreateLeafControl( int width, int height );
 
+/**
+ * @brief Creates a text label for layout tests
+ *
+ * @param[in] text The text of the label.
+ * @param[out] TextLabel The text label.
+ */
+TextLabel CreateTextLabel( const char* text );
+
+/**
+ * @brief Creates an image view for layout tests
+ *
+ * @param[in] url The image url.
+ * @param[in] size The size of the image view.
+ * @param[out] ImageView The image view.
+ */
+ImageView CreateImageView( std::string& url, ImageDimensions size );
+
 } // namespace Toolkit
 
 } // namespace Dali
index b839043..6047364 100644 (file)
@@ -806,3 +806,48 @@ int UtcDaliLayouting_FlexLayout_NestedFlexboxWithSpec(void)
 
   END_TEST;
 }
+
+int UtcDaliLayouting_FlexLayout_WithTextLabel(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliLayouting_FlexLayout_WithTextLabel ");
+
+  Stage stage = Stage::GetCurrent();
+
+  auto flexbox1 = Control::New();
+  auto flexLayout1 = FlexLayout::New();
+  DevelControl::SetLayout( flexbox1, flexLayout1 );
+  flexbox1.SetName( "Flexbox1");
+  flexbox1.SetParentOrigin( ParentOrigin::CENTER );
+  flexbox1.SetAnchorPoint( AnchorPoint::CENTER );
+
+  std::vector< Control > controls;
+  controls.push_back( CreateTextLabel( "W" ) );
+  flexbox1.Add( controls[0] );
+  stage.Add( flexbox1 );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 54.0f, 64.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  // Test flexbox2 is sized to wrap its content
+  DALI_TEST_EQUALS( flexbox1.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( flexbox1.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  TextLabel::DownCast( controls[0] ).SetProperty( TextLabel::Property::TEXT, "WWWW" );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 216.0f, 64.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  DALI_TEST_EQUALS( flexbox1.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( flexbox1.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  END_TEST;
+}
index a9712d0..17fc90f 100644 (file)
@@ -18,6 +18,7 @@
 #include <iostream>
 #include <stdlib.h>
 #include <dali-toolkit-test-suite-utils.h>
+#include <toolkit-event-thread-callback.h>
 
 #include <dali-toolkit/dali-toolkit.h>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
@@ -531,7 +532,6 @@ int UtcDaliLayouting_HboxLayout06(void)
   END_TEST;
 }
 
-
 int UtcDaliLayouting_HboxLayout07(void)
 {
   ToolkitTestApplication application;
@@ -611,7 +611,6 @@ int UtcDaliLayouting_HboxLayout08(void)
 
   Control control1 = CreateLeafControl( 40, 40 );
   rootControl.Add( control1 );
-
   auto hbox = Control::New();
   auto hboxLayout = LinearLayout::New();
   hboxLayout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
@@ -641,6 +640,166 @@ int UtcDaliLayouting_HboxLayout08(void)
   END_TEST;
 }
 
+namespace
+{
+const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR  "/broken.png";
+}
+
+int UtcDaliLayouting_HboxLayout_ImageView(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliLayouting_HboxLayout - Use image view for leaf");
+
+  Stage stage = Stage::GetCurrent();
+  auto hbox = Control::New();
+  auto hboxLayout = LinearLayout::New();
+  DevelControl::SetLayout( hbox, hboxLayout );
+  hbox.SetName( "HBox" );
+
+  std::string url = CreateImageURL( Vector4( 0, 255, 0, 255), ImageDimensions( 100, 100 ) );
+  ImageView imageView = CreateImageView( url, ImageDimensions() );
+
+  hbox.SetParentOrigin( ParentOrigin::CENTER );
+  hbox.SetAnchorPoint( AnchorPoint::CENTER );
+  hbox.Add( imageView );
+  stage.Add( hbox );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 350.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  url = CreateImageURL( Vector4( 0, 255, 0, 255), ImageDimensions( 200, 200 ) );
+  imageView.SetImage( url );
+
+  // Ensure layouting happenss
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 300.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 200.0f, 200.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  imageView.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+  imageView.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
+
+  // Ensure layouting happenss
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 200.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  imageView.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
+  imageView.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+
+  // Ensure layouting happenss
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 300.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 200.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  imageView.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+  imageView.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+
+  Image image = FrameBufferImage::New( 50, 50, Pixel::RGBA8888 );
+  imageView.SetImage( image );
+
+  // Ensure layouting happenss
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 375.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 50.0f, 50.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  Property::Map imagePropertyMap;
+  imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
+  imagePropertyMap[ ImageVisual::Property::URL ] = TEST_IMAGE_FILE_NAME;
+  imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = 150;
+  imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 150;
+  imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, imagePropertyMap );
+
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
+  // Ensure layouting happenss
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 325.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( imageView.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 150.0f, 150.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliLayouting_HboxLayout_TextLabel(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliLayouting_HboxLayout - Use text label for leaf");
+
+  Stage stage = Stage::GetCurrent();
+
+  auto hbox = Control::New();
+  auto hboxLayout = LinearLayout::New();
+  DevelControl::SetLayout( hbox, hboxLayout );
+  hbox.SetName( "HBox" );
+  hbox.SetParentOrigin( ParentOrigin::CENTER );
+  hbox.SetAnchorPoint( AnchorPoint::CENTER );
+
+  std::vector< Control > controls;
+  TextLabel textLabel = CreateTextLabel( "W" );
+  controls.push_back( textLabel );
+  hbox.Add( textLabel );
+  stage.Add( hbox );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( textLabel.GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 368.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( textLabel.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 54.0f, 64.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  textLabel.SetProperty( TextLabel::Property::TEXT, "WWWW" );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 368.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 216.0f, 64.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  textLabel.SetProperty( TextLabel::Property::POINT_SIZE, 10.0f );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 382.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 120.0f, 36.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  controls[0].SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+  controls[0].SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 120.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  controls[0].SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
+  controls[0].SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 382.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 36.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  END_TEST;
+}
+
 // Padding tests
 
 int UtcDaliLayouting_HboxLayout_Padding01(void)
index a4685ff..01f240f 100755 (executable)
@@ -143,6 +143,16 @@ void SetLayout( Control control, Toolkit::LayoutItem layout )
   }
 }
 
+void RequestLayout( Internal::Control& control )
+{
+  Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( control );
+  Toolkit::Internal::LayoutItemPtr layoutItem = controlDataImpl.GetLayout();
+  if ( layoutItem )
+  {
+    layoutItem->RequestLayout();
+  }
+}
+
 } // namespace DevelControl
 
 } // namespace Toolkit
index 9a9e27e..879c767 100755 (executable)
@@ -300,6 +300,13 @@ DALI_TOOLKIT_API void SetLayout( Internal::Control& control, Toolkit::LayoutItem
  */
 DALI_TOOLKIT_API void SetLayout( Control control, Toolkit::LayoutItem layout );
 
+/**
+ * @brief Request the control layout.
+ *
+ * @param[in] control The internal Control to request the layout of
+ */
+DALI_TOOLKIT_API void RequestLayout( Internal::Control& control );
+
 } // namespace DevelControl
 
 } // namespace Toolkit
index 20869d3..4e8d266 100755 (executable)
@@ -119,6 +119,8 @@ void ImageView::SetImage( Image image )
     // Trigger a size negotiation request that may be needed when unregistering a visual.
     RelayoutRequest();
   }
+
+  Toolkit::DevelControl::RequestLayout( *this );
 }
 
 void ImageView::SetImage( const Property::Map& map )
@@ -147,6 +149,8 @@ void ImageView::SetImage( const Property::Map& map )
     // Trigger a size negotiation request that may be needed when unregistering a visual.
     RelayoutRequest();
   }
+
+  Toolkit::DevelControl::RequestLayout( *this );
 }
 
 void ImageView::SetImage( const std::string& url, ImageDimensions size )
@@ -175,6 +179,8 @@ void ImageView::SetImage( const std::string& url, ImageDimensions size )
     // Trigger a size negotiation request that may be needed when unregistering a visual.
     RelayoutRequest();
   }
+
+  Toolkit::DevelControl::RequestLayout( *this );
 }
 
 Image ImageView::GetImage() const
index c3607f5..7c6ef25 100644 (file)
@@ -1008,6 +1008,7 @@ void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
 void TextLabel::RequestTextRelayout()
 {
   RelayoutRequest();
+  Toolkit::DevelControl::RequestLayout( *this );
 }
 
 void TextLabel::SetUpAutoScrolling()