Merge branch 'develop' into bug/JAL-2255_seq-fetcher-broken-on-linux
[jalview.git] / test / jalview / datamodel / ColumnSelectionTest.java
index ec528c5..4d3f611 100644 (file)
@@ -26,17 +26,27 @@ import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
 import static org.testng.AssertJUnit.fail;
 
+import jalview.gui.JvOptionPane;
+
 import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Collections;
 import java.util.ConcurrentModificationException;
 import java.util.List;
 
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class ColumnSelectionTest
 {
 
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   @Test(groups = { "Functional" })
   public void testAddElement()
   {
@@ -95,9 +105,97 @@ public class ColumnSelectionTest
     cs.hideColumns(4, 4);
     assertEquals(4, cs.findColumnPosition(5));
 
+    // hiding column 4 moves column 4 to position 3
+    assertEquals(3, cs.findColumnPosition(4));
+
     // hiding columns 1 and 2 moves column 5 to column 2
     cs.hideColumns(1, 2);
     assertEquals(2, cs.findColumnPosition(5));
+
+    // check with > 1 hidden column regions
+    // where some columns are in the hidden regions
+    ColumnSelection cs2 = new ColumnSelection();
+    cs2.hideColumns(5, 10);
+    cs2.hideColumns(20, 27);
+    cs2.hideColumns(40, 44);
+
+    // hiding columns 5-10 and 20-27 moves column 8 to column 4
+    assertEquals(4, cs2.findColumnPosition(8));
+
+    // and moves column 24 to 13
+    assertEquals(13, cs2.findColumnPosition(24));
+
+    // and moves column 28 to 14
+    assertEquals(14, cs2.findColumnPosition(28));
+
+    // and moves column 40 to 25
+    assertEquals(25, cs2.findColumnPosition(40));
+
+    // check when hidden columns start at 0 that the visible column
+    // is returned as 0
+    ColumnSelection cs3 = new ColumnSelection();
+    cs3.hideColumns(0, 4);
+    assertEquals(0, cs3.findColumnPosition(2));
+
+  }
+
+  /**
+   * Test the method that finds the visible column position a given distance
+   * before another column
+   */
+  @Test(groups = { "Functional" })
+  public void testFindColumnNToLeft()
+  {
+    ColumnSelection cs = new ColumnSelection();
+
+    // test that without hidden columns, findColumnNToLeft returns
+    // position n to left of provided position
+    int pos = cs.subtractVisibleColumns(3, 10);
+    assertEquals(7, pos);
+
+    // 0 returns same position
+    pos = cs.subtractVisibleColumns(0, 10);
+    assertEquals(10, pos);
+
+    // overflow to left returns negative number
+    pos = cs.subtractVisibleColumns(3, 0);
+    assertEquals(-3, pos);
+
+    // test that with hidden columns to left of result column
+    // behaviour is the same as above
+    cs.hideColumns(1, 3);
+
+    // position n to left of provided position
+    pos = cs.subtractVisibleColumns(3, 10);
+    assertEquals(7, pos);
+
+    // 0 returns same position
+    pos = cs.subtractVisibleColumns(0, 10);
+    assertEquals(10, pos);
+
+    // test with one set of hidden columns between start and required position
+    cs.hideColumns(12, 15);
+    pos = cs.subtractVisibleColumns(8, 17);
+    assertEquals(5, pos);
+
+    // test with two sets of hidden columns between start and required position
+    cs.hideColumns(20, 21);
+    pos = cs.subtractVisibleColumns(8, 23);
+    assertEquals(9, pos);
+
+    // repeat last 2 tests with no hidden columns to left of required position
+    cs.revealAllHiddenColumns();
+
+    // test with one set of hidden columns between start and required position
+    cs.hideColumns(12, 15);
+    pos = cs.subtractVisibleColumns(8, 17);
+    assertEquals(5, pos);
+
+    // test with two sets of hidden columns between start and required position
+    cs.hideColumns(20, 21);
+    pos = cs.subtractVisibleColumns(8, 23);
+    assertEquals(9, pos);
+
   }
 
   /**
@@ -195,12 +293,11 @@ public class ColumnSelectionTest
     assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
 
     // hiding column 4 expands [3, 3] to [3, 4]
-    // not fancy enough to coalesce this into [3, 5] though
+    // and merges to [5, 5] to make [3, 5]
     cs.hideColumns(4);
     hidden = cs.getHiddenColumns();
-    assertEquals(2, hidden.size());
-    assertEquals("[3, 4]", Arrays.toString(hidden.get(0)));
-    assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
+    assertEquals(1, hidden.size());
+    assertEquals("[3, 5]", Arrays.toString(hidden.get(0)));
 
     // clear hidden columns (note they are added to selected)
     cs.revealAllHiddenColumns();
@@ -751,4 +848,122 @@ public class ColumnSelectionTest
     assertEquals("[5, 7]", Arrays.toString(cs2.getHiddenColumns().get(0)));
     assertEquals("[10, 11]", Arrays.toString(cs2.getHiddenColumns().get(1)));
   }
+
+  /**
+   * Test for the case when a hidden range encloses more one already hidden
+   * range
+   */
+  @Test(groups = { "Functional" })
+  public void testHideColumns_subsumingHidden()
+  {
+    /*
+     * JAL-2370 bug scenario:
+     * two hidden ranges subsumed by a third
+     */
+    ColumnSelection cs = new ColumnSelection();
+    cs.hideColumns(49, 59);
+    cs.hideColumns(69, 79);
+    List<int[]> hidden = cs.getHiddenColumns();
+    assertEquals(2, hidden.size());
+    assertEquals("[49, 59]", Arrays.toString(hidden.get(0)));
+    assertEquals("[69, 79]", Arrays.toString(hidden.get(1)));
+  
+    cs.hideColumns(48, 80);
+    hidden = cs.getHiddenColumns();
+    assertEquals(1, hidden.size());
+    assertEquals("[48, 80]", Arrays.toString(hidden.get(0)));
+
+    /*
+     * another...joining hidden ranges
+     */
+    cs = new ColumnSelection();
+    cs.hideColumns(10, 20);
+    cs.hideColumns(30, 40);
+    cs.hideColumns(50, 60);
+    // hiding 21-49 should merge to one range
+    cs.hideColumns(21, 49);
+    hidden = cs.getHiddenColumns();
+    assertEquals(1, hidden.size());
+    assertEquals("[10, 60]", Arrays.toString(hidden.get(0)));
+
+    /*
+     * another...lef overlap, subsumption, right overlap,
+     * no overlap of existing hidden ranges
+     */
+    cs = new ColumnSelection();
+    cs.hideColumns(10, 20);
+    cs.hideColumns(10, 20);
+    cs.hideColumns(30, 35);
+    cs.hideColumns(40, 50);
+    cs.hideColumns(60, 70);
+
+    cs.hideColumns(15, 45);
+    hidden = cs.getHiddenColumns();
+    assertEquals(2, hidden.size());
+    assertEquals("[10, 50]", Arrays.toString(hidden.get(0)));
+    assertEquals("[60, 70]", Arrays.toString(hidden.get(1)));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testStretchGroup_expand()
+  {
+    /*
+     * test that emulates clicking column 4 (selected)
+     * and dragging right to column 5 (all base 0)
+     */
+    ColumnSelection cs = new ColumnSelection();
+    cs.addElement(4);
+    SequenceGroup sg = new SequenceGroup();
+    sg.setStartRes(4);
+    sg.setEndRes(4);
+    cs.stretchGroup(5, sg, 4, 4);
+    assertEquals(cs.getSelected().size(), 2);
+    assertTrue(cs.contains(4));
+    assertTrue(cs.contains(5));
+    assertEquals(sg.getStartRes(), 4);
+    assertEquals(sg.getEndRes(), 5);
+
+    /*
+     * emulate drag right with columns 10-20 already selected
+     */
+    cs.clear();
+    for (int i = 10; i <= 20; i++)
+    {
+      cs.addElement(i);
+    }
+    assertEquals(cs.getSelected().size(), 11);
+    sg = new SequenceGroup();
+    sg.setStartRes(10);
+    sg.setEndRes(20);
+    cs.stretchGroup(21, sg, 10, 20);
+    assertEquals(cs.getSelected().size(), 12);
+    assertTrue(cs.contains(10));
+    assertTrue(cs.contains(21));
+    assertEquals(sg.getStartRes(), 10);
+    assertEquals(sg.getEndRes(), 21);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testStretchGroup_shrink()
+  {
+    /*
+     * emulate drag left to 19 with columns 10-20 already selected
+     */
+    ColumnSelection cs = new ColumnSelection();
+    for (int i = 10; i <= 20; i++)
+    {
+      cs.addElement(i);
+    }
+    assertEquals(cs.getSelected().size(), 11);
+    SequenceGroup sg = new SequenceGroup();
+    sg.setStartRes(10);
+    sg.setEndRes(20);
+    cs.stretchGroup(19, sg, 10, 20);
+    assertEquals(cs.getSelected().size(), 10);
+    assertTrue(cs.contains(10));
+    assertTrue(cs.contains(19));
+    assertFalse(cs.contains(20));
+    assertEquals(sg.getStartRes(), 10);
+    assertEquals(sg.getEndRes(), 19);
+  }
 }