Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 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
1.0100%
MockPropChangeListener 957 12 6
1.0100%
 

Contributing tests

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