Skip to content

Commit 9686f7e

Browse files
committed
add tests for CV2
1 parent c6218a8 commit 9686f7e

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal void SelectItem(object selectedItem)
4545
CollectionView.PerformBatchUpdates(null, _ =>
4646
{
4747
CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None);
48+
CollectionView.CellForItem(index)?.UpdateSelectedAccessibility(true);
4849
});
4950
}
5051
}
@@ -75,6 +76,8 @@ void FormsSelectItem(NSIndexPath indexPath)
7576
ItemsView.SelectedItems.Add(GetItemAtIndex(indexPath));
7677
break;
7778
}
79+
80+
CollectionView.CellForItem(indexPath)?.UpdateSelectedAccessibility(true);
7881
}
7982

8083
void FormsDeselectItem(NSIndexPath indexPath)
@@ -91,6 +94,8 @@ void FormsDeselectItem(NSIndexPath indexPath)
9194
ItemsView.SelectedItems.Remove(GetItemAtIndex(indexPath));
9295
break;
9396
}
97+
98+
CollectionView.CellForItem(indexPath)?.UpdateSelectedAccessibility(false);
9499
}
95100

96101
internal void UpdatePlatformSelection()
@@ -130,6 +135,10 @@ internal void UpdateSelectionMode()
130135
{
131136
var mode = ItemsView.SelectionMode;
132137

138+
// We want to make sure we clear the selection trait before we switch modes.
139+
// If we do this after we switch modes, cells that are selected may not show up as selected anymore.
140+
CollectionView.ClearSelectedAccessibilityTraits(CollectionView.GetIndexPathsForSelectedItems());
141+
133142
switch (mode)
134143
{
135144
case SelectionMode.None:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
xmlns:local="clr-namespace:Maui.Controls.Sample"
5+
x:Class="Maui.Controls.Sample.Issues.Issue21375_2">
6+
<VerticalStackLayout>
7+
<local:CollectionView2 ItemsSource="{Binding Items}" SelectionMode="Single" x:Name="collectionView" AutomationId="collectionView" Background="LightBlue">
8+
<CollectionView.ItemTemplate>
9+
<DataTemplate>
10+
<StackLayout Padding="10" SemanticProperties.Description="{Binding Name}" HeightRequest="50">
11+
<Label Text="{Binding Name}" FontAttributes="Bold" FontSize="16"/>
12+
<Label Text="{Binding Description}" FontSize="14"/>
13+
</StackLayout>
14+
</DataTemplate>
15+
</CollectionView.ItemTemplate>
16+
</local:CollectionView2>
17+
<Label Margin="0,10,0,0" Text="SelectionMode" />
18+
<Button Text="None" Clicked="NoneSelectionMode" x:Name="noneButton" AutomationId="noneButton"/>
19+
<Button Text="Single" Clicked="SingleSelectionMode" AutomationId="singleButton"/>
20+
<Button Text="Multiple" Clicked="MultipleSelectionMode" AutomationId="multipleButton" />
21+
<Button Text="Calculate" Clicked="Calculate" x:Name="calculateButton" AutomationId="calculateButton"/>
22+
<Editor x:Name="Output" HeightRequest="400"/>
23+
</VerticalStackLayout>
24+
</ContentPage>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.ObjectModel;
2+
using System.Text;
3+
4+
namespace Maui.Controls.Sample.Issues;
5+
6+
[Issue(IssueTracker.Github, 21375_2, "Selected CollectionView2 item is not announced", PlatformAffected.iOS & PlatformAffected.macOS)]
7+
public partial class Issue21375_2 : ContentPage
8+
{
9+
public ObservableCollection<Item> Items { get; set; }
10+
11+
public Issue21375_2()
12+
{
13+
InitializeComponent();
14+
15+
Items = new ObservableCollection<Item>
16+
{
17+
new Item { Name = "Item 1", Description = "Description for item 1" },
18+
new Item { Name = "Item 2", Description = "Description for item 2" },
19+
new Item { Name = "Item 3", Description = "Description for item 3" },
20+
new Item { Name = "Item 4", Description = "Description for item 4" },
21+
new Item { Name = "Item 5", Description = "Description for item 5" }
22+
};
23+
24+
BindingContext = this;
25+
}
26+
27+
void NoneSelectionMode(object sender, EventArgs e)
28+
{
29+
collectionView.SelectionMode = SelectionMode.None;
30+
}
31+
32+
void SingleSelectionMode(object sender, EventArgs e)
33+
{
34+
collectionView.SelectionMode = SelectionMode.Single;
35+
}
36+
37+
void MultipleSelectionMode(object sender, EventArgs e)
38+
{
39+
collectionView.SelectionMode = SelectionMode.Multiple;
40+
}
41+
42+
void Calculate(object sender, EventArgs e)
43+
{
44+
var sb = new StringBuilder();
45+
#if IOS || MACCATALYST
46+
if (collectionView.Handler?.PlatformView is UIKit.UIView vc
47+
&& vc.Subviews is UIKit.UIView[] subviews && subviews.Length > 0
48+
&& subviews[0] is UIKit.UICollectionView uiCollectionView)
49+
{
50+
for (int i = 0; i < uiCollectionView.VisibleCells.Length; i++)
51+
{
52+
var cell = uiCollectionView.VisibleCells[i];
53+
sb.AppendLine($"Item{i} Cell: {cell.AccessibilityTraits}");
54+
if (cell.ContentView is not null && cell.ContentView.Subviews.Length > 0)
55+
{
56+
var firstChild = cell.ContentView.Subviews[0];
57+
sb.AppendLine($"Item{i} FirstChild: {firstChild.AccessibilityTraits}");
58+
}
59+
}
60+
}
61+
#endif
62+
Output.Text = sb.ToString();
63+
}
64+
65+
public class Item
66+
{
67+
public string Name { get; set; }
68+
public string Description { get; set; }
69+
}
70+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#if IOS || MACCATALYST
2+
using NUnit.Framework;
3+
using NUnit.Framework.Legacy;
4+
using UITest.Appium;
5+
using UITest.Core;
6+
using System.Text;
7+
8+
namespace Microsoft.Maui.TestCases.Tests.Issues;
9+
10+
public class Issue21375_2 : _IssuesUITest
11+
{
12+
public Issue21375_2(TestDevice device) : base(device) { }
13+
14+
public override string Issue => "Selected CollectionView2 item is not announced";
15+
16+
[Test]
17+
[Category(UITestCategories.CollectionView)]
18+
public void SelectedItemsShowSelected ()
19+
{
20+
var collectionView = App.WaitForElement("collectionView");
21+
var centerX = collectionView.GetRect().X + (collectionView.GetRect().Width / 2);
22+
var firstItemY = collectionView.GetRect().Y + 25;
23+
var secondItemY = collectionView.GetRect().Y + 75;
24+
var thirdItemY = collectionView.GetRect().Y + 125;
25+
26+
App.TapCoordinates(centerX, firstItemY);
27+
App.WaitForElement("calculateButton").Tap();
28+
VerifyScreenshot(Issue21375.TestContext.Test.SelectedItemsShowSelected.MethodName + "_single");
29+
30+
App.WaitForElement("multipleButton").Tap();
31+
App.TapCoordinates(centerX, secondItemY);
32+
App.TapCoordinates(centerX, thirdItemY);
33+
App.WaitForElement("calculateButton").Tap();
34+
VerifyScreenshot(Issue21375.TestContext.Test.SelectedItemsShowSelected.MethodName + "_multiple");
35+
36+
App.WaitForElement("noneButton").Tap();
37+
App.WaitForElement("calculateButton").Tap();
38+
VerifyScreenshot(Issue21375.TestContext.Test.SelectedItemsShowSelected.MethodName + "_none");
39+
40+
App.WaitForElement("singleButton").Tap();
41+
App.WaitForElement("calculateButton").Tap();
42+
43+
App.WaitForElement("multipleButton").Tap();
44+
App.WaitForElement("calculateButton").Tap();
45+
VerifyScreenshot(Issue21375.TestContext.Test.SelectedItemsShowSelected.MethodName + "_multiple");
46+
}
47+
}
48+
#endif

0 commit comments

Comments
 (0)