JAL-3253-applet JAL-3423 TestNG/Win
[jalview.git] / test / jalview / gui / AlignmentPanelTest.java
index b77dd98..fd67ac4 100644 (file)
 package jalview.gui;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotEquals;
 
-import jalview.datamodel.Alignment;
+import jalview.bin.Cache;
+import jalview.bin.Jalview;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
+import jalview.io.FileLoader;
+import jalview.viewmodel.ViewportRanges;
 
-import java.awt.Dimension;
+import java.lang.reflect.InvocationTargetException;
 
-import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 public class AlignmentPanelTest
@@ -125,13 +130,39 @@ public class AlignmentPanelTest
           "Seq23",
           "ABCABCABCABCABCABCABCABCBACBACBACBACABCABCABCABCABCABCABCABCBACBACBACBACABCABCABCABCABCABCABCABCBACBACBACBAC");
 
-  @BeforeClass(alwaysRun = true)
-  public void setUp()
+  AlignFrame af;
+
+  @BeforeMethod(alwaysRun = true)
+  public void setUp() throws InvocationTargetException, InterruptedException
   {
-    JvOptionPane.setInteractiveMode(false);
-    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+    Jalview.main(new String[] { "-nonews", "-props",
+        "test/jalview/testProps.jvprops" });
+
+    Cache.setPropertyNoSave("SHOW_IDENTITY",
+            Boolean.TRUE.toString());
+    af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
+            DataSourceType.FILE);
+    
+    af.paintImmediately(af.getBounds());
+
+    /*
+     * wait for Consensus thread to complete
+     */
+    synchronized (this)
+    {
+      while (af.getViewport().getConsensusSeq() == null)
+      {
+        try
+        {
+          wait(250); // BH was 50
+        } catch (InterruptedException e)
+        {
+        }
+      }
+    }
   }
 
+
   /**
    * Test side effect that end residue is set correctly by setScrollValues, with
    * or without hidden columns
@@ -139,32 +170,89 @@ public class AlignmentPanelTest
   @Test(groups = "Functional")
   public void TestSetScrollValues()
   {
-    SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4, seq5,
-        seq6, seq7, seq8, seq9, seq10, seq11, seq12, seq13, seq14, seq15,
-        seq16, seq17, seq18, seq19, seq20, seq21, seq22, seq23 };
-    Alignment al = new Alignment(seqs);
-    al.setDataset(null);
-
-    AlignFrame alignFrame = new AlignFrame(al, 500, 700);
+    ViewportRanges ranges = af.getViewport().getRanges();
+    af.alignPanel.setScrollValues(0, 0);
 
-    alignFrame.alignPanel.getSeqPanel().seqCanvas
-            .setPreferredSize(new Dimension(300, 300));
-
-    int oldres = alignFrame.getViewport().getEndRes();
-    alignFrame.alignPanel.setScrollValues(-1, 5);
+    int oldres = ranges.getEndRes();
+    af.alignPanel.setScrollValues(-1, 5);
 
     // setting -ve x value does not change residue
-    assertEquals(alignFrame.getViewport().getEndRes(), oldres);
+    assertEquals(ranges.getEndRes(), oldres);
 
-    alignFrame.alignPanel.setScrollValues(0, 5);
+    af.alignPanel.setScrollValues(0, 5);
 
     // setting 0 as x value does not change residue
-    assertEquals(alignFrame.getViewport().getEndRes(), oldres);
-
-    alignFrame.alignPanel.setScrollValues(5, 5);
-    // setting x value to 20 extends endRes by 20px converted to residues
-    assertEquals(alignFrame.getViewport().getEndRes(), oldres + 5);
+    assertEquals(ranges.getEndRes(), oldres);
+
+    af.alignPanel.setScrollValues(5, 5);
+    // setting x value to 5 extends endRes by 5 residues
+    assertEquals(ranges.getEndRes(), oldres + 5);
+
+    // scroll to position after hidden columns sets endres to oldres (width) +
+    // position
+    int scrollpos = 60;
+    af.getViewport().hideColumns(30, 50);
+    af.alignPanel.setScrollValues(scrollpos, 5);
+
+    af.paintImmediately(af.getBounds());
+    assertEquals(ranges.getEndRes(), oldres + scrollpos);
+
+    // scroll to position within hidden columns, still sets endres to oldres +
+    // position
+    // not sure if this is actually correct behaviour but this is what Jalview
+    // currently does
+    scrollpos = 40;
+    af.getViewport().showAllHiddenColumns();
+    af.getViewport().hideColumns(30, 50);
+    af.alignPanel.setScrollValues(scrollpos, 5);
+    assertEquals(ranges.getEndRes(), oldres + scrollpos);
+
+    // scroll to position within <width> distance of the end of the alignment
+    // endRes should be set to width of alignment - 1
+    scrollpos = 130;
+    af.getViewport().showAllHiddenColumns();
+    af.alignPanel.setScrollValues(scrollpos, 5);
+    af.paintImmediately(af.getBounds());
+    assertEquals(ranges.getEndRes(), af.getViewport()
+            .getAlignment().getWidth() - 1);
+
+    // now hide some columns, and scroll to position within <width>
+    // distance of the end of the alignment
+    // endRes should be set to width of alignment - 1 - the number of hidden
+    // columns
+    af.getViewport().hideColumns(30, 50);
+    af.paintImmediately(af.getBounds());
+    af.alignPanel.setScrollValues(scrollpos, 5);
+    assertEquals(ranges.getEndRes(), af.getViewport()
+            .getAlignment().getWidth() - 1 - 21); // 21 is the number of hidden
+                                                  // columns
+  }
 
+  /**
+   * Test that update layout reverts to original (unwrapped) values for endRes
+   * when switching from wrapped back to unwrapped mode (JAL-2739)
+   */
+  @Test(groups = "Functional")
+  public void TestUpdateLayout_endRes()
+  {
+    // get details of original alignment dimensions
+    ViewportRanges ranges = af.getViewport().getRanges();
+    int endres = ranges.getEndRes();
+
+    // wrap
+    af.alignPanel.getAlignViewport().setWrapAlignment(true);
+    af.alignPanel.updateLayout();
+    af.paintImmediately(af.getBounds());
+
+    // endRes has changed
+    assertNotEquals(ranges.getEndRes(), endres);
+
+    // unwrap
+    af.alignPanel.getAlignViewport().setWrapAlignment(false);
+    af.alignPanel.updateLayout();
+    af.paintImmediately(af.getBounds());
+    // endRes back to original value
+    assertEquals(ranges.getEndRes(), endres);
 
   }
 }