Fix issue with FirstColumn when no Row or Column specified in UniformGrid. Add more Unit Tests.

This commit is contained in:
mhawker 2018-04-07 22:11:23 -07:00
Родитель b11422227a
Коммит 52ec39121f
4 изменённых файлов: 93 добавлений и 14 удалений

Просмотреть файл

@ -64,13 +64,9 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
// returns the dimensions of the
// grid we need to hold all elements.
#pragma warning disable SA1008 // Opening parenthesis must be spaced correctly
internal (int rows, int columns) GetDimensions(ref IEnumerable<FrameworkElement> visible)
internal static (int rows, int columns) GetDimensions(ref IEnumerable<FrameworkElement> visible, int rows, int cols, int firstColumn) //// TODO: Switch to 'in' for C# 7.2
#pragma warning restore SA1008 // Opening parenthesis must be spaced correctly
{
// Make copy of our properties as we don't want to modify.
int rows = Rows;
int cols = Columns;
// If a dimension isn't specified, we need to figure out the other one (or both).
if (rows == 0 || cols == 0)
{
@ -84,7 +80,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
if (cols > 0)
{
// TODO: Handle RightToLeft?
var first = Math.Min(FirstColumn, cols - 1); // Bound check
var first = Math.Min(firstColumn, cols - 1); // Bound check
// If we have columns but no rows, calculate rows based on column offset and number of children.
rows = (count + first + (cols - 1)) / cols;
@ -92,7 +88,12 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
}
// Otherwise, determine square layout if both are zero.
rows = (int)Math.Ceiling(Math.Sqrt(count));
var size = (int)Math.Ceiling(Math.Sqrt(count));
// Figure out if firstColumn in bounds
var first2 = Math.Min(firstColumn, size - 1); // Bound check
rows = (int)Math.Ceiling(Math.Sqrt(count + first2));
return (rows, rows);
}
else if (cols == 0)
@ -101,7 +102,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
cols = (count + (rows - 1)) / rows;
// Now that we know a rough size of our shape, see if the FirstColumn effects that:
var first = Math.Min(FirstColumn, cols - 1); // Bound check
var first = Math.Min(firstColumn, cols - 1); // Bound check
cols = (count + first + (rows - 1)) / rows;
}

Просмотреть файл

@ -16,12 +16,14 @@ using Microsoft.Toolkit.Extensions;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The UniformGrid control presents information within a Grid with even spacing.
/// </summary>
[Bindable]
public partial class UniformGrid : Grid
{
/// <summary>
@ -34,7 +36,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
// Get all Visible FrameworkElement Children
var visible = Children.Where(item => item.Visibility != Visibility.Collapsed && item is FrameworkElement).Select(item => item as FrameworkElement);
var dim = GetDimensions(ref visible);
var dim = GetDimensions(ref visible, Rows, Columns, FirstColumn);
// Mark existing dev-defined definitions so we don't erase them.
foreach (var rd in RowDefinitions)

Просмотреть файл

@ -10,15 +10,19 @@
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using Microsoft.Toolkit.Extensions;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Extensions;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
using Assert = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.Assert;
@ -27,6 +31,78 @@ namespace UnitTests.UI.Controls
[TestClass]
public class Test_UniformGrid
{
[TestCategory("UniformGrid")]
[UITestMethod]
public void Test_UniformGrid_GetDimensions_AllVisible()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:UniformGrid x:Name=""UniformGrid"">
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
</controls:UniformGrid>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeroot, "Could not load XAML tree.");
var grid = treeroot.FindChildByName("UniformGrid") as UniformGrid;
Assert.IsNotNull(grid, "Could not find UniformGrid in tree.");
var children = grid.Children.Select(item => item as FrameworkElement);
Assert.AreEqual(8, grid.Children.Count());
var dimensions = UniformGrid.GetDimensions(ref children, 0, 0, 0);
Assert.AreEqual(3, dimensions.rows);
Assert.AreEqual(3, dimensions.columns);
}
[TestCategory("UniformGrid")]
[UITestMethod]
public void Test_UniformGrid_GetDimensions_FirstColumn()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
<controls:UniformGrid x:Name=""UniformGrid"">
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
<Border/>
</controls:UniformGrid>
</Page>") as FrameworkElement;
Assert.IsNotNull(treeroot, "Could not load XAML tree.");
var grid = treeroot.FindChildByName("UniformGrid") as UniformGrid;
Assert.IsNotNull(grid, "Could not find UniformGrid in tree.");
var children = grid.Children.Select(item => item as FrameworkElement);
Assert.AreEqual(8, grid.Children.Count());
var dimensions = UniformGrid.GetDimensions(ref children, 0, 0, 2);
Assert.AreEqual(4, dimensions.rows);
Assert.AreEqual(4, dimensions.columns);
}
[TestCategory("UniformGrid")]
[UITestMethod]
public void Test_UniformGrid_GetFreeSpots_Basic()

Просмотреть файл

@ -23,7 +23,7 @@ namespace UnitTests
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
internal sealed partial class App : Application
public sealed partial class App : Application
{
/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.