test: AutoCompleteBox tests.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
@@ -8,8 +10,6 @@ namespace Ursa.Controls;
|
||||
|
||||
public class AutoCompleteBox: Avalonia.Controls.AutoCompleteBox, IClearControl
|
||||
{
|
||||
// protected override Type StyleKeyOverride { get; } = typeof(Avalonia.Controls.AutoCompleteBox);
|
||||
private TextBox? _text;
|
||||
static AutoCompleteBox()
|
||||
{
|
||||
MinimumPrefixLengthProperty.OverrideDefaultValue<AutoCompleteBox>(0);
|
||||
@@ -17,15 +17,9 @@ public class AutoCompleteBox: Avalonia.Controls.AutoCompleteBox, IClearControl
|
||||
|
||||
public AutoCompleteBox()
|
||||
{
|
||||
this.AddHandler(PointerPressedEvent, OnBoxPointerPressed, RoutingStrategies.Tunnel);
|
||||
AddHandler(PointerPressedEvent, OnBoxPointerPressed, RoutingStrategies.Tunnel);
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
_text = e.NameScope.Get<TextBox>("PART_TextBox");
|
||||
}
|
||||
|
||||
|
||||
private void OnBoxPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||
{
|
||||
if (Equals(sender, this) && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
||||
@@ -37,6 +31,7 @@ public class AutoCompleteBox: Avalonia.Controls.AutoCompleteBox, IClearControl
|
||||
protected override void OnGotFocus(GotFocusEventArgs e)
|
||||
{
|
||||
base.OnGotFocus(e);
|
||||
if (IsDropDownOpen) return;
|
||||
SetCurrentValue(IsDropDownOpenProperty, true);
|
||||
}
|
||||
|
||||
|
||||
100
tests/HeadlessTest.Ursa/Controls/AutoCompleteBoxTests/Tests.cs
Normal file
100
tests/HeadlessTest.Ursa/Controls/AutoCompleteBoxTests/Tests.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Layout;
|
||||
using UrsaControl = Ursa.Controls;
|
||||
|
||||
namespace HeadlessTest.Ursa.Controls.AutoCompleteBoxTests;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
[AvaloniaFact]
|
||||
// Ideally Should not open popup when there is no items, but sub class have no access to this status.
|
||||
public void AutoCompleteBox_Focus_And_LostFocus()
|
||||
{
|
||||
var window = new Window();
|
||||
var autoCompleteBox = new UrsaControl.AutoCompleteBox()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Width = 100,
|
||||
Height = 100,
|
||||
};
|
||||
var textBox = new TextBox()
|
||||
{
|
||||
Width = 100, Height = 100,
|
||||
};
|
||||
window.Content = new StackPanel()
|
||||
{
|
||||
Children = { autoCompleteBox, textBox }
|
||||
};
|
||||
window.Show();
|
||||
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
autoCompleteBox.Focus();
|
||||
Assert.True(autoCompleteBox.IsDropDownOpen);
|
||||
textBox.Focus();
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Click_AutoCompleteBox_With_Mouse_Should_Open_Dropdown()
|
||||
{
|
||||
var window = new Window();
|
||||
var autoCompleteBox = new UrsaControl.AutoCompleteBox()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Width = 100,
|
||||
Height = 100,
|
||||
ItemsSource = new List<string> {"Hello", "World"}
|
||||
};
|
||||
window.Content = autoCompleteBox;
|
||||
window.Show();
|
||||
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
window.MouseDown(new Point(10, 10), MouseButton.Left);
|
||||
Assert.True(autoCompleteBox.IsDropDownOpen);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Click_AutoCompleteBox_With_Mouse_Should_NotOpen_Dropdown_When_Empty()
|
||||
{
|
||||
var window = new Window();
|
||||
var autoCompleteBox = new UrsaControl.AutoCompleteBox()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Width = 100,
|
||||
Height = 100,
|
||||
};
|
||||
window.Content = autoCompleteBox;
|
||||
window.Show();
|
||||
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
window.MouseDown(new Point(10, 10), MouseButton.Left);
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Right_Click_AutoCompleteBox_With_Mouse_Should_NotOpen_Dropdown()
|
||||
{
|
||||
var window = new Window();
|
||||
var autoCompleteBox = new UrsaControl.AutoCompleteBox()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Width = 100,
|
||||
Height = 100,
|
||||
ItemsSource = new List<string> {"Hello", "World"},
|
||||
};
|
||||
window.Content = autoCompleteBox;
|
||||
window.Show();
|
||||
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
window.MouseDown(new Point(10, 10), MouseButton.Right);
|
||||
Assert.False(autoCompleteBox.IsDropDownOpen);
|
||||
}
|
||||
}
|
||||
20
tests/Test.Ursa/AutoCompleteBoxTests/Tests.cs
Normal file
20
tests/Test.Ursa/AutoCompleteBoxTests/Tests.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Remote.Protocol.Input;
|
||||
using AutoCompleteBox = Ursa.Controls.AutoCompleteBox;
|
||||
|
||||
namespace Test.Ursa.AutoCompleteBoxTests;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
[Fact]
|
||||
public void Clear_SetsSelectedItemToNull()
|
||||
{
|
||||
var autoCompleteBox = new AutoCompleteBox();
|
||||
autoCompleteBox.SelectedItem = "TestItem";
|
||||
autoCompleteBox.Clear();
|
||||
Assert.Null(autoCompleteBox.SelectedItem);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user