From 47503bb50c09876fad26827da44af47202157fdf Mon Sep 17 00:00:00 2001 From: Manuel Mauky Date: Fri, 2 Aug 2019 10:22:12 +0200 Subject: [PATCH] #595 Fix runningProperty for DelegateCommand in sync mode --- .../utils/commands/DelegateCommand.java | 13 +++++++- .../utils/commands/DelegateCommandTest.java | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/commands/DelegateCommand.java b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/commands/DelegateCommand.java index 04f265ce6..fae8d330d 100644 --- a/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/commands/DelegateCommand.java +++ b/mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/commands/DelegateCommand.java @@ -142,6 +142,14 @@ public void execute() { private ObjectProperty getStateObjectPropertyReadable() { return (ObjectProperty) stateProperty(); } + + /** + * In synchronous mode we need to simulate the lifecycle of the service and therefore manipulate the + * {@link Service#runningProperty()}. + */ + private BooleanProperty getRunningPropertyReadable() { + return (BooleanProperty) runningProperty(); + } protected void callActionAndSynthesizeServiceRun() { try { @@ -153,8 +161,11 @@ protected void callActionAndSynthesizeServiceRun() { Action action = actionSupplier.get(); setWritableExceptionProperty(action); bindServiceExceptionToTaskException(); - + + getRunningPropertyReadable().set(true); action.action(); + getRunningPropertyReadable().set(false); + getStateObjectPropertyReadable().setValue(State.SUCCEEDED); } catch (Exception e) { setException(e); diff --git a/mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/commands/DelegateCommandTest.java b/mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/commands/DelegateCommandTest.java index 9a67cf140..5c3b478a6 100644 --- a/mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/commands/DelegateCommandTest.java +++ b/mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/commands/DelegateCommandTest.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Supplier; @@ -415,4 +416,34 @@ private void sleep(long millis) { e.printStackTrace(); } } + + + @Test + void testRunningPropertyForSynchronousMode() { + CountDownLatch commandExecutedLatch = new CountDownLatch(1); + CountDownLatch listenerCalledLatch = new CountDownLatch(1); + + Command command = new DelegateCommand(() -> new Action() { + @Override + protected void action() throws Exception { + commandExecutedLatch.countDown(); + } + }); + + command.runningProperty().addListener((observable, oldValue, newValue) -> { + listenerCalledLatch.countDown(); + }); + + // when + command.execute(); + + // then + Assertions.assertDoesNotThrow(() -> { + commandExecutedLatch.await(2, TimeUnit.SECONDS); + listenerCalledLatch.await(2, TimeUnit.SECONDS); + }); + + Assertions.assertEquals(0, commandExecutedLatch.getCount(), "Action was not executed"); + Assertions.assertEquals(0, listenerCalledLatch.getCount(), "Listener was not invoked"); + } }