📄 listboxtest.java
字号:
package org.uispec4j;
import junit.framework.AssertionFailedError;
import org.uispec4j.utils.AssertionFailureNotDetectedError;
import org.uispec4j.utils.Counter;
import org.uispec4j.xml.XmlAssert;
import org.uispec4j.xml.EventLogger;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class ListBoxTest extends UIComponentTestCase {
private static final String[] ALL_ITEMS = new String[]{"First Item", "Second Item", "Third Item"};
private JList jList;
private ListBox listBox;
protected void setUp() throws Exception {
super.setUp();
init(new JList(new Object[]{"First Item", "Second Item", "Third Item"}));
}
private void init(JList list) {
jList = list;
jList.setName("myList");
listBox = new ListBox(jList);
}
public void testGetComponentTypeName() throws Exception {
assertEquals("listBox", listBox.getDescriptionTypeName());
}
public void testGetDescription() throws Exception {
XmlAssert.assertEquivalent("<listBox name='myList'/>", listBox.getDescription());
}
public void testFactory() throws Exception {
checkFactory(new JList(), ListBox.class);
}
protected UIComponent createComponent() {
return listBox;
}
public void testEmptyList() throws Exception {
init(new JList());
assertTrue(listBox.isEmpty());
}
public void testAssertEmptyFailure() throws Exception {
try {
assertTrue(listBox.isEmpty());
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("List should be empty but contains: [First Item,Second Item,Third Item]",
e.getMessage());
}
}
public void testContentEquals() throws Exception {
assertTrue(listBox.contentEquals(ALL_ITEMS));
}
public void testContentEqualsWithNullItem() throws Exception {
init(new JList(new Object[]{"First Item", null}));
listBox.contentEquals(new String[]{"First Item", ""});
}
public void testContentEqualsErrors() throws Exception {
try {
assertTrue(listBox.contentEquals(new String[]{"another", "list", "here"}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
try {
assertTrue(listBox.contentEquals(new String[]{"another", "list", "here", "with", "more", "elements"}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
try {
assertTrue(listBox.contentEquals(new String[]{"one element only"}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContains() throws Exception {
init(new JList(new Object[]{"one", "two", "three"}));
assertTrue(listBox.contains("two"));
assertTrue(listBox.contains(new String[]{"three", "one"}));
}
public void testContainsErrors() throws Exception {
init(new JList(new Object[]{"one", "two", "three"}));
try {
assertTrue(listBox.contains("unknown"));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Item 'unknown' not found - actual content:[one, two, three]", e.getMessage());
}
try {
assertTrue(listBox.contains(new String[]{"three", "unknown"}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Item 'unknown' not found - actual content:[one, two, three]", e.getMessage());
}
}
public void testSelectByIndex() throws Exception {
assertEquals(-1, jList.getSelectedIndex());
listBox.selectIndices(new int[]{2});
assertEquals(2, jList.getSelectedIndex());
}
public void testSelectByName() throws Exception {
checkSelectionByName(new String[]{"First Item", "Second Item", "Third Item"});
}
public void testSelectionIsNotCaseSensitive() throws Exception {
checkSelectionByName(new String[]{"first iteM", "second Item", "THIRD iTem"});
}
public void testSelectionWithSubstring() throws Exception {
checkSelectionByName(new String[]{"first", "second", "ird it"});
}
public void testAmbiguityInSelection() throws Exception {
try {
listBox.select("item");
throw new AssertionFailureNotDetectedError();
}
catch (ItemAmbiguityException e) {
assertEquals("3 items are matching the same pattern 'item': [First Item,Second Item,Third Item]", e.getMessage());
}
}
public void testSelectingAnUnknownValueThrowsAnException() throws Exception {
try {
listBox.select("unknown");
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testClearSelection() throws Exception {
jList.setSelectedIndex(1);
listBox.clearSelection();
assertTrue(listBox.selectionIsEmpty());
}
public void testSelectByNameThrowsAnExceptionIfTheItemIsNotFound() throws Exception {
try {
listBox.select("unknown");
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Item 'unknown' not found in [First Item,Second Item,Third Item]",
e.getMessage());
}
}
public void testMultiSelectionWithIndices() throws Exception {
listBox.selectIndices(new int[]{0, 1});
int[] selectedIndices = jList.getSelectedIndices();
assertEquals(2, selectedIndices.length);
assertEquals(0, selectedIndices[0]);
assertEquals(1, selectedIndices[1]);
}
public void testMultiSelectionWithNames() throws Exception {
listBox.select(new String[]{"first", "second"});
int[] selectedIndices = jList.getSelectedIndices();
assertEquals(2, selectedIndices.length);
assertEquals(0, selectedIndices[0]);
assertEquals(1, selectedIndices[1]);
}
public void testMultiSelectionSetsTheAdjustingMode() throws Exception {
final EventLogger logger = new EventLogger();
jList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
logger.log("valueChanged");
}
}
});
listBox.select(new String[]{"first", "second"});
logger.assertEquals("<log><valueChanged/></log>");
listBox.selectIndices(new int[]{0, 1});
logger.assertEquals("<log><valueChanged/></log>");
}
public void testMultiSelectionWithNamesWhenSomeNamesDoNotMatch() throws Exception {
listBox.select(new String[]{"first", "third", "fourth"});
int[] selectedIndices = jList.getSelectedIndices();
assertEquals(2, selectedIndices.length);
assertEquals(0, selectedIndices[0]);
assertEquals(2, selectedIndices[1]);
}
public void testGetSize() throws Exception {
assertEquals(3, listBox.getSize());
}
public void testAssertSelectionEmpty() throws Exception {
jList.setSelectedIndex(-1);
assertTrue(listBox.selectionIsEmpty());
jList.setSelectedIndex(1);
try {
assertTrue(listBox.selectionIsEmpty());
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Selection should be empty but is: [Second Item]", e.getMessage());
}
}
public void testAssertSelection() throws Exception {
jList.setSelectedIndex(0);
assertTrue(listBox.selectionEquals("First Item"));
jList.setSelectedIndex(1);
assertTrue(listBox.selectionEquals("Second Item"));
jList.setSelectedIndices(new int[]{0, 2});
assertTrue(listBox.selectionEquals(new String[]{"First Item", "Third Item"}));
}
public void testAssertSelectionErrors() throws Exception {
jList.setSelectedIndex(0);
try {
assertTrue(listBox.selectionEquals("Second Item"));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Expected: [Second Item]\n" +
"Actual: [First Item]", e.getMessage());
}
try {
assertTrue(listBox.selectionEquals(new String[]{"First Item", "Third Item"}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Expected: [First Item,Third Item]\n" +
"Actual: [First Item]", e.getMessage());
}
}
public void testPressingKeyForNavigatingInTheList() throws Exception {
jList.setSelectedIndex(0);
assertTrue(listBox.selectionEquals("First Item"));
listBox.pressKey(Key.DOWN);
assertTrue(listBox.selectionEquals("Second Item"));
listBox.pressKey(Key.DOWN);
assertTrue(listBox.selectionEquals("Third Item"));
listBox.pressKey(Key.UP);
assertTrue(listBox.selectionEquals("Second Item"));
}
public void testUsingShiftToSelectMultipleElements() throws Exception {
jList.setSelectedIndex(0);
listBox.pressKey(Key.shift(Key.PAGE_DOWN));
assertTrue(listBox.selectionEquals(ALL_ITEMS));
}
public void testPressingKeyNotifiesCustomKeyListeners() throws Exception {
final Counter counter = new Counter();
jList.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent event) {
counter.increment();
}
public void keyReleased(KeyEvent event) {
}
public void keyTyped(KeyEvent event) {
}
});
jList.setSelectedIndex(0);
assertTrue(listBox.selectionEquals("First Item"));
listBox.pressKey(Key.DOWN);
assertEquals(1, counter.getCount());
assertTrue(listBox.selectionEquals("Second Item"));
}
public void testUsingARenderer() throws Exception {
init(new JList(new Object[]{new Integer(3), new Integer(7), new Integer(11)}));
jList.setCellRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
return super.getListCellRendererComponent(list, "-" + value, index, isSelected, cellHasFocus);
}
});
assertTrue(listBox.contentEquals(new String[]{"-3", "-7", "-11"}));
listBox.selectIndex(0);
assertTrue(listBox.selectionEquals("-3"));
listBox.select("-7");
assertTrue(listBox.selectionEquals("-7"));
}
public void testUsingACustomCellRenderer() throws Exception {
init(new JList(new Object[]{new Integer(3), new Integer(7)}));
jList.setCellRenderer(new ListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
return new JButton("-" + value);
}
});
listBox.setCellValueConverter(new ListBoxCellValueConverter() {
public String getValue(int index, Component renderedComponent, Object modelObject) {
return ((JButton)renderedComponent).getText();
}
});
assertTrue(listBox.contentEquals(new String[]{"-3", "-7"}));
listBox.selectIndex(0);
assertTrue(listBox.selectionEquals("-3"));
listBox.select("-7");
assertTrue(listBox.selectionEquals("-7"));
}
public void testAssertContentEqualsAfterSettingACustomCellValueConverter() throws Exception {
assertTrue(listBox.contentEquals(ALL_ITEMS));
listBox.setCellValueConverter(new ListBoxCellValueConverter() {
public String getValue(int index, Component renderedComponent, Object modelObject) {
if (index == 0) {
return "Item converted";
}
return modelObject.toString();
}
});
assertTrue(listBox.contentEquals(new String[]{"Item converted", "Second Item", "Third Item"}));
try {
assertTrue(listBox.contentEquals(ALL_ITEMS));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
// Expected
}
try {
assertTrue(listBox.isEmpty());
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("List should be empty but contains: [Item converted,Second Item,Third Item]",
e.getMessage());
}
}
public void testSelectionAfterSettingACustomCellValueConverter() throws Exception {
assertTrue(listBox.contentEquals(ALL_ITEMS));
listBox.setCellValueConverter(new ListBoxCellValueConverter() {
public String getValue(int index, Component renderedComponent, Object modelObject) {
if (index == 0) {
return "Item converted";
}
return modelObject.toString();
}
});
listBox.select("Item converted");
assertTrue(listBox.selectionEquals("Item converted"));
assertTrue(listBox.selectionEquals(new String[]{"Item converted"}));
try {
listBox.select("First Item");
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Item 'First Item' not found in [Item converted,Second Item,Third Item]",
e.getMessage());
}
}
private void checkSelectionByName(String[] names) {
listBox.select(names[0]);
assertTrue(listBox.selectionEquals("First Item"));
assertEquals(0, jList.getSelectedIndex());
listBox.select(names[1]);
assertTrue(listBox.selectionEquals("Second Item"));
assertEquals(1, jList.getSelectedIndex());
listBox.select(names[2]);
assertTrue(listBox.selectionEquals("Third Item"));
assertEquals(2, jList.getSelectedIndex());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -