From 87741baea232640e4aeed601f10d1e15a99e5635 Mon Sep 17 00:00:00 2001 From: eriklimakc Date: Mon, 20 Feb 2023 17:26:19 +0000 Subject: [PATCH] chore: Adding Runtime Tests --- .../Tests/KeyUpCommandTests.cs | 101 ++++++++++++++++++ .../Uno.Toolkit.RuntimeTests.WinUI.csproj | 3 + 2 files changed, 104 insertions(+) create mode 100644 src/Uno.Toolkit.RuntimeTests/Tests/KeyUpCommandTests.cs diff --git a/src/Uno.Toolkit.RuntimeTests/Tests/KeyUpCommandTests.cs b/src/Uno.Toolkit.RuntimeTests/Tests/KeyUpCommandTests.cs new file mode 100644 index 000000000..5bb47f32f --- /dev/null +++ b/src/Uno.Toolkit.RuntimeTests/Tests/KeyUpCommandTests.cs @@ -0,0 +1,101 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Threading.Tasks; +using System.Windows.Input; +using Uno.Toolkit.RuntimeTests.Helpers; +using Uno.Toolkit.UI; +using Uno.UI.RuntimeTests; +using Windows.System; +using Windows.UI.Input.Preview.Injection; +#if IS_WINUI +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Data; +#else +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Data; +#endif + +namespace Uno.Toolkit.RuntimeTests.Tests; + +[TestClass] +[RunsOnUIThread] +internal class KeyUpCommandTests +{ + [TestMethod] + public async Task OnKeyUp() + { + var viewModel = new ViewModel(); + var page = new Page(); + page.DataContext = viewModel; + + page.SetBinding(InputExtensions.KeyUpCommandProperty, new Binding + { + Path = new PropertyPath(nameof(viewModel.KeyUpCommand)), + Mode = BindingMode.OneWay + }); + + await UnitTestUIContentHelperEx.SetContentAndWait(page); + + InputInjector? inputInjector = InputInjector.TryCreate(); + + if (inputInjector != null) + { + var number0 = new InjectedInputKeyboardInfo + { + VirtualKey = (ushort)(VirtualKey.Number0), + KeyOptions = InjectedInputKeyOptions.KeyUp + }; + + page.Focus(FocusState.Pointer); + inputInjector.InjectKeyboardInput(new[] { number0 }); + } + + Assert.AreEqual("Number0 pressed", viewModel.Text); + } +} + +public class ViewModel +{ + public ICommand KeyUpCommand { get; } + + public string Text { get; set; } = "Nothing pressed"; + + public ViewModel() + { + KeyUpCommand = new RelayCommand(key => Text = $"{key} pressed"); + } +} + +public class RelayCommand : ICommand +{ + private readonly Action _execute; + private readonly Func _canExecute; + + public RelayCommand(Action execute, Func? canExecute = null) + { + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); + _canExecute = canExecute ?? (_ => true); + } + + public bool CanExecute(object? parameter) + { + RaiseCanExecuteChanged(); + + return _canExecute((T)parameter!); + } + + public void Execute(object? parameter) + { + _execute((T)parameter!); + } + + public void RaiseCanExecuteChanged() + { + CanExecuteChanged?.Invoke(this, EventArgs.Empty); + } + + public event EventHandler? CanExecuteChanged; +} + diff --git a/src/Uno.Toolkit.RuntimeTests/Uno.Toolkit.RuntimeTests.WinUI.csproj b/src/Uno.Toolkit.RuntimeTests/Uno.Toolkit.RuntimeTests.WinUI.csproj index 0f6f317cf..075feea0a 100644 --- a/src/Uno.Toolkit.RuntimeTests/Uno.Toolkit.RuntimeTests.WinUI.csproj +++ b/src/Uno.Toolkit.RuntimeTests/Uno.Toolkit.RuntimeTests.WinUI.csproj @@ -5,4 +5,7 @@ + + +