From de93cb645c4c4d3ae0a41e7b86feba39024d6c77 Mon Sep 17 00:00:00 2001 From: Daniel McEwen Date: Tue, 5 Mar 2019 10:12:22 +0000 Subject: [PATCH] Grid layout crash fix When there are no items to display, there was a divide by zero error Change-Id: I51bf2da35b2937e4797c61b20a4bbf027b845cac --- .../src/dali-toolkit/utc-Dali-GridLayout.cpp | 40 +++++++++++++++++++++- dali-toolkit/internal/layouting/grid-impl.cpp | 2 +- dali-toolkit/internal/layouting/grid-locations.cpp | 32 ++++------------- dali-toolkit/internal/layouting/grid-locations.h | 5 ++- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-GridLayout.cpp b/automated-tests/src/dali-toolkit/utc-Dali-GridLayout.cpp index 224c6f4..9438589 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-GridLayout.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-GridLayout.cpp @@ -44,6 +44,44 @@ void utc_dali_toolkit_grid_layouting_cleanup(void) 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; @@ -627,4 +665,4 @@ int UtcDaliLayouting_GridLayoutDownCast(void) DALI_TEST_CHECK( gridLayoutCandidate ); END_TEST; -} \ No newline at end of file +} diff --git a/dali-toolkit/internal/layouting/grid-impl.cpp b/dali-toolkit/internal/layouting/grid-impl.cpp index 09224ee..b633523 100644 --- a/dali-toolkit/internal/layouting/grid-impl.cpp +++ b/dali-toolkit/internal/layouting/grid-impl.cpp @@ -196,7 +196,7 @@ void Grid::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpe 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 ), diff --git a/dali-toolkit/internal/layouting/grid-locations.cpp b/dali-toolkit/internal/layouting/grid-locations.cpp index a4d3060..847bcc1 100644 --- a/dali-toolkit/internal/layouting/grid-locations.cpp +++ b/dali-toolkit/internal/layouting/grid-locations.cpp @@ -55,9 +55,7 @@ GridLocations::~GridLocations(){} 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 ); @@ -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; - 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", @@ -154,4 +136,4 @@ GridLocations::LocationVector GridLocations::GetLocations() } // namespace Internal } // namespace Toolkit -} // namespace Dali \ No newline at end of file +} // namespace Dali diff --git a/dali-toolkit/internal/layouting/grid-locations.h b/dali-toolkit/internal/layouting/grid-locations.h index 2bc4359..afc1437 100644 --- a/dali-toolkit/internal/layouting/grid-locations.h +++ b/dali-toolkit/internal/layouting/grid-locations.h @@ -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, - unsigned int availableHeight, unsigned int numberOfCells, - unsigned int columnWidth, unsigned int rowHeight ); + unsigned int availableHeight, unsigned int numberOfCells ); LocationVector GetLocations(); @@ -95,4 +94,4 @@ private: } // 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 -- 2.7.4