JAL-3438 spotless for 2.11.2.0
[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 import javax.swing.SwingUtilities;
33
34 import org.testng.Assert;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37
38 public class ProgressBarTest
39 {
40
41   @BeforeClass(alwaysRun = true)
42   public void setUpJvOptionPane()
43   {
44     JvOptionPane.setInteractiveMode(false);
45     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46   }
47
48   private JPanel statusPanel;
49
50   private JLabel statusBar;
51
52   @Test(groups = { "Functional" })
53   public void testConstructor_prematureInstantiation()
54   {
55     try
56     {
57       new ProgressBar(null, null);
58       Assert.fail("Expected exception");
59     } catch (NullPointerException e)
60     {
61       // expected
62     }
63   }
64
65   @Test(groups = { "Functional" })
66   public void testConstructor_wrongLayout()
67   {
68     statusPanel = new JPanel();
69     statusPanel.setLayout(new FlowLayout());
70     try
71     {
72       new ProgressBar(statusPanel, null);
73       Assert.fail("expected exception");
74     } catch (IllegalArgumentException e)
75     {
76       // expected
77     }
78   }
79
80   @Test(groups = { "Functional" })
81   public void testSetProgressBar()
82   {
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);
88
89     /*
90      * Add 'hello'
91      */
92     pb.setProgressBar("hello", 1L);
93     verifyProgress(layout, new String[] { "hello" });
94
95     /*
96      * Add 'world'
97      */
98     pb.setProgressBar("world", 2L);
99     verifyProgress(layout, new String[] { "hello", "world" });
100
101     /*
102      * Remove 'hello' with no status bar update
103      */
104     pb.setProgressBar(null, 1L);
105     verifyProgress(layout, new String[] { "world" });
106     assertEquals("nothing", statusBar.getText());
107
108     /*
109      * Remove 'world' with status bar update
110      */
111     pb.setProgressBar("goodbye", 2L);
112     verifyProgress(layout, new String[] {});
113     assertEquals("goodbye", statusBar.getText());
114   }
115
116   /**
117    * Verify the right number of progress bars containing the expected messages
118    * respectively
119    * 
120    * @param layout
121    * @param msgs
122    */
123   private void verifyProgress(final GridLayout layout, final String[] msgs)
124   {
125     try
126     {
127       SwingUtilities.invokeAndWait(new Runnable()
128       {
129         @Override
130         public void run()
131         {
132           int msgCount = msgs.length;
133           assertEquals(1 + msgCount, layout.getRows());
134           assertEquals(msgCount, statusPanel.getComponentCount());
135           int i = 0;
136           for (Component c : statusPanel.getComponents())
137           {
138             assertTrue(c instanceof JPanel);
139             assertTrue(((JPanel) c).getComponent(0) instanceof JLabel);
140             assertEquals(msgs[i++],
141                     ((JLabel) ((JPanel) c).getComponent(0)).getText());
142           }
143         }
144       });
145     } catch (Exception e)
146     {
147       throw new AssertionError(
148               "Unexpected exception waiting for progress bar validation",
149               e);
150     }
151   }
152 }