@ -156,112 +156,127 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Selection_Should_Be_Cleared_On_Recycled_Items ( )
{
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = Enumerable . Range ( 0 , 2 0 ) . Select ( x = > $"Item {x}" ) . ToList ( ) ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } ) ,
SelectedIndex = 0 ,
} ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = Enumerable . Range ( 0 , 2 0 ) . Select ( x = > $"Item {x}" ) . ToList ( ) ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } ) ,
SelectedIndex = 0 ,
} ;
Prepare ( target ) ;
Prepare ( target ) ;
// Make sure we're virtualized and first item is selected.
Assert . Equal ( 1 0 , target . Presenter . Panel . Children . Count ) ;
Assert . True ( ( ( ListBoxItem ) target . Presenter . Panel . Children [ 0 ] ) . IsSelected ) ;
// Make sure we're virtualized and first item is selected.
Assert . Equal ( 1 0 , target . Presenter . Panel . Children . Count ) ;
Assert . True ( ( ( ListBoxItem ) target . Presenter . Panel . Children [ 0 ] ) . IsSelected ) ;
// Scroll down a page.
target . Scroll . Offset = new Vector ( 0 , 1 0 ) ;
// Scroll down a page.
target . Scroll . Offset = new Vector ( 0 , 1 0 ) ;
// Make sure recycled item isn't now selected.
Assert . False ( ( ( ListBoxItem ) target . Presenter . Panel . Children [ 0 ] ) . IsSelected ) ;
// Make sure recycled item isn't now selected.
Assert . False ( ( ( ListBoxItem ) target . Presenter . Panel . Children [ 0 ] ) . IsSelected ) ;
}
}
[Fact]
public void ScrollViewer_Should_Have_Correct_Extent_And_Viewport ( )
{
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = Enumerable . Range ( 0 , 2 0 ) . Select ( x = > $"Item {x}" ) . ToList ( ) ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Width = 2 0 , Height = 1 0 } ) ,
SelectedIndex = 0 ,
} ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = Enumerable . Range ( 0 , 2 0 ) . Select ( x = > $"Item {x}" ) . ToList ( ) ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Width = 2 0 , Height = 1 0 } ) ,
SelectedIndex = 0 ,
} ;
Prepare ( target ) ;
Prepare ( target ) ;
Assert . Equal ( new Size ( 2 0 , 2 0 ) , target . Scroll . Extent ) ;
Assert . Equal ( new Size ( 1 0 0 , 1 0 ) , target . Scroll . Viewport ) ;
Assert . Equal ( new Size ( 2 0 , 2 0 ) , target . Scroll . Extent ) ;
Assert . Equal ( new Size ( 1 0 0 , 1 0 ) , target . Scroll . Viewport ) ;
}
}
[Fact]
public void Containers_Correct_After_Clear_Add_Remove ( )
{
// Issue #1936
var items = new AvaloniaList < string > ( Enumerable . Range ( 0 , 1 1 ) . Select ( x = > $"Item {x}" ) ) ;
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Width = 2 0 , Height = 1 0 } ) ,
SelectedIndex = 0 ,
} ;
// Issue #1936
var items = new AvaloniaList < string > ( Enumerable . Range ( 0 , 1 1 ) . Select ( x = > $"Item {x}" ) ) ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Width = 2 0 , Height = 1 0 } ) ,
SelectedIndex = 0 ,
} ;
Prepare ( target ) ;
Prepare ( target ) ;
items . Clear ( ) ;
items . AddRange ( Enumerable . Range ( 0 , 1 1 ) . Select ( x = > $"Item {x}" ) ) ;
items . Remove ( "Item 2" ) ;
items . Clear ( ) ;
items . AddRange ( Enumerable . Range ( 0 , 1 1 ) . Select ( x = > $"Item {x}" ) ) ;
items . Remove ( "Item 2" ) ;
Assert . Equal (
items ,
target . Presenter . Panel . Children . Cast < ListBoxItem > ( ) . Select ( x = > ( string ) x . Content ) ) ;
Assert . Equal (
items ,
target . Presenter . Panel . Children . Cast < ListBoxItem > ( ) . Select ( x = > ( string ) x . Content ) ) ;
}
}
[Fact]
public void Toggle_Selection_Should_Update_Containers ( )
{
var items = Enumerable . Range ( 0 , 1 0 ) . Select ( x = > $"Item {x}" ) . ToArray ( ) ;
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = items ,
SelectionMode = SelectionMode . Toggle ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } )
} ;
var items = Enumerable . Range ( 0 , 1 0 ) . Select ( x = > $"Item {x}" ) . ToArray ( ) ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = items ,
SelectionMode = SelectionMode . Toggle ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } )
} ;
Prepare ( target ) ;
Prepare ( target ) ;
var lbItems = target . GetLogicalChildren ( ) . OfType < ListBoxItem > ( ) . ToArray ( ) ;
var lbItems = target . GetLogicalChildren ( ) . OfType < ListBoxItem > ( ) . ToArray ( ) ;
var item = lbItems [ 0 ] ;
var item = lbItems [ 0 ] ;
Assert . Equal ( false , item . IsSelected ) ;
Assert . Equal ( false , item . IsSelected ) ;
RaisePressedEvent ( target , item , MouseButton . Left ) ;
RaisePressedEvent ( target , item , MouseButton . Left ) ;
Assert . Equal ( true , item . IsSelected ) ;
Assert . Equal ( true , item . IsSelected ) ;
RaisePressedEvent ( target , item , MouseButton . Left ) ;
RaisePressedEvent ( target , item , MouseButton . Left ) ;
Assert . Equal ( false , item . IsSelected ) ;
Assert . Equal ( false , item . IsSelected ) ;
}
}
[Fact]
public void Can_Decrease_Number_Of_Materialized_Items_By_Removing_From_Source_Collection ( )
{
var items = new AvaloniaList < string > ( Enumerable . Range ( 0 , 2 0 ) . Select ( x = > $"Item {x}" ) ) ;
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } )
} ;
var items = new AvaloniaList < string > ( Enumerable . Range ( 0 , 2 0 ) . Select ( x = > $"Item {x}" ) ) ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } )
} ;
Prepare ( target ) ;
target . Scroll . Offset = new Vector ( 0 , 1 ) ;
Prepare ( target ) ;
target . Scroll . Offset = new Vector ( 0 , 1 ) ;
items . RemoveRange ( 0 , 1 1 ) ;
items . RemoveRange ( 0 , 1 1 ) ;
}
}
private void RaisePressedEvent ( ListBox listBox , ListBoxItem item , MouseButton mouseButton )
@ -272,35 +287,38 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void ListBox_After_Scroll_IndexOutOfRangeException_Shouldnt_Be_Thrown ( )
{
var items = Enumerable . Range ( 0 , 1 1 ) . Select ( x = > $"{x}" ) . ToArray ( ) ;
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 1 } )
} ;
var items = Enumerable . Range ( 0 , 1 1 ) . Select ( x = > $"{x}" ) . ToArray ( ) ;
Prepare ( target ) ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 1 } )
} ;
Prepare ( target ) ;
var panel = target . Presenter . Panel as IVirtualizingPanel ;
var panel = target . Presenter . Panel as IVirtualizingPanel ;
var listBoxItems = panel . Children . OfType < ListBoxItem > ( ) ;
var listBoxItems = panel . Children . OfType < ListBoxItem > ( ) ;
//virtualization should have created exactly 10 items
Assert . Equal ( 1 0 , listBoxItems . Count ( ) ) ;
Assert . Equal ( "0" , listBoxItems . First ( ) . DataContext ) ;
Assert . Equal ( "9" , listBoxItems . Last ( ) . DataContext ) ;
//virtualization should have created exactly 10 items
Assert . Equal ( 1 0 , listBoxItems . Count ( ) ) ;
Assert . Equal ( "0" , listBoxItems . First ( ) . DataContext ) ;
Assert . Equal ( "9" , listBoxItems . Last ( ) . DataContext ) ;
//instead pixeloffset > 0 there could be pretty complex sequence for repro
//it involves add/remove/scroll to end multiple actions
//which i can't find so far :(, but this is the simplest way to add it to unit test
panel . PixelOffset = 1 ;
//instead pixeloffset > 0 there could be pretty complex sequence for repro
//it involves add/remove/scroll to end multiple actions
//which i can't find so far :(, but this is the simplest way to add it to unit test
panel . PixelOffset = 1 ;
//here scroll to end -> IndexOutOfRangeException is thrown
target . Scroll . Offset = new Vector ( 0 , 2 ) ;
//here scroll to end -> IndexOutOfRangeException is thrown
target . Scroll . Offset = new Vector ( 0 , 2 ) ;
Assert . True ( true ) ;
Assert . True ( true ) ;
}
}
[Fact]
@ -374,41 +392,44 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Clicking_Item_Should_Raise_BringIntoView_For_Correct_Control ( )
{
// Issue #3934
var items = Enumerable . Range ( 0 , 1 0 ) . Select ( x = > $"Item {x}" ) . ToArray ( ) ;
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } ) ,
SelectionMode = SelectionMode . AlwaysSelected ,
VirtualizationMode = ItemVirtualizationMode . None ,
} ;
// Issue #3934
var items = Enumerable . Range ( 0 , 1 0 ) . Select ( x = > $"Item {x}" ) . ToArray ( ) ;
var target = new ListBox
{
Template = ListBoxTemplate ( ) ,
Items = items ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Height = 1 0 } ) ,
SelectionMode = SelectionMode . AlwaysSelected ,
VirtualizationMode = ItemVirtualizationMode . None ,
} ;
Prepare ( target ) ;
Prepare ( target ) ;
// First an item that is not index 0 must be selected.
_ mouse . Click ( target . Presenter . Panel . Children [ 1 ] ) ;
Assert . Equal ( 1 , target . Selection . AnchorIndex ) ;
// First an item that is not index 0 must be selected.
_ mouse . Click ( target . Presenter . Panel . Children [ 1 ] ) ;
Assert . Equal ( 1 , target . Selection . AnchorIndex ) ;
// We're going to be clicking on item 9.
var item = ( ListBoxItem ) target . Presenter . Panel . Children [ 9 ] ;
var raised = 0 ;
// We're going to be clicking on item 9.
var item = ( ListBoxItem ) target . Presenter . Panel . Children [ 9 ] ;
var raised = 0 ;
// Make sure a RequestBringIntoView event is raised for item 9. It won't be handled
// by the ScrollContentPresenter as the item is already visible, so we don't need
// handledEventsToo: true. Issue #3934 failed here because item 0 was being scrolled
// into view due to SelectionMode.AlwaysSelected.
target . AddHandler ( Control . RequestBringIntoViewEvent , ( s , e ) = >
{
Assert . Same ( item , e . TargetObject ) ;
+ + raised ;
} ) ;
// Make sure a RequestBringIntoView event is raised for item 9. It won't be handled
// by the ScrollContentPresenter as the item is already visible, so we don't need
// handledEventsToo: true. Issue #3934 failed here because item 0 was being scrolled
// into view due to SelectionMode.AlwaysSelected.
target . AddHandler ( Control . RequestBringIntoViewEvent , ( s , e ) = >
{
Assert . Same ( item , e . TargetObject ) ;
+ + raised ;
} ) ;
// Click item 9.
_ mouse . Click ( item ) ;
// Click item 9.
_ mouse . Click ( item ) ;
Assert . Equal ( 1 , raised ) ;
Assert . Equal ( 1 , raised ) ;
}
}
[Fact]