-
Notifications
You must be signed in to change notification settings - Fork 1
Add tests for ListBox selection synchronization (item→SelectedIndex and SelectedIndex→IsSelected) #163
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
Add tests for ListBox selection synchronization (item→SelectedIndex and SelectedIndex→IsSelected) #163
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||
| 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)); | ||||||
|
||||||
| container2!.RaiseEvent(new RoutedEventArgs(ListBoxItem.SelectedEvent, container2)); | |
| container2!.RaiseEvent(new RoutedEventArgs(ListBoxItem.SelectedEvent)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment implies
ItemsPanelRootis created/populated only after items are added. In the current implementation,ListBoxapplies itsTemplatein the constructor (viaControl.TemplateDP), andItemsPresenter.OnTemplatedParentChanged()createsItemsPanelRootimmediately; adding items just repopulates it. Updating this comment (or explicitly callinglistBox.ApplyTemplate()in the test) would make the test intent clearer and less coupled to the current initialization order.