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 org
.junit
.Assert
.*;
29 import java
.awt
.FlowLayout
;
30 import java
.awt
.GridBagConstraints
;
31 import java
.awt
.GridBagLayout
;
33 import javax
.swing
.JButton
;
34 import javax
.swing
.JFrame
;
35 import javax
.swing
.JLabel
;
36 import javax
.swing
.JPanel
;
37 import javax
.swing
.SwingUtilities
;
39 import net
.sourceforge
.aprog
.tools
.Tools
;
41 import org
.junit
.Test
;
43 * Automated tests using JUnit 4 for {@link SwingTools}.
45 * @author codistmonk (creation 2010-06-26)
47 public final class SwingToolsTest
{
50 public final void testGetIcon() {
51 SwingTools
.setImagesBase(Tools
.getCallerPackagePath());
53 assertNotNull(SwingTools
.getIcon("start.png"));
57 public final void testGetIconOrNull() {
58 assertNull(SwingTools
.getIconOrNull("inexisting_icon"));
60 SwingTools
.setImagesBase(Tools
.getCallerPackagePath());
62 assertNotNull(SwingTools
.getIconOrNull("start.png"));
66 public final void testAdd() throws Exception
{
67 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
68 final JPanel panel
= new JPanel(new FlowLayout());
69 final JLabel label
= new JLabel();
71 assertTrue(panel
.getLayout() instanceof FlowLayout
);
73 SwingTools
.add(panel
, label
, new GridBagConstraints());
75 assertTrue(panel
.getLayout() instanceof GridBagLayout
);
76 assertSame(label
, panel
.getComponents()[0]);
77 assertEquals(1, panel
.getComponentCount());
82 public final void testRollover() throws Exception
{
83 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
84 SwingTools
.setImagesBase(Tools
.getCallerPackagePath());
86 final JButton button
= SwingTools
.rollover(new JButton(), "start", false);
88 assertNotNull(button
.getIcon());
89 assertNotNull(button
.getRolloverIcon());
90 assertFalse(button
.isBorderPainted());
95 public final void testCenter() throws Exception
{
96 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
102 public final void testScrollable() throws Exception
{
103 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
109 public final void testMenuBar() throws Exception
{
110 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
116 public final void testMenu() throws Exception
{
117 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
123 public final void testCanInvokeThisMethodInAWT() {
124 if (SwingTools
.canInvokeThisMethodInAWT(this)) {
125 SwingTools
.checkAWT();
129 @Test(expected
=IllegalStateException
.class)
130 public final void testCheckAWT() throws Exception
{
131 SwingUtilities
.invokeAndWait(new Runnable() {
134 public final void run() {
136 SwingTools
.checkAWT();
142 SwingTools
.checkAWT();
146 public final void testCheckNotAWT() throws Exception
{
148 SwingTools
.checkNotAWT();
150 SwingUtilities
.invokeAndWait(new Runnable() {
153 public final void run() {
156 SwingTools
.checkNotAWT();
158 fail("This section wasn't supposed to be reached");
159 } catch (final IllegalStateException expectedException
) {