Skip to main content
summaryrefslogtreecommitdiffstats
blob: b6b4a59d043cc4dbeef278bacba9fc8de14b6559 (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
package org.eclipse.platform.discovery.ui.test.comp.internal.pageobjects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Basic implementation of a page object which creates UI in a test shell
 * 
 * @author Danail Branekov
 * 
 */
abstract class InShellPageObject
{
	private final SWTBot bot;
	private SWTBotShell botShell;
	private static final String TEST_SHELL_LABEL = "Testing Shell";
	private FormToolkit formToolkit;

	InShellPageObject()
	{
		bot = new SWTBot();
	}

	public void open()
	{
		createShell();
		botShell = bot.shell(TEST_SHELL_LABEL);
		botShell.activate();
	}

	public void close()
	{
		botShell.close();
	}

	/**
	 * Implementors should create the UI to be tested here. This method is invoked from the UI thread
	 * 
	 * @param parent
	 *            the parent shell; by default its layout is set to {@link FillLayout}
	 * @param formToolkit
	 *            toolkit for creating UI stuff
	 */
	protected abstract void createContent(final Shell parent, final FormToolkit formToolkit);

	/**
	 * Implementers may dispose resources upon shell disposal via overriding this method. The method is invoked from the UI thread
	 * 
	 * @param e
	 *            the dispose event
	 */
	protected void dispose(DisposeEvent e)
	{
		this.formToolkit.dispose();
	}

	protected Display display()
	{
		return PlatformUI.getWorkbench().getDisplay();
	}

	private Shell createShell()
	{
		final Shell[] theShell = new Shell[1];
		final Runnable createShellRunnable = new Runnable()
		{
			@Override
			public void run()
			{
				formToolkit = new FormToolkit(display());
				theShell[0] = new Shell(display(), SWT.RESIZE);
				theShell[0].setText(TEST_SHELL_LABEL);
				theShell[0].setLayout(new FillLayout());
				createContent(theShell[0], formToolkit);
				theShell[0].layout(true);
				theShell[0].addDisposeListener(new DisposeListener()
				{
					@Override
					public void widgetDisposed(DisposeEvent e)
					{
						dispose(e);
					}
				});
				theShell[0].open();
			}
		};
		display().syncExec(createShellRunnable);
		return theShell[0];
	}
	
	protected SWTBot bot() {
		return bot;
	}
	
	protected SWTBotShell shell()
	{
		return botShell;
	}
	
}

Back to the top