|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using CommunityToolkit.Tests; |
| 6 | +using CommunityToolkit.Tooling.TestGen; |
| 7 | +using CommunityToolkit.WinUI.Controls; |
| 8 | + |
| 9 | +namespace PrimitivesTests; |
| 10 | + |
| 11 | +[TestClass] |
| 12 | +public partial class Test_WrapPanel_StretchChild : VisualUITestBase |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// When a WrapPanel is inside a parent with infinite width, the last child cannot stretch to fill the remaining space. |
| 16 | + /// Instead, it should measure to its desired size. |
| 17 | + /// </summary> |
| 18 | + [TestCategory("WrapPanel")] |
| 19 | + [UIThreadTestMethod] |
| 20 | + public void VerticalWrapPanelInsideParentWithInfinityHeightTest(VerticalWrapPanelInsideParentWithInfinityHeight page) |
| 21 | + { |
| 22 | + var wrapPanel = page.FindDescendant<WrapPanel>(); |
| 23 | + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); |
| 24 | + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); |
| 25 | + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); |
| 26 | + |
| 27 | + foreach (var child in wrapPanel.Children.Cast<FrameworkElement>()) |
| 28 | + { |
| 29 | + double expectedHeight = child.DesiredSize.Height; |
| 30 | + Assert.AreEqual(expectedHeight, child.ActualHeight, "Child height not as expected."); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// When a WrapPanel is inside a parent with limited height, the last child with Stretch alignment should fill the remaining space. |
| 36 | + /// </summary> |
| 37 | + [TestCategory("WrapPanel")] |
| 38 | + [UIThreadTestMethod] |
| 39 | + public void VerticalWrapPanelInsideParentWithLimitedHeightTest(VerticalWrapPanelInsideParentWithLimitedHeight page) |
| 40 | + { |
| 41 | + var wrapPanel = page.FindDescendant<WrapPanel>(); |
| 42 | + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); |
| 43 | + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); |
| 44 | + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); |
| 45 | + |
| 46 | + var precedingChildren = wrapPanel.Children.Cast<FrameworkElement>().Take(wrapPanel.Children.Count - 1); |
| 47 | + |
| 48 | + foreach (var child in precedingChildren) |
| 49 | + { |
| 50 | + double expectedHeight = child.DesiredSize.Height; |
| 51 | + Assert.AreEqual(expectedHeight, child.ActualHeight, "Preceding child height not as expected."); |
| 52 | + } |
| 53 | + |
| 54 | + var lastChild = wrapPanel.Children.Cast<FrameworkElement>().Last(); |
| 55 | + double lastChildExpectedHeight = wrapPanel.ActualHeight - precedingChildren.Sum(child => child.ActualHeight); |
| 56 | + Assert.AreEqual(lastChildExpectedHeight, lastChild.ActualHeight, "Last child height not as expected."); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// When a WrapPanel is inside a parent with infinite width, the last child cannot stretch to fill the remaining space. |
| 61 | + /// Instead, it should measure to its desired size. |
| 62 | + /// </summary> |
| 63 | + [TestCategory("WrapPanel")] |
| 64 | + [UIThreadTestMethod] |
| 65 | + public void HorizontalWrapPanelInsideParentWithInfinityWidthTest(HorizontalWrapPanelInsideParentWithInfinityWidth page) |
| 66 | + { |
| 67 | + var wrapPanel = page.FindDescendant<WrapPanel>(); |
| 68 | + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); |
| 69 | + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); |
| 70 | + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); |
| 71 | + |
| 72 | + foreach (var child in wrapPanel.Children.Cast<FrameworkElement>()) |
| 73 | + { |
| 74 | + double expectedWidth = child.DesiredSize.Width; |
| 75 | + Assert.AreEqual(expectedWidth, child.ActualWidth, "Preceding child width not as expected."); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// When a WrapPanel is inside a parent with limited width, the last child with Stretch alignment should fill the remaining space. |
| 81 | + /// </summary> |
| 82 | + /// <param name="page"></param> |
| 83 | + [TestCategory("WrapPanel")] |
| 84 | + [UIThreadTestMethod] |
| 85 | + public void HorizontalWrapPanelInsideParentWithLimitedWidthTest(HorizontalWrapPanelInsideParentWithLimitedWidth page) |
| 86 | + { |
| 87 | + var wrapPanel = page.FindDescendant<WrapPanel>(); |
| 88 | + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); |
| 89 | + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); |
| 90 | + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); |
| 91 | + |
| 92 | + var precedingChildren = wrapPanel.Children.Cast<FrameworkElement>().Take(wrapPanel.Children.Count - 1); |
| 93 | + |
| 94 | + foreach (var child in precedingChildren) |
| 95 | + { |
| 96 | + double expectedWidth = child.DesiredSize.Width; |
| 97 | + Assert.AreEqual(expectedWidth, child.ActualWidth, "Child width not as expected."); |
| 98 | + } |
| 99 | + |
| 100 | + var lastChild = wrapPanel.Children.Cast<FrameworkElement>().Last(); |
| 101 | + double lastChildExpectedWidth = wrapPanel.ActualWidth - precedingChildren.Sum(child => child.ActualWidth); |
| 102 | + Assert.AreEqual(lastChildExpectedWidth, lastChild.ActualWidth, "Last child width not as expected."); |
| 103 | + } |
| 104 | +} |
0 commit comments