001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.jface.conformance.databinding;
011:
012: import org.eclipse.core.databinding.observable.value.IObservableValue;
013: import org.eclipse.jface.tests.databinding.RealmTester;
014: import org.eclipse.jface.tests.databinding.EventTrackers.ChangeEventTracker;
015: import org.eclipse.jface.tests.databinding.EventTrackers.ValueChangeEventTracker;
016: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
017:
018: /**
019: * Mutability tests for IObservableValue.
020: *
021: * <p>
022: * This class is experimental and can change at any time. It is recommended to
023: * not subclass or assume the test names will not change. The only API that is
024: * guaranteed to not change are the constructors. The tests will remain public
025: * and not final in order to allow for consumers to turn off a test if needed by
026: * subclassing.
027: * </p>
028: *
029: * @since 3.2
030: */
031: public class MutableObservableValueContractTest extends
032: ObservableDelegateTest {
033: private IObservableValueContractDelegate delegate;
034:
035: private IObservableValue observable;
036:
037: /**
038: * @param delegate
039: */
040: public MutableObservableValueContractTest(
041: IObservableValueContractDelegate delegate) {
042: this (null, delegate);
043: }
044:
045: public MutableObservableValueContractTest(String testName,
046: IObservableValueContractDelegate delegate) {
047: super (testName, delegate);
048: this .delegate = delegate;
049: }
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053:
054: this .observable = (IObservableValue) getObservable();
055: }
056:
057: public void testSetValue_SetsValue() throws Exception {
058: Object value = delegate.createValue(observable);
059:
060: observable.setValue(value);
061: assertEquals(
062: formatFail("IObservableValue.setValue(Object) should set the value of the observable."),
063: value, observable.getValue());
064: }
065:
066: public void testSetValue_ChangeEvent() throws Exception {
067: ChangeEventTracker listener = new ChangeEventTracker()
068: .register(observable);
069:
070: observable.setValue(delegate.createValue(observable));
071:
072: assertEquals(
073: formatFail("Change event listeners were not notified"),
074: 1, listener.count);
075: assertEquals(
076: formatFail("IObservableValue.setValue(Object) should fire one ChangeEvent."),
077: 1, listener.count);
078: assertEquals(
079: formatFail("IObservableValue.setValue(Object)'s change event observable should be the created observable."),
080: observable, listener.event.getObservable());
081: }
082:
083: public void testSetValue_SameValue() throws Exception {
084: // invoke change to ensure observable has a value
085: delegate.change(observable);
086:
087: ValueChangeEventTracker valueChangeListener = new ValueChangeEventTracker()
088: .register(observable);
089: ChangeEventTracker changeListener = new ChangeEventTracker()
090: .register(observable);
091: Object value = observable.getValue();
092: observable.setValue(value);
093:
094: assertEquals(
095: formatFail("IObservableValue.setValue() should not fire a value change event when the value has not change."),
096: 0, valueChangeListener.count);
097: assertEquals(
098: formatFail("IObservableValue.setValue() should not fire a change event when the value has not change."),
099: 0, changeListener.count);
100: }
101:
102: public void testSetValue_RealmChecks() throws Exception {
103: RealmTester.exerciseCurrent(new Runnable() {
104: public void run() {
105: observable.setValue(delegate.createValue(observable));
106: }
107: }, (CurrentRealm) observable.getRealm());
108: }
109: }
|