From 033b01abb201ae276d01393f2a787af56d4acf89 Mon Sep 17 00:00:00 2001 From: dustin-wojciechowski Date: Tue, 27 Aug 2024 09:24:16 -0700 Subject: [PATCH] [Testing] CollectionViewInfiniteScroll Conversion to UI Test (#24411) * 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 --- .../src/Issues.Shared/Github5623.xaml.cs | 1 + .../TestCases.HostApp/Issues/Github5623.cs | 145 ++++++++++++++++++ .../TestCases.HostApp/Issues/Github5623.xaml | 38 +++++ .../Tests/Issues/Github5623.cs | 29 ++++ 4 files changed, 213 insertions(+) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Github5623.cs create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Github5623.xaml create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Github5623.cs diff --git a/src/Compatibility/ControlGallery/src/Issues.Shared/Github5623.xaml.cs b/src/Compatibility/ControlGallery/src/Issues.Shared/Github5623.xaml.cs index b1b0f9d89b6f..899cd7ea907b 100644 --- a/src/Compatibility/ControlGallery/src/Issues.Shared/Github5623.xaml.cs +++ b/src/Compatibility/ControlGallery/src/Issues.Shared/Github5623.xaml.cs @@ -115,6 +115,7 @@ protected override void Init() } #if UITEST + [MovedToAppium] [Test] [Compatibility.UITests.FailsOnMauiIOS] public void CollectionViewInfiniteScroll() diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Github5623.cs b/src/Controls/tests/TestCases.HostApp/Issues/Github5623.cs new file mode 100644 index 000000000000..05d423855aed --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Github5623.cs @@ -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> GetNextSetAsync() + { + return await Task.Run(() => + { + var collection = new ObservableCollection(); + 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; + 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 Items { get; set; } + + public Command RemainingItemsThresholdReachedCommand { get; set; } + + public ItemSizingStrategy ItemSizingStrategy { get; set; } = ItemSizingStrategy.MeasureAllItems; + + public ViewModel5623() + { + var collection = new ObservableCollection(); + 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 + ")"; + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Github5623.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Github5623.xaml new file mode 100644 index 000000000000..27720a659064 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Github5623.xaml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Github5623.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Github5623.cs new file mode 100644 index 000000000000..9974e34de545 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Github5623.cs @@ -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 \ No newline at end of file