Skip to content

Commit

Permalink
chore: Adding Runtime Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Feb 20, 2023
1 parent 0fc9325 commit 87741ba
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/KeyUpCommandTests.cs
Original file line number Diff line number Diff line change
@@ -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<VirtualKey>(key => Text = $"{key} pressed");
}
}

public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;

public RelayCommand(Action<T> execute, Func<T, bool>? 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;
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@

<Import Project="Uno.Toolkit.RuntimeTests.props" />
<Import Project="..\winappsdk-workaround.targets" />
<ItemGroup>
<SourceGeneratorInput Remove="Tests\KeyUpCommandTests.cs" />
</ItemGroup>
</Project>

0 comments on commit 87741ba

Please sign in to comment.