Ghidra is a software reverse engineering (SRE) framework
GT-0 - Test Fixes - fixed test timing issue with table auto lookup feature
dragonmacher74f3bd842c30
diff --git a/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/GTableTest.java b/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/GTableTest.javaindex 679a3deb10..dbbc66b15a 100644--- a/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/GTableTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/GTableTest.java@@ -19,10 +19,12 @@ import static org.junit.Assert.*;import java.awt.BorderLayout;-import javax.swing.*;+import javax.swing.JFrame;+import javax.swing.JScrollPane;import org.junit.*;+import docking.widgets.AutoLookup;import docking.widgets.table.model.TestDataModel;import ghidra.test.AbstractGhidraHeadedIntegrationTest;import ghidra.util.Msg;@@ -32,13 +34,16 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {private TestDataModel model;private GhidraTable table;private JFrame frame;+private long testKeyTimeout = 100;+ private boolean timeoutTriggered;@Beforepublic void setUp() throws Exception {model = new TestDataModel();table = new GhidraTable(model);- table.setAutoLookupTimeout(testKeyTimeout);++ installTestAutoLookupTimeout();frame = new JFrame("Ghidra Table Test");frame.getContentPane().setLayout(new BorderLayout());@@ -50,6 +55,16 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {waitForSort();}+ private void installTestAutoLookupTimeout() {+ // note: this call will not work due to the unpredictable timing of event+ // processing in the various test environments+ // table.setAutoLookupTimeout(testKeyTimeout);++ AutoLookup al = table.getAutoLookup();+ al.setTimeoutPredicate(elapsed -> timeoutTriggered);+ timeout(); // start fresh with an active timeout+ }+@Afterpublic void tearDown() throws Exception {frame.dispose();@@ -62,31 +77,32 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {setSelectedRow(table, 0);- triggerText(table, "a");+ type("a");assertSelectedRow(11, "a");+ pauseTimeout();- triggerText(table, "c");- assertSelectedRow(12, "c");+ type("c");+ assertSelectedRow(12, "c"); // matches 'access'timeout();- triggerText(table, "ad");+ type("ad");assertSelectedRow(24, "ad");timeout();- triggerText(table, "av");+ type("av");assertSelectedRow(70, "av");timeout();- triggerText(table, "x");+ type("x");assertSelectedRow(1920, "x");timeout();- triggerText(table, "a");+ type("a");assertSelectedRow(11, "a");// test the case where no match is found- table.setAutoLookupTimeout(1000); // longer timeout needed for multiple keys- triggerText(table, "zed");+ pauseTimeout();+ type("zed");assertSelectedRow(11, "zed"); // no change}@@ -100,31 +116,32 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {setSelectedRow(table, 0);- triggerText(table, "a");+ type("a");assertSelectedRow(1846, "a");+ pauseTimeout();- triggerText(table, "c");- assertSelectedRow(1902, "c");+ type("c");+ assertSelectedRow(1902, "c"); // matches 'access'timeout();- triggerText(table, "ad");+ type("ad");assertSelectedRow(1885, "ad");timeout();- triggerText(table, "av");+ type("av");assertSelectedRow(1848, "av");timeout();- triggerText(table, "x");+ type("x");assertSelectedRow(0, "x");timeout();- triggerText(table, "a");+ type("a");assertSelectedRow(1846, "a");// test the case where no match is foundtable.setAutoLookupTimeout(1000); // longer timeout needed for multiple keys- triggerText(table, "zed");+ type("zed");assertSelectedRow(1846, "zed"); // no change}@@ -140,31 +157,32 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {// note: the order checked here is the same as the sorted order, since we did not move// any rows after disabling the sort- triggerText(table, "a");+ type("a");assertSelectedRow(11, "a");+ pauseTimeout();- triggerText(table, "c");- assertSelectedRow(12, "c");+ type("c");+ assertSelectedRow(12, "c"); // matches 'access'timeout();- triggerText(table, "ad");+ type("ad");assertSelectedRow(24, "ad");timeout();- triggerText(table, "av");+ type("av");assertSelectedRow(70, "av");timeout();- triggerText(table, "x");+ type("x");assertSelectedRow(1920, "x");timeout();- triggerText(table, "a");+ type("a");assertSelectedRow(11, "a");// test the case where no match is foundtable.setAutoLookupTimeout(1000); // longer timeout needed for multiple keys- triggerText(table, "zed");+ type("zed");assertSelectedRow(11, "zed"); // no change}@@ -175,35 +193,68 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {assertFalse(table.areActionsEnabled());setSelectedRow(table, 0);- triggerText(table, "a");- assertEquals("Auto-lookup failed to change the table row", 11, table.getSelectedRow());+ type("a");+ assertSelectedRow(11);+ timeout();- // this will disable 'auto lookup'- table.setActionsEnabled(true);- setSelectedRow(table, 0);+ enableKeyBindingsActions(); // this will disable 'auto lookup'- triggerText(table, "a");- assertEquals("Auto-lookup should be disabled when actions are enabled", 0,- table.getSelectedRow());+ select(0);+ type("a");+ assertSelectedRow(0);+ timeout();- table.setActionsEnabled(false);- setSelectedRow(table, 0);+ disableKeyBindingsActions();++ select(0);+ type("a");+ assertSelectedRow(11);+ timeout();++ enableKeyBindingsActions(); // this will disable 'auto lookup'++ select(0);+ type("a");+ assertSelectedRow(0);+ timeout();- triggerText(table, "a");- assertEquals("Auto-lookup failed to change the table row", 11, table.getSelectedRow());+ table.setAutoLookupColumn(4); // this disables the key binding actions too+ select(0);+ type("a");+ assertSelectedRow(11);+ }++ private void disableKeyBindingsActions() {+ table.setActionsEnabled(false);+ }++ private void enableKeyBindingsActions() {table.setActionsEnabled(true);- setSelectedRow(table, 0);+ }- triggerText(table, "a");- assertEquals("Auto-lookup should be disabled when actions are enabled", 0,- table.getSelectedRow());+ private void select(int row) throws Exception {+ setSelectedRow(table, row);+ }- table.setAutoLookupColumn(4);- setSelectedRow(table, 0);+ private void type(String chars) {++ if (chars.length() == 1) {+ triggerText(table, chars);+ waitForSwing();+ return;+ }++ // multiple characters is a signal to type rapidly, meaning the timeout will not pass+ chars.chars().forEach(c -> {+ triggerText(table, Character.toString(c));+ pauseTimeout();+ waitForSwing();+ });+ }- triggerText(table, "a");- assertEquals("Auto-lookup failed to change the table row", 11, table.getSelectedRow());+ private void assertSelectedRow(int row) {+ assertEquals(row, table.getSelectedRow());}private void assertSelectedRow(int row, String lookupText) {@@ -216,7 +267,7 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {String actualString = (String) table.getValueAt(actual, col);String message = "Auto-lookup row not selected for '" + lookupText + "'.\n\t" +"Expected text: '" + expectedString + "'; Actual text: '" + actualString + "'";- Msg.out(message);+ Msg.error(this, message);assertEquals(message, row, actual);}}@@ -241,13 +292,17 @@ public class GTableTest extends AbstractGhidraHeadedIntegrationTest {waitForSort();}+ private void pauseTimeout() {+ runSwing(() -> timeoutTriggered = false);+ }+private void timeout() {- sleep(testKeyTimeout * 2);+ runSwing(() -> timeoutTriggered = true);}private void setSelectedRow(final GhidraTable table, final int i) throws Exception {- SwingUtilities.invokeAndWait(() -> table.setRowSelectionInterval(i, i));- waitForPostedSwingRunnables();+ runSwing(() -> table.setRowSelectionInterval(i, i));+ waitForSwing();}}diff --git a/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java b/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.javaindex 49983ddc2b..31eba0046d 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java@@ -48,7 +48,8 @@ import docking.widgets.MultiLineLabel;import docking.widgets.OptionDialog;import docking.widgets.filechooser.GhidraFileChooser;import docking.widgets.table.threaded.ThreadedTableModel;-import docking.widgets.tree.*;+import docking.widgets.tree.GTree;+import docking.widgets.tree.GTreeNode;import generic.test.AbstractGenericTest;import generic.test.ConcurrentTestExceptionHandler;import generic.util.image.ImageUtils;@@ -56,9 +57,7 @@ import ghidra.GhidraTestApplicationLayout;import ghidra.framework.ApplicationConfiguration;import ghidra.util.*;import ghidra.util.exception.AssertException;-import ghidra.util.exception.CancelledException;import ghidra.util.task.SwingUpdateManager;-import ghidra.util.task.TaskMonitor;import ghidra.util.worker.Worker;import junit.framework.AssertionFailedError;import sun.awt.AppContext;@@ -223,9 +222,9 @@ public abstract class AbstractDockingTest extends AbstractGenericTest {/*** Deprecated- * @param parentWindow- * @param text- * @param timeoutMS+ * @param parentWindow the window; unused+ * @param text the window title text part+ * @param timeoutMS the timeout; unused* @return window* @deprecated Instead call one of the methods that does not take a timeout* (we are standardizing timeouts). The timeouts passed to this method will@@ -1754,7 +1753,6 @@ public abstract class AbstractDockingTest extends AbstractGenericTest {char updatedKeyChar = (keyChar == KeyEvent.CHAR_UNDEFINED) ? (char) keyCode : keyChar;KeyEvent typedKE = new KeyEvent(c, KeyEvent.KEY_TYPED, System.currentTimeMillis(),modifiers, KeyEvent.VK_UNDEFINED, updatedKeyChar);-consumer.accept(c, typedKE);}@@ -1766,7 +1764,6 @@ public abstract class AbstractDockingTest extends AbstractGenericTest {private static void processEvent(Component c, KeyEvent e) {runSwing(() -> {-if (TestKeyEventDispatcher.dispatchKeyEvent(e)) {return; // already handled}@@ -2122,9 +2119,6 @@ public abstract class AbstractDockingTest extends AbstractGenericTest {private static void doWaitForTree(GTree gTree) {- // TODO this wait algorithm shouldn't need this call- // submitTaskToGTreeQueue(gTree);-waitForSwing();boolean didWait = false;int waitTime = 0;@@ -2150,25 +2144,6 @@ public abstract class AbstractDockingTest extends AbstractGenericTest {}}- /**- * Create a task to be put onto the GTree queue. This ensures that once the task is processed- * that any previously schedule tasks will have been run.- */- @SuppressWarnings("unused") // TODO leaving this for a bit, in case we need to put it back- private static void submitTaskToGTreeQueue(GTree gTree) {- GTreeFlagTask flagTask = new GTreeFlagTask(gTree);- gTree.runTask(flagTask);-- while (!flagTask.hasRun()) {- try {- Thread.sleep(DEFAULT_WAIT_DELAY);- }- catch (Exception e) {- // who cares?- }- }- }-public static boolean isEnabled(DockingActionIf action) {AtomicBoolean ref = new AtomicBoolean();runSwing(() -> ref.set(action.isEnabledForContext(new ActionContext())));@@ -2310,26 +2285,4 @@ public abstract class AbstractDockingTest extends AbstractGenericTest {ImageUtils.writeFile(image, imageFile);Msg.info(AbstractDockingTest.class, "Wrote image to " + imageFile.getCanonicalPath());}--//==================================================================================================-// Inner Classes-//==================================================================================================-- private static class GTreeFlagTask extends GTreeTask {-- protected GTreeFlagTask(GTree tree) {- super(tree);- }-- private volatile boolean hasRun;-- @Override- public void run(TaskMonitor monitor) throws CancelledException {- hasRun = true;- }-- boolean hasRun() {- return hasRun;- }- }}diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/AutoLookup.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/AutoLookup.javaindex b7f2b09797..882efbd1a6 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/AutoLookup.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/AutoLookup.java@@ -17,6 +17,7 @@ package docking.widgets;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;+import java.util.function.Predicate;import org.apache.commons.lang3.StringUtils;@@ -28,7 +29,9 @@ public abstract class AutoLookup {public static final long KEY_TYPING_TIMEOUT = 800;private static final int MAX_SEARCH_ROWS = 50000;+private long keyTimeout = KEY_TYPING_TIMEOUT;+ private Predicate<Long> keyTimeoutPredicate = elapsedTime -> elapsedTime > keyTimeout;private AutoLookupItem lastLookup;@@ -110,13 +113,26 @@ public abstract class AutoLookup {lastLookup = null;}+ /**+ * Sets the logic for deciding whether the elapsed time between keystrokes is enough to+ * trigger a new auto lookup or to continue with the previous match.+ *+ * <p>This method is intended for tests that need precise control over the timeout mechanism.+ *+ *+ * @param p the predicate that takes the amount of elapsed time+ * @see #setTimeout(long)+ */+ public void setTimeoutPredicate(Predicate<Long> p) {+ this.keyTimeoutPredicate = p;+ }+/*** Clients call this method when the user types keys** @param e the key event*/public void keyTyped(KeyEvent e) {-if (getRowCount() == 0) {return;}@@ -295,7 +311,9 @@ public abstract class AutoLookup {String eventChar = Character.toString(e.getKeyChar());long when = e.getWhen();- if (when - lastTime > keyTimeout) {+ long elapsed = when - lastTime;+ boolean didTimeout = keyTimeoutPredicate.test(elapsed);+ if (didTimeout) {text = eventChar;}else {diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.javaindex 025b7b86fd..77b8cc83e0 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java@@ -323,13 +323,11 @@ public class GTable extends JTable {};}+ removeKeyListener(autoLookupListener);if (lookupColumn >= 0 && lookupColumn < getModel().getColumnCount()) {addKeyListener(autoLookupListener);enableActionKeyBindings = false;}- else {- removeKeyListener(autoLookupListener);- }}/**