Grid layout crash fix 88/200888/3
authorDaniel McEwen <d.mcewen@partner.samsung.com>
Tue, 5 Mar 2019 10:12:22 +0000 (10:12 +0000)
committerDaniel McEwen <d.mcewen@partner.samsung.com>
Tue, 5 Mar 2019 12:41:00 +0000 (12:41 +0000)
When there are no items to display, there was a divide by zero error

Change-Id: I51bf2da35b2937e4797c61b20a4bbf027b845cac

automated-tests/src/dali-toolkit/utc-Dali-GridLayout.cpp
dali-toolkit/internal/layouting/grid-impl.cpp
dali-toolkit/internal/layouting/grid-locations.cpp
dali-toolkit/internal/layouting/grid-locations.h

index 224c6f4..9438589 100644 (file)
@@ -44,6 +44,44 @@ void utc_dali_toolkit_grid_layouting_cleanup(void)
   test_return_value = TET_PASS;
 }
 
   test_return_value = TET_PASS;
 }
 
+int UtcDaliLayouting_GridLayout00(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliLayouting_GridLayout00 1 Column, 0 Items");
+
+  const auto NUMBER_OF_COLUMNS = 1;
+  const auto NUMBER_OF_ITEMS = 0;
+
+  tet_printf( "Testing %d columns with %d items\n", NUMBER_OF_COLUMNS, NUMBER_OF_ITEMS );
+
+  Stage stage = Stage::GetCurrent();
+
+  auto rootControl = Control::New();
+  auto absoluteLayout = AbsoluteLayout::New();
+  DevelControl::SetLayout( rootControl, absoluteLayout );
+  rootControl.SetName( "AbsoluteLayout" );
+  stage.Add( rootControl );
+
+  auto gridContainer = Control::New();
+  auto gridLayout = Grid::New();
+  gridLayout.SetNumberOfColumns( NUMBER_OF_COLUMNS );
+  gridContainer.SetName( "GridLayout");
+  DevelControl::SetLayout( gridContainer, gridLayout );
+  gridContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
+  gridContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
+
+  rootControl.Add( gridContainer );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  tet_printf( "Confirm number of columns is as set\n");
+  DALI_TEST_EQUALS( gridLayout.GetNumberOfColumns(), NUMBER_OF_COLUMNS, TEST_LOCATION );
+
+  END_TEST;
+}
+
 int UtcDaliLayouting_GridLayout01(void)
 {
   ToolkitTestApplication application;
 int UtcDaliLayouting_GridLayout01(void)
 {
   ToolkitTestApplication application;
@@ -627,4 +665,4 @@ int UtcDaliLayouting_GridLayoutDownCast(void)
   DALI_TEST_CHECK( gridLayoutCandidate );
 
   END_TEST;
   DALI_TEST_CHECK( gridLayoutCandidate );
 
   END_TEST;
-}
\ No newline at end of file
+}
index 09224ee..b633523 100644 (file)
@@ -196,7 +196,7 @@ void Grid::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpe
   DetermineNumberOfColumns( availableContentWidth );
 
   // Locations define the start, end,top and bottom of each cell.
   DetermineNumberOfColumns( availableContentWidth );
 
   // Locations define the start, end,top and bottom of each cell.
-  mLocations->CalculateLocations( mNumColumns, availableContentWidth.AsInteger(), availableContentHeight.AsInteger(), childCount, 0, 0 );
+  mLocations->CalculateLocations( mNumColumns, availableContentWidth.AsInteger(), availableContentHeight.AsInteger(), childCount );
 
 
   SetMeasuredDimensions( ResolveSizeAndState( widthSize, widthMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK ),
 
 
   SetMeasuredDimensions( ResolveSizeAndState( widthSize, widthMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK ),
index a4d3060..847bcc1 100644 (file)
@@ -55,9 +55,7 @@ GridLocations::~GridLocations(){}
 void GridLocations::CalculateLocations( int numberOfColumns,
                                         unsigned int availableWidth,
                                         unsigned int availableHeight,
 void GridLocations::CalculateLocations( int numberOfColumns,
                                         unsigned int availableWidth,
                                         unsigned int availableHeight,
-                                        unsigned int numberOfCells,
-                                        unsigned int columnWidth,
-                                        unsigned int rowHeight )
+                                        unsigned int numberOfCells )
 {
   DALI_ASSERT_DEBUG( numberOfColumns > 0 && "number of columns should be greater than 0" );
   numberOfColumns = std::max ( numberOfColumns, 1 );
 {
   DALI_ASSERT_DEBUG( numberOfColumns > 0 && "number of columns should be greater than 0" );
   numberOfColumns = std::max ( numberOfColumns, 1 );
@@ -70,30 +68,14 @@ void GridLocations::CalculateLocations( int numberOfColumns,
   // If number of cells not cleanly dividable by colums, add another row to house remainder cells.
   numberOfRows += (numberOfCells%numberOfColumns)?1:0;
 
   // If number of cells not cleanly dividable by colums, add another row to house remainder cells.
   numberOfRows += (numberOfCells%numberOfColumns)?1:0;
 
-  unsigned int maxColumnWidth = availableWidth / numberOfColumns;
+  // Safe as numberOfColumns is guaranteed to be > 0
+  unsigned int columnWidth = availableWidth / numberOfColumns;
 
 
-  if( columnWidth > 0 )
-  {
-    // Column width supplied so use this unless exceeds available width.
-    columnWidth = std::min( columnWidth, maxColumnWidth );
-  }
-  else
-  {
-    // Column width not supplied so use calculated value
-    columnWidth = maxColumnWidth;
-  }
+  unsigned int rowHeight = availableHeight;
 
 
-  unsigned int maxRowHeight = availableHeight / numberOfRows;
-
-  if( rowHeight > 0 )
-  {
-    // Column height supplied so use this unless exceeds available height.
-    rowHeight = std::min( rowHeight, maxRowHeight );
-  }
-  else
+  if( numberOfRows > 0 )
   {
   {
-    // Column height not supplied so use calculated value
-    rowHeight = maxRowHeight;
+    rowHeight = availableHeight / numberOfRows;
   }
 
   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ColumWidth[%d] RowHeight[%d] NumberOfRows[%d] NumberOfColumns[%d]\n",
   }
 
   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ColumWidth[%d] RowHeight[%d] NumberOfRows[%d] NumberOfColumns[%d]\n",
@@ -154,4 +136,4 @@ GridLocations::LocationVector GridLocations::GetLocations()
 } // namespace Internal
 
 } // namespace Toolkit
 } // namespace Internal
 
 } // namespace Toolkit
-} // namespace Dali
\ No newline at end of file
+} // namespace Dali
index 2bc4359..afc1437 100644 (file)
@@ -73,8 +73,7 @@ public:
    * Uses the given parameters to calculate the x,y coordinates of each cell and cell size.
    */
   void CalculateLocations( int numberOfColumns, unsigned int availableWidth,
    * Uses the given parameters to calculate the x,y coordinates of each cell and cell size.
    */
   void CalculateLocations( int numberOfColumns, unsigned int availableWidth,
-                           unsigned int availableHeight, unsigned int numberOfCells,
-                           unsigned int columnWidth, unsigned int rowHeight );
+                           unsigned int availableHeight, unsigned int numberOfCells );
 
   LocationVector GetLocations();
 
 
   LocationVector GetLocations();
 
@@ -95,4 +94,4 @@ private:
 } // namespace Toolkit
 } // namespace Dali
 
 } // namespace Toolkit
 } // namespace Dali
 
-#endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_GRID_LOCATIONS_H
\ No newline at end of file
+#endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_GRID_LOCATIONS_H