--- /dev/null
+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());
+ }
+ }
+}