JAL-2740 removed unwanted code, added unit test
[jalview.git] / test / jalview / gui / AnnotationColumnChooserTest.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
25 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.HiddenColumns;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.DataSourceType;
33 import jalview.io.FileFormat;
34 import jalview.io.FormatAdapter;
35
36 import java.io.IOException;
37 import java.util.List;
38
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
42
43 /**
44  * Unit tests for AnnotationChooser
45  * 
46  * @author kmourao
47  *
48  */
49 public class AnnotationColumnChooserTest
50 {
51   @BeforeClass(alwaysRun = true)
52   public void setUpJvOptionPane()
53   {
54     JvOptionPane.setInteractiveMode(false);
55     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56   }
57
58   // 4 sequences x 13 positions
59   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
60           + "TIETHKEAELVG-\n"
61           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
62           + "TIETHKEAELVG-\n"
63           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
64           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
65           + "TIETHKEEELTA-\n";
66
67   AnnotationChooser testee;
68
69   AlignmentPanel parentPanel;
70
71   AlignFrame af;
72
73   @BeforeMethod(alwaysRun = true)
74   public void setUp() throws IOException
75   {
76     Cache.loadProperties("test/jalview/io/testProps.jvprops");
77     // pin down annotation sort order for test
78     Cache.applicationProperties.setProperty(Preferences.SORT_ANNOTATIONS,
79             SequenceAnnotationOrder.NONE.name());
80     final String TRUE = Boolean.TRUE.toString();
81     Cache.applicationProperties.setProperty(Preferences.SHOW_AUTOCALC_ABOVE,
82             TRUE);
83     Cache.applicationProperties.setProperty("SHOW_QUALITY", TRUE);
84     Cache.applicationProperties.setProperty("SHOW_CONSERVATION", TRUE);
85     Cache.applicationProperties.setProperty("SHOW_IDENTITY", TRUE);
86
87     AlignmentI al = new FormatAdapter().readFile(TEST_DATA,
88             DataSourceType.PASTE, FileFormat.Fasta);
89     af = new AlignFrame(al, 700, 500);
90     parentPanel = new AlignmentPanel(af, af.getViewport());
91     addAnnotations();
92   }
93
94   /**
95    * Add 4 annotations, 3 of them sequence-specific.
96    * 
97    * <PRE>
98    * ann1 - for sequence 0 - label 'IUPRED' ann2 - not sequence related - label
99    * 'Beauty' ann3 - for sequence 3 - label 'JMol' ann4 - for sequence 2 - label
100    * 'IUPRED' ann5 - for sequence 1 - label 'JMol'
101    */
102   private void addAnnotations()
103   {
104     Annotation an = new Annotation(2f);
105     Annotation[] anns = new Annotation[] { an, an, an };
106     AlignmentAnnotation ann0 = new AlignmentAnnotation("IUPRED", "", anns);
107     AlignmentAnnotation ann1 = new AlignmentAnnotation("Beauty", "", anns);
108     AlignmentAnnotation ann2 = new AlignmentAnnotation("JMol", "", anns);
109     AlignmentAnnotation ann3 = new AlignmentAnnotation("IUPRED", "", anns);
110     AlignmentAnnotation ann4 = new AlignmentAnnotation("JMol", "", anns);
111     SequenceI[] seqs = parentPanel.getAlignment().getSequencesArray();
112     ann0.setSequenceRef(seqs[0]);
113     ann2.setSequenceRef(seqs[3]);
114     ann3.setSequenceRef(seqs[2]);
115     ann4.setSequenceRef(seqs[1]);
116     parentPanel.getAlignment().addAnnotation(ann0);
117     parentPanel.getAlignment().addAnnotation(ann1);
118     parentPanel.getAlignment().addAnnotation(ann2);
119     parentPanel.getAlignment().addAnnotation(ann3);
120     parentPanel.getAlignment().addAnnotation(ann4);
121   }
122
123   /**
124    * Test reset
125    */
126   @Test(groups = { "Functional" })
127   public void testReset()
128   {
129     AnnotationColumnChooser acc = new AnnotationColumnChooser(
130             af.getViewport(), af.alignPanel);
131
132     HiddenColumns oldhidden = new HiddenColumns();
133     oldhidden.hideColumns(10, 20);
134     acc.setOldHiddenColumns(oldhidden);
135
136     HiddenColumns newHidden = new HiddenColumns();
137     newHidden.hideColumns(0, 3);
138     newHidden.hideColumns(22, 25);
139     af.getViewport().setHiddenColumns(newHidden);
140
141     HiddenColumns currentHidden = af.getViewport().getAlignment()
142             .getHiddenColumns();
143     List<int[]> regions = currentHidden.getHiddenColumnsCopy();
144     assertEquals(regions.get(0)[0], 0);
145     assertEquals(regions.get(0)[1], 3);
146     assertEquals(regions.get(1)[0], 22);
147     assertEquals(regions.get(1)[1], 25);
148
149     // now reset hidden columns
150     acc.reset();
151     currentHidden = af.getViewport().getAlignment().getHiddenColumns();
152     regions = currentHidden.getHiddenColumnsCopy();
153     assertEquals(regions.get(0)[0], 10);
154     assertEquals(regions.get(0)[1], 20);
155   }
156 }