/** * */ package ssabsa.swing.component; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.Vector; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import org.fest.swing.core.BasicRobot; import org.fest.swing.core.Robot; import org.fest.swing.fixture.FrameFixture; import org.fest.swing.fixture.JComboBoxFixture; import org.junit.After; import org.junit.Before; import org.junit.Test; public class ComboBoxesRobotTest { protected Robot robot; @Before public void setUp() throws Exception { robot = BasicRobot.robotWithNewAwtHierarchy(); } @Test public void getEnterNumber() throws Exception { Vector years = new Vector(); years.add(1999); years.add(2000); years.add(2001); years.add(2002); JComboBox comboBox = new JComboBox(years); comboBox.setEditable(true); comboBox.setSelectedIndex(-1); String label = "Year: "; startInJFrame(getPanel(label, comboBox)); JComboBoxFixture fixture = comboBoxByLabel(label); assertTrue(fixture.component().isEditable()); Thread.sleep(1000); fixture.enterText("20"); Thread.sleep(2000); JTextField textField = (JTextField) fixture.component().getEditor().getEditorComponent(); assertEquals("20", textField.getText()); } @Test public void getEnterNumber2() throws Exception { Vector years = new Vector(); years.add("1999"); years.add("2000"); years.add("2001"); years.add("2002"); JComboBox comboBox = new JComboBox(years); comboBox.setEditable(true); comboBox.setSelectedIndex(-1); String label = "Year: "; startInJFrame(getPanel(label, comboBox)); JComboBoxFixture fixture = comboBoxByLabel(label); assertTrue(fixture.component().isEditable()); Thread.sleep(1000); fixture.enterText("78"); Thread.sleep(2000); JTextField textField = (JTextField) fixture.component().getEditor().getEditorComponent(); assertEquals("78", textField.getText()); } @After public void tearDown() throws Exception { if (robot != null) { robot.cleanUp(); } } /** * Show the given panel in a JFrame window, for testing. */ protected FrameFixture startInJFrame(JPanel panel) { JFrame frame = new JFrame(); frame.add(panel); frame.setVisible(true); frame.setSize(640, 480); return new FrameFixture(robot, frame); } private JPanel getPanel(String label, JComboBox comboBox) { JLabel fieldLabel = new JLabel(label); fieldLabel.setLabelFor(comboBox); JPanel panel = new JPanel(); panel.add(fieldLabel); panel.add(comboBox); return panel; } protected JComboBoxFixture comboBoxByLabel(String labelText) { return new JComboBoxFixture(robot, robot.finder().findByLabel(labelText, JComboBox.class)); } }