Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1bfe69e8eb6e1ad7d20f38efcf1bc387cd7b1953 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************************************************
 * Copyright (c) 2006 Brad Reynolds and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Brad Reynolds - initial API and implementation
 *     Ashley Cambrell - bug 198904
 *     Matthew Hall - bug 194734, 195222
 ******************************************************************************/

package org.eclipse.jface.tests.internal.databinding.swt;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.internal.databinding.swt.ComboSelectionProperty;
import org.eclipse.jface.internal.databinding.swt.ComboTextProperty;
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;

/**
 * @since 3.2
 * @no
 */
public class ComboObservableValueTest extends AbstractSWTTestCase {
	public void testDispose() throws Exception {
		Combo combo = new Combo(getShell(), SWT.NONE);
		IObservableValue observableValue = SWTObservables.observeText(combo);
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
		observableValue.addValueChangeListener(testCounterValueChangeListener);

		assertEquals("", combo.getText());
		assertEquals("", observableValue.getValue());

		String expected1 = "Test123";
		combo.setText(expected1);

		assertEquals(1, testCounterValueChangeListener.count);
		assertEquals(expected1, combo.getText());
		assertEquals(expected1, observableValue.getValue());

		observableValue.dispose();

		String expected2 = "NewValue123";
		combo.setText(expected2);

		assertEquals(1, testCounterValueChangeListener.count);
		assertEquals(expected2, combo.getText());
	}

	public void testSetValueWithNull() {
		testSetValueWithNull(WidgetProperties.text());
		testSetValueWithNull(WidgetProperties.selection());
	}

	protected void testSetValueWithNull(IValueProperty property) {
		Combo combo = new Combo(getShell(), SWT.NONE);
		combo.setItems(new String[] { "one", "two", "three" });
		IObservableValue observable = property.observe(Realm.getDefault(),
				combo);

		observable.setValue("two");
		assertEquals("two", combo.getText());
		if (property instanceof ComboSelectionProperty) {
			assertEquals("expect selection at index 1 in selection mode", 1,
					combo.getSelectionIndex());
		}

		if (property instanceof ComboTextProperty) {
			observable.setValue(null);
			assertEquals("expect empty text in text mode", "", combo.getText());
		}
	}
}

Back to the top