Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package jalview.gui

File ProgressBarTest.java

 

Code metrics

0
36
6
1
152
95
9
0.25
6
6
1.5

Classes

Class Line # Actions
ProgressBarTest 38 36 9
0.928571492.9%
 

Contributing tests

This file is covered by 3 tests. .

Source view

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