From 9f1587d46e5e1ed3a04e55bbe48feffe91ff8292 Mon Sep 17 00:00:00 2001 From: Filip Ryblad Date: Sat, 7 Sep 2024 00:42:27 +0200 Subject: [PATCH] Fix crashes + some tests --- .../gui/frames/ThemeEditorDialog.java | 2 - .../gui/themeeditor/PreviewDataState.java | 4 +- .../datastate/calibrator/Calibrator.java | 2 +- .../datastate/common/DetachedDomainModel.java | 9 +++- .../model/domainmodel/DataComponent.java | 6 +-- .../model/domainmodel/DomainModel.java | 7 ++- .../domainmodel/EnvironmentComponent.java | 4 +- .../model/domainmodel/IDomainModel.java | 4 +- .../model/domainmodel/InferredComponent.java | 4 +- .../model/domainmodel/ListComponent.java | 6 +-- .../CalibrationIntegrationTests.java | 49 +++++++++++++++++++ .../IntegrationTestBuilder.java | 7 +++ .../ThemeEditorIntegrationTests.java | 32 ++++++++++++ .../simulations/OneEyeAccuracySimulation.java | 2 +- 14 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 src/test/java/ninjabrainbot/integrationtests/CalibrationIntegrationTests.java create mode 100644 src/test/java/ninjabrainbot/integrationtests/ThemeEditorIntegrationTests.java diff --git a/src/main/java/ninjabrainbot/gui/frames/ThemeEditorDialog.java b/src/main/java/ninjabrainbot/gui/frames/ThemeEditorDialog.java index f37e48b9..889cc509 100644 --- a/src/main/java/ninjabrainbot/gui/frames/ThemeEditorDialog.java +++ b/src/main/java/ninjabrainbot/gui/frames/ThemeEditorDialog.java @@ -61,14 +61,12 @@ public class ThemeEditorDialog extends ThemedDialog { private FramePreviewPanel ninBotPreviewDetailed; private final StyleManager previewStyleManager; - private final NinjabrainBotPreferences preferences; private final CustomTheme customTheme; // theme that is saved private final CustomTheme previewTheme; // used for previewing public ThemeEditorDialog(StyleManager styleManager, NinjabrainBotPreferences preferences, JFrame owner, CustomTheme customTheme) { super(styleManager, preferences, owner, I18n.get("settings.themeeditor.themeeditor")); Assert.isTrue(SwingUtilities.isEventDispatchThread()); - this.preferences = preferences; this.customTheme = customTheme; previewTheme = new CustomTheme(); previewTheme.setFromTheme(customTheme, true); diff --git a/src/main/java/ninjabrainbot/gui/themeeditor/PreviewDataState.java b/src/main/java/ninjabrainbot/gui/themeeditor/PreviewDataState.java index 6ca189db..0dee86e8 100644 --- a/src/main/java/ninjabrainbot/gui/themeeditor/PreviewDataState.java +++ b/src/main/java/ninjabrainbot/gui/themeeditor/PreviewDataState.java @@ -6,8 +6,8 @@ import ninjabrainbot.model.datastate.alladvancements.IAllAdvancementsDataState; import ninjabrainbot.model.datastate.blind.BlindResult; import ninjabrainbot.model.datastate.calculator.ICalculatorResult; -import ninjabrainbot.model.datastate.common.IPlayerPosition; import ninjabrainbot.model.datastate.common.DetachedDomainModel; +import ninjabrainbot.model.datastate.common.IPlayerPosition; import ninjabrainbot.model.datastate.common.ResultType; import ninjabrainbot.model.datastate.divine.DivineContext; import ninjabrainbot.model.datastate.divine.DivineResult; @@ -63,7 +63,7 @@ public PreviewDataState() { blindResult = new InferredComponent<>(domainModel); divineResult = new InferredComponent<>(domainModel); - boatDataState = new BoatDataState(null); + boatDataState = new BoatDataState(domainModel); allAdvancementsDataState = new PreviewAllAdvancementsDataState(); } diff --git a/src/main/java/ninjabrainbot/model/datastate/calibrator/Calibrator.java b/src/main/java/ninjabrainbot/model/datastate/calibrator/Calibrator.java index bb81f4c2..3acc7599 100644 --- a/src/main/java/ninjabrainbot/model/datastate/calibrator/Calibrator.java +++ b/src/main/java/ninjabrainbot/model/datastate/calibrator/Calibrator.java @@ -50,7 +50,7 @@ public class Calibrator implements IDisposable { private double lastX; private double lastZ; - private final IDivineContext divineContext = new DivineContext(null); + private final IDivineContext divineContext = new DivineContext(new DetachedDomainModel()); private final DisposeHandler disposeHandler = new DisposeHandler(); private final ObservableProperty whenModified = new ObservableProperty<>(); diff --git a/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java b/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java index ee3dc512..460823d7 100644 --- a/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java +++ b/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java @@ -53,8 +53,13 @@ public boolean isReset() { } @Override - public boolean isFullyInitialized() { - return false; + public boolean isExternalSubscriptionRegistrationAllowed() { + return true; + } + + @Override + public boolean isInternalSubscriptionRegistrationAllowed() { + return true; } @Override diff --git a/src/main/java/ninjabrainbot/model/domainmodel/DataComponent.java b/src/main/java/ninjabrainbot/model/domainmodel/DataComponent.java index cafe27a4..49ab8fc1 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/DataComponent.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/DataComponent.java @@ -73,15 +73,13 @@ public boolean isReset() { @Override public Subscription subscribeInternal(Consumer subscriber) { - if (domainModel != null) - Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe()."); + Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe()."); return observableField.subscribe(subscriber); } @Override public Subscription subscribe(Consumer subscriber) { - if (domainModel != null) - Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal()."); + Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal()."); return externalEvent.subscribe(subscriber); } diff --git a/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java b/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java index a8e400c5..635358e0 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java @@ -119,10 +119,15 @@ public boolean isReset() { } @Override - public boolean isFullyInitialized() { + public boolean isExternalSubscriptionRegistrationAllowed() { return isFullyInitialized; } + @Override + public boolean isInternalSubscriptionRegistrationAllowed() { + return !isFullyInitialized; + } + @Override public ISubscribable whenModified() { return whenModified; diff --git a/src/main/java/ninjabrainbot/model/domainmodel/EnvironmentComponent.java b/src/main/java/ninjabrainbot/model/domainmodel/EnvironmentComponent.java index ca363d26..3c76a5f7 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/EnvironmentComponent.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/EnvironmentComponent.java @@ -46,14 +46,14 @@ public T get() { @Override public Subscription subscribeInternal(Consumer subscriber) { if (domainModel != null) - Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe()."); + Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe()."); return observableField.subscribe(subscriber); } @Override public Subscription subscribe(Consumer subscriber) { if (domainModel != null) - Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal()."); + Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal()."); return externalEvent.subscribe(subscriber); } diff --git a/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java b/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java index 33baec87..724ac097 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java @@ -23,7 +23,9 @@ public interface IDomainModel extends IWriteLock { boolean isReset(); - boolean isFullyInitialized(); + boolean isExternalSubscriptionRegistrationAllowed(); + + boolean isInternalSubscriptionRegistrationAllowed(); ISubscribable whenModified(); diff --git a/src/main/java/ninjabrainbot/model/domainmodel/InferredComponent.java b/src/main/java/ninjabrainbot/model/domainmodel/InferredComponent.java index 514c649a..9f4373f4 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/InferredComponent.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/InferredComponent.java @@ -43,14 +43,14 @@ public T get() { @Override public Subscription subscribeInternal(Consumer subscriber) { if (domainModel != null) - Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IInferredComponent.subscribe()."); + Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IInferredComponent.subscribe()."); return observableField.subscribe(subscriber); } @Override public Subscription subscribe(Consumer subscriber) { if (domainModel != null) - Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IInferredComponent.subscribeInternal()."); + Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IInferredComponent.subscribeInternal()."); return externalEvent.subscribe(subscriber); } diff --git a/src/main/java/ninjabrainbot/model/domainmodel/ListComponent.java b/src/main/java/ninjabrainbot/model/domainmodel/ListComponent.java index db8b7761..09c9a753 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/ListComponent.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/ListComponent.java @@ -128,15 +128,13 @@ public Iterator iterator() { @Override public Subscription subscribeInternal(Consumer> subscriber) { - if (domainModel != null) - Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IListComponent.subscribe()."); + Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IListComponent.subscribe()."); return observableList.subscribe(subscriber); } @Override public Subscription subscribe(Consumer> subscriber) { - if (domainModel != null) - Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IListComponent.subscribeInternal()."); + Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IListComponent.subscribeInternal()."); return externalEvent.subscribe(subscriber); } diff --git a/src/test/java/ninjabrainbot/integrationtests/CalibrationIntegrationTests.java b/src/test/java/ninjabrainbot/integrationtests/CalibrationIntegrationTests.java new file mode 100644 index 00000000..c669753f --- /dev/null +++ b/src/test/java/ninjabrainbot/integrationtests/CalibrationIntegrationTests.java @@ -0,0 +1,49 @@ +package ninjabrainbot.integrationtests; + +import java.util.Locale; + +import javax.swing.SwingUtilities; + +import ninjabrainbot.gui.frames.CalibrationDialog; +import ninjabrainbot.model.datastate.calibrator.CalibratorFactory; +import ninjabrainbot.util.TestUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class CalibrationIntegrationTests { + + @Test + public void canOpenDialog() { + // Arrange + IntegrationTestBuilder integrationTestBuilder = new IntegrationTestBuilder(); + Locale.setDefault(Locale.US); + CalibratorFactory calibratorFactory = integrationTestBuilder.createCalibratorFactory(); + + // Act + Assert + try { + SwingUtilities.invokeAndWait(() -> { + CalibrationDialog dialog = new CalibrationDialog(TestUtils.createStyleManager(), integrationTestBuilder.preferences, calibratorFactory, integrationTestBuilder.actionExecutor, null, false); + }); + } catch (Exception e) { + Assertions.fail(e); + } + } + + @Test + public void canOpenDialog_manualCalibration() { + // Arrange + IntegrationTestBuilder integrationTestBuilder = new IntegrationTestBuilder(); + Locale.setDefault(Locale.US); + CalibratorFactory calibratorFactory = integrationTestBuilder.createCalibratorFactory(); + + // Act + Assert + try { + SwingUtilities.invokeAndWait(() -> { + CalibrationDialog dialog = new CalibrationDialog(TestUtils.createStyleManager(), integrationTestBuilder.preferences, calibratorFactory, integrationTestBuilder.actionExecutor, null, true); + }); + } catch (Exception e) { + Assertions.fail(e); + } + } + +} diff --git a/src/test/java/ninjabrainbot/integrationtests/IntegrationTestBuilder.java b/src/test/java/ninjabrainbot/integrationtests/IntegrationTestBuilder.java index db10413c..5ab87bc0 100644 --- a/src/test/java/ninjabrainbot/integrationtests/IntegrationTestBuilder.java +++ b/src/test/java/ninjabrainbot/integrationtests/IntegrationTestBuilder.java @@ -20,6 +20,7 @@ import ninjabrainbot.model.ModelState; import ninjabrainbot.model.actions.IActionExecutor; import ninjabrainbot.model.datastate.IDataState; +import ninjabrainbot.model.datastate.calibrator.CalibratorFactory; import ninjabrainbot.model.datastate.common.IDetailedPlayerPosition; import ninjabrainbot.model.datastate.endereye.CoordinateInputSource; import ninjabrainbot.model.datastate.endereye.EnderEyeThrowFactory; @@ -175,6 +176,12 @@ public NinjabrainBotFrame createNinjabrainBotFrame() { return frame; } + public CalibratorFactory createCalibratorFactory() { + if (fakeCoordinateInputSource == null) + fakeCoordinateInputSource = new FakeCoordinateInputSource(); + return new CalibratorFactory(environmentState.calculatorSettings(), fakeCoordinateInputSource, preferences); + } + public BoatIcon createBoatIcon() { if (styleManager == null) styleManager = TestUtils.createStyleManager(); return new BoatIcon(styleManager, dataState.boatDataState().boatState(), preferences, new DisposeHandler()); diff --git a/src/test/java/ninjabrainbot/integrationtests/ThemeEditorIntegrationTests.java b/src/test/java/ninjabrainbot/integrationtests/ThemeEditorIntegrationTests.java new file mode 100644 index 00000000..c1d9670a --- /dev/null +++ b/src/test/java/ninjabrainbot/integrationtests/ThemeEditorIntegrationTests.java @@ -0,0 +1,32 @@ +package ninjabrainbot.integrationtests; + +import java.util.Locale; + +import javax.swing.SwingUtilities; + +import ninjabrainbot.gui.frames.ThemeEditorDialog; +import ninjabrainbot.gui.frames.ThemedDialog; +import ninjabrainbot.gui.style.theme.CustomTheme; +import ninjabrainbot.util.TestUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ThemeEditorIntegrationTests { + + @Test + public void canOpenDialog() { + // Arrange + IntegrationTestBuilder integrationTestBuilder = new IntegrationTestBuilder(); + Locale.setDefault(Locale.US); + + // Act + Assert + try { + SwingUtilities.invokeAndWait(() -> { + ThemedDialog dialog = new ThemeEditorDialog(TestUtils.createStyleManager(), integrationTestBuilder.preferences, null, new CustomTheme()); + }); + } catch (Exception e) { + Assertions.fail(e); + } + } + +} diff --git a/src/test/java/ninjabrainbot/simulations/OneEyeAccuracySimulation.java b/src/test/java/ninjabrainbot/simulations/OneEyeAccuracySimulation.java index c563a307..8c6ac195 100644 --- a/src/test/java/ninjabrainbot/simulations/OneEyeAccuracySimulation.java +++ b/src/test/java/ninjabrainbot/simulations/OneEyeAccuracySimulation.java @@ -33,7 +33,7 @@ public class OneEyeAccuracySimulation { public static void main(String[] args) { Logger.enabled = false; calculator = new Calculator(new CalculatorSettings(true, McVersion.PRE_119), new StandardDeviationSettings(std, std, std, std)); - divineContext = new DivineContext(null); + divineContext = new DivineContext(new DetachedDomainModel()); for (int r = 0; r < 3000; r += 100) { int successes = 0;