use of org.eclipse.jface.databinding.conformance.util.ChangeEventTracker in project eclipse.platform.ui by eclipse-platform.
the class DatabindingContextTest method testValidationError.
/**
* Asserts that IStatus is populated and change events are fired when a
* Binding that is associated with a context is in error.
*
* @throws Exception
*/
@Test
public void testValidationError() throws Exception {
WritableValue<String> targetObservable = WritableValue.withValueType(String.class);
WritableValue<String> modelObservable = WritableValue.withValueType(String.class);
final String errorMessage = "error";
ValueChangeEventTracker<IStatus> errorCounter = new ValueChangeEventTracker<>();
ChangeEventTracker errorsCounter = new ChangeEventTracker();
IObservableValue<IStatus> error = new AggregateValidationStatus(dbc.getBindings(), AggregateValidationStatus.MAX_SEVERITY);
error.addValueChangeListener(errorCounter);
assertTrue(error.getValue().isOK());
IObservableMap<Binding, IStatus> errors = dbc.getValidationStatusMap();
errors.addChangeListener(errorsCounter);
assertEquals(0, errors.size());
IValidator<String> validator = value -> ValidationStatus.error(errorMessage);
dbc.bindValue(targetObservable, modelObservable, new UpdateValueStrategy<String, String>().setAfterGetValidator(validator), null);
targetObservable.setValue("");
assertFalse(error.getValue().isOK());
assertEquals(errorMessage, error.getValue().getMessage());
assertEquals(1, errors.size());
assertEquals(1, errorsCounter.count);
assertEquals(1, errorCounter.count);
error.dispose();
}
use of org.eclipse.jface.databinding.conformance.util.ChangeEventTracker in project eclipse.platform.ui by eclipse-platform.
the class AbstractObservableTest method testChangeListener.
@Test
public void testChangeListener() throws Exception {
assertFalse(observable.hasListeners());
ChangeEventTracker listener1 = new ChangeEventTracker();
assertFalse(observable.firstListenerAdded);
observable.addChangeListener(listener1);
assertTrue(observable.firstListenerAdded);
observable.firstListenerAdded = false;
assertTrue(observable.hasListeners());
assertEquals(0, listener1.count);
observable.fireChange();
assertEquals(1, listener1.count);
assertSame(observable, listener1.event.getSource());
// Add a second listener as the 1 vs. 2 listener code is different.
ChangeEventTracker listener2 = new ChangeEventTracker();
observable.addChangeListener(listener2);
assertEquals(0, listener2.count);
observable.fireChange();
assertEquals(2, listener1.count);
assertEquals(1, listener2.count);
// Add a third listener as the 2 vs. 3 or greater code is different.
ChangeEventTracker listener3 = new ChangeEventTracker();
observable.addChangeListener(listener3);
assertEquals(0, listener3.count);
observable.fireChange();
assertEquals(3, listener1.count);
assertEquals(2, listener2.count);
assertEquals(1, listener3.count);
assertFalse(observable.lastListenerRemoved);
observable.removeChangeListener(listener1);
observable.removeChangeListener(listener2);
observable.removeChangeListener(listener3);
assertTrue(observable.lastListenerRemoved);
assertFalse(observable.hasListeners());
}
use of org.eclipse.jface.databinding.conformance.util.ChangeEventTracker in project eclipse.platform.ui by eclipse-platform.
the class AbstractObservableTest method testHasListenersWithChangeAndStaleListeners.
@Test
public void testHasListenersWithChangeAndStaleListeners() throws Exception {
ChangeEventTracker changeListener = new ChangeEventTracker();
StaleEventTracker staleListener = new StaleEventTracker();
assertFalse(observable.hasListeners());
assertFalse(observable.firstListenerAdded);
assertFalse(observable.lastListenerRemoved);
observable.addChangeListener(changeListener);
assertTrue(observable.hasListeners());
assertTrue(observable.firstListenerAdded);
assertFalse(observable.lastListenerRemoved);
// reset
observable.firstListenerAdded = false;
observable.lastListenerRemoved = false;
observable.addStaleListener(staleListener);
assertTrue(observable.hasListeners());
assertFalse(observable.firstListenerAdded);
assertFalse(observable.lastListenerRemoved);
observable.removeChangeListener(changeListener);
assertTrue(observable.hasListeners());
assertFalse(observable.firstListenerAdded);
assertFalse(observable.lastListenerRemoved);
observable.removeStaleListener(staleListener);
assertFalse(observable.hasListeners());
assertFalse(observable.firstListenerAdded);
assertTrue(observable.lastListenerRemoved);
}
use of org.eclipse.jface.databinding.conformance.util.ChangeEventTracker in project eclipse.platform.ui by eclipse-platform.
the class MutableObservableCollectionContractTest method testRetainAll_NoChangeFiresNoChangeEvent.
@Test
public void testRetainAll_NoChangeFiresNoChangeEvent() throws Exception {
ChangeEventTracker tracker = ChangeEventTracker.observe(collection);
collection.retainAll(Collections.EMPTY_LIST);
assertEquals("List.retainAll should not have fired a change event:", 0, tracker.count);
}
use of org.eclipse.jface.databinding.conformance.util.ChangeEventTracker in project eclipse.platform.ui by eclipse-platform.
the class MutableObservableCollectionContractTest method assertChangeEventFired.
/**
* Asserts that a ChangeEvent is fired once when the provided
* <code>runnable</code> is invoked and the source is the provided
* <code>collection</code>.
*
* @param runnable
* @param methodName
* @param collection
*/
/* package */
void assertChangeEventFired(Runnable runnable, String methodName, IObservableCollection collection) {
ChangeEventTracker listener = ChangeEventTracker.observe(collection);
runnable.run();
assertEquals(formatFail(methodName + " should fire one ChangeEvent."), 1, listener.count);
assertEquals(formatFail(methodName + "'s change event observable should be the created Collection."), collection, listener.event.getObservable());
}
Aggregations