From b889ea425e373a429b24e7eb2b2e3d4b6ad766dc Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 29 May 2015 14:07:56 +0100 Subject: [PATCH] JAL-1753 Junit for new class --- test/jalview/gui/ProgressBarTest.java | 112 +++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 test/jalview/gui/ProgressBarTest.java 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()); + } + } +} -- 1.7.10.2