Skip to main content
summaryrefslogtreecommitdiffstats
blob: 49515ed7acf4c5d7e31b6c67458f31ada01f0222 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2008 Ketan Padegaonkar 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:
 *     Ketan Padegaonkar - initial API and implementation
 *     Cédric Chabanois - http://swtbot.org/bugzilla/show_bug.cgi?id=17
 *******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;

import static org.eclipse.swtbot.swt.finder.utils.SWTUtils.isCocoa;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.List;

import org.eclipse.swtbot.swt.finder.test.AbstractControlExampleTest;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 * @version $Id$
 */
public class SWTBotComboTest extends AbstractControlExampleTest {

	@Test
	public void findsCombo() throws Exception {
		SWTBotCombo combo = bot.comboBoxInGroup("Background Mode on Parent");
		assertNotNull(combo.widget);
	}

	@Test
	public void getItems() throws Exception {
		bot.tabItem("Combo").activate();
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		assertEquals(9, comboBox.itemCount());
		List items = Arrays.asList(comboBox.items());
		assertEquals(9, items.size());
		for (int i = 1; i < 9; i++)
			assertTrue("Expected to contain: " + "Line " + i, items.contains("Line " + i));
		assertTrue("Expected to contain: " + "Longest Line In List", items.contains("Longest Line In List"));
	}

	@Test
	public void changeAndVerifiesSelectionInComboByText() throws Exception {
		bot.tabItem("Combo").activate();
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		comboBox.setSelection("Line 7");
		assertEquals("Line 7", comboBox.selection());
	}

	@Test
	public void changeAndVerifiesSelectionInComboByIndex() throws Exception {
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		comboBox.setSelection(2);
		assertEquals(2, comboBox.selectionIndex());
		assertEquals("Line 3", comboBox.selection());
	}

	@Test
	public void throwsExceptionInCaseOfInvalidIndexBasedSelection() throws Exception {
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		try {
			comboBox.setSelection(100);
			fail("Was expecting an exception");
		} catch (RuntimeException e) {
			assertEquals("The index (100) is more than the number of items (9) in the combo.", e.getMessage());
		}
	}

	@Test
	public void throwsExceptionInCaseOfInvalidTextBasedSelection() throws Exception {
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		try {
			comboBox.setSelection("non existent item");
			fail("Was expecting an exception");
		} catch (RuntimeException e) {
			assertEquals("Item `non existent item' not found in combo box.", e.getMessage());
		}
	}

	@Test
	public void changeAndVerifiesTextInCombo() throws Exception {
		bot.checkBox("SWT.READ_ONLY").deselect();
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		comboBox.setText("New Text");
		assertEquals("New Text", comboBox.getText());
	}

	@Test
	public void typeAndVerifiesTextInCombo() throws Exception {
		bot.checkBox("SWT.READ_ONLY").deselect();

		bot.checkBox("Listen").select();
		bot.button("Clear").click();

		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		comboBox.setText("");
		comboBox.typeText("New typed Text");
		assertEquals("New typed Text", comboBox.getText());

		if (!isCocoa())
			assertEventMatches(bot.textInGroup("Listeners"), "KeyDown [1]: KeyEvent{Combo {} time=490981104 data=null character='\\0' keyCode=131072 stateMask=0 doit=true}");
		assertEventMatches(bot.textInGroup("Listeners"), "KeyDown [1]: KeyEvent{Combo {} time=490981272 data=null character='N' keyCode=110 stateMask=131072 doit=true}");
		assertEventMatches(bot.textInGroup("Listeners"), "Verify [25]: VerifyEvent{Combo {} time=490981272 data=null character='N' keyCode=110 stateMask=131072 doit=true start=0 end=0 text=N}");
		assertEventMatches(bot.textInGroup("Listeners"), "KeyUp [2]: KeyEvent{Combo {N} time=490981352 data=null character='N' keyCode=110 stateMask=131072 doit=true}");
		if (!isCocoa())
			assertEventMatches(bot.textInGroup("Listeners"), "KeyUp [2]: KeyEvent{Combo {N} time=490981457 data=null character='\\0' keyCode=131072 stateMask=131072 doit=true}");
	}

	@Test
	public void throwsExceptionInCaseOfChangeTextOfReadOnlyCombo() throws Exception {
		bot.checkBox("SWT.READ_ONLY").select();
		SWTBotCombo comboBox = bot.comboBoxInGroup("Combo");
		try {
			comboBox.setText("Forbidden Text");
			fail("Was expecting an exception");
		} catch (RuntimeException e) {
			assertEquals("This combo box is read-only.", e.getMessage());
		} finally {
			bot.checkBox("SWT.READ_ONLY").deselect();
		}
	}

	@Before
	public void prepareExample() throws Exception {
		bot.tabItem("Combo").activate();
	}

}

Back to the top