001: /*******************************************************************************
002: * Copyright (c) 2007 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: ******************************************************************************/package org.eclipse.jface.tests.databinding.swt;
011:
012: import org.eclipse.core.databinding.observable.list.IObservableList;
013: import org.eclipse.jface.databinding.swt.ISWTObservableValue;
014: import org.eclipse.jface.databinding.swt.SWTObservables;
015: import org.eclipse.jface.internal.databinding.internal.swt.ButtonObservableValue;
016: import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableList;
017: import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableValue;
018: import org.eclipse.jface.internal.databinding.internal.swt.CLabelObservableValue;
019: import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableList;
020: import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableValue;
021: import org.eclipse.jface.internal.databinding.internal.swt.LabelObservableValue;
022: import org.eclipse.jface.internal.databinding.internal.swt.ListObservableList;
023: import org.eclipse.jface.internal.databinding.internal.swt.ListObservableValue;
024: import org.eclipse.jface.internal.databinding.internal.swt.SWTProperties;
025: import org.eclipse.jface.internal.databinding.internal.swt.ScaleObservableValue;
026: import org.eclipse.jface.internal.databinding.internal.swt.SpinnerObservableValue;
027: import org.eclipse.jface.internal.databinding.internal.swt.TableSingleSelectionObservableValue;
028: import org.eclipse.jface.internal.databinding.internal.swt.TextEditableObservableValue;
029: import org.eclipse.jface.internal.databinding.internal.swt.TextObservableValue;
030: import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
031: import org.eclipse.jface.tests.databinding.RealmTester;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.custom.CCombo;
034: import org.eclipse.swt.custom.CLabel;
035: import org.eclipse.swt.graphics.Color;
036: import org.eclipse.swt.graphics.Font;
037: import org.eclipse.swt.widgets.Button;
038: import org.eclipse.swt.widgets.Combo;
039: import org.eclipse.swt.widgets.Label;
040: import org.eclipse.swt.widgets.List;
041: import org.eclipse.swt.widgets.Scale;
042: import org.eclipse.swt.widgets.Shell;
043: import org.eclipse.swt.widgets.Spinner;
044: import org.eclipse.swt.widgets.Table;
045: import org.eclipse.swt.widgets.Text;
046: import org.eclipse.swt.widgets.Tree;
047:
048: /**
049: * @since 1.1
050: */
051: public class SWTObservablesTest extends AbstractSWTTestCase {
052: private Shell shell;
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056:
057: shell = getShell();
058: RealmTester.setDefault(SWTObservables.getRealm(shell
059: .getDisplay()));
060: }
061:
062: protected void tearDown() throws Exception {
063: super .tearDown();
064:
065: RealmTester.setDefault(null);
066: }
067:
068: public void testObserveForeground() throws Exception {
069: ISWTObservableValue value = SWTObservables
070: .observeForeground(shell);
071: assertNotNull(value);
072: assertEquals(Color.class, value.getValueType());
073: }
074:
075: public void testObserveBackground() throws Exception {
076: ISWTObservableValue value = SWTObservables
077: .observeBackground(shell);
078: assertNotNull(value);
079: assertEquals(Color.class, value.getValueType());
080: }
081:
082: public void testObserveFont() throws Exception {
083: ISWTObservableValue value = SWTObservables.observeFont(shell);
084: assertNotNull(value);
085: assertEquals(Font.class, value.getValueType());
086: }
087:
088: public void testObserveSelectionOfSpinner() throws Exception {
089: Spinner spinner = new Spinner(shell, SWT.NONE);
090: ISWTObservableValue value = SWTObservables
091: .observeSelection(spinner);
092: assertNotNull(value);
093: assertTrue(value instanceof SpinnerObservableValue);
094:
095: SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
096: assertEquals(SWTProperties.SELECTION, spinnerObservable
097: .getAttribute());
098: }
099:
100: public void testObserveSelectionOfButton() throws Exception {
101: Button button = new Button(shell, SWT.PUSH);
102: ISWTObservableValue value = SWTObservables
103: .observeSelection(button);
104: assertNotNull(value);
105: assertTrue(value instanceof ButtonObservableValue);
106: }
107:
108: public void testObserveSelectionOfCombo() throws Exception {
109: Combo combo = new Combo(shell, SWT.NONE);
110: ISWTObservableValue value = SWTObservables
111: .observeSelection(combo);
112: assertNotNull(value);
113: assertTrue(value instanceof ComboObservableValue);
114:
115: ComboObservableValue comboObservable = (ComboObservableValue) value;
116: assertEquals(SWTProperties.SELECTION, comboObservable
117: .getAttribute());
118: }
119:
120: public void testObserveSelectionOfCCombo() throws Exception {
121: CCombo combo = new CCombo(shell, SWT.NONE);
122: ISWTObservableValue value = SWTObservables
123: .observeSelection(combo);
124: assertNotNull(value);
125: assertTrue(value instanceof CComboObservableValue);
126:
127: CComboObservableValue ccomboObservable = (CComboObservableValue) value;
128: assertEquals(SWTProperties.SELECTION, ccomboObservable
129: .getAttribute());
130: }
131:
132: public void testObserveSelectionOfList() throws Exception {
133: List list = new List(shell, SWT.NONE);
134: ISWTObservableValue value = SWTObservables
135: .observeSelection(list);
136: assertNotNull(value);
137: assertTrue(value instanceof ListObservableValue);
138: }
139:
140: public void testObserveSelectionOfScale() throws Exception {
141: Scale scale = new Scale(shell, SWT.NONE);
142: ISWTObservableValue value = SWTObservables
143: .observeSelection(scale);
144: assertNotNull(value);
145: assertTrue(value instanceof ScaleObservableValue);
146:
147: ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
148: assertEquals(SWTProperties.SELECTION, scaleObservable
149: .getAttribute());
150: }
151:
152: public void testObserveSelectionOfUnsupportedControl()
153: throws Exception {
154: try {
155: Text text = new Text(shell, SWT.NONE);
156: SWTObservables.observeSelection(text);
157: fail("Exception should have been thrown");
158: } catch (IllegalArgumentException e) {
159: }
160: }
161:
162: public void testObserveTextOfText() throws Exception {
163: Text text = new Text(shell, SWT.NONE);
164: assertFalse(text.isListening(SWT.FocusOut));
165:
166: ISWTObservableValue value = SWTObservables.observeText(text,
167: SWT.FocusOut);
168: assertNotNull(value);
169: assertTrue(value instanceof TextObservableValue);
170: assertTrue(text.isListening(SWT.FocusOut));
171: }
172:
173: public void testObserveTextWithEventOfUnsupportedControl()
174: throws Exception {
175: Label label = new Label(shell, SWT.NONE);
176: try {
177: SWTObservables.observeText(label, SWT.FocusOut);
178: fail("Exception should have been thrown");
179: } catch (Exception e) {
180: }
181: }
182:
183: public void testObserveTextOfLabel() throws Exception {
184: Label label = new Label(shell, SWT.NONE);
185: ISWTObservableValue value = SWTObservables.observeText(label);
186: assertNotNull(label);
187: assertTrue(value instanceof LabelObservableValue);
188: }
189:
190: public void testObserveTextOfCLabel() throws Exception {
191: CLabel label = new CLabel(shell, SWT.NONE);
192: ISWTObservableValue value = SWTObservables.observeText(label);
193: assertNotNull(label);
194: assertTrue(value instanceof CLabelObservableValue);
195: }
196:
197: public void testObserveTextOfCombo() throws Exception {
198: Combo combo = new Combo(shell, SWT.NONE);
199: ISWTObservableValue value = SWTObservables.observeText(combo);
200: assertNotNull(value);
201: assertTrue(value instanceof ComboObservableValue);
202:
203: ComboObservableValue comboObservable = (ComboObservableValue) value;
204: assertEquals(SWTProperties.TEXT, comboObservable.getAttribute());
205: }
206:
207: public void testObserveTextOfCCombo() throws Exception {
208: CCombo combo = new CCombo(shell, SWT.NONE);
209: ISWTObservableValue value = SWTObservables.observeText(combo);
210: assertNotNull(value);
211: assertTrue(value instanceof CComboObservableValue);
212:
213: CComboObservableValue ccomboObservable = (CComboObservableValue) value;
214: assertEquals(SWTProperties.TEXT, ccomboObservable
215: .getAttribute());
216: }
217:
218: public void testObserveTextOfUnsupportedControl() throws Exception {
219: Table table = new Table(shell, SWT.NONE);
220: try {
221: SWTObservables.observeText(table);
222: fail("Exception should have been thrown");
223: } catch (IllegalArgumentException e) {
224: }
225: }
226:
227: public void testObserveItemsOfCombo() throws Exception {
228: Combo combo = new Combo(shell, SWT.NONE);
229: IObservableList list = SWTObservables.observeItems(combo);
230: assertNotNull(list);
231: assertTrue(list instanceof ComboObservableList);
232: }
233:
234: public void testObserveItemsOfCCombo() throws Exception {
235: CCombo ccombo = new CCombo(shell, SWT.NONE);
236: IObservableList list = SWTObservables.observeItems(ccombo);
237: assertNotNull(list);
238: assertTrue(list instanceof CComboObservableList);
239: }
240:
241: public void testObserveItemsOfList() throws Exception {
242: List list = new List(shell, SWT.NONE);
243: IObservableList observableList = SWTObservables
244: .observeItems(list);
245: assertNotNull(observableList);
246: assertTrue(observableList instanceof ListObservableList);
247: }
248:
249: public void testObserveItemsOfUnsupportedControl() throws Exception {
250: Table table = new Table(shell, SWT.NONE);
251: try {
252: SWTObservables.observeItems(table);
253: fail("Exception should have been thrown");
254: } catch (IllegalArgumentException e) {
255: }
256: }
257:
258: public void testObserveSingleSelectionIndexOfTable()
259: throws Exception {
260: Table table = new Table(shell, SWT.NONE);
261: ISWTObservableValue value = SWTObservables
262: .observeSingleSelectionIndex(table);
263: assertNotNull(value);
264: assertTrue(value instanceof TableSingleSelectionObservableValue);
265: }
266:
267: public void testObserveSingleSelectionIndexOfUnsupportedControl()
268: throws Exception {
269: Tree tree = new Tree(shell, SWT.NONE);
270: try {
271: SWTObservables.observeSingleSelectionIndex(tree);
272: fail("Exception should have been thrown");
273: } catch (IllegalArgumentException e) {
274:
275: }
276: }
277:
278: public void testObserveMinOfSpinner() throws Exception {
279: Spinner spinner = new Spinner(shell, SWT.NONE);
280: ISWTObservableValue value = SWTObservables.observeMin(spinner);
281: assertNotNull(value);
282: assertTrue(value instanceof SpinnerObservableValue);
283:
284: SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
285: assertEquals(SWTProperties.MIN, spinnerObservable
286: .getAttribute());
287: }
288:
289: public void testObserveMinOfScale() throws Exception {
290: Scale scale = new Scale(shell, SWT.NONE);
291: ISWTObservableValue value = SWTObservables.observeMin(scale);
292: assertNotNull(value);
293: assertTrue(value instanceof ScaleObservableValue);
294:
295: ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
296: assertEquals(SWTProperties.MIN, scaleObservable.getAttribute());
297: }
298:
299: public void testObserveMinOfUnsupportedControl() throws Exception {
300: Text text = new Text(shell, SWT.NONE);
301: try {
302: SWTObservables.observeMin(text);
303: fail("Exception should have been thrown");
304: } catch (IllegalArgumentException e) {
305: }
306: }
307:
308: public void testObserveMaxOfSpinner() throws Exception {
309: Spinner spinner = new Spinner(shell, SWT.NONE);
310: ISWTObservableValue value = SWTObservables.observeMax(spinner);
311: assertNotNull(value);
312: assertTrue(value instanceof SpinnerObservableValue);
313:
314: SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
315: assertEquals(SWTProperties.MAX, spinnerObservable
316: .getAttribute());
317: }
318:
319: public void testObserveMaxOfScale() throws Exception {
320: Scale scale = new Scale(shell, SWT.NONE);
321: ISWTObservableValue value = SWTObservables.observeMax(scale);
322: assertNotNull(value);
323: assertTrue(value instanceof ScaleObservableValue);
324:
325: ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
326: assertEquals(SWTProperties.MAX, scaleObservable.getAttribute());
327: }
328:
329: public void testObserveMaxOfUnsupportedControl() throws Exception {
330: Text text = new Text(shell, SWT.NONE);
331: try {
332: SWTObservables.observeMax(text);
333: fail("Exception should have been thrown");
334: } catch (IllegalArgumentException e) {
335: }
336: }
337:
338: public void testObserveEditableOfText() throws Exception {
339: Text text = new Text(shell, SWT.NONE);
340: ISWTObservableValue value = SWTObservables
341: .observeEditable(text);
342: assertNotNull(value);
343: assertTrue(value instanceof TextEditableObservableValue);
344: }
345:
346: public void testObserveEditableOfUnsupportedControl()
347: throws Exception {
348: Label label = new Label(shell, SWT.NONE);
349: try {
350: SWTObservables.observeEditable(label);
351: fail("Exception should have been thrown");
352: } catch (IllegalArgumentException e) {
353: }
354: }
355: }
|