001: /*******************************************************************************
002: * Copyright (c) 2006 Brad Reynolds 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: * Brad Reynolds - initial API and implementation
010: * Brad Reynolds - bug 116920
011: * Brad Reynolds - bug 164653, 159768
012: ******************************************************************************/package org.eclipse.core.tests.databinding;
013:
014: import org.eclipse.core.databinding.Binding;
015: import org.eclipse.core.databinding.DataBindingContext;
016: import org.eclipse.core.databinding.UpdateValueStrategy;
017: import org.eclipse.core.databinding.observable.Diffs;
018: import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
019: import org.eclipse.core.databinding.observable.value.IObservableValue;
020: import org.eclipse.core.databinding.observable.value.ValueDiff;
021: import org.eclipse.core.databinding.observable.value.WritableValue;
022: import org.eclipse.core.databinding.validation.IValidator;
023: import org.eclipse.core.databinding.validation.ValidationStatus;
024: import org.eclipse.core.internal.databinding.BindingStatus;
025: import org.eclipse.core.runtime.IStatus;
026: import org.eclipse.core.runtime.MultiStatus;
027: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
028:
029: /**
030: * @since 1.1
031: */
032: public class ValueBindingTest extends AbstractDefaultRealmTestCase {
033: private WritableValue target;
034:
035: private WritableValue model;
036:
037: private DataBindingContext dbc;
038:
039: /*
040: * (non-Javadoc)
041: *
042: * @see org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase#setUp()
043: */
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: target = WritableValue.withValueType(String.class);
048: model = WritableValue.withValueType(String.class);
049: dbc = new DataBindingContext();
050: }
051:
052: /**
053: * Bug 152543.
054: *
055: * @throws Exception
056: */
057: public void testNoUpdateTargetFromModel() throws Exception {
058: try {
059: new DataBindingContext().bindValue(
060: new ObservableValueStub(),
061: new ObservableValueStub(), new UpdateValueStrategy(
062: UpdateValueStrategy.POLICY_NEVER),
063: new UpdateValueStrategy(
064: UpdateValueStrategy.POLICY_NEVER));
065: } catch (Exception e) {
066: fail(e.getMessage());
067: }
068: }
069:
070: public void testValuePropagation() throws Exception {
071: String initialValue = "value";
072: model.setValue(initialValue);
073:
074: assertFalse(model.getValue().equals(target.getValue()));
075: dbc.bindValue(target, model, null, null);
076:
077: assertEquals(target.getValue(), model.getValue());
078: }
079:
080: public void testGetTarget() throws Exception {
081: Binding binding = dbc.bindValue(target, model, null, null);
082:
083: assertEquals(target, binding.getTarget());
084: }
085:
086: public void testGetModel() throws Exception {
087: Binding binding = dbc.bindValue(target, model, null, null);
088:
089: assertEquals(model, binding.getModel());
090: }
091:
092: public void testOKStatusInValidationUpdatesModel() throws Exception {
093: Binding binding = dbc.bindValue(target, model, null, null);
094:
095: String value = "value";
096: assertFalse(value.equals(model.getValue()));
097: target.setValue(value);
098:
099: assertEquals("value copied to model", value, model.getValue());
100: assertTrue(((IStatus) binding.getValidationStatus().getValue())
101: .isOK());
102: }
103:
104: public void testWarningStatusInValidationUpdatesModel()
105: throws Exception {
106: Binding binding = dbc
107: .bindValue(target, model, new UpdateValueStrategy()
108: .setAfterGetValidator(warningValidator()), null);
109:
110: String value = "value";
111: assertFalse(value.equals(model.getValue()));
112: target.setValue(value);
113:
114: assertEquals("value copied to model", value, model.getValue());
115: assertEquals("warning status", IStatus.WARNING,
116: ((IStatus) binding.getValidationStatus().getValue())
117: .getSeverity());
118: }
119:
120: public void testInfoStatusInValidationUpdatesModel()
121: throws Exception {
122: Binding binding = dbc.bindValue(target, model,
123: new UpdateValueStrategy()
124: .setAfterGetValidator(infoValidator()), null);
125:
126: String value = "value";
127: assertFalse(value.equals(model.getValue()));
128: target.setValue(value);
129:
130: assertEquals("value copied to model", value, model.getValue());
131: assertEquals("info status", IStatus.INFO, ((IStatus) binding
132: .getValidationStatus().getValue()).getSeverity());
133: }
134:
135: public void testErrorStatusInValidationDoesNotUpdateModel()
136: throws Exception {
137: Binding binding = dbc.bindValue(target, model,
138: new UpdateValueStrategy()
139: .setAfterGetValidator(errorValidator()), null);
140:
141: String value = "value";
142: assertFalse(value.equals(model.getValue()));
143: target.setValue(value);
144:
145: assertFalse("value not copied to model", value.equals(model
146: .getValue()));
147: assertEquals("error status", IStatus.ERROR, ((IStatus) binding
148: .getValidationStatus().getValue()).getSeverity());
149: }
150:
151: public void testCancelStatusInValidationDoesNotUpdateModel()
152: throws Exception {
153: Binding binding = dbc.bindValue(target, model,
154: new UpdateValueStrategy()
155: .setAfterGetValidator(cancelValidator()), null);
156:
157: String value = "value";
158: assertFalse(value.equals(model.getValue()));
159: target.setValue(value);
160:
161: assertFalse("value not copied to model", value.equals(model
162: .getValue()));
163: assertEquals("cancel status", IStatus.CANCEL,
164: ((IStatus) binding.getValidationStatus().getValue())
165: .getSeverity());
166: }
167:
168: public void testStatusesFromEveryPhaseAreReturned()
169: throws Exception {
170: UpdateValueStrategy strategy = new UpdateValueStrategy() {
171: protected IStatus doSet(IObservableValue observableValue,
172: Object value) {
173: super .doSet(observableValue, value);
174: return ValidationStatus.info("");
175: }
176: };
177:
178: strategy.setAfterGetValidator(warningValidator());
179: strategy.setAfterConvertValidator(infoValidator());
180: strategy.setBeforeSetValidator(warningValidator());
181:
182: Binding binding = dbc.bindValue(target, model, strategy, null);
183: String value = "value";
184: assertFalse(value.equals(model.getValue()));
185:
186: target.setValue(value);
187: assertEquals(value, model.getValue());
188: IStatus status = (IStatus) binding.getValidationStatus()
189: .getValue();
190: assertTrue(status.isMultiStatus());
191: assertEquals("max status", IStatus.WARNING, status
192: .getSeverity());
193:
194: MultiStatus multiStatus = (MultiStatus) status;
195: assertEquals(4, multiStatus.getChildren().length);
196: IStatus[] children = multiStatus.getChildren();
197:
198: assertEquals("after get severity", IStatus.WARNING, children[0]
199: .getSeverity());
200: assertEquals("after convert severity", IStatus.INFO,
201: children[1].getSeverity());
202: assertEquals("before set severity", IStatus.WARNING,
203: children[2].getSeverity());
204: assertEquals("doSet severity", IStatus.INFO, children[3]
205: .getSeverity());
206: }
207:
208: public void testStatusIsInstanceOfBindingStatus() throws Exception {
209: Binding binding = dbc.bindValue(target, model, null, null);
210: assertTrue(binding.getValidationStatus().getValue() instanceof BindingStatus);
211: }
212:
213: public void testDiffsAreCheckedForEqualityBeforeUpdate()
214: throws Exception {
215: class WritableValueStub extends WritableValue {
216: public WritableValueStub() {
217: super ("", String.class);
218: }
219:
220: protected void fireValueChange(ValueDiff diff) {
221: super .fireValueChange(diff);
222: }
223: }
224:
225: WritableValueStub target = new WritableValueStub();
226: WritableValue model = WritableValue.withValueType(String.class);
227:
228: class Strategy extends UpdateValueStrategy {
229: int afterGetCount;
230:
231: public IStatus validateAfterGet(Object value) {
232: afterGetCount++;
233: return super .validateAfterGet(value);
234: }
235: }
236:
237: Strategy strategy = new Strategy();
238: dbc.bindValue(target, model, strategy, null);
239: int count = strategy.afterGetCount;
240:
241: target.fireValueChange(Diffs.createValueDiff("", ""));
242: assertEquals("update does not occur", count,
243: strategy.afterGetCount);
244: }
245:
246: private IValidator warningValidator() {
247: return new IValidator() {
248: public IStatus validate(Object value) {
249: return ValidationStatus.warning("");
250: }
251: };
252: }
253:
254: private IValidator infoValidator() {
255: return new IValidator() {
256: public IStatus validate(Object value) {
257: return ValidationStatus.info("");
258: }
259: };
260: }
261:
262: private IValidator errorValidator() {
263: return new IValidator() {
264: public IStatus validate(Object value) {
265: return ValidationStatus.error("");
266: }
267: };
268: }
269:
270: private IValidator cancelValidator() {
271: return new IValidator() {
272: public IStatus validate(Object value) {
273: return ValidationStatus.cancel("");
274: }
275: };
276: }
277:
278: private static class ObservableValueStub extends
279: AbstractObservableValue {
280: protected Object doGetValue() {
281: // do nothing
282: return null;
283: }
284:
285: public Object getValueType() {
286: // do nothing
287: return null;
288: }
289:
290: protected void doSetValue(Object value) {
291:
292: }
293: }
294: }
|