@ -6,6 +6,7 @@ using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates ;
using Avalonia.Data ;
using Avalonia.Input ;
using Avalonia.Input.GestureRecognizers ;
using Avalonia.Input.Platform ;
using Avalonia.LogicalTree ;
using Avalonia.Styling ;
@ -19,30 +20,34 @@ namespace Avalonia.Controls.UnitTests
public class ListBoxTests_Single
{
MouseTestHelper _ mouse = new MouseTestHelper ( ) ;
MouseTestHelper _ pen = new MouseTestHelper ( PointerType . Pen ) ;
[Fact]
public void Focusing_Item_With_Tab_Should_Not_Select_It ( )
{
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
ApplyTemplate ( target ) ;
Prepar e ( target ) ;
target . Presenter . Panel . Children [ 0 ] . RaiseEvent ( new GotFocusEventArgs
{
NavigationMethod = NavigationMethod . Tab ,
} ) ;
target . Presenter . Panel . Children [ 0 ] . RaiseEvent ( new GotFocusEventArgs
{
NavigationMethod = NavigationMethod . Tab ,
} ) ;
Assert . Equal ( - 1 , target . SelectedIndex ) ;
Assert . Equal ( - 1 , target . SelectedIndex ) ;
}
}
[Fact]
public void Pressing_Space_On_Focused_Item_With_Ctrl_Pressed_Should_Select_It ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -50,7 +55,7 @@ namespace Avalonia.Controls.UnitTests
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
target . Presenter . Panel . Children [ 0 ] . RaiseEvent ( new GotFocusEventArgs
{
@ -72,7 +77,7 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Clicking_Item_Should_Select_It ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -80,17 +85,88 @@ namespace Avalonia.Controls.UnitTests
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
_ mouse . Click ( target . Presenter . Panel . Children [ 0 ] ) ;
Assert . Equal ( 0 , target . SelectedIndex ) ;
}
}
[Fact]
public void Pen_Right_Press_Item_Should_Select_It ( )
{
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Width = 2 0 , Height = 1 0 } ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " }
} ;
Prepare ( target ) ;
_ pen . Down ( target . Presenter . Panel . Children [ 0 ] , MouseButton . Right ) ;
Assert . Equal ( 0 , target . SelectedIndex ) ;
}
}
[Fact]
public void Pen_Left_Press_Item_Should_Not_Select_It ( )
{
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new TextBlock { Width = 2 0 , Height = 1 0 } ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " }
} ;
Prepare ( target ) ;
_ pen . Down ( target . Presenter . Panel . Children [ 0 ] ) ;
Assert . Equal ( - 1 , target . SelectedIndex ) ;
}
}
[Theory]
[InlineData(PointerType.Mouse)]
[InlineData(PointerType.Pen)]
public void Pointer_Right_Click_Should_Select_Item_And_Open_Context ( PointerType type )
{
using ( UnitTestApplication . Start ( TestServices . StyledWindow ) )
{
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
ItemTemplate = new FuncDataTemplate < string > ( ( x , _ ) = > new Border { Height = 1 0 } )
} ;
target . GestureRecognizers . Add ( new ScrollGestureRecognizer ( )
{
CanVerticallyScroll = true , ScrollStartDistance = 5 0
} ) ;
Prepare ( target ) ;
var contextRaised = false ;
target . AddHandler ( Control . ContextRequestedEvent , ( sender , args ) = >
{
contextRaised = true ;
args . Handled = true ;
} ) ;
var pointer = type = = PointerType . Mouse ? _ mouse : _ pen ;
pointer . Click ( target . Presenter . Panel . Children [ 0 ] , MouseButton . Right , position : new Point ( 5 , 5 ) ) ;
Assert . True ( contextRaised ) ;
Assert . Equal ( 0 , target . SelectedIndex ) ;
}
}
[Fact]
public void Clicking_Selected_Item_Should_Not_Deselect_It ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -98,7 +174,7 @@ namespace Avalonia.Controls.UnitTests
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
target . SelectedIndex = 0 ;
_ mouse . Click ( target . Presenter . Panel . Children [ 0 ] ) ;
@ -110,7 +186,7 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Clicking_Item_Should_Select_It_When_SelectionMode_Toggle ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -119,7 +195,7 @@ namespace Avalonia.Controls.UnitTests
SelectionMode = SelectionMode . Single | SelectionMode . Toggle ,
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
_ mouse . Click ( target . Presenter . Panel . Children [ 0 ] ) ;
@ -130,7 +206,7 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Clicking_Selected_Item_Should_Deselect_It_When_SelectionMode_Toggle ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -140,7 +216,7 @@ namespace Avalonia.Controls.UnitTests
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
target . SelectedIndex = 0 ;
_ mouse . Click ( target . Presenter . Panel . Children [ 0 ] ) ;
@ -152,7 +228,7 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -161,7 +237,7 @@ namespace Avalonia.Controls.UnitTests
SelectionMode = SelectionMode . Toggle | SelectionMode . AlwaysSelected ,
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
target . SelectedIndex = 0 ;
_ mouse . Click ( target . Presenter . Panel . Children [ 0 ] ) ;
@ -173,7 +249,7 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Clicking_Another_Item_Should_Select_It_When_SelectionMode_Toggle ( )
{
using ( UnitTestApplication . Start ( ) )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
var target = new ListBox
{
@ -182,7 +258,7 @@ namespace Avalonia.Controls.UnitTests
SelectionMode = SelectionMode . Single | SelectionMode . Toggle ,
} ;
AvaloniaLocator . CurrentMutable . Bind < PlatformHotkeyConfiguration > ( ) . ToConstant ( new PlatformHotkeyConfiguration ( ) ) ;
ApplyTemplat e( target ) ;
Prepar e( target ) ;
target . SelectedIndex = 1 ;
_ mouse . Click ( target . Presenter . Panel . Children [ 0 ] ) ;
@ -194,45 +270,48 @@ namespace Avalonia.Controls.UnitTests
[Fact]
public void Setting_Item_IsSelected_Sets_ListBox_Selection ( )
{
var target = new ListBox
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
ItemsSource = new [ ] { "Foo" , "Bar" , "Baz " } ,
} ;
ApplyTemplate ( target ) ;
Prepar e ( target ) ;
( ( ListBoxItem ) target . GetLogicalChildren ( ) . ElementAt ( 1 ) ) . IsSelected = true ;
( ( ListBoxItem ) target . GetLogicalChildren ( ) . ElementAt ( 1 ) ) . IsSelected = true ;
Assert . Equal ( "Bar" , target . SelectedItem ) ;
Assert . Equal ( 1 , target . SelectedIndex ) ;
Assert . Equal ( "Bar" , target . SelectedItem ) ;
Assert . Equal ( 1 , target . SelectedIndex ) ;
}
}
[Fact]
public void SelectedItem_Should_Not_Cause_StackOverflow ( )
{
var viewModel = new TestStackOverflowViewModel ( )
using ( UnitTestApplication . Start ( TestServices . MockPlatformRenderInterface ) )
{
Items = new List < string > { "foo" , "bar" , "baz" }
} ;
var viewModel = new TestStackOverflowViewModel ( ) { Items = new List < string > { "foo" , "bar" , "baz" } } ;
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
DataContext = viewModel ,
ItemsSource = viewModel . Items
} ;
var target = new ListBox
{
Template = new FuncControlTemplate ( CreateListBoxTemplate ) ,
DataContext = viewModel ,
ItemsSource = viewModel . Items
} ;
target . Bind ( ListBox . SelectedItemProperty ,
new Binding ( "SelectedItem" ) { Mode = BindingMode . TwoWay } ) ;
target . Bind ( ListBox . SelectedItemProperty ,
new Binding ( "SelectedItem" ) { Mode = BindingMode . TwoWay } ) ;
Assert . Equal ( 0 , viewModel . SetterInvokedCount ) ;
Assert . Equal ( 0 , viewModel . SetterInvokedCount ) ;
// In Issue #855, a Stackoverflow occurred here.
target . SelectedItem = viewModel . Items [ 2 ] ;
// In Issue #855, a Stackoverflow occurred here.
target . SelectedItem = viewModel . Items [ 2 ] ;
Assert . Equal ( viewModel . Items [ 1 ] , target . SelectedItem ) ;
Assert . Equal ( 1 , viewModel . SetterInvokedCount ) ;
Assert . Equal ( viewModel . Items [ 1 ] , target . SelectedItem ) ;
Assert . Equal ( 1 , viewModel . SetterInvokedCount ) ;
}
}
private class TestStackOverflowViewModel : INotifyPropertyChanged
@ -295,20 +374,11 @@ namespace Avalonia.Controls.UnitTests
} . RegisterInNameScope ( scope ) ;
}
private static void ApplyTemplat e( ListBox target )
private static void Prepar e( ListBox target )
{
// Apply the template to the ListBox itself.
target . ApplyTemplate ( ) ;
// Then to its inner ScrollViewer.
var scrollViewer = ( ScrollViewer ) target . GetVisualChildren ( ) . Single ( ) ;
scrollViewer . ApplyTemplate ( ) ;
// Then make the ScrollViewer create its child.
scrollViewer . Presenter . UpdateChild ( ) ;
// Now the ItemsPresenter should be registered, so apply its template.
target . Presenter . ApplyTemplate ( ) ;
target . Width = target . Height = 1 0 0 ;
var root = new TestRoot ( target ) ;
root . LayoutManager . ExecuteInitialLayoutPass ( ) ;
}
}
}