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