This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
AbstractSWTTester.java
158 lines (140 loc) · 4.43 KB
/
AbstractSWTTester.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
* Copyright (c) 2015 UT-Battelle, LLC.
* 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:
* Jordan Deyton - Initial API and implementation and/or initial documentation
*
*******************************************************************************/
package org.eclipse.ice.client.widgets.test.utils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBot;
/**
* This abstract class provides a basic framework for performing SWTBot tests
* for an SWT application inside a JUnit test framework. As such, test classes
* sub-classing this abstract class can be run as JUnit tests, JUnit plug-in
* tests (on or off the UI thread), or SWTBot tests.
* <p>
* A {@link Shell} is opened (outside the default Eclipse workbench if used as a
* plug-in test). This shell provides the "sandbox" in which UI tests are
* performed. For instance, to test a specific {@code Composite}, you may create
* an instance of said {@code Composite} in the shell and perform its UI tests.
* The shell is shared among sub-class tests, and can be acquired via
* {@link #getShell()}.
* </p>
* <p>
* An {@link SWTBot} is opened for each test class instance and should be used
* for the actual UI tests. The bot can be acquired via {@link #getBot()}.
* </p>
* <p>
* <b>Note:</b> Changes to the UI must be coordinated with the main UI thread
* using {@link Display#syncExec(Runnable)} or
* {@link Display#asyncExec(Runnable)}. To get the shell's display, you may call
* {@link #getDisplay()}.
* </p>
*
* @author Jordan Deyton
*
*/
public class AbstractSWTTester extends AbstractICEUITester<SWTBot> {
/**
* The {@code SWTBot} for this test class instance. This bot is to aid in
* performing UI tests that simulate user interaction with workbench-based
* plug-ins.
*/
private static SWTBot bot;
/**
* The shell used when creating an {@code SWTBot} for unit tests.
*/
private static Shell shell;
/*
* (non-Javadoc)
*
* @see org.eclipse.ice.client.widgets.test.AbstractICEUITester#getBot()
*/
@Override
protected SWTBot getBot() {
return bot;
}
/**
* Gets the main shell for testing SWT widgets "outside" (although they may
* be embedded in) the Eclipse plug-in infrastructure. This should contain
* top-level SWT {@code Composite}s that will be tested.
*
* @return The main shell for testing.
*/
protected static Shell getShell() {
return shell;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ice.client.widgets.test.AbstractUITester#beforeAllTests()
*/
@Override
public void beforeAllTests() {
super.beforeAllTests();
final Display display = getDisplay();
// Creating the shell must be done on the UI thread.
display.syncExec(new Runnable() {
@Override
public void run() {
// Create and open the shell.
shell = new Shell(display);
shell.open();
}
});
// Initialize static or otherwise shared resources here.
// Set up the SWTBot for the shell.
bot = new SWTBot(shell);
return;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ice.client.widgets.test.AbstractUITester#beforeEachTest()
*/
@Override
public void beforeEachTest() {
super.beforeEachTest();
// Initialize per-test resources here.
// Nothing to do yet.
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ice.client.widgets.test.AbstractUITester#afterEachTest()
*/
@Override
public void afterEachTest() {
// Dispose per-test resources here.
// Nothing to do yet.
// Proceed with the default post-test cleanup.
super.afterEachTest();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ice.client.widgets.test.AbstractUITester#afterAllTests()
*/
@Override
public void afterAllTests() {
// Dispose static or otherwise shared resources here.
// Close and unset the shell. This must be done on the UI thread.
getDisplay().syncExec(new Runnable() {
@Override
public void run() {
shell.close();
}
});
shell = null;
// Proceed with the default post-tests cleanup.
super.afterAllTests();
}
}