JAL-3438 spotless for 2.11.2.0
[jalview.git] / test / jalview / gui / PaintRefresherTest.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.assertSame;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceI;
30 import jalview.viewmodel.AlignmentViewport;
31
32 import java.awt.Component;
33 import java.util.List;
34 import java.util.Map;
35
36 import javax.swing.JPanel;
37
38 import org.testng.annotations.AfterMethod;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
42
43 public class PaintRefresherTest
44 {
45
46   @BeforeClass(alwaysRun = true)
47   public void setUpJvOptionPane()
48   {
49     JvOptionPane.setInteractiveMode(false);
50     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51   }
52
53   // TODO would prefer PaintRefresher to be a single rather than static
54   @BeforeMethod(alwaysRun = true)
55   public void setUp()
56   {
57     PaintRefresher.components.clear();
58   }
59
60   @AfterMethod(alwaysRun = true)
61   public void tearDown()
62   {
63     PaintRefresher.components.clear();
64   }
65
66   @Test(groups = { "Functional" })
67   public void testRegister()
68   {
69     JPanel jp = new JPanel();
70     JPanel jp2 = new JPanel();
71     JPanel jp3 = new JPanel();
72     JPanel jp4 = new JPanel();
73     PaintRefresher.Register(jp, "22");
74     PaintRefresher.Register(jp, "22");
75     PaintRefresher.Register(jp2, "22");
76     PaintRefresher.Register(jp3, "33");
77     PaintRefresher.Register(jp3, "44");
78     PaintRefresher.Register(jp4, "44");
79
80     Map<String, List<Component>> registered = PaintRefresher.components;
81     assertEquals(3, registered.size());
82     assertEquals(2, registered.get("22").size());
83     assertEquals(1, registered.get("33").size());
84     assertEquals(2, registered.get("44").size());
85     assertTrue(registered.get("22").contains(jp));
86     assertTrue(registered.get("22").contains(jp2));
87     assertTrue(registered.get("33").contains(jp3));
88     assertTrue(registered.get("44").contains(jp3));
89     assertTrue(registered.get("44").contains(jp4));
90   }
91
92   @Test(groups = { "Functional" })
93   public void testRemoveComponent()
94   {
95     Map<String, List<Component>> registered = PaintRefresher.components;
96
97     // no error with an empty PaintRefresher
98     JPanel jp = new JPanel();
99     JPanel jp2 = new JPanel();
100     PaintRefresher.RemoveComponent(jp);
101     assertTrue(registered.isEmpty());
102
103     /*
104      * Add then remove one item
105      */
106     PaintRefresher.Register(jp, "11");
107     PaintRefresher.RemoveComponent(jp);
108     assertTrue(registered.isEmpty());
109
110     /*
111      * Add one item under two ids, then remove it. It is removed from both ids,
112      * and the now empty id is removed.
113      */
114     PaintRefresher.Register(jp, "11");
115     PaintRefresher.Register(jp, "22");
116     PaintRefresher.Register(jp2, "22");
117     PaintRefresher.RemoveComponent(jp);
118     // "11" is removed as now empty, only 22/jp2 left
119     assertEquals(1, registered.size());
120     assertEquals(1, registered.get("22").size());
121     assertTrue(registered.get("22").contains(jp2));
122   }
123
124   @Test(groups = { "Functional" })
125   public void testGetAssociatedPanels()
126   {
127     SequenceI[] seqs = new SequenceI[] { new Sequence("", "ABC") };
128     Alignment al = new Alignment(seqs);
129
130     /*
131      * AlignFrame constructor has side-effects: AlignmentPanel is constructed,
132      * and SeqCanvas, IdPanel, AlignmentPanel are all registered under the
133      * sequence set id of the viewport.
134      */
135     AlignmentViewport av = new AlignViewport(al);
136     AlignFrame af = new AlignFrame(al, 4, 1);
137     AlignmentPanel ap1 = af.alignPanel;
138     AlignmentPanel[] panels = PaintRefresher
139             .getAssociatedPanels(av.getSequenceSetId());
140     assertEquals(1, panels.length);
141     assertSame(ap1, panels[0]);
142
143     panels = PaintRefresher.getAssociatedPanels(av.getSequenceSetId() + 1);
144     assertEquals(0, panels.length);
145   }
146 }