|
| 1 | +package com.codename1.samples; |
| 2 | + |
| 3 | +import com.codename1.junit.FormTest; |
| 4 | +import com.codename1.junit.UITestBase; |
| 5 | +import com.codename1.ui.DisplayTest; |
| 6 | +import com.codename1.ui.Form; |
| 7 | +import com.codename1.ui.Label; |
| 8 | +import com.codename1.ui.TextArea; |
| 9 | +import com.codename1.ui.TextField; |
| 10 | +import com.codename1.ui.layouts.BoxLayout; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +class AutoCapitalizationSampleTest extends UITestBase { |
| 15 | + |
| 16 | + @FormTest |
| 17 | + void sentenceConstraintCapitalizesFirstLetters() { |
| 18 | + implementation.setDisplaySize(1080, 1920); |
| 19 | + |
| 20 | + Form form = new Form("Hi World", BoxLayout.y()); |
| 21 | + form.add(new Label("Hi World")); |
| 22 | + TextField textField = createSampleField(TextArea.INITIAL_CAPS_SENTENCE); |
| 23 | + form.add(textField); |
| 24 | + |
| 25 | + form.show(); |
| 26 | + startEditing(textField); |
| 27 | + |
| 28 | + implementation.dispatchKeyPress('h'); |
| 29 | + implementation.dispatchKeyPress('e'); |
| 30 | + implementation.dispatchKeyPress('l'); |
| 31 | + implementation.dispatchKeyPress('l'); |
| 32 | + implementation.dispatchKeyPress('o'); |
| 33 | + implementation.dispatchKeyPress('.'); |
| 34 | + implementation.dispatchKeyPress(' '); |
| 35 | + implementation.dispatchKeyPress('n'); |
| 36 | + implementation.dispatchKeyPress('e'); |
| 37 | + implementation.dispatchKeyPress('x'); |
| 38 | + implementation.dispatchKeyPress('t'); |
| 39 | + implementation.dispatchKeyPress(' '); |
| 40 | + implementation.dispatchKeyPress('s'); |
| 41 | + implementation.dispatchKeyPress('e'); |
| 42 | + implementation.dispatchKeyPress('n'); |
| 43 | + implementation.dispatchKeyPress('t'); |
| 44 | + implementation.dispatchKeyPress('e'); |
| 45 | + implementation.dispatchKeyPress('n'); |
| 46 | + implementation.dispatchKeyPress('c'); |
| 47 | + implementation.dispatchKeyPress('e'); |
| 48 | + |
| 49 | + assertEquals("Hello. Next sentence", textField.getText()); |
| 50 | + } |
| 51 | + |
| 52 | + @FormTest |
| 53 | + void wordConstraintCapitalizesEachWordAcrossLines() { |
| 54 | + implementation.setDisplaySize(1080, 1920); |
| 55 | + |
| 56 | + Form form = new Form("Hi World", BoxLayout.y()); |
| 57 | + form.add(new Label("Hi World")); |
| 58 | + TextField textField = createSampleField(TextArea.INITIAL_CAPS_WORD); |
| 59 | + form.add(textField); |
| 60 | + |
| 61 | + form.show(); |
| 62 | + startEditing(textField); |
| 63 | + |
| 64 | + implementation.dispatchKeyPress('m'); |
| 65 | + implementation.dispatchKeyPress('u'); |
| 66 | + implementation.dispatchKeyPress('l'); |
| 67 | + implementation.dispatchKeyPress('t'); |
| 68 | + implementation.dispatchKeyPress('i'); |
| 69 | + implementation.dispatchKeyPress(' '); |
| 70 | + implementation.dispatchKeyPress('l'); |
| 71 | + implementation.dispatchKeyPress('i'); |
| 72 | + implementation.dispatchKeyPress('n'); |
| 73 | + implementation.dispatchKeyPress('e'); |
| 74 | + implementation.dispatchKeyPress('\n'); |
| 75 | + implementation.dispatchKeyPress('a'); |
| 76 | + implementation.dispatchKeyPress('u'); |
| 77 | + implementation.dispatchKeyPress('t'); |
| 78 | + implementation.dispatchKeyPress('o'); |
| 79 | + implementation.dispatchKeyPress(' '); |
| 80 | + implementation.dispatchKeyPress('c'); |
| 81 | + implementation.dispatchKeyPress('a'); |
| 82 | + implementation.dispatchKeyPress('p'); |
| 83 | + implementation.dispatchKeyPress('i'); |
| 84 | + implementation.dispatchKeyPress('t'); |
| 85 | + implementation.dispatchKeyPress('a'); |
| 86 | + implementation.dispatchKeyPress('l'); |
| 87 | + implementation.dispatchKeyPress('i'); |
| 88 | + implementation.dispatchKeyPress('z'); |
| 89 | + implementation.dispatchKeyPress('a'); |
| 90 | + implementation.dispatchKeyPress('t'); |
| 91 | + implementation.dispatchKeyPress('i'); |
| 92 | + implementation.dispatchKeyPress('o'); |
| 93 | + implementation.dispatchKeyPress('n'); |
| 94 | + |
| 95 | + assertEquals("Multi Line\nAuto Capitalization", textField.getText()); |
| 96 | + } |
| 97 | + |
| 98 | + private void startEditing(TextField textField) { |
| 99 | + textField.startEditingAsync(); |
| 100 | + flushSerialCalls(); |
| 101 | + DisplayTest.flushEdt(); |
| 102 | + flushSerialCalls(); |
| 103 | + } |
| 104 | + |
| 105 | + private TextField createSampleField(int constraint) { |
| 106 | + TextField textField = new TextField(); |
| 107 | + textField.setSingleLineTextArea(false); |
| 108 | + textField.setConstraint(constraint); |
| 109 | + textField.setGrowLimit(-1); |
| 110 | + textField.setMaxSize(1600); |
| 111 | + textField.setHint("Type Something..."); |
| 112 | + textField.setQwertyInput(true); |
| 113 | + return textField; |
| 114 | + } |
| 115 | +} |
0 commit comments