|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package test.com.sun.javafx.scene.control.behavior; |
| 27 | + |
| 28 | +import com.sun.javafx.scene.control.behavior.TextBinding; |
| 29 | +import javafx.scene.input.KeyCombination; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static org.junit.Assert.*; |
| 33 | + |
| 34 | +public class TextBindingTest { |
| 35 | + |
| 36 | + private static void assertKeyCombination(String expected, KeyCombination actual) { |
| 37 | + if (com.sun.javafx.PlatformUtil.isMac()) { |
| 38 | + assertSame(KeyCombination.ModifierValue.DOWN, actual.getMeta()); |
| 39 | + } else { |
| 40 | + assertSame(KeyCombination.ModifierValue.DOWN, actual.getAlt()); |
| 41 | + } |
| 42 | + |
| 43 | + assertEquals(expected, ((TextBinding.MnemonicKeyCombination)actual).getCharacter()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testSimpleMnemonicLetter() { |
| 48 | + var binding = new TextBinding("foo _bar"); |
| 49 | + assertEquals("foo bar", binding.getText()); |
| 50 | + assertEquals("b", binding.getMnemonic()); |
| 51 | + assertKeyCombination("b", binding.getMnemonicKeyCombination()); |
| 52 | + assertEquals(4, binding.getMnemonicIndex()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testSimpleMnemonicDigit() { |
| 57 | + var binding = new TextBinding("foo _1 bar"); |
| 58 | + assertEquals("foo 1 bar", binding.getText()); |
| 59 | + assertEquals("1", binding.getMnemonic()); |
| 60 | + assertKeyCombination("1", binding.getMnemonicKeyCombination()); |
| 61 | + assertEquals(4, binding.getMnemonicIndex()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testExtendedMnemonicLetter() { |
| 66 | + var binding = new TextBinding("foo _(x)bar"); |
| 67 | + assertEquals("foo bar", binding.getText()); |
| 68 | + assertEquals("x", binding.getMnemonic()); |
| 69 | + assertKeyCombination("x", binding.getMnemonicKeyCombination()); |
| 70 | + assertEquals(4, binding.getMnemonicIndex()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testExtendedMnemonicUnderscore() { |
| 75 | + var binding = new TextBinding("foo _(_)bar"); |
| 76 | + assertEquals("foo bar", binding.getText()); |
| 77 | + assertEquals("_", binding.getMnemonic()); |
| 78 | + assertKeyCombination("_", binding.getMnemonicKeyCombination()); |
| 79 | + assertEquals(4, binding.getMnemonicIndex()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testExtendedMnemonicClosingBrace() { |
| 84 | + var binding = new TextBinding("foo _())bar"); |
| 85 | + assertEquals("foo bar", binding.getText()); |
| 86 | + assertEquals(")", binding.getMnemonic()); |
| 87 | + assertKeyCombination(")", binding.getMnemonicKeyCombination()); |
| 88 | + assertEquals(4, binding.getMnemonicIndex()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testEscapedMnemonicSymbol() { |
| 93 | + var binding = new TextBinding("foo __bar"); |
| 94 | + assertEquals("foo _bar", binding.getText()); |
| 95 | + assertNull(binding.getMnemonic()); |
| 96 | + assertNull(binding.getMnemonicKeyCombination()); |
| 97 | + assertEquals(-1, binding.getMnemonicIndex()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testWhitespaceIsNotProcessedAsExtendedMnemonic() { |
| 102 | + var binding = new TextBinding("foo _( ) bar"); |
| 103 | + assertEquals("foo ( ) bar", binding.getText()); |
| 104 | + assertEquals("(", binding.getMnemonic()); |
| 105 | + assertKeyCombination("(", binding.getMnemonicKeyCombination()); |
| 106 | + assertEquals(4, binding.getMnemonicIndex()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testUnderscoreNotFollowedByAlphabeticCharIsNotAMnemonic() { |
| 111 | + var binding = new TextBinding("foo_ bar"); |
| 112 | + assertEquals("foo_ bar", binding.getText()); |
| 113 | + assertNull(binding.getMnemonic()); |
| 114 | + assertNull(binding.getMnemonicKeyCombination()); |
| 115 | + assertEquals(-1, binding.getMnemonicIndex()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testUnderscoreAtEndOfTextIsNotAMnemonic() { |
| 120 | + var binding = new TextBinding("foo_"); |
| 121 | + assertEquals("foo_", binding.getText()); |
| 122 | + assertNull(binding.getMnemonic()); |
| 123 | + assertNull(binding.getMnemonicKeyCombination()); |
| 124 | + assertEquals(-1, binding.getMnemonicIndex()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testMnemonicParsingStopsAfterFirstSimpleMnemonic() { |
| 129 | + var binding = new TextBinding("_foo _bar _qux"); |
| 130 | + assertEquals("foo _bar _qux", binding.getText()); |
| 131 | + assertEquals("f", binding.getMnemonic()); |
| 132 | + assertKeyCombination("f", binding.getMnemonicKeyCombination()); |
| 133 | + assertEquals(0, binding.getMnemonicIndex()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testMnemonicParsingStopsAfterFirstExtendedMnemonic() { |
| 138 | + var binding = new TextBinding("_(x)foo _bar _qux"); |
| 139 | + assertEquals("foo _bar _qux", binding.getText()); |
| 140 | + assertEquals("x", binding.getMnemonic()); |
| 141 | + assertKeyCombination("x", binding.getMnemonicKeyCombination()); |
| 142 | + assertEquals(0, binding.getMnemonicIndex()); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments