|
26 | 26 | package test.jfx.incubator.scene.control.richtext;
|
27 | 27 |
|
28 | 28 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 29 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | +import java.util.List; |
| 33 | +import javafx.css.CssMetaData; |
| 34 | +import javafx.css.Styleable; |
| 35 | +import javafx.scene.Scene; |
| 36 | +import javafx.scene.text.Font; |
29 | 37 | import org.junit.jupiter.api.AfterEach;
|
30 |
| -import org.junit.jupiter.api.Assertions; |
31 | 38 | import org.junit.jupiter.api.BeforeEach;
|
32 | 39 | import org.junit.jupiter.api.Test;
|
33 | 40 | import jfx.incubator.scene.control.richtext.CodeArea;
|
| 41 | +import jfx.incubator.scene.control.richtext.RichTextArea; |
34 | 42 | import jfx.incubator.scene.control.richtext.model.CodeTextModel;
|
35 | 43 | import jfx.incubator.scene.control.richtext.model.RichTextModel;
|
36 | 44 | import jfx.incubator.scene.control.richtext.skin.CodeAreaSkin;
|
| 45 | +import test.jfx.incubator.scene.util.TUtil; |
37 | 46 |
|
38 | 47 | /**
|
39 |
| - * Tests CodeArea. |
| 48 | + * Tests the CodeArea control. |
40 | 49 | */
|
41 | 50 | public class CodeAreaTest {
|
42 | 51 | private CodeArea control;
|
43 | 52 |
|
44 | 53 | @BeforeEach
|
45 | 54 | public void beforeEach() {
|
46 |
| - setUncaughtExceptionHandler(); |
| 55 | + TUtil.setUncaughtExceptionHandler(); |
47 | 56 | control = new CodeArea();
|
48 | 57 | control.setSkin(new CodeAreaSkin(control));
|
49 | 58 | }
|
50 | 59 |
|
51 | 60 | @AfterEach
|
52 | 61 | public void afterEach() {
|
53 |
| - removeUncaughtExceptionHandler(); |
| 62 | + TUtil.removeUncaughtExceptionHandler(); |
54 | 63 | }
|
55 | 64 |
|
56 |
| - private void setUncaughtExceptionHandler() { |
57 |
| - Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> { |
58 |
| - if (throwable instanceof RuntimeException) { |
59 |
| - throw (RuntimeException)throwable; |
60 |
| - } else { |
61 |
| - Thread.currentThread().getThreadGroup().uncaughtException(thread, throwable); |
62 |
| - } |
| 65 | + // constructors |
| 66 | + |
| 67 | + @Test |
| 68 | + public void defaultModelIsCodeTextModel() { |
| 69 | + assertTrue(control.getModel() instanceof CodeTextModel); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void nullModelInConstructor() { |
| 74 | + control = new CodeArea(null); |
| 75 | + assertTrue(control.getModel() == null); |
| 76 | + |
| 77 | + control = new CodeArea(null); |
| 78 | + CodeTextModel m = new CodeTextModel() { }; |
| 79 | + control.setModel(m); |
| 80 | + assertSame(m, control.getModel()); |
| 81 | + } |
| 82 | + |
| 83 | + // properties |
| 84 | + |
| 85 | + @Test |
| 86 | + public void propertiesSettersAndGetters() { |
| 87 | + TUtil.testProperty(control.fontProperty(), control::getFont, control::setFont, new Font("Bogus", 22)); |
| 88 | + TUtil.testBooleanProperty(control.lineNumbersEnabledProperty(), control::isLineNumbersEnabled, control::setLineNumbersEnabled); |
| 89 | + TUtil.testProperty(control.lineSpacingProperty(), control::getLineSpacing, (n) -> control.setLineSpacing(n.doubleValue()), 10.0, 22.0); |
| 90 | + TUtil.testProperty(control.tabSizeProperty(), control::getTabSize, (n) -> control.setTabSize(n.intValue()), 1, 2); |
| 91 | + } |
| 92 | + |
| 93 | + // default values |
| 94 | + |
| 95 | + @Test |
| 96 | + public void defaultPropertyValues() { |
| 97 | + TUtil.checkDefaultValue(control.fontProperty(), control::getFont, (f) -> { |
| 98 | + Font expected = new Font("Monospace", -1); |
| 99 | + assertEquals(f.getFamily(), expected.getFamily()); |
| 100 | + assertEquals(f.getSize(), expected.getSize()); |
| 101 | + return true; |
63 | 102 | });
|
| 103 | + TUtil.testDefaultValue(control.lineNumbersEnabledProperty(), control::isLineNumbersEnabled, false); |
| 104 | + TUtil.testDefaultValue(control.lineSpacingProperty(), control::getLineSpacing, 0.0); |
| 105 | + TUtil.testDefaultValue(control.tabSizeProperty(), control::getTabSize, 8); |
64 | 106 | }
|
65 | 107 |
|
66 |
| - private void removeUncaughtExceptionHandler() { |
67 |
| - Thread.currentThread().setUncaughtExceptionHandler(null); |
| 108 | + // css |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testFontCSS() { |
| 112 | + Scene s = new Scene(control); |
| 113 | + control.setStyle("-fx-font: 24 Amble"); |
| 114 | + control.applyCss(); |
| 115 | + assertEquals(Font.font("Amble", 24), control.getFont()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testLineSpacingCSS() { |
| 120 | + Scene s = new Scene(control); |
| 121 | + control.setStyle("-fx-line-spacing: 5.55"); |
| 122 | + control.applyCss(); |
| 123 | + assertEquals(5.55, control.getLineSpacing()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testTabSizeCSS() { |
| 128 | + Scene s = new Scene(control); |
| 129 | + control.setStyle("-fx-tab-size: 17"); |
| 130 | + control.applyCss(); |
| 131 | + assertEquals(17, control.getTabSize()); |
| 132 | + } |
| 133 | + |
| 134 | + // property binding |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testPropertyBinding() { |
| 138 | + TUtil.testBinding(control.fontProperty(), control::getFont, new Font("Bogus", 22)); |
| 139 | + TUtil.testBinding(control.lineNumbersEnabledProperty(), control::isLineNumbersEnabled); |
| 140 | + TUtil.testBinding(control.lineSpacingProperty(), control::getLineSpacing, 10.0, 22.0); |
| 141 | + TUtil.testBinding(control.tabSizeProperty(), control::getTabSize, 1, 2, 5, 17); |
| 142 | + } |
| 143 | + |
| 144 | + // functional API tests |
| 145 | + |
| 146 | + @Test |
| 147 | + public void getControlCssMetaData() { |
| 148 | + List<CssMetaData<? extends Styleable, ?>> md = control.getControlCssMetaData(); |
| 149 | + // CodeArea:395 |
| 150 | + int styleablesCount = 3; |
| 151 | + assertEquals(md.size(), RichTextArea.getClassCssMetaData().size() + styleablesCount); |
68 | 152 | }
|
69 | 153 |
|
70 | 154 | @Test
|
@@ -92,30 +176,26 @@ public void getText() {
|
92 | 176 |
|
93 | 177 | /** can set a null and non-null CodeTextModel */
|
94 | 178 | @Test
|
95 |
| - public void nullModel() { |
96 |
| - CodeArea t = new CodeArea(); |
97 |
| - t.setModel(null); |
98 |
| - t.setModel(new CodeTextModel()); |
99 |
| - |
| 179 | + public void modelNull() { |
| 180 | + control.setModel(null); |
| 181 | + control.setModel(new CodeTextModel()); |
100 | 182 | }
|
101 | 183 |
|
102 | 184 | /** disallows setting model other than CodeTextModel */
|
103 | 185 | @Test
|
104 |
| - public void wrongModel() { |
105 |
| - CodeArea t = new CodeArea(); |
106 |
| - Assertions.assertThrows(IllegalArgumentException.class, () -> { |
107 |
| - t.setModel(new RichTextModel()); |
| 186 | + public void modelWrong() { |
| 187 | + var m = control.getModel(); |
| 188 | + assertThrows(IllegalArgumentException.class, () -> { |
| 189 | + control.setModel(new RichTextModel()); |
108 | 190 | });
|
109 |
| - Assertions.assertTrue(t.getModel() instanceof CodeTextModel); |
| 191 | + assertTrue(control.getModel() == m); |
110 | 192 | }
|
111 | 193 |
|
112 | 194 | /** acceptable custom model */
|
113 | 195 | @Test
|
114 |
| - public void acceptableModel() { |
115 |
| - class M extends CodeTextModel { } |
116 |
| - M custom = new M(); |
117 |
| - CodeArea t = new CodeArea(); |
118 |
| - t.setModel(custom); |
119 |
| - Assertions.assertTrue(t.getModel() instanceof M); |
| 196 | + public void modelAcceptable() { |
| 197 | + CustomCodeTextModel m = new CustomCodeTextModel(); |
| 198 | + control.setModel(m); |
| 199 | + assertTrue(control.getModel() == m); |
120 | 200 | }
|
121 | 201 | }
|
0 commit comments