From 38301c257efe7d20d85ccded982904c67440be95 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 20 Sep 2021 14:56:15 +0100 Subject: [PATCH] JAL-3829 tests need to wait for dialog to be closed and background processes to finish before they can check functionality --- src/jalview/gui/StructureChooser.java | 84 +++++++++++++++++----------- test/jalview/gui/StructureChooserTest.java | 22 ++++++++ 2 files changed, 74 insertions(+), 32 deletions(-) diff --git a/src/jalview/gui/StructureChooser.java b/src/jalview/gui/StructureChooser.java index 2e7019a..82ad03b 100644 --- a/src/jalview/gui/StructureChooser.java +++ b/src/jalview/gui/StructureChooser.java @@ -21,11 +21,24 @@ 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; + } } diff --git a/test/jalview/gui/StructureChooserTest.java b/test/jalview/gui/StructureChooserTest.java index 4685e4c..a87e1c6 100644 --- a/test/jalview/gui/StructureChooserTest.java +++ b/test/jalview/gui/StructureChooserTest.java @@ -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 ss = (Collection) 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 ss = (Collection) 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() { -- 1.7.10.2