01: /*******************************************************************************
02: * Copyright (c) 2006 Brad Reynolds and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Brad Reynolds - initial API and implementation
10: * Brad Reynolds - bug 147515
11: ******************************************************************************/package org.eclipse.core.tests.internal.databinding.observable.masterdetail;
12:
13: import org.eclipse.core.databinding.observable.IObservable;
14: import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
15: import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
16: import org.eclipse.core.databinding.observable.value.IObservableValue;
17: import org.eclipse.core.databinding.observable.value.WritableValue;
18: import org.eclipse.core.internal.databinding.observable.masterdetail.DetailObservableValue;
19: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
20:
21: /**
22: * @since 3.2
23: */
24: public class DetailObservableValueTest extends
25: AbstractDefaultRealmTestCase {
26: private WritableValue outerObservable;
27:
28: protected void setUp() throws Exception {
29: super .setUp();
30: outerObservable = new WritableValue();
31: }
32:
33: public void testSetValue() throws Exception {
34: WritableValueFactory factory = new WritableValueFactory();
35: outerObservable.setValue("");
36:
37: IObservableValue detailObservable = MasterDetailObservables
38: .detailValue(outerObservable, factory, null);
39: WritableValue innerObservable = factory.innerObservable;
40: Object value = new Object();
41:
42: assertFalse(value.equals(innerObservable.getValue()));
43: detailObservable.setValue(value);
44: assertEquals("inner value", value, innerObservable.getValue());
45: }
46:
47: public void testGetValueType() throws Exception {
48: DetailObservableValue detailObservable = new DetailObservableValue(
49: outerObservable, null, String.class);
50: assertEquals(String.class, detailObservable.getValueType());
51: }
52:
53: /**
54: * Asserts that when a null value type is set for the detail observable no
55: * type checking is performed and the value type is always <code>null</code>.
56: */
57: public void testGetValueTypeNullValueType() throws Exception {
58: WritableValueFactory factory = new WritableValueFactory();
59: DetailObservableValue detailObservable = new DetailObservableValue(
60: outerObservable, factory, null);
61: assertNull(detailObservable.getValueType());
62: factory.type = String.class;
63:
64: // force the inner observable to be recreated
65: outerObservable.setValue("1");
66: assertNull("value type should be ignored", detailObservable
67: .getValueType());
68:
69: factory.type = Object.class;
70:
71: // force the inner observable to be recreated
72: outerObservable.setValue("2");
73: assertNull("value type should be ignored", detailObservable
74: .getValueType());
75: }
76:
77: /**
78: * Factory that creates WritableValues with the target as the value.
79: */
80: static class WritableValueFactory implements IObservableFactory {
81: WritableValue innerObservable;
82: Object type;
83:
84: public IObservable createObservable(Object target) {
85: return innerObservable = new WritableValue(target, type);
86: }
87: }
88: }
|