JAL-1270 test suits import order refactor
[jalview.git] / test / jalview / gui / ProgressBarTest.java
1 package jalview.gui;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertTrue;
5
6 import java.awt.Component;
7 import java.awt.FlowLayout;
8 import java.awt.GridLayout;
9
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12
13 import org.testng.Assert;
14 import org.testng.annotations.Test;
15
16 public class ProgressBarTest
17 {
18
19   private JPanel statusPanel;
20
21   private JLabel statusBar;
22
23   @Test
24   public void testConstructor_prematureInstantiation()
25   {
26     try
27     {
28       new ProgressBar(null, null);
29       Assert.fail("Expected exception");
30     } catch (NullPointerException e)
31     {
32       // expected
33     }
34   }
35
36   @Test
37   public void testConstructor_wrongLayout()
38   {
39     statusPanel = new JPanel();
40     statusPanel.setLayout(new FlowLayout());
41     try
42     {
43       new ProgressBar(statusPanel, null);
44       Assert.fail("expected exception");
45     } catch (IllegalArgumentException e)
46     {
47       // expected
48     }
49   }
50
51   @Test
52   public void testSetProgressBar()
53   {
54     statusPanel = new JPanel();
55     GridLayout layout = new GridLayout(1, 1);
56     statusPanel.setLayout(layout);
57     statusBar = new JLabel("nothing");
58     ProgressBar pb = new ProgressBar(statusPanel, statusBar);
59
60     /*
61      * Add 'hello'
62      */
63     pb.setProgressBar("hello", 1L);
64     verifyProgress(layout, new String[]
65     { "hello" });
66
67     /*
68      * Add 'world'
69      */
70     pb.setProgressBar("world", 2L);
71     verifyProgress(layout, new String[]
72     { "hello", "world" });
73
74     /*
75      * Remove 'hello' with no status bar update
76      */
77     pb.setProgressBar(null, 1L);
78     verifyProgress(layout, new String[]
79     { "world" });
80     assertEquals("nothing", statusBar.getText());
81
82     /*
83      * Remove 'world' with status bar update
84      */
85     pb.setProgressBar("goodbye", 2L);
86     verifyProgress(layout, new String[]
87     {});
88     assertEquals("goodbye", statusBar.getText());
89   }
90
91   /**
92    * Verify the right number of progress bars containing the expected messages
93    * respectively
94    * 
95    * @param layout
96    * @param msgs
97    */
98   private void verifyProgress(GridLayout layout, String[] msgs)
99   {
100     int msgCount = msgs.length;
101     assertEquals(1 + msgCount, layout.getRows());
102     assertEquals(msgCount, statusPanel.getComponentCount());
103     int i = 0;
104     for (Component c : statusPanel.getComponents())
105     {
106       assertTrue(c instanceof JPanel);
107       assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
108       assertEquals(msgs[i++],
109               ((JLabel) ((JPanel) c).getComponent(0)).getText());
110     }
111   }
112 }