[Aprog]
[aprog.git] / Aprog / test / net / sourceforge / aprog / swing / SwingToolsTest.java
blob8e7714c4675c8e1b581ab7bce27fa5beb9fdac7a
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
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 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;
52 /**
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 {
60 @Test
61 public final void testGetIcon() {
62 SwingTools.setImagesBase(getThisPackagePath());
64 assertNotNull(SwingTools.getIcon("start.png"));
67 @Test
68 public final void testGetIconOrNull() {
69 assertNull(SwingTools.getIconOrNull("inexisting_icon"));
71 SwingTools.setImagesBase(getThisPackagePath());
73 assertNotNull(SwingTools.getIconOrNull("start.png"));
76 @Test
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());
92 @Test
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());
105 @Test
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());
115 @Test
116 public final void testMenuBar() throws Exception {
117 if (SwingTools.canInvokeThisMethodInAWT(this)) {
118 final JMenuBar menuBar = SwingTools.menuBar(
119 null,
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());
135 @Test
136 public final void testMenu() throws Exception {
137 if (SwingTools.canInvokeThisMethodInAWT(this)) {
138 final JMenu menu = SwingTools.menu("menu",
139 new JMenuItem("item1"),
140 null,
141 null,
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());
152 @Test
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);
159 assertNotNull(box);
160 assertArrayEquals(array(component1, component2), box.getComponents());
164 @Test
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);
171 assertNotNull(box);
172 assertArrayEquals(array(component1, component2), box.getComponents());
176 @Test
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());
190 @Test
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());
204 @Test
205 public final void testGetFiles() {
206 // TODO
207 debugPrint("TODO");
210 @Test
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);
223 semaphore.acquire();
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() {
234 @Override
235 public void run() {
236 actionListener.actionPerformed(new ActionEvent(this, 0, ""));
241 semaphore.acquire();
244 @Test(expected=IllegalStateException.class)
245 public final void testCheckAWT() throws Exception {
246 SwingUtilities.invokeAndWait(new Runnable() {
248 @Override
249 public final void run() {
250 // Doesn't throw
251 SwingTools.checkAWT();
256 // Throws
257 SwingTools.checkAWT();
260 @Test
261 public final void testCheckNotAWT() throws Exception {
262 // Doesn't throw
263 SwingTools.checkNotAWT();
265 SwingUtilities.invokeAndWait(new Runnable() {
267 @Override
268 public final void run() {
269 try {
270 // Throws
271 SwingTools.checkNotAWT();
273 fail("This section wasn't supposed to be reached");
274 } catch (final IllegalStateException expectedException) {
275 // Ignore
283 * {@value} milliseconds.
285 public static final long TEST_TIMEOUT = 2000L;
288 * @param semaphore
289 * <br>Not null
290 * <br>Input-output
291 * <br>Shared
293 private static final void releaseInAWT(final Semaphore semaphore) {
294 if (SwingTools.canInvokeLaterThisMethodInAWT(null, semaphore)) {
295 SwingTools.checkAWT();
297 semaphore.release();