From 7cb190620c0c505b58a51347764527c83fa02caf Mon Sep 17 00:00:00 2001 From: dragonmacher <48328597+dragonmacher@users.noreply.github.com> Date: Sat, 12 Oct 2024 15:17:07 -0400 Subject: [PATCH] Test fixes --- .../dialog/KeyBindingUtilsTest.java | 11 +++++-- .../plugintool/dialog/KeyBindingsTest.java | 30 ++++--------------- .../bean/opteditor/OptionsDialogTest.java | 14 ++++----- .../src/main/java/docking/KeyEntryPanel.java | 12 ++++++-- .../main/java/docking/KeyEntryTextField.java | 14 +++++++++ .../plugintool/dialog/KeyBindingsPanel.java | 2 +- 6 files changed, 46 insertions(+), 37 deletions(-) diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingUtilsTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingUtilsTest.java index 32a8ca538c..2cb9bd2fa3 100644 --- a/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingUtilsTest.java +++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingUtilsTest.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -30,6 +30,7 @@ import javax.swing.tree.TreePath; import org.junit.*; +import docking.KeyEntryPanel; import docking.action.DockingActionIf; import docking.actions.KeyBindingUtils; import docking.options.editor.OptionsDialog; @@ -429,7 +430,11 @@ public class KeyBindingUtilsTest extends AbstractGhidraHeadedIntegrationTest { panel = (KeyBindingsPanel) getEditorPanel(keyBindingsNode, optionsDialog); table = findComponent(panel, JTable.class); Object actionBindingPanel = getInstanceField("actionBindingPanel", panel); - keyField = (JTextField) getInstanceField("keyEntryField", actionBindingPanel); + + KeyEntryPanel keyEntryPanel = + (KeyEntryPanel) getInstanceField("keyEntryPanel", actionBindingPanel); + keyField = keyEntryPanel.getTextField(); + model = table.getModel(); debug("ff"); diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingsTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingsTest.java index 0f70d38374..03e80c4f71 100644 --- a/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingsTest.java +++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/framework/plugintool/dialog/KeyBindingsTest.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -94,8 +94,7 @@ public class KeyBindingsTest extends AbstractGhidraHeadedIntegrationTest { // look for the info panel MultiLineLabel label = findComponent(panel, MultiLineLabel.class); String str = "To add or change a key binding, select an action\n" + - "and type any key combination\n" + " \n" + - "To remove a key binding, select an action and\n" + "press or "; + "and type any key combination."; assertEquals(str, label.getLabel()); @@ -239,18 +238,7 @@ public class KeyBindingsTest extends AbstractGhidraHeadedIntegrationTest { selectRowForAction(action1); - typeKeyStroke(KeyEvent.VK_ENTER); - assertNoKeyStrokeText(); - - apply(); - assertNull(getKeyStroke(action1)); - } - - @Test - public void testClearKeyBinding2() throws Exception { - - selectRowForAction(action1); - typeBackspace(); + pressButtonByName(panel, "Clear Key Binding"); assertNoKeyStrokeText(); apply(); @@ -290,7 +278,6 @@ public class KeyBindingsTest extends AbstractGhidraHeadedIntegrationTest { selectRowForAction(action1); typeKeyStroke(reservedKeystroke); - assertNoKeyStrokeText(); assertMessage("F4 in use by System action 'Set KeyBinding'"); @@ -313,7 +300,7 @@ public class KeyBindingsTest extends AbstractGhidraHeadedIntegrationTest { selectRowForAction(action1); assertEquals(validKeyStroke, getKeyStroke(action1)); - typeKeyStroke(reservedKeystroke.getKeyCode()); + typeKeyStroke(reservedKeystroke); assertNoKeyStrokeText(); assertMessage("F4 in use by System action 'Set KeyBinding'"); @@ -384,7 +371,7 @@ public class KeyBindingsTest extends AbstractGhidraHeadedIntegrationTest { assertMessage(systemKsText + " in use by System action 'Show Context Menu'"); selectRowForAction(systemAction); - typeBackspace(); + pressButtonByName(panel, "Clear Key Binding"); apply(); assertEquals(null, getKeyStroke(systemAction)); @@ -422,11 +409,6 @@ public class KeyBindingsTest extends AbstractGhidraHeadedIntegrationTest { assertMessage(""); } - private void typeBackspace() { - triggerBackspaceKey(keyField); - waitForSwing(); - } - private void typeKeyStroke(KeyStroke ks) { triggerKey(keyField, ks); waitForSwing(); diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/util/bean/opteditor/OptionsDialogTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/util/bean/opteditor/OptionsDialogTest.java index 153e181534..70c13ca757 100644 --- a/Ghidra/Features/Base/src/test.slow/java/ghidra/util/bean/opteditor/OptionsDialogTest.java +++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/util/bean/opteditor/OptionsDialogTest.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils; import org.junit.*; import docking.DialogComponentProvider; +import docking.KeyEntryPanel; import docking.action.DockingActionIf; import docking.actions.KeyBindingUtils; import docking.options.editor.*; @@ -1010,7 +1011,9 @@ public class OptionsDialogTest extends AbstractGhidraHeadedIntegrationTest { setToggleButtonSelected(panel, "Enter Mouse Binding", false); JPanel actionBindingPanel = (JPanel) getInstanceField("actionBindingPanel", panel); - JTextField textField = (JTextField) getInstanceField("keyEntryField", actionBindingPanel); + KeyEntryPanel keyEntryPanel = + (KeyEntryPanel) getInstanceField("keyEntryPanel", actionBindingPanel); + JTextField textField = keyEntryPanel.getTextField(); triggerKey(textField, modifiers, keyCode, keyChar); waitForSwing(); @@ -1030,10 +1033,7 @@ public class OptionsDialogTest extends AbstractGhidraHeadedIntegrationTest { setToggleButtonSelected(panel, "Enter Mouse Binding", false); - JPanel actionBindingPanel = (JPanel) getInstanceField("actionBindingPanel", panel); - JTextField textField = (JTextField) getInstanceField("keyEntryField", actionBindingPanel); - - triggerBackspaceKey(textField); + pressButtonByName(panel, "Clear Key Binding"); waitForSwing(); KeyStroke currentBinding = getKeyBindingFromTable(actionName, actionOwner); diff --git a/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryPanel.java b/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryPanel.java index e33fc5fdab..424f1ca080 100644 --- a/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryPanel.java +++ b/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryPanel.java @@ -42,14 +42,22 @@ public class KeyEntryPanel extends JPanel { keyEntryField = new KeyEntryTextField(columns, listener); clearButton = new EmptyBorderButton(Icons.DELETE_ICON); - clearButton.setToolTipText("Clear the current key binding"); - clearButton.addActionListener(e -> keyEntryField.clearField()); + clearButton.setName("Clear Key Binding"); + clearButton.addActionListener(e -> keyEntryField.clearKeyStroke()); add(keyEntryField); add(Box.createHorizontalStrut(2)); add(clearButton); } + /** + * Returns the text field used by this class + * @return the text field + */ + public JTextField getTextField() { + return keyEntryField; + } + /** * Sets the key stroke on this panel * @param ks the key stroke diff --git a/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryTextField.java b/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryTextField.java index 3ff2f824e0..dad11ec184 100644 --- a/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryTextField.java +++ b/Ghidra/Framework/Docking/src/main/java/docking/KeyEntryTextField.java @@ -81,7 +81,21 @@ public class KeyEntryTextField extends HintTextField { setText(KeyBindingUtils.parseKeyStroke(ks)); } + /** + * Clears the state of this class, but does not notify listeners. This allows clients to + * control the state of the field without having a callback change the client state. + */ public void clearField() { + ksName = null; + setText(""); + currentKeyStroke = null; + } + + /** + * Clears the state of this class and notifies this client. This effectively allows for the + * programmatic setting of the keystroke in use to be null, or in the 'no keystroke set' state. + */ + public void clearKeyStroke() { if (currentKeyStroke == null) { return; } diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/dialog/KeyBindingsPanel.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/dialog/KeyBindingsPanel.java index 39632c098c..3b4efc558b 100644 --- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/dialog/KeyBindingsPanel.java +++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/plugintool/dialog/KeyBindingsPanel.java @@ -455,8 +455,8 @@ public class KeyBindingsPanel extends JPanel { ToolActions toolActions = (ToolActions) tool.getToolActions(); String errorMessage = toolActions.validateActionKeyBinding(action, ks); if (errorMessage != null) { - statusLabel.setText(errorMessage); actionBindingPanel.clearKeyStroke(); + statusLabel.setText(errorMessage); return; }