Search in sources :

Example 1 with ChangeEventTracker

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();
}
Also used : Binding(org.eclipse.core.databinding.Binding) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) DataBindingContext(org.eclipse.core.databinding.DataBindingContext) WritableList(org.eclipse.core.databinding.observable.list.WritableList) ArrayList(java.util.ArrayList) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IStatus(org.eclipse.core.runtime.IStatus) After(org.junit.After) Assert.fail(org.junit.Assert.fail) AggregateValidationStatus(org.eclipse.core.databinding.AggregateValidationStatus) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WritableValue(org.eclipse.core.databinding.observable.value.WritableValue) Binding(org.eclipse.core.databinding.Binding) AbstractDefaultRealmTestCase(org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase) Assert.assertFalse(org.junit.Assert.assertFalse) UpdateListStrategy(org.eclipse.core.databinding.UpdateListStrategy) ChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ChangeEventTracker) ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) Assert.assertEquals(org.junit.Assert.assertEquals) IObservableMap(org.eclipse.core.databinding.observable.map.IObservableMap) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IStatus(org.eclipse.core.runtime.IStatus) AggregateValidationStatus(org.eclipse.core.databinding.AggregateValidationStatus) ChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ChangeEventTracker) ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) Test(org.junit.Test)

Example 2 with ChangeEventTracker

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());
}
Also used : ChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ChangeEventTracker) Test(org.junit.Test) ObservableStaleContractTest(org.eclipse.jface.databinding.conformance.ObservableStaleContractTest) ObservableContractTest(org.eclipse.jface.databinding.conformance.ObservableContractTest)

Example 3 with ChangeEventTracker

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);
}
Also used : ChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ChangeEventTracker) StaleEventTracker(org.eclipse.jface.databinding.conformance.util.StaleEventTracker) Test(org.junit.Test) ObservableStaleContractTest(org.eclipse.jface.databinding.conformance.ObservableStaleContractTest) ObservableContractTest(org.eclipse.jface.databinding.conformance.ObservableContractTest)

Example 4 with ChangeEventTracker

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);
}
Also used : ChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ChangeEventTracker) Test(org.junit.Test)

Example 5 with ChangeEventTracker

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());
}
Also used : ChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ChangeEventTracker)

Aggregations

ChangeEventTracker (org.eclipse.jface.databinding.conformance.util.ChangeEventTracker)16 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)3 WritableList (org.eclipse.core.databinding.observable.list.WritableList)3 StaleEventTracker (org.eclipse.jface.databinding.conformance.util.StaleEventTracker)3 ValueChangeEventTracker (org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker)3 IObservablesListener (org.eclipse.core.databinding.observable.IObservablesListener)2 IObservableList (org.eclipse.core.databinding.observable.list.IObservableList)2 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)2 ObservableContractTest (org.eclipse.jface.databinding.conformance.ObservableContractTest)2 ObservableStaleContractTest (org.eclipse.jface.databinding.conformance.ObservableStaleContractTest)2 SWTMutableObservableValueContractTest (org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest)2 SWTObservableValueContractTest (org.eclipse.jface.databinding.conformance.swt.SWTObservableValueContractTest)2 DisposeEventTracker (org.eclipse.jface.databinding.conformance.util.DisposeEventTracker)2 ListChangeEventTracker (org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker)2 SetChangeEventTracker (org.eclipse.jface.databinding.conformance.util.SetChangeEventTracker)2 List (java.util.List)1 AggregateValidationStatus (org.eclipse.core.databinding.AggregateValidationStatus)1 Binding (org.eclipse.core.databinding.Binding)1 DataBindingContext (org.eclipse.core.databinding.DataBindingContext)1