001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.ColorUtils;
006: import org.uispec4j.utils.UIComponentFactory;
007: import org.uispec4j.xml.XmlAssert;
008:
009: import javax.swing.*;
010: import java.awt.*;
011:
012: public class TabGroupTest extends UIComponentTestCase {
013: private TabGroup tabGroup;
014: private JTabbedPane jTabbedPane;
015:
016: protected void setUp() throws Exception {
017: super .setUp();
018: jTabbedPane = new JTabbedPane();
019: jTabbedPane.setName("myTabbedPane");
020: addTab("1", "tab1");
021: addTab("2", "tab2");
022: addTab("3", "tab3");
023: tabGroup = new TabGroup(jTabbedPane);
024: }
025:
026: public void testGetComponentTypeName() throws Exception {
027: assertEquals("tabGroup", UIComponentFactory.createUIComponent(
028: new JTabbedPane()).getDescriptionTypeName());
029: }
030:
031: public void testGetDescription() throws Exception {
032: checkTabDescription("1");
033: checkTabDescription("2");
034: checkTabDescription("3");
035: }
036:
037: protected UIComponent createComponent() {
038: return tabGroup;
039: }
040:
041: public void testCheckCurrentTab() throws Exception {
042: assertTrue(tabGroup.selectedTabEquals("1"));
043: try {
044: assertTrue(tabGroup.selectedTabEquals("2"));
045: throw new AssertionFailureNotDetectedError();
046: } catch (AssertionFailedError e) {
047: }
048: }
049:
050: public void testClickOnTabWithPartOfItsKey() throws Exception {
051: addTab("GrosseTable", "table");
052: tabGroup.selectTab("grosse");
053: assertTrue(tabGroup.selectedTabEquals("GrosseTable"));
054: }
055:
056: public void testCheckTabs() throws Exception {
057: assertTrue(tabGroup
058: .tabNamesEquals(new String[] { "1", "2", "3" }));
059: try {
060: assertTrue(tabGroup.tabNamesEquals(new String[] { "this",
061: "is", "wrong" }));
062: throw new AssertionFailureNotDetectedError();
063: } catch (AssertionFailedError e) {
064: // Expected
065: }
066: }
067:
068: public void testSetCurrentTab() throws Exception {
069: tabGroup.selectTab("2");
070: assertEquals("2", jTabbedPane.getTitleAt(jTabbedPane
071: .getSelectedIndex()));
072: tabGroup.selectTab("3");
073: assertEquals("3", jTabbedPane.getTitleAt(jTabbedPane
074: .getSelectedIndex()));
075: }
076:
077: public void testSetCurrentTabError() throws Exception {
078: try {
079: tabGroup.selectTab("unknown");
080: throw new AssertionFailureNotDetectedError();
081: } catch (AssertionFailedError e) {
082: assertEquals(TabGroup.tabNotFound("unknown"), e
083: .getMessage());
084: }
085: }
086:
087: public void testGetDescriptionWhenTheTabContainsAPanel()
088: throws Exception {
089: JButton button = new JButton("btn");
090: JPanel panel = new JPanel();
091: panel.add(button);
092: jTabbedPane.addTab("4", panel);
093: tabGroup.selectTab("4");
094: XmlAssert.assertEquivalent("<tabGroup name='myTabbedPane'>"
095: + " <button label='btn'/>" + "</tabGroup>", tabGroup
096: .getDescription());
097: }
098:
099: public void testFactory() throws Exception {
100: checkFactory(new JTabbedPane(), TabGroup.class);
101: }
102:
103: public void testTabLabelColor() throws Exception {
104: jTabbedPane.setForegroundAt(0, Color.RED);
105: assertTrue(tabGroup.tabColorEquals(new String[] { "RED",
106: "BLACK", "BLACK" }));
107: }
108:
109: public void testCheckColorErrors() throws Exception {
110: try {
111: assertTrue(tabGroup.tabColorEquals(new String[] { "BLACK",
112: "GREEN" }));
113: throw new AssertionFailureNotDetectedError();
114: } catch (AssertionFailedError e) {
115: assertEquals(
116: "You specified 2 colors but there are 3 tabs - expected:<2> but was:<3>",
117: e.getMessage());
118: }
119:
120: try {
121: jTabbedPane.setForegroundAt(1, Color.BLACK);
122: assertTrue(tabGroup.tabColorEquals(new String[] { "BLACK",
123: "BLUE", "GREEN" }));
124: throw new AssertionFailureNotDetectedError();
125: } catch (AssertionFailedError e) {
126: assertEquals("Unexpected color for tab '2' (index 1) - "
127: + "expected "
128: + ColorUtils.getColorDescription("BLUE")
129: + " but was "
130: + ColorUtils.getColorDescription("000000"), e
131: .getMessage());
132: }
133: }
134:
135: public void testSearchComponentsWhenVisibleTabIsAPanel()
136: throws Exception {
137: JButton jButton = new JButton("button");
138: Component jPanel1WithButton = createPanelWithComponent(jButton);
139: JTable jtable = new JTable();
140: Component jPanel2WithTable = createPanelWithComponent(jtable);
141:
142: jTabbedPane = new JTabbedPane();
143: jTabbedPane.addTab("panel1WithButton", jPanel1WithButton);
144: jTabbedPane.addTab("panel2WithTable", jPanel2WithTable);
145: tabGroup = new TabGroup(jTabbedPane);
146:
147: assertSame(jPanel1WithButton, tabGroup.getSelectedTab()
148: .getAwtComponent());
149: assertSame(jButton, tabGroup.getSelectedTab().getButton(
150: "button").getAwtComponent());
151:
152: tabGroup.selectTab("panel2WithTable");
153: assertSame(jPanel2WithTable, tabGroup.getSelectedTab()
154: .getAwtComponent());
155: assertSame(jtable, tabGroup.getSelectedTab().getTable()
156: .getAwtComponent());
157: }
158:
159: public void testSearchComponentsFailsWhenVisibleTabIsNotAPanel()
160: throws Exception {
161: jTabbedPane = new JTabbedPane();
162: jTabbedPane.addTab("tree", new JTree());
163: tabGroup = new TabGroup(jTabbedPane);
164:
165: try {
166: tabGroup.getSelectedTab();
167: throw new AssertionFailureNotDetectedError();
168: } catch (AssertionFailedError e) {
169: assertEquals(
170: "tabGroup.getSelectedTab() only supports JPanel components inside a JTabbedPane",
171: e.getMessage());
172: }
173: }
174:
175: private Component createPanelWithComponent(Component component) {
176: JPanel panel = new JPanel();
177: panel.add(component);
178: return panel;
179: }
180:
181: private void checkTabDescription(String tabLabel) throws Exception {
182: tabGroup.selectTab(tabLabel);
183: XmlAssert.assertEquivalent("<tabGroup name='myTabbedPane'>"
184: + " <textBox name='tab" + tabLabel + "'/>"
185: + "</tabGroup>", tabGroup.getDescription());
186: }
187:
188: private void addTab(String index, String tabName) {
189: JLabel label = new JLabel("");
190: label.setName(tabName);
191: jTabbedPane.addTab(index, label);
192: }
193: }
|