Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8087827

Bindings.bindContent doesn't work as expected with ListBinding

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • 9
    • 8u45
    • javafx
    • None

      Test case below.

      import javafx.beans.binding.Bindings;
      import javafx.beans.binding.ListBinding;
      import javafx.beans.property.ListProperty;
      import javafx.beans.property.ReadOnlyListProperty;
      import javafx.beans.property.SimpleListProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import org.junit.Assert;
      import org.junit.Ignore;
      import org.junit.Test;

      import java.util.Date;
      import java.util.List;

      /**
       * This test case show that {@link Bindings#bindContent(List, ObservableList)} doesn't work was expected when a binding is perform between an {@link ObservableList} and an other one that come from a computed list binding.
       *
       * Could be nice to explain what goes wrong if it's not a bug.Thank you
       *
       * Created by Kevin Senechal (kevin.senechal@dooapp.com) on 04/06/15.
       */
      public class BindingsBindContentBugTest{

          /**
           * A mapper that expose a list of dates and that compute a list of string (representing the dates toString()).
           */
          private class ExempleMapper {
              private ListBinding<String> datesAsListBinding;
              private ListProperty<Date> dates = new SimpleListProperty<>(FXCollections.observableArrayList());
              private ListProperty<String> datesAsString = new SimpleListProperty<>();

              public ExempleMapper() {
                  datesAsListBinding = new ListBinding<String>() {
                      {super.bind(dates);}
                      @Override
                      protected ObservableList<String> computeValue() {
                          ObservableList<String> result = FXCollections.observableArrayList();
                          for (Date date : getDates()){
                              result.add(date.toString());
                          }
                          return result;
                      }
                  };
                  datesAsString.bind(datesAsListBinding);
              }

              public ObservableList<Date> getDates() {
                  return dates.get();
              }

              public ObservableList<String> getDatesAsString() {
                  return FXCollections.unmodifiableObservableList(datesAsString.get());
              }

              public ReadOnlyListProperty<String> datesAsStringProperty() {
                  return datesAsString;
              }
          }

          /**
           * Test that will fail!
           */
          @Test
          public void testUsingBindingsBindContentWithAnObservableListThatComeFromAListPropertyThatComeFromAListBinding(){
              ExempleMapper mapper = new ExempleMapper();

              ObservableList<String> testedList = FXCollections.observableArrayList();
              Bindings.bindContent(testedList, mapper.getDatesAsString());

              Date date = new Date();
              mapper.getDates().add(date);
              Assert.assertEquals(1,testedList.size());
              Assert.assertEquals(date.toString(),testedList.get(0));
          }

          /**
           * Test that is ok
           */
          @Test
          public void testUsingListPropertyBind(){
              ExempleMapper mapper = new ExempleMapper();

              ListProperty<String> testedList = new SimpleListProperty<>();
              testedList.bind(mapper.datesAsStringProperty());

              Date date = new Date();
              mapper.getDates().add(date);
              Assert.assertEquals(1, testedList.size());
              Assert.assertEquals(date.toString(), testedList.get(0));
          }

          /**
           * Test that is ok
           */
          @Test
          public void testUsingBindingsBindContentWithAnObservableListThatComeFromAListProperty(){
              ListProperty<String> datas = new SimpleListProperty<>(FXCollections.observableArrayList());

              ObservableList<String> testedList = FXCollections.observableArrayList();
              Bindings.bindContent(testedList, datas.get());

              String data = "DATA";
              datas.get().add(data);

              Assert.assertEquals(1,testedList.size());
              Assert.assertEquals(data,testedList.get(0));
          }



          /**
           * Test that is ok
           */
          @Test
          public void testBasicBindingsBindContent(){
              ObservableList<String> mainList = FXCollections.observableArrayList();
              ObservableList<String> testedList = FXCollections.observableArrayList();

              Bindings.bindContent(testedList, mainList);

              String data = "DATA";
              mainList.add(data);

              Assert.assertEquals(1, testedList.size());
              Assert.assertEquals(data, testedList.get(0));
          }
      }

            vadim Vadim Pakhnushev
            skevinjfx Senechal Kevin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: