JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / gui / ProgressBarTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.Test;
35
36 public class ProgressBarTest
37 {
38
39   private JPanel statusPanel;
40
41   private JLabel statusBar;
42
43   @Test(groups = { "Functional" })
44   public void testConstructor_prematureInstantiation()
45   {
46     try
47     {
48       new ProgressBar(null, null);
49       Assert.fail("Expected exception");
50     } catch (NullPointerException e)
51     {
52       // expected
53     }
54   }
55
56   @Test(groups = { "Functional" })
57   public void testConstructor_wrongLayout()
58   {
59     statusPanel = new JPanel();
60     statusPanel.setLayout(new FlowLayout());
61     try
62     {
63       new ProgressBar(statusPanel, null);
64       Assert.fail("expected exception");
65     } catch (IllegalArgumentException e)
66     {
67       // expected
68     }
69   }
70
71   @Test(groups = { "Functional" })
72   public void testSetProgressBar()
73   {
74     statusPanel = new JPanel();
75     GridLayout layout = new GridLayout(1, 1);
76     statusPanel.setLayout(layout);
77     statusBar = new JLabel("nothing");
78     ProgressBar pb = new ProgressBar(statusPanel, statusBar);
79
80     /*
81      * Add 'hello'
82      */
83     pb.setProgressBar("hello", 1L);
84     verifyProgress(layout, new String[] { "hello" });
85
86     /*
87      * Add 'world'
88      */
89     pb.setProgressBar("world", 2L);
90     verifyProgress(layout, new String[] { "hello", "world" });
91
92     /*
93      * Remove 'hello' with no status bar update
94      */
95     pb.setProgressBar(null, 1L);
96     verifyProgress(layout, new String[] { "world" });
97     assertEquals("nothing", statusBar.getText());
98
99     /*
100      * Remove 'world' with status bar update
101      */
102     pb.setProgressBar("goodbye", 2L);
103     verifyProgress(layout, new String[] {});
104     assertEquals("goodbye", statusBar.getText());
105   }
106
107   /**
108    * Verify the right number of progress bars containing the expected messages
109    * respectively
110    * 
111    * @param layout
112    * @param msgs
113    */
114   private void verifyProgress(GridLayout layout, String[] msgs)
115   {
116     int msgCount = msgs.length;
117     assertEquals(1 + msgCount, layout.getRows());
118     assertEquals(msgCount, statusPanel.getComponentCount());
119     int i = 0;
120     for (Component c : statusPanel.getComponents())
121     {
122       assertTrue(c instanceof JPanel);
123       assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
124       assertEquals(msgs[i++],
125               ((JLabel) ((JPanel) c).getComponent(0)).getText());
126     }
127   }
128 }