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
;
39 import javax
.swing
.JButton
;
40 import javax
.swing
.JLabel
;
41 import javax
.swing
.JMenu
;
42 import javax
.swing
.JMenuBar
;
43 import javax
.swing
.JMenuItem
;
44 import javax
.swing
.JPanel
;
45 import javax
.swing
.JScrollPane
;
46 import javax
.swing
.SwingUtilities
;
48 import org
.junit
.Test
;
51 * Automated tests using JUnit 4 for {@link SwingTools}.
52 * <br>Some methods that affect the visual appearance of components are not tested.
54 * @author codistmonk (creation 2010-06-26)
56 public final class SwingToolsTest
{
59 public final void testGetIcon() {
60 SwingTools
.setImagesBase(getThisPackagePath());
62 assertNotNull(SwingTools
.getIcon("start.png"));
66 public final void testGetIconOrNull() {
67 assertNull(SwingTools
.getIconOrNull("inexisting_icon"));
69 SwingTools
.setImagesBase(getThisPackagePath());
71 assertNotNull(SwingTools
.getIconOrNull("start.png"));
75 public final void testAdd() throws Exception
{
76 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
77 final JPanel panel
= new JPanel(new FlowLayout());
78 final JLabel label
= new JLabel();
80 assertTrue(panel
.getLayout() instanceof FlowLayout
);
82 SwingTools
.add(panel
, label
, new GridBagConstraints());
84 assertTrue(panel
.getLayout() instanceof GridBagLayout
);
85 assertSame(label
, panel
.getComponents()[0]);
86 assertEquals(1, panel
.getComponentCount());
91 public final void testRollover() throws Exception
{
92 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
93 SwingTools
.setImagesBase(getThisPackagePath());
95 final JButton button
= SwingTools
.rollover(new JButton(), "start", false);
97 assertNotNull(button
.getIcon());
98 assertNotNull(button
.getRolloverIcon());
99 assertFalse(button
.isBorderPainted());
104 public final void testScrollable() throws Exception
{
105 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
106 final Component component
= new JLabel();
107 final JScrollPane scrollPane
= SwingTools
.scrollable(component
);
109 assertSame(component
, scrollPane
.getViewport().getView());
114 public final void testMenuBar() throws Exception
{
115 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
116 final JMenuBar menuBar
= SwingTools
.menuBar(
118 SwingTools
.menu("menu1",
119 new JMenuItem("item1"),
120 new JMenuItem("item2")),
121 SwingTools
.menu("menu2",
122 new JMenuItem("item3"),
123 new JMenuItem("item4"),
124 new JMenuItem("item5")));
126 assertEquals("menu1", menuBar
.getMenu(0).getText());
127 assertEquals(2, menuBar
.getMenu(0).getItemCount());
128 assertEquals("menu2", menuBar
.getMenu(1).getText());
129 assertEquals(3, menuBar
.getMenu(1).getItemCount());
134 public final void testMenu() throws Exception
{
135 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
136 final JMenu menu
= SwingTools
.menu("menu",
137 new JMenuItem("item1"),
140 new JMenuItem("item2"));
142 assertEquals("menu", menu
.getText());
143 assertEquals(3, menu
.getItemCount());
144 assertEquals("item1", menu
.getItem(0).getText());
145 assertNull(menu
.getItem(1));
146 assertEquals("item2", menu
.getItem(2).getText());
151 public final void testGetFiles() {
156 public final void testCanInvokeThisMethodInAWT() {
157 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
158 SwingTools
.checkAWT();
162 @Test(timeout
=TEST_TIMEOUT
)
163 public final void testCanInvokeLaterThisMethodInAWT() throws InterruptedException
{
164 final Semaphore semaphore
= new Semaphore(0);
166 releaseInAWT(semaphore
);
171 @Test(timeout
=TEST_TIMEOUT
)
172 public final void testAction() throws InterruptedException
{
173 final Semaphore semaphore
= new Semaphore(0);
175 final ActionListener actionListener
= SwingTools
.action(this.getClass(), "releaseInAWT", semaphore
);
177 SwingUtilities
.invokeLater(new Runnable() {
181 actionListener
.actionPerformed(new ActionEvent(this, 0, ""));
189 @Test(expected
=IllegalStateException
.class)
190 public final void testCheckAWT() throws Exception
{
191 SwingUtilities
.invokeAndWait(new Runnable() {
194 public final void run() {
196 SwingTools
.checkAWT();
202 SwingTools
.checkAWT();
206 public final void testCheckNotAWT() throws Exception
{
208 SwingTools
.checkNotAWT();
210 SwingUtilities
.invokeAndWait(new Runnable() {
213 public final void run() {
216 SwingTools
.checkNotAWT();
218 fail("This section wasn't supposed to be reached");
219 } catch (final IllegalStateException expectedException
) {
228 * {@value} milliseconds.
230 public static final long TEST_TIMEOUT
= 2000L;
233 * XXX this method should be private, but Tools.invoke() doesn't seem to work as expected.
240 public static final void releaseInAWT(final Semaphore semaphore
) {
241 if (SwingTools
.canInvokeLaterThisMethodInAWT(null, semaphore
)) {
242 SwingTools
.checkAWT();