Search in sources :

Example 1 with ValueChangeEventTracker

use of org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker 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 ValueChangeEventTracker

use of org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker in project eclipse.platform.ui by eclipse-platform.

the class ObservableValueContractTest method testRemoveValueChangeListener_RemovesListener.

@Test
public void testRemoveValueChangeListener_RemovesListener() throws Exception {
    ValueChangeEventTracker listener = ValueChangeEventTracker.observe(observable);
    delegate.change(observable);
    // precondition
    assertEquals(formatFail("Value change listeners should be notified on change."), 1, listener.count);
    observable.removeValueChangeListener(listener);
    delegate.change(observable);
    assertEquals(formatFail("Value change listeners should not be notified after they've been removed from the observable."), 1, listener.count);
}
Also used : ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) Test(org.junit.Test)

Example 3 with ValueChangeEventTracker

use of org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker in project eclipse.platform.ui by eclipse-platform.

the class ObservableValueContractTest method testChange_ValueChangeEventDiff.

@Test
public void testChange_ValueChangeEventDiff() throws Exception {
    ValueChangeEventTracker listener = ValueChangeEventTracker.observe(observable);
    Object oldValue = observable.getValue();
    delegate.change(observable);
    ValueChangeEvent event = listener.event;
    assertTrue(formatFail("Change Listeners were not notified on change."), listener.count > 0);
    assertEquals(formatFail("When a value change event is fired the old value should be the previous value of the observable value."), oldValue, event.diff.getOldValue());
    assertEquals(formatFail("When a value change event is fired the new value should be the same as the current value of the observable value."), observable.getValue(), event.diff.getNewValue());
}
Also used : ValueChangeEvent(org.eclipse.core.databinding.observable.value.ValueChangeEvent) ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) Test(org.junit.Test)

Example 4 with ValueChangeEventTracker

use of org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker in project eclipse.platform.ui by eclipse-platform.

the class ObservableValueContractTest method testChange_ValueChangeEvent.

@Test
public void testChange_ValueChangeEvent() throws Exception {
    ValueChangeEventTracker listener = ValueChangeEventTracker.observe(observable);
    delegate.change(observable);
    assertEquals(formatFail("On change value change listeners should be notified."), 1, listener.count);
}
Also used : ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) Test(org.junit.Test)

Example 5 with ValueChangeEventTracker

use of org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker in project eclipse.platform.ui by eclipse-platform.

the class ValidatedObservableValueTest method testSetValue_SingleValueChangeEvent.

@Test
public void testSetValue_SingleValueChangeEvent() {
    ValueChangeEventTracker tracker = ValueChangeEventTracker.observe(validated);
    validated.setValue(newValue);
    assertEquals(1, tracker.count);
    assertEquals(oldValue, tracker.event.diff.getOldValue());
    assertEquals(newValue, tracker.event.diff.getNewValue());
}
Also used : ValueChangeEventTracker(org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker) MutableObservableValueContractTest(org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest) Test(org.junit.Test)

Aggregations

ValueChangeEventTracker (org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker)30 Test (org.junit.Test)26 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)13 MutableObservableValueContractTest (org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest)8 DateTime (org.eclipse.swt.widgets.DateTime)4 SWTMutableObservableValueContractTest (org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest)3 SWTObservableValueContractTest (org.eclipse.jface.databinding.conformance.swt.SWTObservableValueContractTest)3 Before (org.junit.Before)3 LocalDate (java.time.LocalDate)2 LocalTime (java.time.LocalTime)2 ChangeEventTracker (org.eclipse.jface.databinding.conformance.util.ChangeEventTracker)2 ISWTObservableValue (org.eclipse.jface.databinding.swt.ISWTObservableValue)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 Shell (org.eclipse.swt.widgets.Shell)2 ArrayList (java.util.ArrayList)1 AggregateValidationStatus (org.eclipse.core.databinding.AggregateValidationStatus)1 Binding (org.eclipse.core.databinding.Binding)1 DataBindingContext (org.eclipse.core.databinding.DataBindingContext)1 UpdateListStrategy (org.eclipse.core.databinding.UpdateListStrategy)1