-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from sialcasa/310_command_without_ui_thread
#310 other implementation of a state machine for synchronous commands
- Loading branch information
Showing
3 changed files
with
151 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/commands/CommandsWithoutUiThreadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package de.saxsys.mvvmfx.utils.commands; | ||
|
||
import javafx.beans.property.BooleanProperty; | ||
import javafx.beans.property.IntegerProperty; | ||
import javafx.beans.property.SimpleBooleanProperty; | ||
import javafx.beans.property.SimpleIntegerProperty; | ||
import org.junit.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* There are use cases where the commands have to be used without a UI thread, for example when running unit tests. | ||
* | ||
* To verify this behaviour we use a dedicated test class where we don't use a special JUnit runner for UI threading. | ||
*/ | ||
public class CommandsWithoutUiThreadTest { | ||
|
||
public class MyViewModel { | ||
|
||
public IntegerProperty a = new SimpleIntegerProperty(); | ||
public IntegerProperty b = new SimpleIntegerProperty(); | ||
public IntegerProperty result = new SimpleIntegerProperty(); | ||
|
||
private final DelegateCommand calcCommand; | ||
|
||
public MyViewModel() { | ||
|
||
BooleanProperty executable = new SimpleBooleanProperty(); | ||
executable.bind(a.greaterThan(0).and(b.greaterThan(0))); | ||
|
||
calcCommand = new DelegateCommand(() -> { | ||
return new Action() { | ||
@Override | ||
protected void action() throws Exception { | ||
result.set(a.get() + b.get()); | ||
} | ||
}; | ||
}, executable, false); | ||
} | ||
|
||
public Command calcCommand() { | ||
return calcCommand; | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void test() { | ||
|
||
MyViewModel viewModel = new MyViewModel(); | ||
assertThat(viewModel.calcCommand.isExecutable()).isFalse(); | ||
assertThat(viewModel.result.get()).isEqualTo(0); | ||
|
||
|
||
viewModel.a.setValue(2); | ||
assertThat(viewModel.calcCommand.isExecutable()).isFalse(); | ||
assertThat(viewModel.result.get()).isEqualTo(0); | ||
|
||
viewModel.b.setValue(3); | ||
assertThat(viewModel.calcCommand.isExecutable()).isTrue(); | ||
assertThat(viewModel.result.get()).isEqualTo(0); | ||
|
||
viewModel.calcCommand.execute(); | ||
|
||
assertThat(viewModel.result.get()).isEqualTo(5); | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters