|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using System; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using System.Windows.Input; |
| 5 | +using Uno.Toolkit.RuntimeTests.Helpers; |
| 6 | +using Uno.Toolkit.UI; |
| 7 | +using Uno.UI.RuntimeTests; |
| 8 | +using Windows.System; |
| 9 | +using Windows.UI.Input.Preview.Injection; |
| 10 | +#if IS_WINUI |
| 11 | +using Microsoft.UI.Xaml; |
| 12 | +using Microsoft.UI.Xaml.Controls; |
| 13 | +using Microsoft.UI.Xaml.Data; |
| 14 | +#else |
| 15 | +using Windows.UI.Xaml; |
| 16 | +using Windows.UI.Xaml.Controls; |
| 17 | +using Windows.UI.Xaml.Data; |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace Uno.Toolkit.RuntimeTests.Tests; |
| 21 | + |
| 22 | +[TestClass] |
| 23 | +[RunsOnUIThread] |
| 24 | +internal class KeyUpCommandTests |
| 25 | +{ |
| 26 | + [TestMethod] |
| 27 | + public async Task OnKeyUp() |
| 28 | + { |
| 29 | + var viewModel = new ViewModel(); |
| 30 | + var page = new Page(); |
| 31 | + page.DataContext = viewModel; |
| 32 | + |
| 33 | + page.SetBinding(InputExtensions.KeyUpCommandProperty, new Binding |
| 34 | + { |
| 35 | + Path = new PropertyPath(nameof(viewModel.KeyUpCommand)), |
| 36 | + Mode = BindingMode.OneWay |
| 37 | + }); |
| 38 | + |
| 39 | + await UnitTestUIContentHelperEx.SetContentAndWait(page); |
| 40 | + |
| 41 | + InputInjector? inputInjector = InputInjector.TryCreate(); |
| 42 | + |
| 43 | + if (inputInjector != null) |
| 44 | + { |
| 45 | + var number0 = new InjectedInputKeyboardInfo |
| 46 | + { |
| 47 | + VirtualKey = (ushort)(VirtualKey.Number0), |
| 48 | + KeyOptions = InjectedInputKeyOptions.KeyUp |
| 49 | + }; |
| 50 | + |
| 51 | + page.Focus(FocusState.Pointer); |
| 52 | + inputInjector.InjectKeyboardInput(new[] { number0 }); |
| 53 | + } |
| 54 | + |
| 55 | + Assert.AreEqual("Number0 pressed", viewModel.Text); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +public class ViewModel |
| 60 | +{ |
| 61 | + public ICommand KeyUpCommand { get; } |
| 62 | + |
| 63 | + public string Text { get; set; } = "Nothing pressed"; |
| 64 | + |
| 65 | + public ViewModel() |
| 66 | + { |
| 67 | + KeyUpCommand = new RelayCommand<VirtualKey>(key => Text = $"{key} pressed"); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public class RelayCommand<T> : ICommand |
| 72 | +{ |
| 73 | + private readonly Action<T> _execute; |
| 74 | + private readonly Func<T, bool> _canExecute; |
| 75 | + |
| 76 | + public RelayCommand(Action<T> execute, Func<T, bool>? canExecute = null) |
| 77 | + { |
| 78 | + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); |
| 79 | + _canExecute = canExecute ?? (_ => true); |
| 80 | + } |
| 81 | + |
| 82 | + public bool CanExecute(object? parameter) |
| 83 | + { |
| 84 | + RaiseCanExecuteChanged(); |
| 85 | + |
| 86 | + return _canExecute((T)parameter!); |
| 87 | + } |
| 88 | + |
| 89 | + public void Execute(object? parameter) |
| 90 | + { |
| 91 | + _execute((T)parameter!); |
| 92 | + } |
| 93 | + |
| 94 | + public void RaiseCanExecuteChanged() |
| 95 | + { |
| 96 | + CanExecuteChanged?.Invoke(this, EventArgs.Empty); |
| 97 | + } |
| 98 | + |
| 99 | + public event EventHandler? CanExecuteChanged; |
| 100 | +} |
| 101 | + |
0 commit comments