2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
26 import java.awt.Component;
27 import java.awt.FlowLayout;
28 import java.awt.GridLayout;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.SwingUtilities;
34 import org.testng.Assert;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
38 public class ProgressBarTest
41 @BeforeClass(alwaysRun = true)
42 public void setUpJvOptionPane()
44 JvOptionPane.setInteractiveMode(false);
45 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48 private JPanel statusPanel;
50 private JLabel statusBar;
52 @Test(groups = { "Functional" })
53 public void testConstructor_prematureInstantiation()
57 new ProgressBar(null, null);
58 Assert.fail("Expected exception");
59 } catch (NullPointerException e)
65 @Test(groups = { "Functional" })
66 public void testConstructor_wrongLayout()
68 statusPanel = new JPanel();
69 statusPanel.setLayout(new FlowLayout());
72 new ProgressBar(statusPanel, null);
73 Assert.fail("expected exception");
74 } catch (IllegalArgumentException e)
80 @Test(groups = { "Functional" })
81 public void testSetProgressBar()
83 statusPanel = new JPanel();
84 GridLayout layout = new GridLayout(1, 1);
85 statusPanel.setLayout(layout);
86 statusBar = new JLabel("nothing");
87 ProgressBar pb = new ProgressBar(statusPanel, statusBar);
92 pb.setProgressBar("hello", 1L);
93 verifyProgress(layout, new String[] { "hello" });
98 pb.setProgressBar("world", 2L);
99 verifyProgress(layout, new String[] { "hello", "world" });
102 * Remove 'hello' with no status bar update
104 pb.setProgressBar(null, 1L);
105 verifyProgress(layout, new String[] { "world" });
106 assertEquals("nothing", statusBar.getText());
109 * Remove 'world' with status bar update
111 pb.setProgressBar("goodbye", 2L);
112 verifyProgress(layout, new String[] {});
113 assertEquals("goodbye", statusBar.getText());
117 * Verify the right number of progress bars containing the expected messages
123 private void verifyProgress(final GridLayout layout, final String[] msgs)
127 SwingUtilities.invokeAndWait(new Runnable()
132 int msgCount = msgs.length;
133 assertEquals(1 + msgCount, layout.getRows());
134 assertEquals(msgCount, statusPanel.getComponentCount());
136 for (Component c : statusPanel.getComponents())
138 assertTrue(c instanceof JPanel);
139 assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
140 assertEquals(msgs[i++],
141 ((JLabel) ((JPanel) c).getComponent(0)).getText());
145 } catch (Exception e)
147 throw new AssertionError(
148 "Unexpected exception waiting for progress bar validation",