View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import static org.junit.jupiter.api.Assumptions.assumeFalse;
23  
24  import java.awt.Component;
25  import java.awt.Container;
26  import java.awt.GraphicsEnvironment;
27  
28  import javax.swing.JComboBox;
29  
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  
33  import com.github.caciocavallosilano.cacio.ctc.junit.CacioExtension;
34  import com.github.caciocavallosilano.cacio.ctc.junit.CacioTest;
35  import com.puppycrawl.tools.checkstyle.gui.MainFrameModel;
36  
37  /**
38   * Abstract base class for testing GUI components.
39   *
40   * @noinspection NewClassNamingConvention
41   * @noinspectionreason Abstract base class for GUI tests
42   */
43  @CacioTest
44  @ExtendWith(CacioExtension.class)
45  public abstract class AbstractGuiTestSupport extends AbstractPathTestSupport {
46  
47      /**
48       * Validates the graphics environment.
49       */
50      @BeforeEach
51      public void validateGraphicsEnvironment() {
52          final boolean isHeadless = GraphicsEnvironment.isHeadless();
53          assumeFalse(isHeadless, "This test is incompatible with headless environment");
54      }
55  
56      /**
57       * Helper method to find a component by its name.
58       *
59       * @param root the root component to start search
60       * @param name the name of component to find
61       * @param clazz the subtype of component
62       * @param <T> the type of component to find
63       * @return the component if found, {@code null} otherwise
64       */
65      protected static <T extends Component> T findComponentByName(Component root, String name,
66                                                                   Class<T> clazz) {
67          Component result = null;
68          if (name.equals(root.getName())) {
69              result = root;
70          }
71          else if (root instanceof Container container) {
72              final Component[] children = container.getComponents();
73              for (Component component : children) {
74                  result = findComponentByName(component, name, clazz);
75                  if (result != null) {
76                      break;
77                  }
78              }
79          }
80          return clazz.cast(result);
81      }
82  
83      /**
84       * Helper method to find a component by its name.
85       *
86       * @param root the root component to start search
87       * @param name the name of component to find
88       * @return the component if found, {@code null} otherwise
89       * @noinspection unchecked
90       * @noinspectionreason unchecked - unchecked cast is ok on test code
91       */
92      protected static JComboBox<MainFrameModel.ParseMode> findComponentComboBoxByName(
93              Component root, String name) {
94          return findComponentByName(root, name, JComboBox.class);
95      }
96  }