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(ListViewBase): Adjust indices repairing considering recursive list manipulation #11841

Merged
merged 4 commits into from
Mar 30, 2023
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 @@ -15,6 +15,9 @@
using TreeViewItem = Microsoft.UI.Xaml.Controls.TreeViewItem;
using Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls.TreeViewTests;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Uno.UI;
using MUXControlsTestApp.Utilities;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls;

Expand All @@ -36,7 +39,6 @@ public async Task When_Open_Close_Twice()

var root = new MyNode();
root.Name = "root";
root.Children = new List<MyNode>();
var child1 = new MyNode { Name = "Child 1" };
var child2 = new MyNode { Name = "Child 2" };
root.Children.Add(child1);
Expand Down Expand Up @@ -83,7 +85,6 @@ public async Task When_Open_Close_Twice_Grid()
var root = new MyNode();
root.Name = "root 4";
root.IsDirectory = true;
root.Children = new List<MyNode>();
var child1 = new MyNode { Name = "Child 1" };
var child2 = new MyNode { Name = "Child 2" };
root.Children.Add(child1);
Expand Down Expand Up @@ -123,6 +124,98 @@ public async Task When_Open_Close_Twice_Grid()

Assert.AreEqual(child2, child2NodeAfter.DataContext);
}

#if __ANDROID__
[Ignore("Test is not operational on Android, items are not returned properly https://github.com/unoplatform/uno/issues/9080")]
#endif
[TestMethod]
public async Task When_Open_Close_Root_Twice_Keep_State()
{
var container = new Grid();
container.Width = 500;
container.Height = 500;

var SUT = new When_Open_Close_Twice_Grid();

container.Children.Add(SUT);

TestServices.WindowHelper.WindowContent = container;

var root = new MyNode();
root.Name = "root 4";
root.IsDirectory = true;
var child1 = new MyNode { Name = "Child 1", IsDirectory = true };
var child2 = new MyNode { Name = "Child 2", IsDirectory = true };
var child3 = new MyNode { Name = "Child 3", IsDirectory = true };
root.Children.Add(child1);
root.Children.Add(child2);
root.Children.Add(child3);

var child11 = new MyNode { Name = "Child 11" };
var child12 = new MyNode { Name = "Child 12" };
child1.Children.Add(child11);
child1.Children.Add(child12);

var child21 = new MyNode { Name = "Child 21" };
var child22 = new MyNode { Name = "Child 22" };
child2.Children.Add(child21);
child2.Children.Add(child22);

var child31 = new MyNode { Name = "Child 31" };
var child32 = new MyNode { Name = "Child 32" };
child3.Children.Add(child31);
child3.Children.Add(child32);

SUT.myTree.ItemsSource = new[] { root };
await TestServices.WindowHelper.WaitForIdle();

var rootNode = (TreeViewItem)SUT.myTree.ContainerFromItem(root);
rootNode.IsExpanded = true;
await TestServices.WindowHelper.WaitForIdle();

var child2Node = (TreeViewItem)SUT.myTree.ContainerFromItem(child2);
child2Node.IsExpanded = true;
await TestServices.WindowHelper.WaitForIdle();

rootNode.IsExpanded = false;
await TestServices.WindowHelper.WaitForIdle();

rootNode.IsExpanded = true;
await TestServices.WindowHelper.WaitForIdle();

// Force refresh
container.Width = 700;
container.Height = 700;

await TestServices.WindowHelper.WaitForIdle();

var child1Node = (TreeViewItem)SUT.myTree.ContainerFromItem(child1);
Assert.IsNotNull(child1Node);
Assert.AreEqual(child1, child1Node.DataContext);

rootNode.IsExpanded = false;
await TestServices.WindowHelper.WaitForIdle();

rootNode.IsExpanded = true;
await TestServices.WindowHelper.WaitForIdle();

var child3Node = (TreeViewItem)SUT.myTree.ContainerFromItem(child3);

// When index mappings are incorrect, the third node is expanded because
// of recursive items removal in the TreeView control.
Assert.IsFalse(child3Node.IsExpanded);
child3Node.IsExpanded = false;
await TestServices.WindowHelper.WaitForIdle();

child3Node.IsExpanded = true;
await TestServices.WindowHelper.WaitForIdle();
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved

#if HAS_UNO
Assert.AreEqual(
1,
MUXTestPage.FindVisualChildrenByType<TextBlock>(SUT.myTree).Count(c => c.Text == "Child 21"));
#endif
}
}

public class MyNode
Expand All @@ -131,7 +224,7 @@ public class MyNode

public bool IsDirectory { get; set; }

public List<MyNode> Children { get; set; }
public ObservableCollection<MyNode> Children { get; } = new ObservableCollection<MyNode>();
}

public class FSObjectTemplateSelector : DataTemplateSelector
Expand Down
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 @@ -575,8 +575,8 @@ internal override void OnItemsSourceSingleCollectionChanged(object sender, Notif
SaveContainersBeforeRemoveForIndexRepair(args.OldStartingIndex, args.OldItems.Count);
var removedContainers = CaptureContainers(args.OldStartingIndex, args.OldItems.Count);
RemoveItems(args.OldStartingIndex, args.OldItems.Count, section);
CleanUpContainers(removedContainers);
RepairIndices();
CleanUpContainers(removedContainers);

break;
case NotifyCollectionChangedAction.Replace:
Expand Down