|
| 1 | +using Microsoft.UI.Xaml; |
| 2 | +using Microsoft.UI.Xaml.Controls; |
| 3 | +using Microsoft.UI.Xaml.Data; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace WinUI.TableView.Tests; |
| 9 | + |
| 10 | +[TestClass] |
| 11 | +public class TableViewSelectionUnitTests |
| 12 | +{ |
| 13 | + [UITestMethod] |
| 14 | + public async Task CellWithRow_CellClickSelectsCellAndOwningRow() |
| 15 | + { |
| 16 | + var tableView = await CreateTableViewAsync(TableViewSelectionUnit.CellWithRow); |
| 17 | + |
| 18 | + tableView.MakeSelection(new TableViewCellSlot(1, 0), false, false); |
| 19 | + |
| 20 | + await Task.Yield(); // Allow selection to propagate |
| 21 | + |
| 22 | + Assert.IsTrue(tableView.SelectedCells.Contains(new TableViewCellSlot(1, 0))); |
| 23 | + Assert.AreEqual(1, tableView.SelectedItems.Count); |
| 24 | + Assert.AreSame(tableView.Items[1], tableView.SelectedItem); |
| 25 | + } |
| 26 | + |
| 27 | + [UITestMethod] |
| 28 | + public async Task CellAndRow_RowHeaderClickSelectsOnlyRow() |
| 29 | + { |
| 30 | + var tableView = await CreateTableViewAsync(TableViewSelectionUnit.CellWithRow); |
| 31 | + |
| 32 | + tableView.MakeSelection(new TableViewCellSlot(1, -1), false, false); |
| 33 | + |
| 34 | + Assert.AreEqual(0, tableView.SelectedCells.Count); |
| 35 | + Assert.AreEqual(1, tableView.SelectedItems.Count); |
| 36 | + Assert.AreSame(tableView.Items[1], tableView.SelectedItem); |
| 37 | + } |
| 38 | + |
| 39 | + [UITestMethod] |
| 40 | + public async Task CellSelectionUnitStillSelectsOnlyCells() |
| 41 | + { |
| 42 | + var tableView = await CreateTableViewAsync(TableViewSelectionUnit.Cell); |
| 43 | + |
| 44 | + tableView.MakeSelection(new TableViewCellSlot(0, 0), false, false); |
| 45 | + |
| 46 | + await Task.Yield(); // Allow selection to propagate |
| 47 | + |
| 48 | + tableView.MakeSelection(new TableViewCellSlot(1, 1), false, true); |
| 49 | + |
| 50 | + await Task.Yield(); // Allow selection to propagate |
| 51 | + |
| 52 | + Assert.AreEqual(0, tableView.SelectedItems.Count); |
| 53 | + Assert.AreEqual(2, tableView.SelectedCells.Count); |
| 54 | + Assert.IsTrue(tableView.SelectedCells.Contains(new TableViewCellSlot(0, 0))); |
| 55 | + Assert.IsTrue(tableView.SelectedCells.Contains(new TableViewCellSlot(1, 1))); |
| 56 | + } |
| 57 | + |
| 58 | + [UITestMethod] |
| 59 | + public async Task CellOrRowSelectionUnitStillUsesCellAndRowSemantics() |
| 60 | + { |
| 61 | + var tableView = await CreateTableViewAsync(TableViewSelectionUnit.CellOrRow); |
| 62 | + |
| 63 | + tableView.MakeSelection(new TableViewCellSlot(1, 0), false, false); |
| 64 | + Assert.AreEqual(0, tableView.SelectedItems.Count); |
| 65 | + |
| 66 | + tableView.MakeSelection(new TableViewCellSlot(0, -1), false, false); |
| 67 | + Assert.AreEqual(1, tableView.SelectedItems.Count); |
| 68 | + Assert.AreEqual(0, tableView.SelectedCells.Count); |
| 69 | + } |
| 70 | + |
| 71 | + [UITestMethod] |
| 72 | + public async Task CellWithRow_MultiSelectionAddsCellAndRowSelections() |
| 73 | + { |
| 74 | + var tableView = await CreateTableViewAsync(TableViewSelectionUnit.CellWithRow, ListViewSelectionMode.Multiple); |
| 75 | + |
| 76 | + tableView.MakeSelection(new TableViewCellSlot(0, 0), false, false); |
| 77 | + |
| 78 | + await Task.Yield(); // Allow selection to propagate |
| 79 | + |
| 80 | + tableView.MakeSelection(new TableViewCellSlot(1, 1), false, true); |
| 81 | + |
| 82 | + await Task.Delay(200); // Allow selection to propagate |
| 83 | + |
| 84 | + Assert.AreEqual(2, tableView.SelectedItems.Count); |
| 85 | + Assert.AreEqual(2, tableView.SelectedCells.Count); |
| 86 | + Assert.IsTrue(tableView.SelectedCells.Contains(new TableViewCellSlot(0, 0))); |
| 87 | + Assert.IsTrue(tableView.SelectedCells.Contains(new TableViewCellSlot(1, 1))); |
| 88 | + } |
| 89 | + |
| 90 | + private static async Task<TableView> CreateTableViewAsync(TableViewSelectionUnit selectionUnit, ListViewSelectionMode selectionMode = ListViewSelectionMode.Extended) |
| 91 | + { |
| 92 | + var tableView = new TableView |
| 93 | + { |
| 94 | + SelectionMode = selectionMode, |
| 95 | + SelectionUnit = selectionUnit |
| 96 | + }; |
| 97 | + |
| 98 | + tableView.Columns.Add(new TableViewTextColumn |
| 99 | + { |
| 100 | + Header = "Name", |
| 101 | + Binding = new Binding { Path = new PropertyPath(nameof(SelectionItem.Name)) } |
| 102 | + }); |
| 103 | + tableView.Columns.Add(new TableViewTextColumn |
| 104 | + { |
| 105 | + Header = "Value", |
| 106 | + Binding = new Binding { Path = new PropertyPath(nameof(SelectionItem.Value)) } |
| 107 | + }); |
| 108 | + tableView.ItemsSource = new[] |
| 109 | + { |
| 110 | + new SelectionItem { Name = "A", Value = 1 }, |
| 111 | + new SelectionItem { Name = "B", Value = 2 } |
| 112 | + }; |
| 113 | + |
| 114 | + await UnitTestApp.Current.MainWindow.LoadTestContentAsync(tableView); |
| 115 | + |
| 116 | + return tableView; |
| 117 | + } |
| 118 | + |
| 119 | + private sealed class SelectionItem |
| 120 | + { |
| 121 | + public string Name { get; set; } = string.Empty; |
| 122 | + public int Value { get; set; } |
| 123 | + } |
| 124 | +} |
0 commit comments