Added method canInvokeThisMethodInAWT() to SwingTools.
[aprog.git] / Aprog / test / net / sourceforge / aprog / swing / SwingToolsTest.java
blob855154aa3e344f4542309b63acf25e6edd76c0d8
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 greg.
5 *
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
22 * THE SOFTWARE.
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;
42 /**
43 * Automated tests using JUnit 4 for {@link SwingTools}.
45 * @author codistmonk (creation 2010-06-26)
47 public final class SwingToolsTest {
49 @Test
50 public final void testGetIcon() {
51 SwingTools.setImagesBase(Tools.getCallerPackagePath());
53 assertNotNull(SwingTools.getIcon("start.png"));
56 @Test
57 public final void testGetIconOrNull() {
58 assertNull(SwingTools.getIconOrNull("inexisting_icon"));
60 SwingTools.setImagesBase(Tools.getCallerPackagePath());
62 assertNotNull(SwingTools.getIconOrNull("start.png"));
65 @Test
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());
81 @Test
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());
94 @Test
95 public final void testCenter() throws Exception {
96 if (SwingTools.canInvokeThisMethodInAWT(this)) {
97 fail("TODO");
101 @Test
102 public final void testScrollable() throws Exception {
103 if (SwingTools.canInvokeThisMethodInAWT(this)) {
104 fail("TODO");
108 @Test
109 public final void testMenuBar() throws Exception {
110 if (SwingTools.canInvokeThisMethodInAWT(this)) {
111 fail("TODO");
115 @Test
116 public final void testMenu() throws Exception {
117 if (SwingTools.canInvokeThisMethodInAWT(this)) {
118 fail("TODO");
122 @Test
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() {
133 @Override
134 public final void run() {
135 // Doesn't throw
136 SwingTools.checkAWT();
141 // Throws
142 SwingTools.checkAWT();
145 @Test
146 public final void testCheckNotAWT() throws Exception {
147 // Doesn't throw
148 SwingTools.checkNotAWT();
150 SwingUtilities.invokeAndWait(new Runnable() {
152 @Override
153 public final void run() {
154 try {
155 // Throws
156 SwingTools.checkNotAWT();
158 fail("This section wasn't supposed to be reached");
159 } catch (final IllegalStateException expectedException) {
160 // Ignore