|
| 1 | +package com.codename1.ui; |
| 2 | + |
| 3 | +import com.codename1.test.UITestBase; |
| 4 | +import com.codename1.ui.Image; |
| 5 | +import com.codename1.ui.list.DefaultListModel; |
| 6 | +import com.codename1.ui.list.ListModel; |
| 7 | +import com.codename1.ui.plaf.UIManager; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Hashtable; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +public class AutoCompleteTextComponentTest extends UITestBase { |
| 19 | + |
| 20 | + private ListModel<String> suggestionModel; |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + void initModel() { |
| 24 | + suggestionModel = new DefaultListModel<String>(new String[]{"alpha", "beta", "gamma"}); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void constructorAppliesCustomFilterAndProvidesEditorAccess() throws Exception { |
| 29 | + final List<String> filtered = new ArrayList<String>(); |
| 30 | + AutoCompleteTextComponent.AutoCompleteFilter filter = new AutoCompleteTextComponent.AutoCompleteFilter() { |
| 31 | + public boolean filter(String text) { |
| 32 | + filtered.add(text); |
| 33 | + return text.length() > 0; |
| 34 | + } |
| 35 | + }; |
| 36 | + AutoCompleteTextComponent component = new AutoCompleteTextComponent(suggestionModel, filter); |
| 37 | + AutoCompleteTextField field = component.getAutoCompleteField(); |
| 38 | + Method filterMethod = AutoCompleteTextField.class.getDeclaredMethod("filter", String.class); |
| 39 | + filterMethod.setAccessible(true); |
| 40 | + Boolean result = (Boolean) filterMethod.invoke(field, "alp"); |
| 41 | + assertTrue(result.booleanValue(), "Custom filter should return its result"); |
| 42 | + assertEquals(1, filtered.size(), "Filter should be invoked with provided text"); |
| 43 | + assertEquals("alp", filtered.get(0)); |
| 44 | + assertSame(field, component.getField(), "getField should expose the underlying AutoCompleteTextField"); |
| 45 | + assertSame(field, component.getEditor(), "getEditor should return the AutoCompleteTextField instance"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void focusAnimationFollowsThemeAndManualOverrides() { |
| 50 | + Hashtable theme = new Hashtable(); |
| 51 | + theme.put("@textComponentAnimBool", "true"); |
| 52 | + UIManager.getInstance().setThemeProps(theme); |
| 53 | + |
| 54 | + AutoCompleteTextComponent component = new AutoCompleteTextComponent(suggestionModel, AcceptAllFilter.INSTANCE); |
| 55 | + assertTrue(component.isFocusAnimation(), "Theme constant should enable focus animation by default"); |
| 56 | + |
| 57 | + component.focusAnimation(false); |
| 58 | + assertFalse(component.isFocusAnimation(), "Explicit false should override theme"); |
| 59 | + |
| 60 | + component.focusAnimation(true); |
| 61 | + assertTrue(component.isFocusAnimation(), "Explicit true should override theme"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void fluentSettersUpdateUnderlyingField() { |
| 66 | + AutoCompleteTextComponent component = new AutoCompleteTextComponent(suggestionModel, AcceptAllFilter.INSTANCE); |
| 67 | + Image hintIcon = Image.createImage(2, 2); |
| 68 | + |
| 69 | + component |
| 70 | + .text("username") |
| 71 | + .hint("Enter username") |
| 72 | + .hint(hintIcon) |
| 73 | + .multiline(true) |
| 74 | + .columns(12) |
| 75 | + .rows(3) |
| 76 | + .constraint(TextArea.EMAILADDR); |
| 77 | + |
| 78 | + AutoCompleteTextField field = component.getAutoCompleteField(); |
| 79 | + assertEquals("username", field.getText()); |
| 80 | + assertEquals("Enter username", field.getHint()); |
| 81 | + assertFalse(field.isSingleLineTextArea(), "Multiline true should mark the field as multi-line"); |
| 82 | + assertEquals(12, field.getColumns()); |
| 83 | + assertEquals(3, field.getRows()); |
| 84 | + assertEquals(TextArea.EMAILADDR, field.getConstraint()); |
| 85 | + assertSame(hintIcon, field.getHintIcon()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void propertyMetadataAndValuesReflectFieldState() { |
| 90 | + AutoCompleteTextComponent component = new AutoCompleteTextComponent(suggestionModel, AcceptAllFilter.INSTANCE); |
| 91 | + component |
| 92 | + .text("initial") |
| 93 | + .label("Label") |
| 94 | + .hint("Hint") |
| 95 | + .multiline(true) |
| 96 | + .columns(7) |
| 97 | + .rows(4) |
| 98 | + .constraint(TextArea.NUMERIC); |
| 99 | + |
| 100 | + assertArrayEquals(new String[]{"text", "label", "hint", "multiline", "columns", "rows", "constraint"}, component.getPropertyNames()); |
| 101 | + assertArrayEquals(new Class[]{String.class, String.class, String.class, Boolean.class, Integer.class, Integer.class, Integer.class}, component.getPropertyTypes()); |
| 102 | + assertArrayEquals(new String[]{"String", "String", "String", "Boolean", "Integer", "Integer", "Integer"}, component.getPropertyTypeNames()); |
| 103 | + |
| 104 | + assertEquals("initial", component.getPropertyValue("text")); |
| 105 | + assertEquals("Hint", component.getPropertyValue("hint")); |
| 106 | + assertEquals(Boolean.TRUE, component.getPropertyValue("multiline")); |
| 107 | + assertEquals(Integer.valueOf(7), component.getPropertyValue("columns")); |
| 108 | + assertEquals(Integer.valueOf(4), component.getPropertyValue("rows")); |
| 109 | + assertEquals(Integer.valueOf(TextArea.NUMERIC), component.getPropertyValue("constraint")); |
| 110 | + |
| 111 | + component.setPropertyValue("text", "updated"); |
| 112 | + component.setPropertyValue("hint", "Another"); |
| 113 | + component.setPropertyValue("multiline", Boolean.FALSE); |
| 114 | + component.setPropertyValue("columns", Integer.valueOf(5)); |
| 115 | + component.setPropertyValue("rows", Integer.valueOf(2)); |
| 116 | + component.setPropertyValue("constraint", Integer.valueOf(TextArea.PHONENUMBER)); |
| 117 | + |
| 118 | + AutoCompleteTextField field = component.getAutoCompleteField(); |
| 119 | + assertEquals("updated", field.getText()); |
| 120 | + assertEquals("Another", field.getHint()); |
| 121 | + assertTrue(field.isSingleLineTextArea(), "Setting multiline false should restore single line mode"); |
| 122 | + assertEquals(5, field.getColumns()); |
| 123 | + assertEquals(2, field.getRows()); |
| 124 | + assertEquals(TextArea.PHONENUMBER, field.getConstraint()); |
| 125 | + } |
| 126 | + |
| 127 | + private enum AcceptAllFilter implements AutoCompleteTextComponent.AutoCompleteFilter { |
| 128 | + INSTANCE; |
| 129 | + |
| 130 | + public boolean filter(String text) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments