JAL-1645 source formatting and organise imports
[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(groups = { "Functional" })
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(groups = { "Functional" })
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(groups = { "Functional" })
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[] { "hello" });
65
66     /*
67      * Add 'world'
68      */
69     pb.setProgressBar("world", 2L);
70     verifyProgress(layout, new String[] { "hello", "world" });
71
72     /*
73      * Remove 'hello' with no status bar update
74      */
75     pb.setProgressBar(null, 1L);
76     verifyProgress(layout, new String[] { "world" });
77     assertEquals("nothing", statusBar.getText());
78
79     /*
80      * Remove 'world' with status bar update
81      */
82     pb.setProgressBar("goodbye", 2L);
83     verifyProgress(layout, new String[] {});
84     assertEquals("goodbye", statusBar.getText());
85   }
86
87   /**
88    * Verify the right number of progress bars containing the expected messages
89    * respectively
90    * 
91    * @param layout
92    * @param msgs
93    */
94   private void verifyProgress(GridLayout layout, String[] msgs)
95   {
96     int msgCount = msgs.length;
97     assertEquals(1 + msgCount, layout.getRows());
98     assertEquals(msgCount, statusPanel.getComponentCount());
99     int i = 0;
100     for (Component c : statusPanel.getComponents())
101     {
102       assertTrue(c instanceof JPanel);
103       assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
104       assertEquals(msgs[i++],
105               ((JLabel) ((JPanel) c).getComponent(0)).getText());
106     }
107   }
108 }