Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.viewmodel

File ViewportRangesTest.java

 

Code metrics

10
517
35
2
1,004
707
41
0.08
14.77
17.5
1.17

Classes

Class Line # Actions
ViewportRangesTest 43 505 35
0.00%
MockPropChangeListener 957 12 6
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.viewmodel;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertFalse;
25    import static org.testng.Assert.assertTrue;
26   
27    import jalview.analysis.AlignmentGenerator;
28    import jalview.datamodel.AlignmentI;
29    import jalview.datamodel.ColumnSelection;
30    import jalview.datamodel.HiddenColumns;
31    import jalview.datamodel.HiddenSequences;
32   
33    import java.beans.PropertyChangeEvent;
34    import java.util.ArrayList;
35    import java.util.Arrays;
36    import java.util.Collections;
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    public class ViewportRangesTest
44    {
45   
46    AlignmentGenerator gen = new AlignmentGenerator(false);
47   
48    AlignmentI al = gen.generate(20, 30, 1, 5, 5);
49   
50    AlignmentI smallAl = gen.generate(7, 2, 2, 5, 5);
51   
 
52  0 toggle @BeforeClass(alwaysRun = true)
53    public void setUp()
54    {
55  0 gen = new AlignmentGenerator(false);
56  0 al = gen.generate(20, 30, 1, 5, 5);
57  0 smallAl = gen.generate(7, 2, 2, 5, 5);
58    }
59   
 
60  0 toggle @BeforeMethod(alwaysRun = true)
61    public void cleanUp()
62    {
63  0 ColumnSelection sel = new ColumnSelection();
64  0 al.getHiddenColumns().revealAllHiddenColumns(sel);
65  0 al.getHiddenSequences().showAll(null);
66  0 smallAl.getHiddenColumns().revealAllHiddenColumns(sel);
67  0 smallAl.getHiddenSequences().showAll(null);
68    }
69   
 
70  0 toggle @Test(groups = { "Functional" })
71    public void testViewportRanges()
72    {
73  0 ViewportRanges vr = new ViewportRanges(al);
74   
75  0 assertEquals(vr.getStartRes(), 0);
76  0 assertEquals(vr.getEndRes(), al.getWidth() - 1);
77  0 assertEquals(vr.getStartSeq(), 0);
78  0 assertEquals(vr.getEndSeq(), al.getHeight() - 1);
79    }
80   
 
81  0 toggle @Test(groups = { "Functional" })
82    public void testGetAbsoluteAlignmentHeight()
83    {
84  0 ViewportRanges vr = new ViewportRanges(al);
85   
86  0 assertEquals(vr.getAbsoluteAlignmentHeight(), al.getHeight());
87   
88  0 al.getHiddenSequences().hideSequence(al.getSequenceAt(3));
89  0 assertEquals(vr.getAbsoluteAlignmentHeight(), al.getHeight() + 1);
90    }
91   
 
92  0 toggle @Test(groups = { "Functional" })
93    public void testGetAbsoluteAlignmentWidth()
94    {
95  0 ViewportRanges vr = new ViewportRanges(al);
96  0 assertEquals(vr.getAbsoluteAlignmentWidth(), al.getWidth());
97    }
98   
 
99  0 toggle @Test(groups = { "Functional" })
100    public void testSetEndSeq()
101    {
102  0 ViewportRanges vr = new ViewportRanges(al);
103  0 vr.setEndSeq(-1);
104  0 assertEquals(vr.getEndSeq(), 0);
105   
106  0 vr.setEndSeq(al.getHeight());
107  0 assertEquals(vr.getEndSeq(), al.getHeight() - 1);
108   
109  0 vr.setEndSeq(al.getHeight() - 1);
110  0 assertEquals(vr.getEndSeq(), al.getHeight() - 1);
111    }
112   
 
113  0 toggle @Test(groups = { "Functional" })
114    public void testSetStartRes()
115    {
116  0 ViewportRanges vr = new ViewportRanges(al);
117  0 vr.setStartRes(-1);
118  0 assertEquals(vr.getStartRes(), 0);
119   
120  0 vr.setStartRes(al.getWidth());
121  0 assertEquals(vr.getStartRes(), al.getWidth() - 1);
122   
123  0 vr.setStartRes(al.getWidth() - 1);
124  0 assertEquals(vr.getStartRes(), al.getWidth() - 1);
125    }
126   
 
127  0 toggle @Test(groups = { "Functional" })
128    public void testSetStartSeq()
129    {
130  0 ViewportRanges vr = new ViewportRanges(al);
131  0 vr.setStartSeq(-1);
132  0 assertEquals(vr.getStartSeq(), 0);
133   
134  0 vr.setStartSeq(al.getHeight() - vr.getViewportHeight() + 1);
135  0 assertEquals(vr.getStartSeq(), al.getHeight() - vr.getViewportHeight());
136   
137  0 vr.setStartSeq(al.getHeight() - vr.getViewportHeight());
138  0 assertEquals(vr.getStartSeq(), al.getHeight() - vr.getViewportHeight());
139    }
140   
 
141  0 toggle @Test(groups = { "Functional" })
142    public void testSetStartEndRes()
143    {
144  0 ViewportRanges vr = new ViewportRanges(al);
145  0 vr.setStartEndRes(-1, -1);
146  0 assertEquals(vr.getStartRes(), 0);
147  0 assertEquals(vr.getEndRes(), 0);
148   
149  0 vr.setStartEndRes(5, 19);
150  0 assertEquals(vr.getStartRes(), 5);
151  0 assertEquals(vr.getEndRes(), 19);
152   
153  0 vr.setStartEndRes(al.getWidth(), al.getWidth());
154  0 assertEquals(vr.getEndRes(), al.getWidth() - 1);
155   
156  0 ViewportRanges vrsmall = new ViewportRanges(smallAl);
157  0 vrsmall.setStartEndRes(al.getWidth(), al.getWidth());
158  0 assertEquals(vrsmall.getEndRes(), 6);
159   
160    // make visible alignment width = 0
161  0 smallAl.getHiddenColumns().hideColumns(0, 6);
162  0 vrsmall.setStartEndRes(0, 4);
163  0 assertEquals(vrsmall.getStartRes(), 0);
164  0 assertEquals(vrsmall.getEndRes(), 0);
165    }
166   
 
167  0 toggle @Test(groups = { "Functional" })
168    public void testSetStartEndSeq()
169    {
170  0 ViewportRanges vr = new ViewportRanges(al);
171  0 vr.setStartEndSeq(-1, -1);
172  0 assertEquals(vr.getStartSeq(), 0);
173  0 assertEquals(vr.getEndSeq(), 0);
174   
175  0 vr.setStartEndSeq(5, 19);
176  0 assertEquals(vr.getStartSeq(), 5);
177  0 assertEquals(vr.getEndSeq(), 19);
178   
179  0 vr.setStartEndSeq(al.getHeight(), al.getHeight());
180  0 assertEquals(vr.getEndSeq(), al.getHeight() - 1);
181   
182    // make visible alignment height = 0
183  0 smallAl.getHiddenSequences().hideSequence(smallAl.getSequenceAt(0));
184  0 smallAl.getHiddenSequences().hideSequence(smallAl.getSequenceAt(0));
185  0 ViewportRanges vrsmall = new ViewportRanges(smallAl);
186  0 vrsmall.setStartEndSeq(0, 3);
187  0 assertEquals(vrsmall.getStartSeq(), 0);
188  0 assertEquals(vrsmall.getEndSeq(), 0);
189    }
190   
 
191  0 toggle @Test(groups = { "Functional" })
192    public void testSetStartResAndSeq()
193    {
194  0 ViewportRanges vr = new ViewportRanges(al);
195  0 vr.setViewportHeight(10);
196  0 vr.setStartResAndSeq(3, 6);
197  0 assertEquals(vr.getStartRes(), 3);
198  0 assertEquals(vr.getStartSeq(), 6);
199  0 assertEquals(vr.getEndRes(), 3 + vr.getViewportWidth() - 1);
200  0 assertEquals(vr.getEndSeq(), 6 + vr.getViewportHeight() - 1);
201   
202  0 vr.setStartResAndSeq(10, 25);
203  0 assertEquals(vr.getStartRes(), 10);
204  0 assertEquals(vr.getStartSeq(), 19);
205  0 assertEquals(vr.getEndRes(), 10 + vr.getViewportWidth() - 1);
206  0 assertEquals(vr.getEndSeq(), 19 + vr.getViewportHeight() - 1);
207    }
208   
 
209  0 toggle @Test(groups = { "Functional" })
210    public void testSetViewportHeight()
211    {
212  0 ViewportRanges vr = new ViewportRanges(al);
213  0 vr.setViewportHeight(13);
214  0 assertEquals(vr.getViewportHeight(), 13);
215    }
216   
 
217  0 toggle @Test(groups = { "Functional" })
218    public void testSetViewportWidth()
219    {
220  0 ViewportRanges vr = new ViewportRanges(al);
221  0 vr.setViewportWidth(13);
222  0 assertEquals(vr.getViewportWidth(), 13);
223    }
224   
 
225  0 toggle @Test(groups = { "Functional" })
226    public void testSetViewportStartAndHeight()
227    {
228  0 ViewportRanges vr = new ViewportRanges(al);
229  0 vr.setViewportStartAndHeight(2, 6);
230  0 assertEquals(vr.getViewportHeight(), 6);
231  0 assertEquals(vr.getStartSeq(), 2);
232   
233    // reset -ve values of start to 0
234  0 vr.setViewportStartAndHeight(-1, 7);
235  0 assertEquals(vr.getViewportHeight(), 7);
236  0 assertEquals(vr.getStartSeq(), 0);
237   
238    // reset out of bounds start values to within bounds
239  0 vr.setViewportStartAndHeight(35, 5);
240  0 assertEquals(vr.getViewportHeight(), 5);
241  0 assertEquals(vr.getStartSeq(), 24);
242    }
243   
 
244  0 toggle @Test(groups = { "Functional" })
245    public void testSetViewportStartAndWidth()
246    {
247  0 ViewportRanges vr = new ViewportRanges(al);
248  0 vr.setViewportStartAndWidth(2, 6);
249  0 assertEquals(vr.getViewportWidth(), 6);
250  0 assertEquals(vr.getStartRes(), 2);
251   
252    // reset -ve values of start to 0
253  0 vr.setViewportStartAndWidth(-1, 7);
254  0 assertEquals(vr.getViewportWidth(), 7);
255  0 assertEquals(vr.getStartRes(), 0);
256   
257    // reset out of bounds start values to within bounds
258  0 vr.setViewportStartAndWidth(35, 5);
259  0 assertEquals(vr.getViewportWidth(), 5);
260  0 assertEquals(vr.getStartRes(), 16);
261   
262    // small alignment doesn't get bounds reset
263  0 ViewportRanges vrsmall = new ViewportRanges(smallAl);
264  0 vrsmall.setViewportStartAndWidth(0, 63);
265  0 assertEquals(vrsmall.getViewportWidth(), 7);
266  0 assertEquals(vrsmall.getStartRes(), 0);
267    }
268   
 
269  0 toggle @Test(groups = { "Functional" })
270    public void testPageUpDown()
271    {
272  0 ViewportRanges vr = new ViewportRanges(al);
273  0 vr.setViewportStartAndHeight(8, 6);
274  0 vr.pageDown();
275  0 assertEquals(vr.getStartSeq(), 13);
276   
277  0 vr.pageUp();
278  0 assertEquals(vr.getStartSeq(), 8);
279   
280  0 vr.pageUp();
281  0 assertEquals(vr.getStartSeq(), 3);
282   
283  0 vr.pageUp();
284    // pageup does not go beyond 0, viewport height stays the same
285  0 assertEquals(vr.getStartSeq(), 0);
286  0 assertEquals(vr.getViewportHeight(), 6);
287   
288  0 vr.pageDown();
289  0 vr.pageDown();
290  0 vr.pageDown();
291  0 vr.pageDown();
292  0 vr.pageDown();
293   
294    // pagedown to bottom does not go beyond end, and height stays same
295  0 assertEquals(vr.getStartSeq(), 24);
296  0 assertEquals(vr.getViewportHeight(), 6);
297    }
298   
 
299  0 toggle @Test(groups = { "Functional" })
300    public void testScrollUp()
301    {
302  0 ViewportRanges vr = new ViewportRanges(al);
303  0 vr.setViewportStartAndHeight(1, 5);
304  0 vr.scrollUp(true);
305  0 assertEquals(vr.getStartSeq(), 0);
306    // can't scroll above top
307  0 vr.scrollUp(true);
308  0 assertEquals(vr.getStartSeq(), 0);
309   
310  0 vr.setViewportStartAndHeight(24, 5);
311  0 vr.scrollUp(false);
312  0 assertEquals(vr.getStartSeq(), 25);
313    // can't scroll beyond bottom
314  0 vr.scrollUp(false);
315  0 assertEquals(vr.getStartSeq(), 25);
316    }
317   
 
318  0 toggle @Test(groups = { "Functional" })
319    public void testScrollUpWithHidden()
320    {
321  0 ViewportRanges vr = new ViewportRanges(al);
322   
323    // hide last sequence
324  0 HiddenSequences hidden = new HiddenSequences(al);
325  0 hidden.hideSequence(al.getSequenceAt(29));
326   
327  0 vr.setViewportStartAndHeight(1, 5);
328  0 vr.scrollUp(true);
329  0 assertEquals(vr.getStartSeq(), 0);
330    // can't scroll above top
331  0 vr.scrollUp(true);
332  0 assertEquals(vr.getStartSeq(), 0);
333   
334  0 vr.setViewportStartAndHeight(23, 5);
335  0 vr.scrollUp(false);
336  0 assertEquals(vr.getStartSeq(), 24);
337    // can't scroll beyond bottom
338  0 vr.scrollUp(false);
339  0 assertEquals(vr.getStartSeq(), 24);
340    }
341   
 
342  0 toggle @Test(groups = { "Functional" })
343    public void testScrollRight()
344    {
345  0 ViewportRanges vr = new ViewportRanges(al);
346  0 vr.setViewportStartAndWidth(1, 5);
347  0 vr.scrollRight(false);
348  0 assertEquals(vr.getStartRes(), 0);
349    // can't scroll left past start
350  0 vr.scrollRight(false);
351  0 assertEquals(vr.getStartRes(), 0);
352   
353  0 vr.setViewportStartAndWidth(15, 5);
354  0 vr.scrollRight(true);
355  0 assertEquals(vr.getStartRes(), 16);
356    // can't scroll right past end
357  0 vr.scrollRight(true);
358  0 assertEquals(vr.getStartRes(), 16);
359    }
360   
 
361  0 toggle @Test(groups = { "Functional" })
362    public void testScrollRightWithHidden()
363    {
364  0 ViewportRanges vr = new ViewportRanges(al);
365   
366    // hide last 2 columns
367  0 HiddenColumns cols = new HiddenColumns();
368  0 cols.hideColumns(19, 20);
369  0 al.setHiddenColumns(cols);
370   
371  0 vr.setViewportStartAndWidth(1, 5);
372  0 vr.scrollRight(false);
373  0 assertEquals(vr.getStartRes(), 0);
374    // can't scroll left past start
375  0 vr.scrollRight(false);
376  0 assertEquals(vr.getStartRes(), 0);
377   
378  0 vr.setViewportStartAndWidth(13, 5);
379  0 vr.scrollRight(true);
380  0 assertEquals(vr.getStartRes(), 14);
381    // can't scroll right past last visible col
382  0 vr.scrollRight(true);
383  0 assertEquals(vr.getStartRes(), 14);
384    }
385   
 
386  0 toggle @Test(groups = { "Functional" })
387    public void testScrollToWrappedVisible()
388    {
389  0 AlignmentI al2 = gen.generate(60, 30, 1, 5, 5);
390   
391  0 ViewportRanges vr = new ViewportRanges(al2);
392   
393    // start with viewport on 5-14
394  0 vr.setViewportStartAndWidth(5, 10);
395  0 assertEquals(vr.getStartRes(), 5);
396  0 assertEquals(vr.getEndRes(), 14);
397   
398    // scroll to 12 - no change
399  0 assertFalse(vr.scrollToWrappedVisible(12));
400  0 assertEquals(vr.getStartRes(), 5);
401   
402    // scroll to 2 - back to 0-9
403  0 assertTrue(vr.scrollToWrappedVisible(2));
404  0 assertEquals(vr.getStartRes(), 0);
405  0 assertEquals(vr.getEndRes(), 9);
406   
407    // scroll to 9 - no change
408  0 assertFalse(vr.scrollToWrappedVisible(9));
409  0 assertEquals(vr.getStartRes(), 0);
410   
411    // scroll to 12 - moves to 10-19
412  0 assertTrue(vr.scrollToWrappedVisible(12));
413  0 assertEquals(vr.getStartRes(), 10);
414  0 assertEquals(vr.getEndRes(), 19);
415   
416  0 vr.setStartRes(13);
417  0 assertEquals(vr.getStartRes(), 13);
418  0 assertEquals(vr.getEndRes(), 22);
419   
420    // scroll to 45 - jumps to 43-52
421  0 assertTrue(vr.scrollToWrappedVisible(45));
422  0 assertEquals(vr.getStartRes(), 43);
423  0 assertEquals(vr.getEndRes(), 52);
424    }
425   
 
426  0 toggle @Test(groups = { "Functional" })
427    public void testScrollToVisible()
428    {
429  0 ViewportRanges vr = new ViewportRanges(al);
430  0 vr.setViewportStartAndWidth(12, 5);
431  0 vr.setViewportStartAndHeight(10, 6);
432  0 vr.scrollToVisible(13, 14);
433   
434    // no change
435  0 assertEquals(vr.getStartRes(), 12);
436  0 assertEquals(vr.getStartSeq(), 10);
437   
438  0 vr.scrollToVisible(5, 6);
439  0 assertEquals(vr.getStartRes(), 5);
440  0 assertEquals(vr.getStartSeq(), 6);
441   
442    // test for hidden columns too
443  0 al.getHiddenColumns().hideColumns(1, 3);
444  0 vr.scrollToVisible(13, 3);
445  0 assertEquals(vr.getStartRes(), 6);
446  0 assertEquals(vr.getStartSeq(), 3);
447   
448  0 vr.scrollToVisible(2, 9);
449  0 assertEquals(vr.getStartRes(), 0);
450  0 assertEquals(vr.getStartSeq(), 4);
451    }
452   
 
453  0 toggle @Test(groups = { "Functional" })
454    public void testEventFiring()
455    {
456  0 ViewportRanges vr = new ViewportRanges(al);
457  0 MockPropChangeListener l = new MockPropChangeListener(vr);
458  0 List<String> emptylist = new ArrayList<>();
459   
460  0 vr.setViewportWidth(5);
461  0 vr.setViewportHeight(5);
462  0 l.reset();
463   
464    // one event fired when startRes is called with new value
465  0 vr.setStartRes(4);
466  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES)));
467  0 l.reset();
468   
469    // no event fired for same value
470  0 vr.setStartRes(4);
471  0 assertTrue(l.verify(0, emptylist));
472  0 l.reset();
473   
474  0 vr.setStartSeq(4);
475  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
476  0 l.reset();
477   
478  0 vr.setStartSeq(4);
479  0 assertTrue(l.verify(0, emptylist));
480  0 l.reset();
481   
482  0 vr.setEndSeq(10);
483  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
484  0 l.reset();
485   
486  0 vr.setEndSeq(10);
487  0 assertTrue(l.verify(0, emptylist));
488  0 l.reset();
489   
490  0 vr.setStartEndRes(2, 15);
491  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES)));
492  0 l.reset();
493   
494  0 vr.setStartEndRes(2, 15);
495  0 assertTrue(l.verify(0, emptylist));
496  0 l.reset();
497   
498    // check new value fired by event is corrected startres
499  0 vr.setStartEndRes(-1, 5);
500  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES),
501    Arrays.asList(0)));
502  0 l.reset();
503   
504    // check new value fired by event is corrected endres
505  0 vr.setStartEndRes(0, -1);
506  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.ENDRES),
507    Arrays.asList(0)));
508  0 l.reset();
509   
510  0 vr.setStartEndSeq(2, 15);
511  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
512  0 l.reset();
513   
514  0 vr.setStartEndSeq(2, 15);
515  0 assertTrue(l.verify(0, emptylist));
516  0 l.reset();
517   
518  0 vr.setStartEndRes(2, 2); // so seq and res values should be different, in
519    // case of transposing in code
520  0 l.reset();
521   
522    // check new value fired by event is corrected startseq
523  0 vr.setStartEndSeq(-1, 5);
524  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ),
525    Arrays.asList(0)));
526  0 l.reset();
527   
528    // check new value fired by event is corrected endseq
529  0 vr.setStartEndSeq(0, -1);
530  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.ENDSEQ),
531    Arrays.asList(0)));
532  0 l.reset();
533   
534    // reset for later tests
535  0 vr.setStartEndSeq(2, 15);
536  0 l.reset();
537   
538    // test viewport height and width setting triggers event
539  0 vr.setViewportHeight(10);
540  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.ENDSEQ)));
541  0 l.reset();
542   
543  0 vr.setViewportWidth(18);
544  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.ENDRES)));
545  0 l.reset();
546   
547    // already has seq start set to 2, so triggers endseq
548  0 vr.setViewportStartAndHeight(2, 16);
549  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.ENDSEQ)));
550  0 l.reset();
551   
552  0 vr.setViewportStartAndWidth(1, 14);
553  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES)));
554  0 l.reset();
555   
556    // test page up/down triggers event
557  0 vr.pageUp();
558  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
559  0 l.reset();
560   
561  0 vr.pageDown();
562  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
563  0 l.reset();
564   
565    // test scrolling triggers event
566  0 vr.scrollUp(true);
567  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
568  0 l.reset();
569   
570  0 vr.scrollUp(false);
571  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTSEQ)));
572  0 l.reset();
573   
574  0 vr.scrollRight(true);
575  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES)));
576  0 l.reset();
577   
578  0 vr.scrollRight(false);
579  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES)));
580  0 l.reset();
581   
582  0 vr.scrollToVisible(10, 10);
583  0 assertTrue(l.verify(4,
584    Arrays.asList(ViewportRanges.STARTSEQ, ViewportRanges.STARTSEQ,
585    ViewportRanges.STARTSEQ, ViewportRanges.STARTSEQ)));
586  0 l.reset();
587   
588    /*
589    * scrollToWrappedVisible does nothing if the target position is
590    * within the current startRes-endRes range
591    */
592  0 assertFalse(vr.scrollToWrappedVisible(5));
593  0 assertTrue(l.verify(0, Collections.<String> emptyList()));
594  0 l.reset();
595   
596  0 vr.scrollToWrappedVisible(25);
597  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRES)));
598  0 l.reset();
599   
600    // test setStartResAndSeq triggers one event
601  0 vr.setStartResAndSeq(5, 7);
602  0 assertTrue(l.verify(1, Arrays.asList(ViewportRanges.STARTRESANDSEQ),
603    Arrays.asList(5, 7)));
604   
605  0 l.reset();
606    }
607   
 
608  0 toggle @Test(groups = { "Functional" })
609    public void testGetWrappedScrollPosition()
610    {
611  0 AlignmentI al2 = gen.generate(157, 15, 1, 5, 5);
612  0 ViewportRanges vr = new ViewportRanges(al2);
613  0 vr.setStartEndRes(0, 39);
614  0 int width = vr.getViewportWidth(); // 40
615   
616    /*
617    * scroll is 0 at column 0 (only)
618    */
619  0 assertEquals(vr.getWrappedScrollPosition(0), 0);
620   
621    /*
622    * scroll is 1 at columns 1-40
623    */
624  0 int i = 1;
625  0 int j = width;
626  0 for (; i <= j; i++)
627    {
628  0 assertEquals(1, vr.getWrappedScrollPosition(i));
629    }
630   
631    /*
632    * scroll is 2 at columns 41-80, etc
633    */
634  0 j += width;
635  0 for (; i <= j; i++)
636    {
637  0 assertEquals(2, vr.getWrappedScrollPosition(i), "For " + i);
638    }
639    }
640   
 
641  0 toggle @Test(groups = { "Functional" })
642    public void testPageUpDownWrapped()
643    {
644    /*
645    * 15 sequences, 110 residues wide (+gaps)
646    */
647  0 AlignmentI al2 = gen.generate(110, 15, 1, 5, 5);
648   
649  0 ViewportRanges vr = new ViewportRanges(al2);
650  0 vr.setWrappedMode(true);
651   
652    // first row
653  0 vr.setViewportStartAndWidth(0, 40);
654  0 int width = vr.getViewportWidth();
655  0 assertEquals(width, 40);
656  0 assertEquals(vr.getStartRes(), 0);
657  0 assertEquals(vr.getEndRes(), 39);
658  0 assertEquals(vr.getStartSeq(), 0);
659  0 assertEquals(vr.getEndSeq(), 14);
660   
661    // second row
662  0 vr.pageDown();
663  0 assertEquals(vr.getStartRes(), 40);
664  0 assertEquals(vr.getEndRes(), 79);
665  0 assertEquals(vr.getStartSeq(), 0);
666  0 assertEquals(vr.getEndSeq(), 14);
667   
668    // third and last row
669    // note endRes is nominal (>width) to preserve viewport width
670  0 vr.pageDown();
671  0 assertEquals(vr.getStartRes(), 80);
672  0 assertEquals(vr.getEndRes(), 119);
673  0 assertEquals(vr.getStartSeq(), 0);
674  0 assertEquals(vr.getEndSeq(), 14);
675   
676    // another pageDown should do nothing
677  0 vr.pageDown();
678  0 assertEquals(vr.getStartRes(), 80);
679  0 assertEquals(vr.getEndRes(), 119);
680  0 assertEquals(vr.getStartSeq(), 0);
681  0 assertEquals(vr.getEndSeq(), 14);
682   
683    // back to second row
684  0 vr.pageUp();
685  0 assertEquals(vr.getStartRes(), 40);
686  0 assertEquals(vr.getEndRes(), 79);
687  0 assertEquals(vr.getStartSeq(), 0);
688  0 assertEquals(vr.getEndSeq(), 14);
689   
690    // back to first row
691  0 vr.pageUp();
692  0 assertEquals(vr.getStartRes(), 0);
693  0 assertEquals(vr.getEndRes(), 39);
694  0 assertEquals(vr.getStartSeq(), 0);
695  0 assertEquals(vr.getEndSeq(), 14);
696   
697    // another pageUp should do nothing
698  0 vr.pageUp();
699  0 assertEquals(vr.getStartRes(), 0);
700  0 assertEquals(vr.getEndRes(), 39);
701  0 assertEquals(vr.getStartSeq(), 0);
702  0 assertEquals(vr.getEndSeq(), 14);
703   
704    /*
705    * simulate scroll right a few positions
706    */
707  0 vr.setStartRes(5);
708  0 assertEquals(vr.getStartRes(), 5);
709  0 assertEquals(vr.getEndRes(), 5 + width - 1); // 44
710   
711  0 vr.pageDown(); // 5-44 shifts to 45-84
712  0 assertEquals(vr.getStartRes(), 45);
713  0 assertEquals(vr.getEndRes(), 84);
714   
715  0 vr.pageDown(); // 45-84 shifts to 85-124
716  0 assertEquals(vr.getStartRes(), 85);
717  0 assertEquals(vr.getEndRes(), 124);
718   
719  0 vr.pageDown(); // no change - at end already
720  0 assertEquals(vr.getStartRes(), 85);
721  0 assertEquals(vr.getEndRes(), 124);
722   
723  0 vr.pageUp(); // back we go
724  0 assertEquals(vr.getStartRes(), 45);
725  0 assertEquals(vr.getEndRes(), 84);
726   
727  0 vr.pageUp();
728  0 assertEquals(vr.getStartRes(), 5);
729  0 assertEquals(vr.getEndRes(), 44);
730   
731  0 vr.pageUp(); // back to the start
732  0 assertEquals(vr.getStartRes(), 0);
733  0 assertEquals(vr.getEndRes(), 39);
734    }
735   
 
736  0 toggle @Test(groups = { "Functional" })
737    public void testSetStartEndResWrapped()
738    {
739  0 ViewportRanges vr = new ViewportRanges(al);
740  0 vr.setWrappedMode(true);
741  0 vr.setStartEndRes(-1, -1);
742  0 assertEquals(vr.getStartRes(), 0);
743  0 assertEquals(vr.getEndRes(), 0);
744   
745  0 vr.setStartEndRes(5, 19);
746  0 assertEquals(vr.getStartRes(), 5);
747  0 assertEquals(vr.getEndRes(), 19);
748   
749    // bounds are not constrained to alignment width
750    // when in wrapped mode
751  0 vr.setStartEndRes(88, 888);
752  0 assertEquals(vr.getStartRes(), 88);
753  0 assertEquals(vr.getEndRes(), 888);
754   
755  0 ViewportRanges vrsmall = new ViewportRanges(smallAl);
756  0 vrsmall.setWrappedMode(true);
757  0 vrsmall.setStartEndRes(88, 888);
758  0 assertEquals(vrsmall.getStartRes(), 88);
759  0 assertEquals(vrsmall.getEndRes(), 888);
760   
761    // make visible alignment width = 0
762  0 smallAl.getHiddenColumns().hideColumns(0, 6);
763  0 vrsmall.setStartEndRes(0, 4);
764  0 assertEquals(vrsmall.getStartRes(), 0);
765  0 assertEquals(vrsmall.getEndRes(), 4);
766    }
767   
 
768  0 toggle @Test(groups = { "Functional" })
769    public void testSetViewportStartAndWidthWrapped()
770    {
771  0 ViewportRanges vr = new ViewportRanges(al);
772  0 vr.setWrappedMode(true);
773  0 vr.setViewportStartAndWidth(2, 6);
774  0 assertEquals(vr.getViewportWidth(), 6);
775  0 assertEquals(vr.getStartRes(), 2);
776   
777    // reset -ve values of start to 0
778  0 vr.setViewportStartAndWidth(-1, 7);
779  0 assertEquals(vr.getViewportWidth(), 7);
780  0 assertEquals(vr.getStartRes(), 0);
781   
782    // out of bounds values are not forced to within bounds
783  0 vr.setViewportStartAndWidth(35, 5);
784  0 assertEquals(vr.getViewportWidth(), 5);
785  0 assertEquals(vr.getStartRes(), 35);
786   
787    // small alignment doesn't get bounds reset
788  0 ViewportRanges vrsmall = new ViewportRanges(smallAl);
789  0 vrsmall.setViewportStartAndWidth(0, 63);
790  0 assertEquals(vrsmall.getViewportWidth(), 7);
791  0 assertEquals(vrsmall.getStartRes(), 0);
792    }
793   
 
794  0 toggle @Test(groups = { "Functional" })
795    public void testGetWrappedMaxScroll()
796    {
797    // generate an ungapped alignment of width 140
798  0 int alignmentWidth = 140;
799  0 AlignmentI al2 = gen.generate(alignmentWidth, 15, 1, 0, 5);
800  0 ViewportRanges vr = new ViewportRanges(al2);
801  0 vr.setStartEndRes(0, 39);
802  0 int width = vr.getViewportWidth(); // 40
803  0 int partWidth = alignmentWidth % width; // 20
804   
805    /*
806    * there are 3 * 40 remainder 20 residues
807    * number of widths depends on offset (scroll right)
808    * 4 widths (maxScroll = 3) if offset by 0 or more than 19 columns
809    * 5 widths (maxScroll = 4) if 1 <= offset <= 19
810    */
811  0 for (int col = 0; col < alignmentWidth; col++)
812    {
813  0 int offset = col % width;
814  0 if (offset > 0 && offset < partWidth)
815    {
816  0 assertEquals(vr.getWrappedMaxScroll(col), 4, "col " + col);
817    }
818    else
819    {
820  0 assertEquals(vr.getWrappedMaxScroll(col), 3, "col " + col);
821    }
822    }
823    }
824   
 
825  0 toggle @Test(groups = { "Functional" })
826    public void testScrollUp_wrapped()
827    {
828    /*
829    * alignment 30 tall and 45 wide
830    */
831  0 AlignmentI al2 = gen.generate(45, 30, 1, 0, 5);
832   
833    /*
834    * wrapped view, 5 sequences high, start at sequence offset 1
835    */
836  0 ViewportRanges vr = new ViewportRanges(al2);
837  0 vr.setWrappedMode(true);
838  0 vr.setViewportStartAndHeight(1, 5);
839   
840    /*
841    * offset wrapped view to column 3
842    */
843  0 vr.setStartEndRes(3, 22);
844   
845  0 int startRes = vr.getStartRes();
846  0 int width = vr.getViewportWidth();
847  0 assertEquals(startRes, 3);
848  0 assertEquals(width, 20);
849   
850    // in wrapped mode, we change startRes but not startSeq
851    // scroll down:
852  0 vr.scrollUp(false);
853  0 assertEquals(vr.getStartSeq(), 1);
854  0 assertEquals(vr.getStartRes(), 23);
855   
856    // scroll up returns to original position
857  0 vr.scrollUp(true);
858  0 assertEquals(vr.getStartSeq(), 1);
859  0 assertEquals(vr.getStartRes(), 3);
860   
861    // scroll up again returns to 'origin'
862  0 vr.scrollUp(true);
863  0 assertEquals(vr.getStartSeq(), 1);
864  0 assertEquals(vr.getStartRes(), 0);
865   
866    /*
867    * offset 3 columns once more and do some scroll downs
868    */
869  0 vr.setStartEndRes(3, 22);
870  0 vr.scrollUp(false);
871  0 assertEquals(vr.getStartSeq(), 1);
872  0 assertEquals(vr.getStartRes(), 23);
873  0 vr.scrollUp(false);
874  0 assertEquals(vr.getStartSeq(), 1);
875  0 assertEquals(vr.getStartRes(), 43);
876   
877    /*
878    * scroll down beyond end of alignment does nothing
879    */
880  0 vr.scrollUp(false);
881  0 assertEquals(vr.getStartSeq(), 1);
882  0 assertEquals(vr.getStartRes(), 43);
883    }
884   
 
885  0 toggle @Test(groups = { "Functional" })
886    public void testSetViewportLocation()
887    {
888  0 AlignmentI al2 = gen.generate(60, 80, 1, 0, 0);
889   
890  0 ViewportRanges vr = new ViewportRanges(al2);
891   
892    // start with viewport on 5-14
893  0 vr.setViewportStartAndWidth(5, 10);
894  0 assertEquals(vr.getStartRes(), 5);
895  0 assertEquals(vr.getEndRes(), 14);
896   
897  0 vr.setViewportStartAndHeight(3, 13);
898  0 assertEquals(vr.getStartSeq(), 3);
899  0 assertEquals(vr.getEndSeq(), 15);
900   
901    // set location to (8,5) - no change
902  0 vr.setViewportLocation(8, 5);
903  0 assertEquals(vr.getStartRes(), 5);
904  0 assertEquals(vr.getEndRes(), 14);
905  0 assertEquals(vr.getStartSeq(), 3);
906  0 assertEquals(vr.getEndSeq(), 15);
907   
908    // set location to (40,50) - change to top left (40,50)
909  0 vr.setViewportLocation(40, 50);
910  0 assertEquals(vr.getStartRes(), 40);
911  0 assertEquals(vr.getEndRes(), 49);
912  0 assertEquals(vr.getStartSeq(), 50);
913  0 assertEquals(vr.getEndSeq(), 62);
914   
915    // set location past end of alignment - resets to leftmost pos
916  0 vr.setViewportLocation(63, 85);
917  0 assertEquals(vr.getStartRes(), 50);
918  0 assertEquals(vr.getEndRes(), 59);
919  0 assertEquals(vr.getStartSeq(), 67);
920  0 assertEquals(vr.getEndSeq(), 79);
921   
922    // hide some columns
923  0 al2.getHiddenColumns().hideColumns(20, 50);
924  0 vr.setViewportLocation(55, 4);
925  0 assertEquals(vr.getStartRes(), 19);
926  0 assertEquals(vr.getEndRes(), 28);
927  0 assertEquals(vr.getStartSeq(), 4);
928  0 assertEquals(vr.getEndSeq(), 16);
929   
930    // hide some sequences
931  0 al2.getHiddenSequences().hideSequence(al2.getSequenceAt(3));
932  0 al2.getHiddenSequences().hideSequence(al2.getSequenceAt(4));
933  0 vr.setViewportLocation(17, 5);
934  0 assertEquals(vr.getStartRes(), 17);
935  0 assertEquals(vr.getEndRes(), 26);
936  0 assertEquals(vr.getStartSeq(), 3);
937  0 assertEquals(vr.getEndSeq(), 15);
938   
939    // set wrapped mode
940  0 vr.setWrappedMode(true);
941  0 vr.setViewportLocation(1, 8);
942  0 assertEquals(vr.getStartRes(), 0);
943  0 assertEquals(vr.getEndRes(), 9);
944  0 assertEquals(vr.getStartSeq(), 3);
945  0 assertEquals(vr.getEndSeq(), 15);
946   
947    // try further down the alignment
948  0 vr.setViewportLocation(57, 5);
949  0 assertEquals(vr.getStartRes(), 20);
950  0 assertEquals(vr.getEndRes(), 29);
951  0 assertEquals(vr.getStartSeq(), 3);
952  0 assertEquals(vr.getEndSeq(), 15);
953    }
954    }
955   
956    // mock listener for property change events
 
957    class MockPropChangeListener implements ViewportListenerI
958    {
959    private int firecount = 0;
960   
961    private List<String> events = new ArrayList<>();
962   
963    private List<Integer> newvalues = new ArrayList<>();
964   
 
965  0 toggle public MockPropChangeListener(ViewportRanges vr)
966    {
967  0 vr.addPropertyChangeListener(this);
968    }
969   
 
970  0 toggle @Override
971    public void propertyChange(PropertyChangeEvent evt)
972    {
973  0 firecount++;
974  0 events.add(evt.getPropertyName());
975  0 if (evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ))
976    {
977  0 newvalues.add(((int[]) evt.getNewValue())[0]);
978  0 newvalues.add(((int[]) evt.getNewValue())[1]);
979    }
980    else
981    {
982  0 newvalues.add((Integer) evt.getNewValue());
983    }
984    }
985   
 
986  0 toggle public boolean verify(int count, List<String> eventslist,
987    List<Integer> valueslist)
988    {
989  0 return (count == firecount) && events.equals(eventslist)
990    && newvalues.equals(valueslist);
991    }
992   
 
993  0 toggle public boolean verify(int count, List<String> eventslist)
994    {
995  0 return (count == firecount) && events.equals(eventslist);
996    }
997   
 
998  0 toggle public void reset()
999    {
1000  0 firecount = 0;
1001  0 events.clear();
1002  0 newvalues.clear();
1003    }
1004    }