Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce3709fae3cd74fec60c696986c643957ac19f7b (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
/*******************************************************************************
 * Copyright 2007 new SWTBot, http://swtbot.org/
 * 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
 *******************************************************************************/
package org.eclipse.swtbot.swt.finder.finders;

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;

import java.util.List;

import junit.framework.Assert;

import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.finders.ControlFinder;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTabItem;

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

	private ControlFinder	finder;

	public void testFindsAGroup() throws Exception {
		final List frames = finder.findControls(instanceOf(Group.class));
		assertEquals(12, frames.size());
		assertText("Image Buttons", (Widget) frames.get(2));
	}

	public void testFindsAllTabItem() throws Exception {
		List tabItems = finder.findControls(instanceOf(TabItem.class));
		Assert.assertEquals(24, tabItems.size());
	}

	public void testFindsAShell() throws Exception {
		List shells = finder.findShells("SWT Controls");
		Assert.assertFalse(shells.isEmpty());
		Assert.assertEquals(1, shells.size());
		Assert.assertEquals(controlShell, shells.get(0));
	}

	public void testFindsATabItem() throws Exception {
		List tabItems = finder.findControls(allOf(instanceOf(TabItem.class), withText("Dialog")));
		final TabItem items[] = new TabItem[] { null };
		display.syncExec(new Runnable() {
			public void run() {
				items[0] = ((TabFolder) controlShell.getChildren()[0]).getItems()[5];
			}
		});

		Assert.assertEquals(1, tabItems.size());
		Assert.assertEquals(items[0], tabItems.get(0));
	}

	public void testFindsText() throws Exception {
		List textBoxes = finder.findControls(instanceOf(Text.class));
		Assert.assertEquals(1, textBoxes.size());
	}

	public void testFindsTwoButtons() throws Exception {
		final List buttons = finder.findControls(allOf(instanceOf(Button.class), withText("One")));
		assertEquals(2, buttons.size());
		assertText("One", (Widget) buttons.get(0));
		assertText("One", (Widget) buttons.get(1));
	}

	public void testGetsControlPath() throws Exception {
		List labels = finder.findControls(allOf(instanceOf(Button.class), withText("One")));
		Widget w = (Widget) labels.get(0);
		TreePath path = finder.getPath(w);
		Assert.assertEquals(7, path.getSegmentCount());
	}

	public void testGetsControlPathToTabItem() throws Exception {
		List tabItems = finder.findControls(allOf(instanceOf(TabItem.class), withText("Dialog")));
		TreePath path = finder.getPath((Widget) tabItems.get(0));
		Assert.assertEquals(3, path.getSegmentCount());
	}

	public void testGetsOnlyVisibleControls() throws Exception {
		// use the default tab
		List textBoxesOnButtonTab = finder.findControls(instanceOf(Text.class));
		assertEquals(1, textBoxesOnButtonTab.size());
		assertText("", (Widget) textBoxesOnButtonTab.get(0));

		// switch to another tab
		List tabItems = finder.findControls(allOf(instanceOf(TabItem.class), withText("Text")));
		new SWTBotTabItem((TabItem) tabItems.get(0)).activate();

		// should get different tabs this time
		List textBoxesOnTextTab = finder.findControls(instanceOf(Text.class));
		assertEquals(2, textBoxesOnTextTab.size());
		assertTextContains("The quick brown fox jumps over the lazy dog", (Widget) textBoxesOnTextTab.get(0));
		assertText("", (Widget) textBoxesOnTextTab.get(1));
		assertNotSameWidget((Widget) textBoxesOnButtonTab.get(0), (Widget) textBoxesOnTextTab.get(0));
	}

	protected void setUp() throws Exception {
		super.setUp();
		finder = new ControlFinder();
		new  SWTBot().tabItem("Button").activate();
	}

}

Back to the top