JAL-1369 additional asserts in testStoreAndRecoverReferenceSeqSettings
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 27 Jun 2016 13:02:15 +0000 (14:02 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 27 Jun 2016 13:02:15 +0000 (14:02 +0100)
test/jalview/io/Jalview2xmlTests.java

index 00d4ee0..a77e10a 100644 (file)
  */
 package jalview.io;
 
+import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertTrue;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.ViewStyleI;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenSequences;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
@@ -34,9 +38,12 @@ import jalview.gui.Desktop;
 import jalview.gui.Jalview2XML;
 import jalview.schemes.AnnotationColourGradient;
 import jalview.schemes.ColourSchemeI;
+import jalview.viewmodel.AlignmentViewport;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.testng.Assert;
@@ -66,10 +73,9 @@ public class Jalview2xmlTests
   public static void tearDownAfterClass() throws Exception
   {
     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
-
   }
 
-  public int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
+  int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
   {
     int numdsann = 0;
     for (SequenceI sq : avp.getAlignment().getDataset().getSequences())
@@ -361,7 +367,7 @@ public class Jalview2xmlTests
    * 
    * @throws Exception
    */
-  @Test(groups = { "Functional" }, enabled = false)
+  @Test(groups = { "Functional" }, enabled = true)
   public void testStoreAndRecoverExpandedviews() throws Exception
   {
     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
@@ -417,7 +423,8 @@ public class Jalview2xmlTests
   }
 
   /**
-   * based on above test store and recovery of expanded views.
+   * Test save and reload of a project with a different representative sequence
+   * in each view.
    * 
    * @throws Exception
    */
@@ -425,36 +432,69 @@ public class Jalview2xmlTests
   public void testStoreAndRecoverReferenceSeqSettings() throws Exception
   {
     Desktop.instance.closeAll_actionPerformed(null);
-    String afid = "";
-    {
-      AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
-              "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
-      assertTrue("Didn't read in the example file correctly.", af != null);
-      afid = af.getViewport().getSequenceSetId();
-    }
-    Map<String, SequenceI> refs = new HashMap<String, SequenceI>();
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
+            "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
+    assertTrue("Didn't read in the example file correctly.", af != null);
+    String afid = af.getViewport().getSequenceSetId();
+
+    /*
+     * remember representative and hidden sequences marked 
+     * on each panel
+     */
+    Map<String, SequenceI> refseqs = new HashMap<String, SequenceI>();
+    Map<String, List<String>> hiddenSeqNames = new HashMap<String, List<String>>();
+
+    /*
+     * mark sequence 2, 3, 4.. in panels 1, 2, 3...
+     * as reference sequence for itself and the preceding sequence
+     */
     int n = 1;
     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
     {
-      // mark representative
-      SequenceI rep = ap.getAlignment().getSequenceAt(
-              n++ % ap.getAlignment().getHeight());
-      refs.put(ap.getViewName(), rep);
+      AlignViewportI av = ap.getAlignViewport();
+      AlignmentI alignment = ap.getAlignment();
+      int repIndex = n % alignment.getHeight();
+      SequenceI rep = alignment.getSequenceAt(repIndex);
+      refseqs.put(ap.getViewName(), rep);
+      List<String> hiddenNames = new ArrayList<String>();
+      hiddenSeqNames.put(ap.getViewName(), hiddenNames);
+
+      /*
+       * hide rep sequence and the one before it
+       */
+      SequenceI seqToHide = alignment.getSequenceAt(repIndex);
+      SequenceGroup sg = new SequenceGroup();
+      sg.addSequence(seqToHide, false);
+      SequenceI precedingSeq = alignment.getSequenceAt(repIndex - 1);
+      sg.addSequence(precedingSeq, false);
+      av.setSelectionGroup(sg);
+      ((AlignmentViewport) av).hideSequences(seqToHide, true);
+
+      /*
+       * record names of hidden sequences
+       */
+      HiddenSequences hs = alignment.getHiddenSequences();
+      for (SequenceI seq : hs.hiddenSequences)
+      {
+        if (seq != null)
+        {
+          hiddenNames.add(seq.getName());
+        }
+      }
       // code from mark/unmark sequence as reference in jalview.gui.PopupMenu
-      // todo refactor the to an alignment view controller
-      ap.getAlignViewport().setDisplayReferenceSeq(true);
-      ap.getAlignViewport().setColourByReferenceSeq(true);
-      ap.getAlignViewport().getAlignment().setSeqrep(rep);
+      // todo refactor this to an alignment view controller
+      av.setDisplayReferenceSeq(true);
+      av.setColourByReferenceSeq(true);
+      av.getAlignment().setSeqrep(rep);
+
+      n++;
     }
     File tfile = File.createTempFile("testStoreAndRecoverReferenceSeq",
             ".jvp");
     try
     {
       new Jalview2XML(false).saveState(tfile);
-    } catch (Error e)
-    {
-      Assert.fail("Didn't save the expanded view state", e);
-    } catch (Exception e)
+    } catch (Throwable e)
     {
       Assert.fail("Didn't save the expanded view state", e);
     }
@@ -463,28 +503,39 @@ public class Jalview2xmlTests
     {
       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
     }
-    {
-      AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+
+    af = new FileLoader().LoadFileWaitTillLoaded(
             tfile.getAbsolutePath(), FormatAdapter.FILE);
-      afid = af.getViewport().getSequenceSetId();
-    }
+    afid = af.getViewport().getSequenceSetId();
+
     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
     {
       // check representative
-      SequenceI rep = ap.getAlignment().getSeqrep();
+      AlignmentI alignment = ap.getAlignment();
+      SequenceI rep = alignment.getSeqrep();
       Assert.assertNotNull(rep,
               "Couldn't restore sequence representative from project");
       // can't use a strong equals here, because by definition, the sequence IDs
       // will be different.
       // could set vamsas session save/restore flag to preserve IDs across
       // load/saves.
-      Assert.assertEquals(refs.get(ap.getViewName()).toString(),
+      Assert.assertEquals(refseqs.get(ap.getViewName()).toString(),
               rep.toString(),
               "Representative wasn't the same when recovered.");
       Assert.assertTrue(ap.getAlignViewport().isDisplayReferenceSeq(),
               "Display reference sequence view setting not set.");
       Assert.assertTrue(ap.getAlignViewport().isColourByReferenceSeq(),
               "Colour By Reference Seq view setting not set.");
+
+      /*
+       * verify hidden sequences in restored panel
+       */
+      List<String> hidden = hiddenSeqNames.get(ap.getViewName());
+      HiddenSequences hs = alignment.getHiddenSequences();
+      assertEquals(
+              "wrong number of restored hidden sequences in "
+                      + ap.getViewName(),
+              hidden.size(), hs.getSize());
     }
   }