4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.swing
;
27 import static net
.sourceforge
.aprog
.tools
.Tools
.*;
29 import static org
.junit
.Assert
.*;
31 import java
.awt
.Component
;
32 import java
.awt
.FlowLayout
;
33 import java
.awt
.GridBagConstraints
;
34 import java
.awt
.GridBagLayout
;
35 import java
.awt
.event
.ActionEvent
;
36 import java
.awt
.event
.ActionListener
;
37 import java
.util
.concurrent
.Semaphore
;
38 import javax
.swing
.Box
;
40 import javax
.swing
.JButton
;
41 import javax
.swing
.JLabel
;
42 import javax
.swing
.JMenu
;
43 import javax
.swing
.JMenuBar
;
44 import javax
.swing
.JMenuItem
;
45 import javax
.swing
.JPanel
;
46 import javax
.swing
.JScrollPane
;
47 import javax
.swing
.JSplitPane
;
48 import javax
.swing
.SwingUtilities
;
50 import org
.junit
.Test
;
53 * Automated tests using JUnit 4 for {@link SwingTools}.
54 * <br>Some methods that affect the visual appearance of components are not tested.
56 * @author codistmonk (creation 2010-06-26)
58 public final class SwingToolsTest
{
61 public final void testGetIcon() {
62 SwingTools
.setImagesBase(getThisPackagePath());
64 assertNotNull(SwingTools
.getIcon("start.png"));
68 public final void testGetIconOrNull() {
69 assertNull(SwingTools
.getIconOrNull("inexisting_icon"));
71 SwingTools
.setImagesBase(getThisPackagePath());
73 assertNotNull(SwingTools
.getIconOrNull("start.png"));
77 public final void testAdd() throws Exception
{
78 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
79 final JPanel panel
= new JPanel(new FlowLayout());
80 final JLabel label
= new JLabel();
82 assertTrue(panel
.getLayout() instanceof FlowLayout
);
84 SwingTools
.add(panel
, label
, new GridBagConstraints());
86 assertTrue(panel
.getLayout() instanceof GridBagLayout
);
87 assertSame(label
, panel
.getComponents()[0]);
88 assertEquals(1, panel
.getComponentCount());
93 public final void testRollover() throws Exception
{
94 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
95 SwingTools
.setImagesBase(getThisPackagePath());
97 final JButton button
= SwingTools
.rollover(new JButton(), "start", false);
99 assertNotNull(button
.getIcon());
100 assertNotNull(button
.getRolloverIcon());
101 assertFalse(button
.isBorderPainted());
106 public final void testScrollable() throws Exception
{
107 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
108 final Component component
= new JLabel();
109 final JScrollPane scrollPane
= SwingTools
.scrollable(component
);
111 assertSame(component
, scrollPane
.getViewport().getView());
116 public final void testMenuBar() throws Exception
{
117 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
118 final JMenuBar menuBar
= SwingTools
.menuBar(
120 SwingTools
.menu("menu1",
121 new JMenuItem("item1"),
122 new JMenuItem("item2")),
123 SwingTools
.menu("menu2",
124 new JMenuItem("item3"),
125 new JMenuItem("item4"),
126 new JMenuItem("item5")));
128 assertEquals("menu1", menuBar
.getMenu(0).getText());
129 assertEquals(2, menuBar
.getMenu(0).getItemCount());
130 assertEquals("menu2", menuBar
.getMenu(1).getText());
131 assertEquals(3, menuBar
.getMenu(1).getItemCount());
136 public final void testMenu() throws Exception
{
137 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
138 final JMenu menu
= SwingTools
.menu("menu",
139 new JMenuItem("item1"),
142 new JMenuItem("item2"));
144 assertEquals("menu", menu
.getText());
145 assertEquals(3, menu
.getItemCount());
146 assertEquals("item1", menu
.getItem(0).getText());
147 assertNull(menu
.getItem(1));
148 assertEquals("item2", menu
.getItem(2).getText());
153 public final void testHorizontalBox() {
154 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
155 final JLabel component1
= new JLabel("1");
156 final JLabel component2
= new JLabel("2");
157 final Box box
= SwingTools
.horizontalBox(component1
, component2
);
160 assertArrayEquals(array(component1
, component2
), box
.getComponents());
165 public final void testVerticalBox() {
166 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
167 final JLabel component1
= new JLabel("1");
168 final JLabel component2
= new JLabel("2");
169 final Box box
= SwingTools
.verticalBox(component1
, component2
);
172 assertArrayEquals(array(component1
, component2
), box
.getComponents());
177 public final void testHorizontalSplit() {
178 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
179 final JLabel component1
= new JLabel("1");
180 final JLabel component2
= new JLabel("2");
181 final JSplitPane splitPane
= SwingTools
.horizontalSplit(component1
, component2
);
183 assertNotNull(splitPane
);
184 assertEquals(JSplitPane
.HORIZONTAL_SPLIT
, splitPane
.getOrientation());
185 assertEquals(splitPane
, component1
.getParent());
186 assertEquals(splitPane
, component2
.getParent());
191 public final void testVerticalSplit() {
192 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
193 final JLabel component1
= new JLabel("1");
194 final JLabel component2
= new JLabel("2");
195 final JSplitPane splitPane
= SwingTools
.verticalSplit(component1
, component2
);
197 assertNotNull(splitPane
);
198 assertEquals(JSplitPane
.VERTICAL_SPLIT
, splitPane
.getOrientation());
199 assertEquals(splitPane
, component1
.getParent());
200 assertEquals(splitPane
, component2
.getParent());
205 public final void testGetFiles() {
211 public final void testCanInvokeThisMethodInAWT() {
212 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
213 SwingTools
.checkAWT();
217 @Test(timeout
=TEST_TIMEOUT
)
218 public final void testCanInvokeLaterThisMethodInAWT() throws InterruptedException
{
219 final Semaphore semaphore
= new Semaphore(0);
221 releaseInAWT(semaphore
);
226 @Test(timeout
=TEST_TIMEOUT
)
227 public final void testAction() throws InterruptedException
{
228 final Semaphore semaphore
= new Semaphore(0);
230 final ActionListener actionListener
= SwingTools
.action(this.getClass(), "releaseInAWT", semaphore
);
232 SwingUtilities
.invokeLater(new Runnable() {
236 actionListener
.actionPerformed(new ActionEvent(this, 0, ""));
244 @Test(expected
=IllegalStateException
.class)
245 public final void testCheckAWT() throws Exception
{
246 SwingUtilities
.invokeAndWait(new Runnable() {
249 public final void run() {
251 SwingTools
.checkAWT();
257 SwingTools
.checkAWT();
261 public final void testCheckNotAWT() throws Exception
{
263 SwingTools
.checkNotAWT();
265 SwingUtilities
.invokeAndWait(new Runnable() {
268 public final void run() {
271 SwingTools
.checkNotAWT();
273 fail("This section wasn't supposed to be reached");
274 } catch (final IllegalStateException expectedException
) {
283 * {@value} milliseconds.
285 public static final long TEST_TIMEOUT
= 2000L;
293 private static final void releaseInAWT(final Semaphore semaphore
) {
294 if (SwingTools
.canInvokeLaterThisMethodInAWT(null, semaphore
)) {
295 SwingTools
.checkAWT();