JAL-3829 tests need to wait for dialog to be closed and background processes to finis...
authorJim Procter <j.procter@dundee.ac.uk>
Mon, 20 Sep 2021 13:56:15 +0000 (14:56 +0100)
committerJim Procter <j.procter@dundee.ac.uk>
Mon, 20 Sep 2021 13:56:15 +0000 (14:56 +0100)
src/jalview/gui/StructureChooser.java
test/jalview/gui/StructureChooserTest.java

index 2e7019a..82ad03b 100644 (file)
 
 package jalview.gui;
 
+import java.awt.event.ItemEvent;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.concurrent.Executors;
+
+import javax.swing.JCheckBox;
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
+import javax.swing.JTable;
+import javax.swing.SwingUtilities;
+import javax.swing.table.AbstractTableModel;
+
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.bin.Jalview;
-import jalview.datamodel.DBRefEntry;
-import jalview.datamodel.DBRefSource;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.fts.api.FTSData;
@@ -45,28 +58,9 @@ import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
 import jalview.ws.DBRefFetcher;
+import jalview.ws.seqfetcher.DbSourceProxy;
 import jalview.ws.sifts.SiftsSettings;
 
-import java.awt.event.ItemEvent;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Objects;
-import java.util.Set;
-import java.util.Vector;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Executor;
-import java.util.concurrent.Executors;
-
-import javax.swing.JCheckBox;
-import javax.swing.JComboBox;
-import javax.swing.JLabel;
-import javax.swing.JTable;
-import javax.swing.SwingUtilities;
-import javax.swing.table.AbstractTableModel;
-
 /**
  * Provides the behaviors for the Structure chooser Panel
  * 
@@ -133,15 +127,18 @@ public class StructureChooser extends GStructureChooser
 
     chk_superpose.setSelected(Cache.getDefault(AUTOSUPERIMPOSE, true));
 
-    // ensure a filter option is in force for search
-    populateFilterComboBox(true, cachedPDBExists);
-    // todo change to futures I guess
-
     final Runnable discoverPDBStructures = new Runnable()
     {
       @Override
       public void run()
       {
+        // check which FTS engine to use
+        data = StructureChooserQuerySource
+                .getQuerySourceFor(selectedSequences);
+
+        // ensure a filter option is in force for search
+        populateFilterComboBox(true, cachedPDBExists);
+
         // looks for any existing structures already loaded
         // for the sequences (the cached ones)
         // then queries the StructureChooserQuerySource to
@@ -176,17 +173,23 @@ public class StructureChooser extends GStructureChooser
         long progressId = System.currentTimeMillis();
 
         int y = seqsWithoutSourceDBRef.size();
+        
         setProgressBar(MessageManager.formatMessage(
                 "status.fetching_dbrefs_for_sequences_without_valid_refs",
                 y), progressId);
         SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
                 .toArray(new SequenceI[y]);
-        DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef);
+        
+        DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef,
+                progressBar, new DbSourceProxy[]
+                { new jalview.ws.dbsources.Uniprot() },
+                null, false);
+        
         dbRefFetcher.fetchDBRefs(true);
 
         setProgressBar("Fetch complete.", progressId); // todo i18n
 
-        SwingUtilities.invokeLater(discoverPDBStructures);
+        Executors.defaultThreadFactory().newThread(discoverPDBStructures).start();
       }
     };
 
@@ -203,17 +206,23 @@ public class StructureChooser extends GStructureChooser
                     .checkUniprotRefs(seq.getDBRefs());
             if (dbRef < 0)
             {
-              seqsWithoutSourceDBRef.add(seq);
+              if (seq.getAllPDBEntries()==null && seq.getAllPDBEntries().size()==0)
+              {
+                seqsWithoutSourceDBRef.add(seq);
+              }
             }
           }
         }
         // retrieve database refs for protein sequences
         if (!seqsWithoutSourceDBRef.isEmpty())
         {
+          // need cancel and no to result in the discoverPDB action - mocked is 'cancel'
           JvOptionPane.newOptionDialog(Desktop.getDesktop())
-                  .setResponseHandler(0, discoverCanonicalDBrefs)
-                  .setResponseHandler(1, discoverPDBStructures)
-                  .showDialog(MessageManager.formatMessage("label.fetch_references_for",seqsWithoutSourceDBRef.size()), MessageManager.getString(
+                  .setResponseHandler(JvOptionPane.OK_OPTION, discoverCanonicalDBrefs)
+                  .setResponseHandler(JvOptionPane.CANCEL_OPTION, discoverPDBStructures)
+                  .setResponseHandler(JvOptionPane.NO_OPTION, discoverPDBStructures)
+                  .showDialog(MessageManager.formatMessage("label.fetch_references_for",
+                          seqsWithoutSourceDBRef.size()), MessageManager.getString(
                           "label.fetch_uniprot_references"),
                           JvOptionPane.YES_NO_OPTION,
                           JvOptionPane.PLAIN_MESSAGE, null, new Object[]
@@ -1318,4 +1327,15 @@ public class StructureChooser extends GStructureChooser
     data.setDocFieldPrefs(newPrefs);
     
   }
+
+  /**
+   * 
+   * @return true when all initialisation threads have finished and dialog is visible
+   */
+  public boolean isDialogVisible()
+  {
+    return mainFrame != null && data != null && cmb_filterOption != null
+            && mainFrame.isVisible()
+            && cmb_filterOption.getSelectedItem() != null;
+  }
 }
index 4685e4c..a87e1c6 100644 (file)
@@ -131,6 +131,7 @@ public class StructureChooserTest
 
     SequenceI[] selectedSeqs = new SequenceI[] { seq };
     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
+    ThreadwaitFor(200, sc);
     
     // if structures are not discovered then don't
     // populate filter options
@@ -169,6 +170,9 @@ public class StructureChooserTest
     PDBFTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
     SequenceI[] selectedSeqs = new SequenceI[] { seq };
     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
+    // not mocked, wait for 2s 
+    ThreadwaitFor(2000, sc);
+    
     sc.fetchStructuresMetaData();
     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
             "discoveredStructuresSet");
@@ -183,6 +187,8 @@ public class StructureChooserTest
     PDBFTSRestClientTest.setMock();
     SequenceI[] selectedSeqs = new SequenceI[] { upSeq };
     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
+    ThreadwaitFor(500, sc);
+    
     sc.fetchStructuresMetaData();
     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
             "discoveredStructuresSet");
@@ -190,6 +196,22 @@ public class StructureChooserTest
     assertTrue(ss.size() > 0);
   }
 
+  private void ThreadwaitFor(int i, StructureChooser sc)
+  {
+    long timeout = i+System.currentTimeMillis();
+    while (!sc.isDialogVisible() && timeout > System.currentTimeMillis())
+    {
+      try {
+        Thread.sleep(50);
+      } catch (InterruptedException x)
+      {
+        
+      }
+    }
+    
+  }
+
+
   @Test(groups = { "Functional" })
   public void sanitizeSeqNameTest()
   {