Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -596,7 +596,8 @@ void ApplyItemsUpdatingScrollMode()
return;
}

if (!PlatformView.IsLoaded)
// Collapsed views can be loaded without an ItemsRepeater; WinUI cannot scroll them.
if (!PlatformView.IsLoaded || PlatformView.Visibility != WVisibility.Visible)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,85 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class CollectionViewTests
{
[Theory]
[InlineData(ItemsUpdatingScrollMode.KeepItemsInView, false)]
[InlineData(ItemsUpdatingScrollMode.KeepItemsInView, true)]
[InlineData(ItemsUpdatingScrollMode.KeepLastItemInView, false)]
[InlineData(ItemsUpdatingScrollMode.KeepLastItemInView, true)]
public async Task CollectionView2SharedItemsSourceUpdatesWhileHidden(ItemsUpdatingScrollMode scrollMode, bool hiddenFirst)
{
SetupBuilderCollectionView2();

var items = new ObservableCollection<string> { "Item 1", "Item 2", "Item 3" };
var itemTemplate = new Controls.DataTemplate(() =>
{
var label = new Label { HeightRequest = 40 };
label.SetBinding(Label.TextProperty, ".");
return label;
});
var visible = new CollectionView
{
ItemsSource = items,
ItemTemplate = itemTemplate,
ItemsUpdatingScrollMode = scrollMode,
HeightRequest = 200
};
var hidden = new CollectionView
{
ItemsSource = items,
ItemTemplate = itemTemplate,
ItemsUpdatingScrollMode = scrollMode,
HeightRequest = 200,
IsVisible = false
};
var layout = new VerticalStackLayout();
layout.Add(hiddenFirst ? hidden : visible);
layout.Add(hiddenFirst ? visible : hidden);

await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async handler =>
{
var visiblePlatformView = Assert.IsType<CollectionViewHandler2>(visible.Handler).PlatformView;
var hiddenPlatformView = Assert.IsType<CollectionViewHandler2>(hidden.Handler).PlatformView;
await AssertEventually(() => visiblePlatformView.IsLoaded && hiddenPlatformView.IsLoaded);

items.Add("Item 4");
handler.PlatformView.UpdateLayout();
await Task.Delay(100);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[moderate] Regression Prevention and Test Coverage - The visible CollectionView does not provide a discriminating negative test for the new visibility guard: its only assertions compare ItemsSource contents, which are updated independently of ApplyItemsUpdatingScrollMode. The reveal assertions likewise check loaded labels rather than scrolling, and the fixture starts with only three 40px rows in a 200px viewport. Returning unconditionally from ApplyItemsUpdatingScrollMode would still satisfy these assertions, while breaking automatic scrolling for visible views. Add an overflowing visible-view case and assert the resulting scroll position for both modes (starting away from the first item for KeepItemsInView), so the test proves that visible updates are not suppressed along with hidden ones.

Assert.Equal(items, Assert.IsAssignableFrom<System.Collections.IEnumerable>(visiblePlatformView.ItemsSource)
.Cast<ItemTemplateContext2>().Select(context => Assert.IsType<string>(context.Item)));
Assert.Equal(items, Assert.IsAssignableFrom<System.Collections.IEnumerable>(hiddenPlatformView.ItemsSource)
.Cast<ItemTemplateContext2>().Select(context => Assert.IsType<string>(context.Item)));

hidden.IsVisible = true;
await AssertEventually(() => hidden.LogicalChildrenInternal.OfType<Label>()
.Any(label => label.Text == "Item 4" && label.IsLoaded));

// Hide after layout has queued a scroll but before the dispatcher executes it.
items.Add("Item 5");
handler.PlatformView.UpdateLayout();
hidden.IsVisible = false;
await Task.Delay(100);

items.RemoveAt(0);
items[0] = "Updated item";
items.Move(0, items.Count - 1);
items.Clear();
items.Add("After reset");
handler.PlatformView.UpdateLayout();
await Task.Delay(100);

Assert.Equal(items, Assert.IsAssignableFrom<System.Collections.IEnumerable>(visiblePlatformView.ItemsSource)
.Cast<ItemTemplateContext2>().Select(context => Assert.IsType<string>(context.Item)));
Assert.Equal(items, Assert.IsAssignableFrom<System.Collections.IEnumerable>(hiddenPlatformView.ItemsSource)
.Cast<ItemTemplateContext2>().Select(context => Assert.IsType<string>(context.Item)));

hidden.IsVisible = true;
await AssertEventually(() => hidden.LogicalChildrenInternal.OfType<Label>()
.Any(label => label.Text == "After reset" && label.IsLoaded));
});
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down
Loading