Skip to content
Merged
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
115 changes: 115 additions & 0 deletions src/Tedd.TUI.Tests/ListBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,119 @@ public void Render_FallsBackToGetItemText_WhenNoTemplate()
Assert.Equal("Hello", row0);
Assert.Equal("World", row1);
}

[Fact]
public void ItemIsSelected_ViaIsSelectedProperty_UpdatesListBoxSelectedIndex()
{
var listBox = new ListBox();
listBox.Items.Add("Item 1");
listBox.Items.Add("Item 2");
listBox.Items.Add("Item 3");

// ItemsPanelRoot is populated after items are added (via OnItemsCollectionChanged → PopulatePanel)
Assert.NotNull(listBox.ItemsPanelRoot);
var container1 = listBox.ItemsPanelRoot!.Children[1] as ListBoxItem;
Comment on lines +113 to +115

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

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

The comment implies ItemsPanelRoot is created/populated only after items are added. In the current implementation, ListBox applies its Template in the constructor (via Control.Template DP), and ItemsPresenter.OnTemplatedParentChanged() creates ItemsPanelRoot immediately; adding items just repopulates it. Updating this comment (or explicitly calling listBox.ApplyTemplate() in the test) would make the test intent clearer and less coupled to the current initialization order.

Copilot uses AI. Check for mistakes.
Assert.NotNull(container1);

// Act: set IsSelected = true on the second container; this raises SelectedEvent which bubbles to ListBox
container1!.IsSelected = true;

// Assert: SelectedIndex should be updated to match the container's position
Assert.Equal(1, listBox.SelectedIndex);
}

[Fact]
public void ItemSelectedEvent_RaisedOnContainer_UpdatesListBoxSelectedIndex()
{
var listBox = new ListBox();
listBox.Items.Add("A");
listBox.Items.Add("B");
listBox.Items.Add("C");

Assert.NotNull(listBox.ItemsPanelRoot);
var container2 = listBox.ItemsPanelRoot!.Children[2] as ListBoxItem;
Assert.NotNull(container2);

// Act: raise SelectedEvent directly on the third container
container2!.RaiseEvent(new RoutedEventArgs(ListBoxItem.SelectedEvent, container2));

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

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

RaiseEvent always overwrites RoutedEventArgs.Source with this, so passing container2 into the RoutedEventArgs(routedEvent, source) constructor here is redundant and can be misleading. Consider using the single-argument RoutedEventArgs(ListBoxItem.SelectedEvent) constructor for clarity.

Suggested change
container2!.RaiseEvent(new RoutedEventArgs(ListBoxItem.SelectedEvent, container2));
container2!.RaiseEvent(new RoutedEventArgs(ListBoxItem.SelectedEvent));

Copilot uses AI. Check for mistakes.

// Assert: SelectedIndex should be updated to 2
Assert.Equal(2, listBox.SelectedIndex);
}

[Fact]
public void SelectedIndex_Change_Updates_ContainersIsSelected()
{
var listBox = new ListBox();
listBox.Items.Add("Alpha");
listBox.Items.Add("Beta");
listBox.Items.Add("Gamma");

Assert.NotNull(listBox.ItemsPanelRoot);

// Act: select the second item
listBox.SelectedIndex = 1;

var container0 = listBox.ItemsPanelRoot!.Children[0] as ListBoxItem;
var container1 = listBox.ItemsPanelRoot!.Children[1] as ListBoxItem;
var container2 = listBox.ItemsPanelRoot!.Children[2] as ListBoxItem;

Assert.NotNull(container0);
Assert.NotNull(container1);
Assert.NotNull(container2);

Assert.False(container0!.IsSelected);
Assert.True(container1!.IsSelected);
Assert.False(container2!.IsSelected);
}

[Fact]
public void SelectedIndex_Change_Deselects_PreviousContainer()
{
var listBox = new ListBox();
listBox.Items.Add("X");
listBox.Items.Add("Y");
listBox.Items.Add("Z");

Assert.NotNull(listBox.ItemsPanelRoot);

// Select the first item
listBox.SelectedIndex = 0;
var container0 = listBox.ItemsPanelRoot!.Children[0] as ListBoxItem;
var container1 = listBox.ItemsPanelRoot!.Children[1] as ListBoxItem;
Assert.NotNull(container0);
Assert.NotNull(container1);
Assert.True(container0!.IsSelected);
Assert.False(container1!.IsSelected);

// Act: change selection to the second item
listBox.SelectedIndex = 1;

// Previous container should be deselected, new one selected
Assert.False(container0.IsSelected);
Assert.True(container1.IsSelected);
}

[Fact]
public void SelectedIndex_MinusOne_Deselects_AllContainers()
{
var listBox = new ListBox();
listBox.Items.Add("One");
listBox.Items.Add("Two");

Assert.NotNull(listBox.ItemsPanelRoot);

listBox.SelectedIndex = 0;
var container0 = listBox.ItemsPanelRoot!.Children[0] as ListBoxItem;
var container1 = listBox.ItemsPanelRoot!.Children[1] as ListBoxItem;
Assert.NotNull(container0);
Assert.NotNull(container1);
Assert.True(container0!.IsSelected);

// Act: clear selection
listBox.SelectedIndex = -1;

Assert.False(container0.IsSelected);
Assert.False(container1!.IsSelected);
}
}
Loading