Merge remote-tracking branch 'origin/GP-0-dragonmacher-test-fixes-11-21-22'

This commit is contained in:
Ryan Kurtz
2022-11-22 00:47:06 -05:00
4 changed files with 42 additions and 22 deletions

View File

@@ -649,7 +649,6 @@ public class OptionsDialogTest extends AbstractGhidraHeadedIntegrationTest {
Options options = tool.getOptions(ToolConstants.TOOL_OPTIONS); Options options = tool.getOptions(ToolConstants.TOOL_OPTIONS);
Color c = options.getColor("Favorite Color", Palette.RED);
String currentValue = options.getString("Favorite String", null); String currentValue = options.getString("Favorite String", null);
assertEquals("Bar", currentValue); assertEquals("Bar", currentValue);

View File

@@ -246,15 +246,21 @@ public class OptionsTest extends AbstractGenericTest {
options.addOptionsChangeListener(listener1); options.addOptionsChangeListener(listener1);
options.addOptionsChangeListener(listener2); options.addOptionsChangeListener(listener2);
options.setColor("COLOR", Palette.BLUE); try {
options.setColor("COLOR", Palette.BLUE);
fail("Expected an OptionsVetoExcepton");
}
catch (OptionsVetoException e) {
// expected
}
assertEquals(Palette.RED, options.getColor("COLOR", Palette.RED)); assertEquals(Palette.RED, options.getColor("COLOR", Palette.RED));
if (listener1.callOrder == 1) { if (listener1.callCount == 1) {
assertEquals(Palette.RED, listener1.value); assertEquals(Palette.RED, listener1.value);
assertEquals(null, listener2.value); assertEquals(null, listener2.value);
} }
if (listener2.callOrder == 1) { if (listener2.callCount == 1) {
assertEquals(Palette.RED, listener2.value); assertEquals(Palette.RED, listener2.value);
assertEquals(null, listener1.value); assertEquals(null, listener1.value);
} }
@@ -601,17 +607,17 @@ public class OptionsTest extends AbstractGenericTest {
private static class OptionsChangeListenerForTestVeto implements OptionsChangeListener { private static class OptionsChangeListenerForTestVeto implements OptionsChangeListener {
static int count = 0; static int count = 0;
private Object value; private Object value;
private int callOrder = -1; private int callCount = -1;
@Override @Override
public void optionsChanged(ToolOptions options, String optionName, Object oldValue, public void optionsChanged(ToolOptions options, String optionName, Object oldValue,
Object newValue) { Object newValue) {
if (callOrder < 0) { if (callCount < 0) {
callOrder = ++count; callCount = ++count;
} }
if (callOrder > 1) { if (callCount > 1) {
throw new OptionsVetoException("Test"); throw new OptionsVetoException("Test");
} }

View File

@@ -175,9 +175,8 @@ public abstract class AbstractOptions implements Options {
} }
private void warnShouldUseTheme(String optionType) { private void warnShouldUseTheme(String optionType) {
Throwable throwable = Throwable throwable = ReflectionUtilities
ReflectionUtilities.createThrowableWithStackOlderThan(AbstractOptions.class, .createThrowableWithStackOlderThan(AbstractOptions.class, SubOptions.class);
SubOptions.class);
String call = throwable.getStackTrace()[0].toString(); String call = throwable.getStackTrace()[0].toString();
Msg.warn(this, "Registering a direct " + optionType + " in the options is deprecated." + Msg.warn(this, "Registering a direct " + optionType + " in the options is deprecated." +
" Use registerTheme" + optionType + "Binding() instead!\n Called from " + call + "\n"); " Use registerTheme" + optionType + "Binding() instead!\n Called from " + call + "\n");
@@ -343,8 +342,15 @@ public abstract class AbstractOptions implements Options {
Object oldValue = option.getCurrentValue(); Object oldValue = option.getCurrentValue();
option.setCurrentValue(newValue); option.setCurrentValue(newValue);
if (!notifyOptionChanged(optionName, oldValue, newValue)) { boolean success = false;
option.setCurrentValue(oldValue); try {
// this can throw an OptionsVetoException
success = notifyOptionChanged(optionName, oldValue, newValue);
}
finally {
if (!success) {
option.setCurrentValue(oldValue);
}
} }
} }

View File

@@ -117,16 +117,25 @@ public class EditorState implements PropertyChangeListener {
return; return;
} }
options.putObject(name, currentValue); //
Object newValue = options.getObject(name, null); // The call to put() may throw an exception or may choose not to take the new value. Handle
boolean success = Objects.equals(currentValue, newValue); // both cases using a finally block along with checking the value after making the put()
if (success) { // call.
originalValue = newValue; //
currentValue = newValue; try {
options.putObject(name, currentValue);
} }
else { finally {
editor.setValue(originalValue); Object newValue = options.getObject(name, null);
currentValue = originalValue; boolean success = Objects.equals(currentValue, newValue);
if (success) {
originalValue = newValue;
currentValue = newValue;
}
else {
editor.setValue(originalValue);
currentValue = originalValue;
}
} }
} }