diff --git a/src/name/mlopatkin/andlogview/MainFrame.java b/src/name/mlopatkin/andlogview/MainFrame.java index e04645de..e9871d8f 100644 --- a/src/name/mlopatkin/andlogview/MainFrame.java +++ b/src/name/mlopatkin/andlogview/MainFrame.java @@ -536,9 +536,8 @@ private void saveTableToFile(File file) { } private void onAdbServicesStatusChanged(AdbServicesStatus.StatusValue newStatus) { - boolean adbInitFailed = newStatus instanceof AdbServicesStatus.InitFailed; - acConnectToDevice.setEnabled(!adbInitFailed); - acDumpDevice.setEnabled(!adbInitFailed); + acConnectToDevice.setEnabled(!newStatus.isFailed()); + acDumpDevice.setEnabled(!newStatus.isFailed()); } public static class Factory implements Provider { diff --git a/src/name/mlopatkin/andlogview/ui/device/AdbServicesStatus.java b/src/name/mlopatkin/andlogview/ui/device/AdbServicesStatus.java index 7bd53dfb..2d6df571 100644 --- a/src/name/mlopatkin/andlogview/ui/device/AdbServicesStatus.java +++ b/src/name/mlopatkin/andlogview/ui/device/AdbServicesStatus.java @@ -50,7 +50,13 @@ interface Observer { * Base class for service status. Poor man's sealed class. */ abstract class StatusValue { - private StatusValue() {} + private final boolean isInitialized; + private final boolean isFailed; + + private StatusValue(boolean isInitialized, boolean isFailed) { + this.isInitialized = isInitialized; + this.isFailed = isFailed; + } public static NotInitialized notInitialized() { return NotInitialized.INSTANCE; @@ -67,6 +73,14 @@ public static Initialized initialized() { public static InitFailed failed(String failureMessage) { return new InitFailed(failureMessage); } + + public final boolean isInitialized() { + return isInitialized; + } + + public final boolean isFailed() { + return isFailed; + } } /** @@ -75,7 +89,9 @@ public static InitFailed failed(String failureMessage) { final class NotInitialized extends StatusValue { private static final NotInitialized INSTANCE = new NotInitialized(); - private NotInitialized() {} + private NotInitialized() { + super(false, false); + } @Override public String toString() { @@ -89,7 +105,9 @@ public String toString() { final class Initializing extends StatusValue { private static final Initializing INSTANCE = new Initializing(); - private Initializing() {} + private Initializing() { + super(false, false); + } @Override public String toString() { @@ -103,7 +121,9 @@ public String toString() { final class Initialized extends StatusValue { private static final Initialized INSTANCE = new Initialized(); - private Initialized() {} + private Initialized() { + super(true, false); + } @Override public String toString() { @@ -118,6 +138,7 @@ final class InitFailed extends StatusValue { private final String failureMessage; private InitFailed(String failureMessage) { + super(false, true); this.failureMessage = failureMessage; } diff --git a/src/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenter.java b/src/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenter.java index 89a75d29..638322dd 100644 --- a/src/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenter.java +++ b/src/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenter.java @@ -18,6 +18,7 @@ import name.mlopatkin.andlogview.preferences.AdbConfigurationPref; import name.mlopatkin.andlogview.ui.device.AdbServicesInitializationPresenter; +import name.mlopatkin.andlogview.ui.device.AdbServicesStatus; import java.util.Objects; import java.util.function.Predicate; @@ -52,15 +53,18 @@ interface View { private final View view; private final AdbConfigurationPref adbConfigurationPref; private final AdbServicesInitializationPresenter adbServicesPresenter; + private final AdbServicesStatus adbServicesStatus; @Inject ConfigurationDialogPresenter( View view, AdbConfigurationPref adbConfigurationPref, - AdbServicesInitializationPresenter adbServicesPresenter) { + AdbServicesInitializationPresenter adbServicesPresenter, + AdbServicesStatus adbServicesStatus) { this.view = view; this.adbConfigurationPref = adbConfigurationPref; this.adbServicesPresenter = adbServicesPresenter; + this.adbServicesStatus = adbServicesStatus; } public void openDialog() { @@ -85,7 +89,7 @@ private void tryCommit() { adbConfigurationPref.setAutoReconnectEnabled(view.isAutoReconnectEnabled()); view.hide(); - if (hasLocationChanged) { + if (hasLocationChanged || adbServicesStatus.getStatus().isFailed()) { adbServicesPresenter.restartAdb(); } } diff --git a/test/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenterTest.java b/test/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenterTest.java index 9960dfe8..975414c0 100644 --- a/test/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenterTest.java +++ b/test/name/mlopatkin/andlogview/ui/preferences/ConfigurationDialogPresenterTest.java @@ -21,12 +21,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import name.mlopatkin.andlogview.config.FakeInMemoryConfigStorage; import name.mlopatkin.andlogview.preferences.AdbConfigurationPref; import name.mlopatkin.andlogview.test.Expectations; import name.mlopatkin.andlogview.test.TestActionHandler; import name.mlopatkin.andlogview.ui.device.AdbServicesInitializationPresenter; +import name.mlopatkin.andlogview.ui.device.AdbServicesStatus; import name.mlopatkin.andlogview.utils.SystemPathResolver; import org.junit.jupiter.api.BeforeEach; @@ -61,11 +63,15 @@ class ConfigurationDialogPresenterTest { @Mock private AdbServicesInitializationPresenter adbServicesInitPresenter; + @Mock(strictness = Mock.Strictness.LENIENT) + private AdbServicesStatus adbServicesStatus; @BeforeEach void setUp() { adbConfiguration.setAutoReconnectEnabled(DEFAULT_AUTO_RECONNECT); adbConfiguration.setAdbLocation(DEFAULT_ADB_LOCATION); + + when(adbServicesStatus.getStatus()).thenReturn(AdbServicesStatus.StatusValue.initialized()); } @Test @@ -152,8 +158,22 @@ void settingValidAdbLocationClearsError() { assertFalse(fakeView.isInvalidAdbLocationHighlighted()); } + @Test + void committingRestartsAdbWithoutChangesIfItWasNotRunning() { + adbConfiguration.setAdbLocation(VALID_ADB_LOCATION); + when(adbServicesStatus.getStatus()).thenReturn(AdbServicesStatus.StatusValue.failed("Not initialized")); + + var presenter = createPresenter(); + + presenter.openDialog(); + fakeView.commit(); + + verify(adbServicesInitPresenter).restartAdb(); + } + private ConfigurationDialogPresenter createPresenter() { - return new ConfigurationDialogPresenter(fakeView, adbConfiguration, adbServicesInitPresenter); + return new ConfigurationDialogPresenter(fakeView, adbConfiguration, adbServicesInitPresenter, + adbServicesStatus); } static class FakeView implements ConfigurationDialogPresenter.View {