Skip to content

Commit d9ac00e

Browse files
Added the test case
1 parent db7834f commit d9ac00e

4 files changed

Lines changed: 181 additions & 2 deletions

File tree

src/Controls/src/Core/Handlers/Items2/iOS/TemplatedCell2.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin
110110
if (_measureInvalidated || _cachedConstraints != constraints)
111111
{
112112
// Only use the cached first-item measurement for actual item cells (not headers/footers)
113-
// Supplementary views (headers/footers) set the flag `isHeaderOrFooterChanged` during Bind
114-
// so we can detect them here and avoid using the item cache for their measurement.
115113
if (!isSupplementaryView)
116114
{
117115
var cachedSize = GetCachedFirstItemSizeFromHandler();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue33130"
5+
xmlns:local="clr-namespace:Maui.Controls.Sample"
6+
Title="Issue33130">
7+
<Grid Margin="20"
8+
RowDefinitions="Auto, *">
9+
<StackLayout Grid.Row="0"
10+
Spacing="10"
11+
Margin="0,10">
12+
<Button Text="Switch to MeasureFirstItem"
13+
Clicked="OnSwitchToMeasureFirstItem"
14+
AutomationId="SwitchStrategyButton"/>
15+
<Label x:Name="StatusLabel"
16+
AutomationId="StatusLabel"
17+
Text="ItemSizingStrategy: MeasureAllItems"/>
18+
</StackLayout>
19+
20+
<local:CollectionView2 Grid.Row="1"
21+
x:Name="TestCollectionView"
22+
ItemsSource="{Binding Animals}"
23+
IsGrouped="true"
24+
AutomationId="TestCollectionView"
25+
ItemSizingStrategy="MeasureAllItems">
26+
<local:CollectionView2.ItemTemplate>
27+
<DataTemplate>
28+
<Grid Padding="10">
29+
<Grid.RowDefinitions>
30+
<RowDefinition Height="Auto"/>
31+
<RowDefinition Height="Auto"/>
32+
</Grid.RowDefinitions>
33+
<Grid.ColumnDefinitions>
34+
<ColumnDefinition Width="Auto"/>
35+
<ColumnDefinition Width="*"/>
36+
</Grid.ColumnDefinitions>
37+
<Image Grid.RowSpan="2"
38+
Source="{Binding ImageUrl}"
39+
Aspect="AspectFill"
40+
WidthRequest="80"
41+
HeightRequest="80"/>
42+
<Label Grid.Column="1"
43+
Text="{Binding Name}"
44+
FontAttributes="Bold"/>
45+
<Label Grid.Row="1"
46+
Grid.Column="1"
47+
Text="{Binding Location}"
48+
FontAttributes="Italic"
49+
VerticalOptions="End"/>
50+
</Grid>
51+
</DataTemplate>
52+
</local:CollectionView2.ItemTemplate>
53+
<local:CollectionView2.GroupHeaderTemplate>
54+
<DataTemplate>
55+
<Label x:Name="GroupHeaderLabel"
56+
Text="{Binding Name}"
57+
BackgroundColor="LightGray"
58+
FontSize="20"
59+
FontAttributes="Bold"
60+
AutomationId="GroupHeader"/>
61+
</DataTemplate>
62+
</local:CollectionView2.GroupHeaderTemplate>
63+
<local:CollectionView2.GroupFooterTemplate>
64+
<DataTemplate>
65+
<Label Text="{Binding Count, StringFormat='Total animals: {0:D}'}"
66+
Margin="0,0,0,10"/>
67+
</DataTemplate>
68+
</local:CollectionView2.GroupFooterTemplate>
69+
</local:CollectionView2>
70+
</Grid>
71+
</ContentPage>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using Microsoft.Maui.Controls;
4+
5+
namespace Maui.Controls.Sample.Issues;
6+
7+
[Issue(IssueTracker.Github, 33130, "CollectionView group header size changes with ItemSizingStrategy", PlatformAffected.iOS | PlatformAffected.macOS)]
8+
public partial class Issue33130 : ContentPage
9+
{
10+
public Issue33130()
11+
{
12+
InitializeComponent();
13+
BindingContext = new GroupedAnimalsViewModel();
14+
}
15+
16+
private void OnSwitchToMeasureFirstItem(object sender, EventArgs e)
17+
{
18+
TestCollectionView.ItemSizingStrategy = ItemSizingStrategy.MeasureFirstItem;
19+
StatusLabel.Text = $"ItemSizingStrategy: {TestCollectionView.ItemSizingStrategy}";
20+
}
21+
}
22+
23+
public class GroupedAnimalsViewModel
24+
{
25+
public ObservableCollection<GroupHeaderTestAnimalGroup> Animals { get; set; }
26+
27+
public GroupedAnimalsViewModel()
28+
{
29+
Animals = new ObservableCollection<GroupHeaderTestAnimalGroup>
30+
{
31+
new GroupHeaderTestAnimalGroup("Bears")
32+
{
33+
new GroupHeaderTestAnimal { Name = "Grizzly Bear", Location = "North America", ImageUrl = "bear.jpg" },
34+
new GroupHeaderTestAnimal { Name = "Polar Bear", Location = "Arctic", ImageUrl = "bear.jpg" },
35+
},
36+
new GroupHeaderTestAnimalGroup("Monkeys")
37+
{
38+
new GroupHeaderTestAnimal { Name = "Baboon", Location = "Africa", ImageUrl = "monkey.jpg" },
39+
new GroupHeaderTestAnimal { Name = "Capuchin Monkey", Location = "South America", ImageUrl = "monkey.jpg" },
40+
new GroupHeaderTestAnimal { Name = "Spider Monkey", Location = "Central America", ImageUrl = "monkey.jpg" },
41+
},
42+
new GroupHeaderTestAnimalGroup("Elephants")
43+
{
44+
new GroupHeaderTestAnimal { Name = "African Elephant", Location = "Africa", ImageUrl = "elephant.jpg" },
45+
new GroupHeaderTestAnimal { Name = "Asian Elephant", Location = "Asia", ImageUrl = "elephant.jpg" },
46+
}
47+
};
48+
}
49+
}
50+
51+
public class GroupHeaderTestAnimalGroup : ObservableCollection<GroupHeaderTestAnimal>
52+
{
53+
public string Name { get; set; }
54+
55+
public GroupHeaderTestAnimalGroup(string name) : base()
56+
{
57+
Name = name;
58+
}
59+
}
60+
61+
public class GroupHeaderTestAnimal
62+
{
63+
public string Name { get; set; }
64+
public string Location { get; set; }
65+
public string ImageUrl { get; set; }
66+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue33130 : _IssuesUITest
8+
{
9+
public override string Issue => "CollectionView group header size changes with ItemSizingStrategy";
10+
11+
public Issue33130(TestDevice device) : base(device) { }
12+
[Test]
13+
[Category(UITestCategories.CollectionView)]
14+
public void GroupHeaderSizeShouldNotChangeWithItemSizingStrategy()
15+
{
16+
// Wait for the CollectionView to load
17+
App.WaitForElement("TestCollectionView");
18+
App.WaitForElement("GroupHeader");
19+
20+
// Get the initial header size (before changing ItemSizingStrategy)
21+
var headerElementBefore = App.FindElement("GroupHeader");
22+
var headerRectBefore = headerElementBefore.GetRect();
23+
24+
Assert.That(headerRectBefore.Height, Is.GreaterThan(0), "Header should have a height before strategy change");
25+
26+
// Switch ItemSizingStrategy
27+
App.WaitForElement("SwitchStrategyButton");
28+
App.Tap("SwitchStrategyButton");
29+
30+
// Get the header size after changing ItemSizingStrategy
31+
var headerElementAfter = App.FindElement("GroupHeader");
32+
var headerRectAfter = headerElementAfter.GetRect();
33+
34+
Assert.That(headerRectAfter.Height, Is.GreaterThan(0), "Header should have a height after strategy change");
35+
36+
// The header size should remain the same (within a small tolerance for rendering differences)
37+
// Allow for small rounding differences but not significant changes
38+
var heightDifference = Math.Abs(headerRectBefore.Height - headerRectAfter.Height);
39+
40+
// Assert that the height difference is minimal (less than 5 pixels tolerance)
41+
Assert.That(heightDifference, Is.EqualTo(0),
42+
$"Header height should not change significantly. Before: {headerRectBefore.Height}, After: {headerRectAfter.Height}, Difference: {heightDifference}");
43+
}
44+
}

0 commit comments

Comments
 (0)