Skip to content

Commit

Permalink
fix(tests): Fix several Given_TextBox tests on macOS/skia
Browse files Browse the repository at this point in the history
  • Loading branch information
spouliot committed Nov 4, 2024
1 parent d5f60ec commit 7e21bee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ public async Task When_Cut_While_Pointer_Held()
Assert.AreEqual(1, SUT.SelectionStart);
Assert.AreEqual(9, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.X, VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.X, _platformCtrlKey));
await WindowHelper.WaitForIdle();

Assert.AreEqual("Hd", SUT.Text);
Expand Down Expand Up @@ -2151,67 +2151,69 @@ public async Task When_Multiline_Keyboard_Chunking()
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(0, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
// on macOS selecting the next word is `shift` + `option` (alt/menu) + `right`
var mod = VirtualKeyModifiers.Shift | (OperatingSystem.IsMacOS() ? VirtualKeyModifiers.Menu : VirtualKeyModifiers.Control);
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(6, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(7, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(12, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(13, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(14, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(19, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(20, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(21, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(24, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(25, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(29, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(30, SUT.SelectionLength);

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod));
await WindowHelper.WaitForIdle();
Assert.AreEqual(0, SUT.SelectionStart);
Assert.AreEqual(31, SUT.SelectionLength);
Expand Down Expand Up @@ -3075,7 +3077,7 @@ public async Task When_Cut_Paste_Breaks_Typing(VirtualKey key)
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.E, VirtualKeyModifiers.None, unicodeKey: 'e'));
await WindowHelper.WaitForIdle();

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, key, VirtualKeyModifiers.Control));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, key, _platformCtrlKey));
await WindowHelper.WaitForIdle();

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.L, VirtualKeyModifiers.None, unicodeKey: 'l'));
Expand Down
32 changes: 30 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,26 @@ args.Key is not (VirtualKey.Up or VirtualKey.Down or VirtualKey.Left or VirtualK
switch (args.Key)
{
case VirtualKey.Up:
KeyDownUpArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength);
// on macOS start of document is `Command` and `Up`
if (ctrl && OperatingSystem.IsMacOS())
{
KeyDownHome(args, text, ctrl, shift, ref selectionStart, ref selectionLength);
}
else
{
KeyDownUpArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength);
}
break;
case VirtualKey.Down:
KeyDownDownArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength);
// on macOS end of document is `Command` and `Down`
if (ctrl && OperatingSystem.IsMacOS())
{
KeyDownEnd(args, text, ctrl, shift, ref selectionStart, ref selectionLength);
}
else
{
KeyDownDownArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength);
}
break;
case VirtualKey.Left:
KeyDownLeftArrow(args, text, shift, ctrl, ref selectionStart, ref selectionLength);
Expand Down Expand Up @@ -689,6 +705,12 @@ private void KeyDownLeftArrow(KeyRoutedEventArgs args, string text, bool shift,

private void KeyDownRightArrow(KeyRoutedEventArgs args, string text, bool ctrl, bool shift, ref int selectionStart, ref int selectionLength)
{
// on macOS it is `shift` + `option` + `right` that select the next word
if (shift && OperatingSystem.IsMacOS())
{
ctrl = args.KeyboardModifiers.HasFlag(VirtualKeyModifiers.Menu);
}

if (HasPointerCapture)
{
return;
Expand Down Expand Up @@ -814,6 +836,12 @@ private void KeyDownEnd(KeyRoutedEventArgs args, string text, bool ctrl, bool sh

private void KeyDownDelete(KeyRoutedEventArgs args, ref string text, bool ctrl, bool shift, ref int selectionStart, ref int selectionLength)
{
// on macOS it is `option` + `delete>` that removes the next word
if (OperatingSystem.IsMacOS())
{
ctrl = args.KeyboardModifiers.HasFlag(VirtualKeyModifiers.Menu);

Check notice on line 842 in src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs#L842

Introduce a new variable instead of reusing the parameter 'ctrl'.
}

if (HasPointerCapture)
{
return;
Expand Down

0 comments on commit 7e21bee

Please sign in to comment.