Skip to content

Commit

Permalink
test: add ut.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism committed Apr 21, 2024
1 parent 4f21cb1 commit 2cc81fd
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void AddHandler<TArgs, TControl>(
public static void AddHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
IEnumerable<TControl> controls,
IEnumerable<TControl?> controls,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false)
where TArgs : RoutedEventArgs
Expand Down Expand Up @@ -102,7 +102,7 @@ public static void RemoveHandler<TArgs, TControl>(
public static void RemoveHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
IEnumerable<TControl> controls)
IEnumerable<TControl?> controls)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
Expand All @@ -118,7 +118,7 @@ public static IDisposable AddDisposableHandler<TArgs>(
params Interactive?[] controls)
where TArgs : RoutedEventArgs
{
List<IDisposable?> list = new List<IDisposable?>(controls.Length);
var list = new List<IDisposable?>(controls.Length);
foreach (var t in controls)
{
var disposable = t?.AddDisposableHandler(routedEvent, handler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Helpers;

Expand Down Expand Up @@ -175,7 +177,7 @@ void Handler(object? sender, RoutedEventArgs args)
public void EventHandler_AddDisposable_With_Strategy_Success()
{
var button = new Button();
var button2 = new Button();
var button2 = new Panel();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
Expand All @@ -190,4 +192,217 @@ void Handler(object? sender, RoutedEventArgs args)
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
Assert.Equal(2, count);
}

[Fact]
public void EventHandler_AddHandler_AddList_Success()
{
var button = new Button();
var button2 = new Button();
var button3 = new Button();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
count++;
}
Button.ClickEvent.AddHandler(Handler, new List<Button>{button, button2, button3});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);

Button.ClickEvent.RemoveHandler(Handler, new List<Button>{button, button2, button3});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);
}

[Fact]
public void EventHandler_AddHandler_AddArray_Success()
{
var button = new Button();
var button2 = new Button();
var button3 = new Button();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
count++;
}
Button.ClickEvent.AddHandler(Handler, new Button[]{button, button2, button3});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);

Button.ClickEvent.RemoveHandler(Handler, new Button[]{button, button2, button3});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);
}

[Fact]
public void EventHandler_AddHandler_AddArray_Null_Success()
{
var button = new Button();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
count++;
}
Button.ClickEvent.AddHandler(Handler, new Button?[]{button, null});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
Assert.Equal(1, count);
}

[Fact]
public void EventHandler_AddHandler_AddList_Null_Success()
{
var button = new Button();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
count++;
}
Button.ClickEvent.AddHandler(Handler, new List<Button?>{button, null});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
Assert.Equal(1, count);
}

[Fact]
public void EventHandler_AddHandler_With_Strategy_AddList_Success()
{
var button = new Button();
var button2 = new Button();
var button3 = new Button();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
count++;
}
Button.ClickEvent.AddHandler(Handler, new List<Button>{button, button2, button3}, RoutingStrategies.Bubble, true);
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);

Button.ClickEvent.RemoveHandler(Handler, new List<Button>{button, button2, button3});
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);
}

[Fact]
public void EventHandler_AddDisposableHandler_With_Strategy_AddList_Success()
{
var button = new Button();
var button2 = new Button();
var button3 = new Button();
int count = 0;
void Handler(object? sender, RoutedEventArgs args)
{
count++;
}
var disposables = Button.ClickEvent.AddDisposableHandler(Handler, new Button[]{button, button2, button3}, RoutingStrategies.Bubble, true);
button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);

disposables.Dispose();

button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent});
button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent});
button3.RaiseEvent(new RoutedEventArgs(){ Source = button3, RoutedEvent = Button.ClickEvent});
Assert.Equal(3, count);
}

[Fact]
public void EventHandler_AddHandler_Multi_Type_Success()
{
var button = new Button();
var panel = new Panel();
int count = 0;
void Handler(object? sender, GotFocusEventArgs args)
{
count++;
}
InputElement.GotFocusEvent.AddHandler(Handler, button, panel);
button.RaiseEvent(new GotFocusEventArgs(){ Source = button, RoutedEvent = InputElement.GotFocusEvent});
panel.RaiseEvent(new GotFocusEventArgs(){ Source = panel, RoutedEvent = InputElement.GotFocusEvent});

Assert.Equal(2, count);

InputElement.GotFocusEvent.RemoveHandler(Handler, button, panel);
button.RaiseEvent(new GotFocusEventArgs(){ Source = button, RoutedEvent = InputElement.GotFocusEvent});
panel.RaiseEvent(new GotFocusEventArgs(){ Source = panel, RoutedEvent = InputElement.GotFocusEvent});

Assert.Equal(2, count);
}

[Fact]
public void EventHandler_AddDisposableHandler_Multi_Type_Success()
{
var button = new Button();
var panel = new Panel();
int count = 0;
void Handler(object? sender, GotFocusEventArgs args)
{
count++;
}
var disposable = InputElement.GotFocusEvent.AddDisposableHandler(Handler, button, panel);
button.RaiseEvent(new GotFocusEventArgs(){ Source = button, RoutedEvent = InputElement.GotFocusEvent});
panel.RaiseEvent(new GotFocusEventArgs(){ Source = panel, RoutedEvent = InputElement.GotFocusEvent});

Assert.Equal(2, count);

disposable.Dispose();

Assert.Equal(2, count);
}

[Fact]
public void EventHandler_AddHandler_Multi_With_Strategy_Type_Success()
{
var button = new Button();
var panel = new Panel();
int count = 0;
void Handler(object? sender, GotFocusEventArgs args)
{
count++;
}
InputElement.GotFocusEvent.AddHandler(Handler, RoutingStrategies.Bubble, false, button, panel);
button.RaiseEvent(new GotFocusEventArgs(){ Source = button, RoutedEvent = InputElement.GotFocusEvent});
panel.RaiseEvent(new GotFocusEventArgs(){ Source = panel, RoutedEvent = InputElement.GotFocusEvent});

Assert.Equal(2, count);

InputElement.GotFocusEvent.RemoveHandler(Handler, button, panel);
button.RaiseEvent(new GotFocusEventArgs(){ Source = button, RoutedEvent = InputElement.GotFocusEvent});
panel.RaiseEvent(new GotFocusEventArgs(){ Source = panel, RoutedEvent = InputElement.GotFocusEvent});

Assert.Equal(2, count);
}

[Fact]
public void EventHandler_AddDisposableHandler_With_Strategy_Multi_Type_Success()
{
var button = new Button();
var panel = new Panel();
int count = 0;
void Handler(object? sender, GotFocusEventArgs args)
{
count++;
}
var disposable = InputElement.GotFocusEvent.AddDisposableHandler(Handler, RoutingStrategies.Bubble, false, button, panel);
button.RaiseEvent(new GotFocusEventArgs(){ Source = button, RoutedEvent = InputElement.GotFocusEvent});
panel.RaiseEvent(new GotFocusEventArgs(){ Source = panel, RoutedEvent = InputElement.GotFocusEvent});

Assert.Equal(2, count);

disposable.Dispose();

Assert.Equal(2, count);
}
}

0 comments on commit 2cc81fd

Please sign in to comment.