public void testModifyObservableList_FiresListChange() {
    Bean bean = new Bean(new ArrayList());
    IObservableList observable = BeansObservables.observeList(bean, "list");
    ListChangeEventTracker tracker = ListChangeEventTracker.observe(observable);

    Object element = new Object();
    observable.add(element);

    assertEquals(1, tracker.count);
    assertDiff(tracker.event.diff, Collections.EMPTY_LIST, Collections.singletonList(element));
  }
  public void testSetBeanPropertyOutsideRealm_FiresEventInsideRealm() {
    Bean bean = new Bean(Collections.EMPTY_LIST);
    CurrentRealm realm = new CurrentRealm(true);
    IObservableList observable = BeansObservables.observeList(realm, bean, "list");
    ListChangeEventTracker tracker = ListChangeEventTracker.observe(observable);

    realm.setCurrent(false);
    bean.setList(Collections.singletonList("element"));
    assertEquals(0, tracker.count);

    realm.setCurrent(true);
    assertEquals(1, tracker.count);
    assertDiff(tracker.event.diff, Collections.EMPTY_LIST, Collections.singletonList("element"));
  }
  public void testSetListChangeEvent() throws Exception {
    String oldElement = "old";
    String newElement = "new";
    list.add(oldElement);

    ListChangeEventTracker listener = ListChangeEventTracker.observe(list);
    assertEquals(0, listener.count);

    list.set(0, newElement);

    assertEquals(1, listener.count);
    ListChangeEvent event = listener.event;
    assertSame(list, event.getObservableList());

    assertDiff(
        event.diff, Collections.singletonList(oldElement), Collections.singletonList(newElement));
  }
  public void testSetBeanProperty_CorrectForNullOldAndNewValues() {
    // The java bean spec allows the old and new values in a
    // PropertyChangeEvent to be null, which indicates that an unknown
    // change occured.

    // This test ensures that JavaBeanObservableValue fires the correct
    // value diff even if the bean implementor is lazy :-P

    Bean bean = new AnnoyingBean();
    bean.setList(Collections.singletonList("old"));
    IObservableList observable = BeansObservables.observeList(new CurrentRealm(true), bean, "list");
    ListChangeEventTracker tracker = ListChangeEventTracker.observe(observable);
    bean.setList(Collections.singletonList("new"));

    assertEquals(1, tracker.count);

    List list = new ArrayList();
    list.add("old");
    tracker.event.diff.applyTo(list);
    assertEquals(Collections.singletonList("new"), list);
  }