AutoCompleteDecorator { public static void decorate(JComboBox comboBox) { decorate(comboBox, null); } private  AutoCompleteDecorator(); static void decorate(JComboBox comboBox); static void decorate(JComboBox comboBox, ObjectToStringConverter stringConverter); static void decorate(JList list, JTextComponent textComponent); static void decorate(JList list, JTextComponent textComponent, ObjectToStringConverter stringConverter); static void decorate(JTextComponent textComponent, List<?> items, boolean strictMatching); static void decorate(JTextComponent textComponent, List<?> items, boolean strictMatching, ObjectToStringConverter stringConverter); static void decorate(JTextComponent textComponent, AutoCompleteDocument document, AbstractAutoCompleteAdaptor adaptor);  }
@Test public void testDefaultConverterJList() { JList list = new JList(combo.getModel()); JTextComponent text = new JTextField(); AutoCompleteDecorator.decorate(list, text); text.setText("A"); }
@Test public void testDefaultConverterList() { List<?> list = Arrays.asList(new String[]{"Alpha", "Bravo", "Charlie", "Delta"}); JTextComponent text = new JTextField(); AutoCompleteDecorator.decorate(text, list, true); text.setText("A"); }
@Test public void testDefaultConverterCombo() { AutoCompleteDecorator.decorate(combo); JTextComponent text = (JTextComponent) combo.getEditor().getEditorComponent(); text.setText("A"); }
@Test public void testRedecorateComboBox() { AutoCompleteDecorator.decorate(combo); Component editor = combo.getEditor().getEditorComponent(); int expectedFocusListenerCount = editor.getFocusListeners().length; int expectedKeyListenerCount = editor.getKeyListeners().length; int expectedPropListenerCount = combo.getPropertyChangeListeners("editor").length; int expectedActionListenerCount = combo.getActionListeners().length; AutoCompleteDecorator.decorate(combo); editor = combo.getEditor().getEditorComponent(); assertThat(editor.getFocusListeners().length, is(expectedFocusListenerCount)); assertThat(editor.getKeyListeners().length, is(expectedKeyListenerCount)); assertThat(combo.getPropertyChangeListeners("editor").length, is(expectedPropListenerCount)); assertThat(combo.getActionListeners().length, is(expectedActionListenerCount)); }
@Test public void testRedecorateList() { JList list = new JList(); JTextField textField = new JTextField(); AutoCompleteDecorator.decorate(list, textField); int expectedListSelectionListenerCount = list.getListSelectionListeners().length; AutoCompleteDecorator.decorate(list, textField); assertThat(list.getListSelectionListeners().length, is(expectedListSelectionListenerCount)); }
@Test public void testRedecorateTextComponent() { JTextField textField = new JTextField(); AutoCompleteDecorator.decorate(textField, Collections.emptyList(), true); int expectedFocusListenerLength = textField.getFocusListeners().length; AutoCompleteDecorator.decorate(textField, Collections.emptyList(), true); assertThat(textField.getFocusListeners().length, is(expectedFocusListenerLength)); }
@Test public void testDecoratingJTextPane() { List<String> strings = Arrays.asList("Alpha", "Bravo", "Charlie", "Delta"); AutoCompleteDecorator.decorate(new JTextPane(), strings, true); }
@Test public void testAddingItemsAfterDecorating() { AutoCompleteDecorator.decorate(combo); combo.addItem("Echo"); }
@Test public void testAddingItemsAfterDecoratingEmpty() { JComboBox box = new JComboBox(); AutoCompleteDecorator.decorate(box); box.addItem("Alhpa"); }
@Test public void testRemovingItemsAfterDecorating() { AutoCompleteDecorator.decorate(combo); combo.removeAll(); }
@Test @Category(Visual.class) public void testNonStrictCompletionWithKeyMovement() { assumeThat(GraphicsEnvironment.isHeadless(), is(false)); combo.setEditable(true); AutoCompleteDecorator.decorate(combo); combo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if ("".equals(combo.getSelectedItem())) { fail("received illegal value"); } } }); JFrame frame = new JFrame(); frame.add(combo); frame.pack(); frame.setVisible(true); assertThat((String) combo.getSelectedItem(), is("Alpha")); assumeThat(combo.isPopupVisible(), is(false)); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_DOWN, CHAR_UNDEFINED)); assertThat(combo.isPopupVisible(), is(true)); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_DOWN, CHAR_UNDEFINED)); assertThat((String) combo.getSelectedItem(), is("Bravo")); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_PAGE_DOWN, CHAR_UNDEFINED)); assertThat((String) combo.getSelectedItem(), is("Delta")); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_UP, CHAR_UNDEFINED)); assertThat((String) combo.getSelectedItem(), is("Charlie")); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_PAGE_UP, CHAR_UNDEFINED)); assertThat((String) combo.getSelectedItem(), is("Alpha")); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_END, CHAR_UNDEFINED)); assertThat((String) combo.getSelectedItem(), is("Delta")); combo.processKeyEvent(new KeyEvent(combo, KEY_PRESSED, currentTimeMillis(), 0, VK_HOME, CHAR_UNDEFINED)); assertThat((String) combo.getSelectedItem(), is("Alpha")); }