1 /* $Id$ */
2 /***************************************************************************
3  *                   (C) Copyright 2003-2010 - Stendhal                    *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 package games.stendhal.client.gui;
14 
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18 
19 import javax.swing.JComponent;
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
22 
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 
26 import games.stendhal.client.util.UserInterfaceTestHelper;
27 
28 public class InternalWindowTest {
29 	@BeforeClass
init()30 	public static void init() {
31 		UserInterfaceTestHelper.initUserInterface();
32 	}
33 
34 	/**
35 	 * Test that setCloseable works properly, and that the window defaults to
36 	 * being closeable.
37 	 */
38 	@Test
testCloseable()39 	public void testCloseable() {
40 		InternalWindow window = new InternalWindow("Test window");
41 
42 		assertTrue("Closeable by default", window.closeButton.isVisible());
43 		// Check disabling
44 		window.setCloseable(false);
45 		assertFalse("Should not be closeable", window.closeButton.isVisible());
46 		// Check restoring
47 		window.setCloseable(true);
48 		assertTrue("Should be closeable", window.closeButton.isVisible());
49 	}
50 
51 	/**
52 	 * Test that setHideOnClose works correctly
53 	 */
54 	@Test
testSetHideOnClose()55 	public void testSetHideOnClose() {
56 		// A container where we can test the appearing and disappearing of the
57 		// window
58 		JComponent container = new JPanel();
59 		InternalWindow window = new InternalWindow("Test window");
60 
61 		assertEquals("Sanity check", container.getComponentCount(), 0);
62 		// Check default behavior
63 		container.add(window);
64 		assertEquals("Added the window", container.getComponentCount(), 1);
65 		window.closeButton.doClick();
66 		assertEquals("Window removed", container.getComponentCount(), 0);
67 
68 		// check preserving behavior
69 		container.add(window);
70 		assertEquals("Added the window", container.getComponentCount(), 1);
71 		window.setHideOnClose(true);
72 		window.closeButton.doClick();
73 		assertEquals("Window preserved", container.getComponentCount(), 1);
74 		assertFalse("Window is hidden", window.isVisible());
75 
76 		// Check restoring the default behavior
77 		window.setHideOnClose(false);
78 		window.closeButton.doClick();
79 		assertEquals("Window removed", container.getComponentCount(), 0);
80 	}
81 
82 	/**
83 	 * Test that setMinimizable works properly, and that the window defaults to
84 	 * being minimizable.
85 	 */
86 	@Test
testMinimizable()87 	public void testMinimizable() {
88 		InternalWindow window = new InternalWindow("Test window");
89 
90 		assertTrue("Minimizable by default", window.minimizeButton.isVisible());
91 		// Check disabling
92 		window.setMinimizable(false);
93 		assertFalse("Should not be minimizable", window.minimizeButton.isVisible());
94 		// Check restoring
95 		window.setMinimizable(true);
96 		assertTrue("Should be minimizable", window.minimizeButton.isVisible());
97 	}
98 
99 	/**
100 	 * Test minimizing the window using setMinimized and clicking the minimize
101 	 * button.
102 	 */
103 	@Test
testMinimizing()104 	public void testMinimizing() {
105 		InternalWindow window = new InternalWindow("Test window");
106 		JLabel content = new JLabel("Hello");
107 		window.setContent(content);
108 
109 		assertFalse("Default to visible", window.isMinimized());
110 		// check the window manager interface
111 		window.setMinimized(true);
112 		assertTrue("Should be minimized", window.isMinimized());
113 		window.setMinimized(false);
114 		assertFalse("Should be restored", window.isMinimized());
115 
116 		// check the button use
117 		window.minimizeButton.doClick();
118 		assertTrue("Should be minimized", window.isMinimized());
119 		window.minimizeButton.doClick();
120 		assertFalse("Should be restored", window.isMinimized());
121 	}
122 
123 	/**
124 	 * Test setting the title text.
125 	 */
126 	@Test
testSetTitle()127 	public void testSetTitle() {
128 		InternalWindow window = new InternalWindow("Test window");
129 		assertEquals("Initial title text", "<html>Test&nbsp;window</html>", window.titleLabel.getText());
130 		window.setTitle("Changed");
131 		// html ellipsis workaround
132 		assertEquals("Changed title text", "<html>Changed</html>", window.titleLabel.getText());
133 		// which should not be used when there are spaces in the title
134 		window.setTitle("Changed again");
135 		assertEquals("Changed title text", "<html>Changed&nbsp;again</html>", window.titleLabel.getText());
136 	}
137 }
138