Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix support of ISelectionInfo in ListViewBase #17582

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,45 @@
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Private.Infrastructure;
using Uno.UI.RuntimeTests.Helpers;
using Windows.Foundation;
using Windows.UI.Input.Preview.Injection;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;

namespace Uno.UI.RuntimeTests.Tests.Microsoft_UI_Xaml_Controls
{
[TestClass]
public class Given_ListView
{
[TestMethod]
[RunsOnUIThread]
#if !HAS_INPUT_INJECTOR
[Ignore("InputInjector is only supported on Skia #14948")]
vatsashah45 marked this conversation as resolved.
Show resolved Hide resolved
#endif
public async Task When_ItemClicked_SelectsCorrectIndex()
{
var loggingSelectionInfo = new LoggingSelectionInfo();
var listViewBase = new ListViewBase
{
ItemsSource = loggingSelectionInfo
};

TestServices.WindowHelper.WindowContent = listViewBase;
await TestServices.WindowHelper.WaitForLoaded(listViewBase);

// We don't use ActualWidth because of https://github.com/unoplatform/uno/issues/15982
var tapTarget = listViewBase.TransformToVisual(null).TransformPoint(new Point(112 * 0.9, listViewBase.ActualHeight / 2));
var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
using var finger = injector.GetFinger();

finger.Press(tapTarget);
finger.Release();

var selectionInfo = (MockSelectionInfo)listViewBase.ItemsSource;
Assert.AreEqual(0, selectionInfo.SelectedIndex);
}
vatsashah45 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Microsoft.UI.Xaml.Data;

public class LoggingSelectionInfo : ISelectionInfo
vatsashah45 marked this conversation as resolved.
Show resolved Hide resolved
{
public List<string> MethodLog { get; } = new List<string>();

Check warning on line 6 in src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/LoggingSelectionInfo.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/LoggingSelectionInfo.cs#L6

Refactor this property to use a generic collection designed for inheritance.

public void SelectRange(ItemIndexRange itemIndexRange)
{
MethodLog.Add($"SelectRange({itemIndexRange.FirstIndex}, {itemIndexRange.Length})");
}

public void DeselectRange(ItemIndexRange itemIndexRange)
{
MethodLog.Add($"DeselectRange({itemIndexRange.FirstIndex}, {itemIndexRange.Length})");
}

public bool IsSelected(int index)
{
MethodLog.Add($"IsSelected({index})");
return false;
}

public IReadOnlyList<ItemIndexRange> GetSelectedRanges()
{
MethodLog.Add("GetSelectedRanges()");
return new List<ItemIndexRange>();
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/ListViewBase/ListViewBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ internal override void OnItemClicked(int clickedIndex, VirtualKeyModifiers modif
void SingleSelectionCase()
{

if (ItemsSource is ICollectionView collectionView)
if (ItemsSource is ICollectionView collectionView and not ISelectionInfo)
vatsashah45 marked this conversation as resolved.
Show resolved Hide resolved
vatsashah45 marked this conversation as resolved.
Show resolved Hide resolved
{
//NOTE: Windows seems to call MoveCurrentTo(item); we set position instead to have expected behavior when you have duplicate items in the list.
collectionView.MoveCurrentToPosition(clickedIndex);
Expand Down
Loading