|
| 1 | +/* |
| 2 | +Copyright (c) 2026, Lars Brubaker |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | +1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer in the documentation |
| 12 | + and/or other materials provided with the distribution. |
| 13 | +
|
| 14 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +
|
| 25 | +The views and conclusions contained in the software and documentation are those |
| 26 | +of the authors and should not be interpreted as representing official policies, |
| 27 | +either expressed or implied, of the FreeBSD Project. |
| 28 | +*/ |
| 29 | + |
| 30 | +using System.Threading.Tasks; |
| 31 | +using MatterHackers.Agg.Platform.Mac; |
| 32 | +using TUnit.Assertions; |
| 33 | +using TUnit.Assertions.Extensions; |
| 34 | +using TUnit.Core; |
| 35 | + |
| 36 | +using static MatterHackers.Agg.Platform.Mac.AppKitConstants; |
| 37 | + |
| 38 | +namespace MatterHackers.Agg.UI.Tests |
| 39 | +{ |
| 40 | + /// <summary> |
| 41 | + /// A keyDown carries a hardware key position, not a letter, so every letter and digit shortcut has to |
| 42 | + /// be resolved from the layout-resolved characters instead. Without that, Cmd+S arrived as a bare |
| 43 | + /// Control modifier with no key attached and no shortcut in the application could ever match it. |
| 44 | + /// </summary> |
| 45 | + public class MacKeyTranslationTests |
| 46 | + { |
| 47 | + /// <summary>The US-layout hardware position of S; on another layout it is another letter, which is |
| 48 | + /// exactly why a key code cannot be what a letter shortcut is matched on.</summary> |
| 49 | + private const ushort VkSOnUsLayout = 0x01; |
| 50 | + |
| 51 | + [Test] |
| 52 | + public async Task CommandSIsControlS() |
| 53 | + { |
| 54 | + KeyEventArgs keyEvent = MacSystemWindow.MakeKeyEventArgs(VkSOnUsLayout, "s", NSEventModifierFlagCommand); |
| 55 | + |
| 56 | + await Assert.That(keyEvent.KeyCode).IsEqualTo(Keys.S); |
| 57 | + await Assert.That(keyEvent.Control).IsTrue(); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public async Task PhysicalControlSIsAlsoControlS() |
| 62 | + { |
| 63 | + KeyEventArgs keyEvent = MacSystemWindow.MakeKeyEventArgs(VkSOnUsLayout, "s", NSEventModifierFlagControl); |
| 64 | + |
| 65 | + await Assert.That(keyEvent.KeyCode).IsEqualTo(Keys.S); |
| 66 | + await Assert.That(keyEvent.Control).IsTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary>The everyday shortcuts, each of which was equally dead before the letters resolved.</summary> |
| 70 | + [Test] |
| 71 | + [Arguments("z", Keys.Z)] |
| 72 | + [Arguments("y", Keys.Y)] |
| 73 | + [Arguments("a", Keys.A)] |
| 74 | + [Arguments("c", Keys.C)] |
| 75 | + [Arguments("v", Keys.V)] |
| 76 | + [Arguments("x", Keys.X)] |
| 77 | + public async Task CommandLetterShortcutsResolve(string characters, Keys expected) |
| 78 | + { |
| 79 | + KeyEventArgs keyEvent = MacSystemWindow.MakeKeyEventArgs(0, characters, NSEventModifierFlagCommand); |
| 80 | + |
| 81 | + await Assert.That(keyEvent.KeyCode).IsEqualTo(expected); |
| 82 | + await Assert.That(keyEvent.Control).IsTrue(); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// charactersIgnoringModifiers drops Command and Option but keeps Shift, so a shifted key arrives |
| 87 | + /// spelled differently while WinForms reports the same key code either way. |
| 88 | + /// </summary> |
| 89 | + [Test] |
| 90 | + public async Task ShiftedSpellingsShareTheirKeyCode() |
| 91 | + { |
| 92 | + KeyEventArgs shiftedZ = MacSystemWindow.MakeKeyEventArgs( |
| 93 | + 0, |
| 94 | + "Z", |
| 95 | + NSEventModifierFlagCommand | NSEventModifierFlagShift); |
| 96 | + |
| 97 | + await Assert.That(shiftedZ.KeyCode).IsEqualTo(Keys.Z); |
| 98 | + await Assert.That(shiftedZ.Control).IsTrue(); |
| 99 | + await Assert.That(shiftedZ.Shift).IsTrue(); |
| 100 | + |
| 101 | + // The 3D view's zoom shortcuts: Cmd+= and Cmd++ are one key, as are Cmd+- and Cmd+_. |
| 102 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, "=", NSEventModifierFlagCommand).KeyCode) |
| 103 | + .IsEqualTo(Keys.Oemplus); |
| 104 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, "+", NSEventModifierFlagCommand).KeyCode) |
| 105 | + .IsEqualTo(Keys.Oemplus); |
| 106 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, "-", NSEventModifierFlagCommand).KeyCode) |
| 107 | + .IsEqualTo(Keys.OemMinus); |
| 108 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, "_", NSEventModifierFlagCommand).KeyCode) |
| 109 | + .IsEqualTo(Keys.OemMinus); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public async Task DigitsResolve() |
| 114 | + { |
| 115 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, "1", 0).KeyCode).IsEqualTo(Keys.D1); |
| 116 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, "0", 0).KeyCode).IsEqualTo(Keys.D0); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// The named keys have to keep winning: their characters are private-use-area codes that are not |
| 121 | + /// text and must never be mistaken for one. |
| 122 | + /// </summary> |
| 123 | + [Test] |
| 124 | + public async Task NamedKeysBeatTheirCharacters() |
| 125 | + { |
| 126 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(VkForwardDelete, "", 0).KeyCode) |
| 127 | + .IsEqualTo(Keys.Delete); |
| 128 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(VkReturn, "\r", 0).KeyCode).IsEqualTo(Keys.Enter); |
| 129 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(VkEscape, "", 0).KeyCode).IsEqualTo(Keys.Escape); |
| 130 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(VkDelete, "", 0).KeyCode).IsEqualTo(Keys.Back); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary>A dead key, or a character agg has no key for, still has to carry its modifiers.</summary> |
| 134 | + [Test] |
| 135 | + public async Task UnknownCharactersAreNoKeyButKeepTheirModifiers() |
| 136 | + { |
| 137 | + KeyEventArgs keyEvent = MacSystemWindow.MakeKeyEventArgs(0, "é", NSEventModifierFlagCommand); |
| 138 | + |
| 139 | + await Assert.That(keyEvent.KeyCode).IsEqualTo(Keys.None); |
| 140 | + await Assert.That(keyEvent.Control).IsTrue(); |
| 141 | + |
| 142 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, null, 0).KeyCode).IsEqualTo(Keys.None); |
| 143 | + await Assert.That(MacSystemWindow.MakeKeyEventArgs(0, string.Empty, 0).KeyCode).IsEqualTo(Keys.None); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments