From: gmungoc Date: Fri, 29 May 2015 13:07:56 +0000 (+0100) Subject: JAL-1753 Junit for new class X-Git-Tag: Release_2_10_0~650^2~2 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=b889ea425e373a429b24e7eb2b2e3d4b6ad766dc JAL-1753 Junit for new class --- diff --git a/test/jalview/gui/ProgressBarTest.java b/test/jalview/gui/ProgressBarTest.java new file mode 100644 index 0000000..2d3db68 --- /dev/null +++ b/test/jalview/gui/ProgressBarTest.java @@ -0,0 +1,112 @@ +package jalview.gui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.awt.Component; +import java.awt.FlowLayout; +import java.awt.GridLayout; + +import javax.swing.JLabel; +import javax.swing.JPanel; + +import org.junit.Test; + +public class ProgressBarTest +{ + + private JPanel statusPanel; + + private JLabel statusBar; + + @Test + public void testConstructor_prematureInstantiation() + { + try + { + new ProgressBar(null, null); + fail("Expected exception"); + } catch (NullPointerException e) + { + // expected + } + } + + @Test + public void testConstructor_wrongLayout() + { + statusPanel = new JPanel(); + statusPanel.setLayout(new FlowLayout()); + try + { + new ProgressBar(statusPanel, null); + fail("expected exception"); + } catch (IllegalArgumentException e) + { + // expected + } + } + + @Test + public void testSetProgressBar() + { + statusPanel = new JPanel(); + GridLayout layout = new GridLayout(1, 1); + statusPanel.setLayout(layout); + statusBar = new JLabel("nothing"); + ProgressBar pb = new ProgressBar(statusPanel, statusBar); + + /* + * Add 'hello' + */ + pb.setProgressBar("hello", 1L); + verifyProgress(layout, new String[] + { "hello" }); + + /* + * Add 'world' + */ + pb.setProgressBar("world", 2L); + verifyProgress(layout, new String[] + { "hello", "world" }); + + /* + * Remove 'hello' with no status bar update + */ + pb.setProgressBar(null, 1L); + verifyProgress(layout, new String[] + { "world" }); + assertEquals("nothing", statusBar.getText()); + + /* + * Remove 'world' with status bar update + */ + pb.setProgressBar("goodbye", 2L); + verifyProgress(layout, new String[] + {}); + assertEquals("goodbye", statusBar.getText()); + } + + /** + * Verify the right number of progress bars containing the expected messages + * respectively + * + * @param layout + * @param msgs + */ + private void verifyProgress(GridLayout layout, String[] msgs) + { + int msgCount = msgs.length; + assertEquals(1 + msgCount, layout.getRows()); + assertEquals(msgCount, statusPanel.getComponentCount()); + int i = 0; + for (Component c : statusPanel.getComponents()) + { + assertTrue(c instanceof JPanel); + assertTrue(((JPanel) c).getComponent(0) instanceof JLabel); + assertEquals(msgs[i++], + ((JLabel) ((JPanel) c).getComponent(0)).getText()); + } + } +}