Skip to content

Commit fd44ced

Browse files
LogishaSelvarajSF4525PureWeen
authored andcommitted
[Testing] Feature Matrix UITest Cases for CollectionView Selection Feature (#29165)
* added the selection feature tests * added snapshots
1 parent 1b2db22 commit fd44ced

19 files changed

+1297
-0
lines changed

src/Controls/tests/TestCases.HostApp/FeatureMatrix/CollectionView/CollectionViewFeaturePage.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@
2727
HorizontalOptions="Center"
2828
AutomationId="ScrollingButton"
2929
WidthRequest="400"/>
30+
<Button Text="Selection FeaturePage"
31+
Clicked="OnSelectionButtonClicked"
32+
HorizontalOptions="Center"
33+
AutomationId="SelectionPageButton"
34+
WidthRequest="400"/>
3035
</VerticalStackLayout>
3136
</ContentPage>

src/Controls/tests/TestCases.HostApp/FeatureMatrix/CollectionView/CollectionViewFeaturePage.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ private async void OnScrollingButtonClicked(object sender, EventArgs e)
3737
await Navigation.PushAsync(new CollectionViewScrollPage());
3838
}
3939

40+
private async void OnSelectionButtonClicked(object sender, EventArgs e)
41+
{
42+
await Navigation.PushAsync(new CollectionViewSelectionPage());
43+
}
44+
4045
}

src/Controls/tests/TestCases.HostApp/FeatureMatrix/CollectionView/CollectionViewViewModel.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
using Microsoft.Maui.Graphics;
99
using Maui.Controls.Sample.CollectionViewGalleries;
1010
using System.Windows.Input;
11+
using System.Collections.Specialized;
1112

1213
namespace Maui.Controls.Sample;
14+
1315
public class Grouping<TKey, TItem> : ObservableCollection<TItem>
1416
{
1517
public TKey Key { get; }
@@ -62,6 +64,12 @@ public class CollectionViewViewModel : INotifyPropertyChanged
6264
private ObservableCollection<ItemModel> _observableCollection2;
6365
private List<Grouping<string, CollectionViewTestItem>> _groupedList3;
6466
private List<Grouping<string, ItemModel>> _groupedList2;
67+
private SelectionMode _selectionMode = SelectionMode.None;
68+
private object _selectedItem;
69+
private ObservableCollection<object> _selectedItems = new ObservableCollection<object>();
70+
private int _selectionChangedEventCount = 0;
71+
private string _previousSelectionText;
72+
private string _currentSelectionText;
6573

6674
public bool ShowAddRemoveButtons => ItemsSourceType == ItemsSourceType.ObservableCollectionT3 || ItemsSourceType == ItemsSourceType.GroupedListT3;
6775

@@ -99,6 +107,8 @@ public CollectionViewViewModel()
99107
});
100108

101109
SetItemTemplate();
110+
SelectedItems = new ObservableCollection<object>();
111+
SelectedItems.CollectionChanged += OnSelectedItemsChanged;
102112
}
103113

104114
public object EmptyView
@@ -270,6 +280,105 @@ public ItemsUpdatingScrollMode ItemsUpdatingScrollMode
270280
}
271281
}
272282

283+
public SelectionMode SelectionMode
284+
{
285+
get => _selectionMode;
286+
set
287+
{
288+
if (_selectionMode != value)
289+
{
290+
_selectionMode = value;
291+
OnPropertyChanged();
292+
}
293+
}
294+
}
295+
296+
public object SelectedItem
297+
{
298+
get => _selectedItem;
299+
set
300+
{
301+
if (_selectedItem != value)
302+
{
303+
_selectedItem = value;
304+
OnPropertyChanged();
305+
OnPropertyChanged(nameof(SelectedItemText));
306+
OnPropertyChanged(nameof(SelectedItemsCount));
307+
}
308+
}
309+
}
310+
311+
public ObservableCollection<object> SelectedItems
312+
{
313+
get => _selectedItems;
314+
set
315+
{
316+
if (_selectedItems != value)
317+
{
318+
_selectedItems = value;
319+
OnPropertyChanged();
320+
OnPropertyChanged(nameof(SelectedItemsCount));
321+
OnPropertyChanged(nameof(SelectedItemText));
322+
}
323+
}
324+
}
325+
public int SelectedItemsCount
326+
{
327+
get
328+
{
329+
if (SelectionMode == SelectionMode.Single)
330+
{
331+
return SelectedItem != null ? 1 : 0;
332+
}
333+
else if (SelectionMode == SelectionMode.Multiple)
334+
{
335+
return SelectedItems?.Count ?? 0;
336+
}
337+
return 0;
338+
339+
}
340+
}
341+
public string SelectedItemText
342+
{
343+
get
344+
{
345+
if (SelectionMode == SelectionMode.Single && SelectedItem is CollectionViewTestItem item)
346+
{
347+
return $"{item.Caption}";
348+
}
349+
else if (SelectionMode == SelectionMode.Multiple && SelectedItems?.Count > 0)
350+
{
351+
var selectedCaptions = SelectedItems
352+
.OfType<CollectionViewTestItem>()
353+
.Select(i => i.Caption);
354+
return string.Join(", ", selectedCaptions);
355+
}
356+
return "No items selected";
357+
}
358+
}
359+
360+
public int SelectionChangedEventCount
361+
{
362+
get => _selectionChangedEventCount;
363+
set
364+
{
365+
_selectionChangedEventCount = value;
366+
OnPropertyChanged();
367+
}
368+
}
369+
370+
public string PreviousSelectionText
371+
{
372+
get => _previousSelectionText;
373+
set { _previousSelectionText = value; OnPropertyChanged(); }
374+
}
375+
376+
public string CurrentSelectionText
377+
{
378+
get => _currentSelectionText;
379+
set { _currentSelectionText = value; OnPropertyChanged(); }
380+
}
381+
273382

274383
private void LoadItems()
275384
{
@@ -433,6 +542,12 @@ private void SetItemTemplate()
433542
}
434543
}
435544

545+
private void OnSelectedItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
546+
{
547+
OnPropertyChanged(nameof(SelectedItemsCount));
548+
OnPropertyChanged(nameof(SelectedItemText));
549+
}
550+
436551
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
437552
{
438553
if (propertyName == nameof(IsGrouped))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.CollectionViewSelectionPage"
6+
Title="CollectionViewControlPage">
7+
<ContentPage.ToolbarItems>
8+
<ToolbarItem Text="Options"
9+
Clicked="NavigateToOptionsPage_Clicked"
10+
AutomationId="Options"/>
11+
</ContentPage.ToolbarItems>
12+
<Grid Padding="20"
13+
RowSpacing="10"
14+
ColumnSpacing="10">
15+
<Grid.RowDefinitions>
16+
<RowDefinition Height="*"/>
17+
<RowDefinition Height="Auto"/>
18+
<RowDefinition Height="Auto"/>
19+
<RowDefinition Height="Auto"/>
20+
<RowDefinition Height="Auto"/>
21+
<RowDefinition Height="Auto"/>
22+
</Grid.RowDefinitions>
23+
<Grid.ColumnDefinitions>
24+
<ColumnDefinition Width="*"/>
25+
<ColumnDefinition Width="*"/>
26+
</Grid.ColumnDefinitions>
27+
28+
<!-- CollectionView -->
29+
<CollectionView
30+
x:Name="collectionView"
31+
Grid.Row="0"
32+
Grid.ColumnSpan="2"
33+
ItemsSource="{Binding ItemsSource}"
34+
ItemTemplate="{Binding ItemTemplate}"
35+
ItemsLayout="{Binding ItemsLayout}"
36+
GroupHeaderTemplate="{Binding GroupHeaderTemplate}"
37+
IsGrouped="{Binding IsGrouped}"
38+
SelectionMode="{Binding SelectionMode}"
39+
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
40+
SelectedItems="{Binding SelectedItems, Mode=TwoWay}"
41+
SelectionChanged="OnCollectionViewSelectionChanged"
42+
AutomationId="CollectionViewControl">
43+
</CollectionView>
44+
45+
<!-- Display Selected Item -->
46+
<Label Text="Selected Item:"
47+
FontSize="14"
48+
FontAttributes="Bold"
49+
Grid.Row="1"
50+
Grid.Column="0"/>
51+
<Label Text="{Binding SelectedItemText}"
52+
AutomationId="SelectedSingle"
53+
FontSize="14"
54+
TextColor="Blue"
55+
Grid.Row="1"
56+
Grid.Column="1"/>
57+
58+
<!-- Display Selected Items Count -->
59+
<Label Text="Selected Items Count:"
60+
FontSize="14"
61+
FontAttributes="Bold"
62+
Grid.Row="2"
63+
Grid.Column="0"/>
64+
<Label Text="{Binding SelectedItemsCount}"
65+
AutomationId="SelectedMultiple"
66+
FontSize="14"
67+
TextColor="Blue"
68+
Grid.Row="2"
69+
Grid.Column="1"/>
70+
71+
72+
<!-- Display SelectionChangedEventArgs -->
73+
<Label Text="SelectionChangedEvent Trigger Count:"
74+
FontSize="14"
75+
FontAttributes="Bold"
76+
Grid.Row="3"
77+
Grid.Column="0"/>
78+
<Label Text="{Binding SelectionChangedEventCount, StringFormat='{0} times'}"
79+
FontSize="14"
80+
TextColor="Blue"
81+
Grid.Row="3"
82+
Grid.Column="1"
83+
AutomationId="SelectionChangedEventCountLabel"/>
84+
85+
<!--Display Previous Selection Text-->
86+
<Label Text="Previous Selection:"
87+
FontSize="14"
88+
FontAttributes="Bold"
89+
Grid.Row="4"
90+
Grid.Column="0"/>
91+
<Label Text="{Binding PreviousSelectionText}"
92+
FontSize="14"
93+
TextColor="Blue"
94+
Grid.Row="4"
95+
Grid.Column="1"
96+
AutomationId="PreviousSelectionTextLabel"/>
97+
<!--Display Current Selection Text-->
98+
<Label Text="Current Selection:"
99+
FontSize="14"
100+
FontAttributes="Bold"
101+
Grid.Row="5"
102+
Grid.Column="0"/>
103+
<Label Text="{Binding CurrentSelectionText}"
104+
FontSize="14"
105+
TextColor="Blue"
106+
Grid.Row="5"
107+
Grid.Column="1"
108+
AutomationId="CurrentSelectionTextLabel"/>
109+
</Grid>
110+
</ContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using Microsoft.Maui.Controls;
3+
using System.Collections.ObjectModel;
4+
5+
namespace Maui.Controls.Sample
6+
{
7+
public partial class CollectionViewSelectionPage : ContentPage
8+
{
9+
private CollectionViewViewModel _viewModel;
10+
11+
public CollectionViewSelectionPage()
12+
{
13+
InitializeComponent();
14+
_viewModel = new CollectionViewViewModel();
15+
_viewModel.ItemsSourceType = ItemsSourceType.ObservableCollection5T;
16+
BindingContext = _viewModel;
17+
}
18+
19+
private async void NavigateToOptionsPage_Clicked(object sender, EventArgs e)
20+
{
21+
BindingContext = _viewModel = new CollectionViewViewModel();
22+
_viewModel.ItemsSourceType = ItemsSourceType.ObservableCollection5T;
23+
await Navigation.PushAsync(new SelectionOptionsPage(_viewModel));
24+
}
25+
26+
void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
27+
{
28+
if (BindingContext is CollectionViewViewModel vm)
29+
{
30+
vm.SelectionChangedEventCount++;
31+
32+
var previousSelection = e.PreviousSelection.Any()
33+
? string.Join(", ", e.PreviousSelection.OfType<CollectionViewViewModel.CollectionViewTestItem>().Select(item => item.Caption))
34+
: "No previous items";
35+
36+
var currentSelection = e.CurrentSelection.Any()
37+
? string.Join(", ", e.CurrentSelection.OfType<CollectionViewViewModel.CollectionViewTestItem>().Select(item => item.Caption))
38+
: "No current items";
39+
vm.PreviousSelectionText = $"{previousSelection}";
40+
vm.CurrentSelectionText = $"{currentSelection}";
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)