a1715e96d811dac7910b5ca0ccb2b6c8317f5457
[jalview.git] / test / jalview / gui / ProgressBarTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.awt.Component;
27 import java.awt.FlowLayout;
28 import java.awt.GridLayout;
29
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class ProgressBarTest
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   private JPanel statusPanel;
48
49   private JLabel statusBar;
50
51   @Test(groups = { "Functional" })
52   public void testConstructor_prematureInstantiation()
53   {
54     try
55     {
56       new ProgressBar(null, null);
57       Assert.fail("Expected exception");
58     } catch (NullPointerException e)
59     {
60       // expected
61     }
62   }
63
64   @Test(groups = { "Functional" })
65   public void testConstructor_wrongLayout()
66   {
67     statusPanel = new JPanel();
68     statusPanel.setLayout(new FlowLayout());
69     try
70     {
71       new ProgressBar(statusPanel, null);
72       Assert.fail("expected exception");
73     } catch (IllegalArgumentException e)
74     {
75       // expected
76     }
77   }
78
79   @Test(groups = { "Functional" })
80   public void testSetProgressBar()
81   {
82     statusPanel = new JPanel();
83     GridLayout layout = new GridLayout(1, 1);
84     statusPanel.setLayout(layout);
85     statusBar = new JLabel("nothing");
86     ProgressBar pb = new ProgressBar(statusPanel, statusBar);
87
88     /*
89      * Add 'hello'
90      */
91     pb.setProgressBar("hello", 1L);
92     verifyProgress(layout, new String[] { "hello" });
93
94     /*
95      * Add 'world'
96      */
97     pb.setProgressBar("world", 2L);
98     verifyProgress(layout, new String[] { "hello", "world" });
99
100     /*
101      * Remove 'hello' with no status bar update
102      */
103     pb.setProgressBar(null, 1L);
104     verifyProgress(layout, new String[] { "world" });
105     assertEquals("nothing", statusBar.getText());
106
107     /*
108      * Remove 'world' with status bar update
109      */
110     pb.setProgressBar("goodbye", 2L);
111     verifyProgress(layout, new String[] {});
112     assertEquals("goodbye", statusBar.getText());
113   }
114
115   /**
116    * Verify the right number of progress bars containing the expected messages
117    * respectively
118    * 
119    * @param layout
120    * @param msgs
121    */
122   private void verifyProgress(GridLayout layout, String[] msgs)
123   {
124     int msgCount = msgs.length;
125     assertEquals(1 + msgCount, layout.getRows());
126     assertEquals(msgCount, statusPanel.getComponentCount());
127     int i = 0;
128     for (Component c : statusPanel.getComponents())
129     {
130       assertTrue(c instanceof JPanel);
131       assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
132       assertEquals(msgs[i++],
133               ((JLabel) ((JPanel) c).getComponent(0)).getText());
134     }
135   }
136 }