Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public LoopObservableItemsSource2(IEnumerable itemSource, UICollectionViewContro

protected override NSIndexPath[] CreateIndexesFrom(int startIndex, int count)
{
if (!Loop)
{
return base.CreateIndexesFrom(startIndex, count);
}
if (ItemCount == 0)
{
count += 2;
Expand Down
74 changes: 74 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue31535.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 31535, "[iOS] Crash occurred on CarouselView2 when deleting last one remaining item with loop as false", PlatformAffected.iOS)]
public class Issue31535 : ContentPage
{
ObservableCollection<string> _items = new();
public ObservableCollection<string> Items
{
get => _items;
set
{
_items = value;
OnPropertyChanged();
}
}
public Issue31535()
{
// Initialize items
Items = new ObservableCollection<string>
{
"Item 1",
"Item 2",
};

// Create CarouselView
var carousel = new CarouselView2
{
Loop = false,
HeightRequest = 200,
ItemsSource = Items,
AutomationId = "TestCarouselView",
ItemTemplate = new DataTemplate(() =>
{
var border = new Border
{
Margin = 10,
WidthRequest = 200,
BackgroundColor = Colors.Red
};

var label = new Label();
label.SetBinding(Label.TextProperty, ".");

border.Content = label;
return border;
})
};

// Create Button
var button = new Button
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can include other button to add items?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test covering scenarios.

  1. Started with Empty colleciton
    2.Added Single Item
    3.Removed single item
    4.Added Multiple Items
    5.Removed All Items

@jsuarezruiz

{
AutomationId = "RemoveLastItemButton",
Text = "Remove last item"
};

button.Clicked += (s, e) =>
{
if (Items.Count > 0)
Items.RemoveAt(Items.Count - 1);
};

Content = new VerticalStackLayout
{
Children =
{
carousel,
button
}
};
BindingContext = this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue31535 : _IssuesUITest
{
public Issue31535(TestDevice device) : base(device) { }

public override string Issue => "[iOS] Crash occurred on CarouselView2 when deleting last one remaining item with loop as false";

[Test]
[Category(UITestCategories.CarouselView)]
public void CarouselView2ShouldNotCrashOnRemoveLastItem()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could include another test, similar but removing more than one time the last item?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current test already performs the remove action twice, so it seems you might be referring to the same scenario. Could you please confirm if this is sufficient, or would you prefer that we add more items and invoke the remove function multiple times? @jsuarezruiz

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like:

  1. Start with empty collection
  2. Add single item - should not crash
  3. Add multiple items - should work correctly
  4. Remove all items - should handle gracefully

{
App.WaitForElement("RemoveLastItemButton");
App.Tap("RemoveLastItemButton");
//remove last one remaining item
App.Tap("RemoveLastItemButton");
App.WaitForElement("TestCarouselView");
}
}
Loading