Skip to content

Instantly share code, notes, and snippets.

@amischler
Created February 12, 2015 10:02
Show Gist options
  • Save amischler/1cdaf0de2e423f69effe to your computer and use it in GitHub Desktop.
Save amischler/1cdaf0de2e423f69effe to your computer and use it in GitHub Desktop.
BidirectionalBinding unit test
package com.sun.javafx.binding;
import javafx.beans.property.*;
import org.junit.Assert;
import org.junit.Test;
public class BidirectionalBindingTest {
@Test
public void testBind() throws Exception {
DoubleProperty doubleProperty = new SimpleDoubleProperty(1.0);
BooleanProperty booleanProperty = new SimpleBooleanProperty(false);
BidirectionalBinding.bind(
doubleProperty,
booleanProperty,
d -> (d.doubleValue() > 0.0),
b -> b ? 1.0 : -1.0);
Assert.assertEquals(-1.0, doubleProperty.getValue(), 0.1);
doubleProperty.setValue(1.0);
Assert.assertTrue(booleanProperty.get());
doubleProperty.set(-1.0);
Assert.assertFalse(booleanProperty.get());
booleanProperty.setValue(true);
Assert.assertEquals(1.0, doubleProperty.getValue(), 0.1);
booleanProperty.setValue(false);
Assert.assertEquals(-1.0, doubleProperty.getValue(), 0.1);
BidirectionalBinding.unbind(doubleProperty, booleanProperty);
booleanProperty.setValue(true);
Assert.assertEquals(-1.0, doubleProperty.getValue(), 0.1);
booleanProperty.setValue(false);
doubleProperty.setValue(1.0);
Assert.assertFalse(booleanProperty.get());
}
@Test
public void testBindWithMethodReference() throws Exception {
DoubleProperty doubleProperty = new SimpleDoubleProperty(1.0);
StringProperty stringProperty = new SimpleStringProperty("0.0");
BidirectionalBinding.bind(
doubleProperty,
stringProperty,
d -> d.toString(),
Double::valueOf);
Assert.assertEquals(0.0, doubleProperty.get(), 0.1);
stringProperty.setValue("1.23");
Assert.assertEquals(1.23, doubleProperty.get(), 0.01);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment