|
|
|
@ -1,7 +1,6 @@ |
|
|
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|
|
|
|
|
|
|
using System; |
|
|
|
using Avalonia.Controls; |
|
|
|
using Avalonia.Input; |
|
|
|
using Avalonia.Interactivity; |
|
|
|
@ -12,45 +11,56 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
|
|
|
public class EventTests : XamlTestBase |
|
|
|
{ |
|
|
|
[Fact] |
|
|
|
public void Event_Is_Attached() |
|
|
|
public void Event_Is_Assigned() |
|
|
|
{ |
|
|
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='OnClick'/>"; |
|
|
|
var loader = new AvaloniaXamlLoader(); |
|
|
|
var target = new MyButton(); |
|
|
|
|
|
|
|
loader.Load(xaml, rootInstance: target); |
|
|
|
RaiseClick(target); |
|
|
|
|
|
|
|
Assert.True(target.Clicked); |
|
|
|
target.RaiseEvent(new RoutedEventArgs |
|
|
|
{ |
|
|
|
RoutedEvent = Button.ClickEvent, |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.True(target.WasClicked); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Exception_Is_Thrown_If_Event_Not_Found() |
|
|
|
public void Attached_Event_Is_Assigned() |
|
|
|
{ |
|
|
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
|
|
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Gestures.Tapped='OnTapped'/>"; |
|
|
|
var loader = new AvaloniaXamlLoader(); |
|
|
|
var target = new MyButton(); |
|
|
|
|
|
|
|
XamlTestHelpers.AssertThrowsXamlException(() => loader.Load(xaml, rootInstance: target)); |
|
|
|
} |
|
|
|
loader.Load(xaml, rootInstance: target); |
|
|
|
|
|
|
|
private void RaiseClick(MyButton target) |
|
|
|
{ |
|
|
|
target.RaiseEvent(new KeyEventArgs |
|
|
|
target.RaiseEvent(new RoutedEventArgs |
|
|
|
{ |
|
|
|
RoutedEvent = Button.KeyDownEvent, |
|
|
|
Key = Key.Enter, |
|
|
|
RoutedEvent = Gestures.TappedEvent, |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.True(target.WasTapped); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Exception_Is_Thrown_If_Event_Not_Found() |
|
|
|
{ |
|
|
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
|
|
|
var loader = new AvaloniaXamlLoader(); |
|
|
|
var target = new MyButton(); |
|
|
|
|
|
|
|
XamlTestHelpers.AssertThrowsXamlException(() => loader.Load(xaml, rootInstance: target)); |
|
|
|
} |
|
|
|
|
|
|
|
public class MyButton : Button |
|
|
|
{ |
|
|
|
public bool Clicked { get; private set; } |
|
|
|
public bool WasClicked { get; private set; } |
|
|
|
public bool WasTapped { get; private set; } |
|
|
|
|
|
|
|
public void OnClick(object sender, RoutedEventArgs e) |
|
|
|
{ |
|
|
|
Clicked = true; |
|
|
|
} |
|
|
|
public void OnClick(object sender, RoutedEventArgs e) => WasClicked = true; |
|
|
|
public void OnTapped(object sender, RoutedEventArgs e) => WasTapped = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|