blob: a22f665226357ceed4808a211d28d42277c771df (
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
|
/*****************************************************************************
* Copyright (c) 2016 Christian W. Damus 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:
* Christian W. Damus - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.junit.utils.rules;
import org.eclipse.papyrus.junit.utils.JUnitUtils;
import org.eclipse.swt.widgets.Display;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* A JUnit rule that runs its test synchronously on the UI thread.
* This should be used only when the test manipulates API that
* require the current thread to be the UI thread.
*/
public class UIThreadRule implements TestRule {
private final boolean requireAnnotation;
/**
* Initializes me without the requirement for any annotation: all tests
* in my scope will run on the UI thread.
*/
public UIThreadRule() {
this(false);
}
/**
* Initializes me with the requirement that individual tests needin the UI
* thread be annotated with {@link UIThread @UIThread}.
*
* @param requireAnnotation
*/
public UIThreadRule(boolean requireAnnotation) {
super();
this.requireAnnotation = requireAnnotation;
}
@Override
public Statement apply(final Statement base, Description description) {
if (!requiresUIThread(description)) {
return base; // Nothing to do for this test
} else {
return new Statement() {
@Override
public void evaluate() throws Throwable {
final Throwable[] caught = { null };
final Runnable runTest = new Runnable() {
@Override
public void run() {
try {
base.evaluate();
} catch (Throwable t) {
caught[0] = t;
}
}
};
if (Display.getCurrent() != null) {
// Just run it
runTest.run();
} else {
// Run it on the UI thread
Display.getDefault().syncExec(runTest);
}
if (caught[0] != null) {
throw caught[0];
}
}
};
}
}
private boolean requiresUIThread(Description description) {
return !requireAnnotation
|| JUnitUtils.getAnnotation(description, UIThread.class) != null;
}
}
|