Skip to content

Commit

Permalink
[Testing] CollectionViewInfiniteScroll Conversion to UI Test (#24411)
Browse files Browse the repository at this point in the history
* Initial Commit

* Solved issue of automation id not picking up

* Renamed files, set correct issue text

* Removed unnecessary variables, comments

* Disabled test for Catalyst as well

* Fixed previous

* Added MovedToAppium tag for original test
  • Loading branch information
dustin-wojciechowski authored Aug 27, 2024
1 parent d93e48d commit 033b01a
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ protected override void Init()
}

#if UITEST
[MovedToAppium]
[Test]
[Compatibility.UITests.FailsOnMauiIOS]
public void CollectionViewInfiniteScroll()
Expand Down
145 changes: 145 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Github5623.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

using System.Collections.ObjectModel;
using Microsoft.Maui.Controls.Internals;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 5623, "CollectionView with Incremental Collection (RemainingItemsThreshold)", PlatformAffected.All)]
public partial class Github5623 : ContentPage
{
int _itemCount = 10;
const int MaximumItemCount = 100;
const int PageSize = 10;
static readonly SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1, 1);
public Github5623()
{
InitializeComponent();
BindingContext = new ViewModel5623();
}

void CollectionView_OnScrolled(object sender, ItemsViewScrolledEventArgs e)
{
Label1.Text = "HorizontalDelta: " + e.HorizontalDelta;
Label2.Text = "VerticalDelta: " + e.VerticalDelta;
Label3.Text = "HorizontalOffset: " + e.HorizontalOffset;
Label4.Text = "VerticalOffset: " + e.VerticalOffset;
Label5.Text = "FirstVisibleItemIndex: " + e.FirstVisibleItemIndex;
Label6.Text = "CenterItemIndex: " + e.CenterItemIndex;
Label7.Text = "LastVisibleItemIndex: " + e.LastVisibleItemIndex;
}

async Task<ObservableCollection<Model5623>> GetNextSetAsync()
{
return await Task.Run(() =>
{
var collection = new ObservableCollection<Model5623>();
var count = PageSize;
if (_itemCount + count > MaximumItemCount)
count = MaximumItemCount - _itemCount;
for (var i = _itemCount; i < _itemCount + count; i++)
{
collection.Add(new Model5623((BindingContext as ViewModel5623).ItemSizingStrategy == ItemSizingStrategy.MeasureAllItems)
{
Text = i.ToString(),
BackgroundColor = i % 2 == 0 ? Colors.AntiqueWhite : Colors.Lavender,
AutomationId = i.ToString()
});
}
_itemCount += count;
return collection;
});
}

async void CollectionView_RemainingItemsThresholdReached(object sender, System.EventArgs e)
{
await SemaphoreSlim.WaitAsync();
try
{
var itemsSource = (sender as CollectionView).ItemsSource as ObservableCollection<Model5623>;
var nextSet = await GetNextSetAsync();

// nothing to add
if (nextSet.Count == 0)
return;

Dispatcher.Dispatch(() =>
{
foreach (var item in nextSet)
{
itemsSource.Add(item);
}
});

System.Diagnostics.Debug.WriteLine("Count: " + itemsSource.Count);
}
finally
{
SemaphoreSlim.Release();
}
}
}

[Preserve(AllMembers = true)]
public class ViewModel5623
{
public ObservableCollection<Model5623> Items { get; set; }

public Command RemainingItemsThresholdReachedCommand { get; set; }

public ItemSizingStrategy ItemSizingStrategy { get; set; } = ItemSizingStrategy.MeasureAllItems;

public ViewModel5623()
{
var collection = new ObservableCollection<Model5623>();
var pageSize = 10;

for (var i = 0; i < pageSize; i++)
{
collection.Add(new Model5623(ItemSizingStrategy == ItemSizingStrategy.MeasureAllItems)
{
Text = i.ToString(),
BackgroundColor = i % 2 == 0 ? Colors.AntiqueWhite : Colors.Lavender,
AutomationId = i.ToString()
});
}

Items = collection;

RemainingItemsThresholdReachedCommand = new Command(() =>
{
System.Diagnostics.Debug.WriteLine($"{nameof(RemainingItemsThresholdReachedCommand)} called");
});
}
}

[Preserve(AllMembers = true)]
public class Model5623
{
Random random = new Random();

public string Text { get; set; }

public Color BackgroundColor { get; set; }

public int Height { get; set; } = 200;

public string HeightText { get; private set; }

public string AutomationId {get; set;}

public Model5623(bool isUneven)
{
var byteArray = new byte[4];
random.NextBytes(byteArray);

if (isUneven)
Height = 20 + (BitConverter.ToInt32(byteArray, 0) % 300 + 300) % 300;

HeightText = "(Height: " + Height + ")";
}
}
}
38 changes: 38 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Github5623.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui.Controls.Sample.CollectionViewGalleries"
x:Class="Maui.Controls.Sample.Issues.Github5623">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition/>
</Grid.RowDefinitions>

<StackLayout Orientation="Vertical" Spacing="5" Grid.Row="0" VerticalOptions="Center">
<Label x:Name="Label" LineBreakMode="WordWrap" Text="Scroll down until you hit 99" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label1" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label2" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label3" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label4" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label5" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label6" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Label x:Name="Label7" LineBreakMode="WordWrap" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
</StackLayout>

<CollectionView Grid.Row="1" AutomationId="CollectionView5623" ItemSizingStrategy="{Binding ItemSizingStrategy}" ItemsSource="{Binding Items}" Scrolled="CollectionView_OnScrolled" RemainingItemsThreshold="25" RemainingItemsThresholdReached="CollectionView_RemainingItemsThresholdReached" RemainingItemsThresholdReachedCommand="{Binding RemainingItemsThresholdReachedCommand}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="{Binding Height}" BackgroundColor="{Binding BackgroundColor}">
<StackLayout Spacing="10" HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="{Binding Text}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" AutomationId="{Binding AutomationId}"/>
<Label Text="{Binding HeightText}" FontSize="Micro" HorizontalTextAlignment="Center" VerticalTextAlignment="End"/>
</StackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<BoxView Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="Center" BackgroundColor="Red" HeightRequest="5"/>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if !MACCATALYST && !IOS
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Github5623 : _IssuesUITest
{
const string automationId = "CollectionView5623";

public Github5623(TestDevice device)
: base(device)
{
}

public override string Issue => "CollectionView with Incremental Collection (RemainingItemsThreshold)";

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewInfiniteScroll()
{
App.WaitForElement(automationId);
App.ScrollTo("20");
}
}
}
#endif

0 comments on commit 033b01a

Please sign in to comment.