/
AbstractGuiTestSupport.java
70 lines (63 loc) · 2.69 KB
/
AbstractGuiTestSupport.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
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import java.awt.Component;
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import org.junit.jupiter.api.BeforeAll;
/**
* Abstract base class for testing GUI components.
*/
public abstract class AbstractGuiTestSupport extends AbstractPathTestSupport {
/**
* Validates the graphics environment.
*/
@BeforeAll
public static void validateGraphicsEnvironment() {
final boolean isHeadless = GraphicsEnvironment.isHeadless();
assumeFalse(isHeadless, "This test is incompatible with headless environment");
}
/**
* Helper method to find a component by its name.
*
* @param root the root component to start search
* @param name the name of component to find
* @param <T> the type of component to find
* @return the component if found, {@code null} otherwise
* @noinspection unchecked
* @noinspectionreason unchecked - we know that any component is OK to typecast to T
*/
protected static <T extends Component> T findComponentByName(Component root, String name) {
Component result = null;
if (name.equals(root.getName())) {
result = root;
}
else if (root instanceof Container) {
final Component[] children = ((Container) root).getComponents();
for (Component component : children) {
result = findComponentByName(component, name);
if (result != null) {
break;
}
}
}
return (T) result;
}
}