001: package org.uispec4j;
002:
003: import org.uispec4j.utils.UIComponentFactory;
004: import org.uispec4j.xml.XmlAssert;
005: import org.uispec4j.finder.ComponentMatcher;
006:
007: import javax.swing.*;
008: import java.awt.Component;
009:
010: public class PanelTest extends UIComponentTestCase {
011:
012: public void testGetComponentTypeName() throws Exception {
013: assertEquals("panel", UIComponentFactory.createUIComponent(
014: new JPanel()).getDescriptionTypeName());
015: }
016:
017: public void testGetDescription() throws Exception {
018: JTabbedPane tabbedPane = new JTabbedPane();
019: tabbedPane.setName("myTabbedPane");
020:
021: JPanel jPanel = new JPanel();
022: jPanel.setName("myPanel");
023: jPanel.add(tabbedPane);
024:
025: JTextField textField = new JTextField();
026: textField.setName("myText");
027: tabbedPane.addTab("1", textField);
028: tabbedPane.addTab("2", new JButton("myButton"));
029:
030: Panel panel = new Panel(jPanel);
031: XmlAssert.assertEquivalent("<panel name='myPanel'>"
032: + " <tabGroup name='myTabbedPane'>"
033: + " <textBox name='myText'/>" + " </tabGroup>"
034: + "</panel>", panel.getDescription());
035: }
036:
037: public void testFactory() throws Exception {
038: checkFactory(new JPanel(), Panel.class);
039: }
040:
041: protected UIComponent createComponent() {
042: return new Panel(new JPanel());
043: }
044:
045: public void testContainsLabel() throws Exception {
046: JPanel jPanel = new JPanel();
047: jPanel.add(new JLabel("Some text"));
048: Panel panel = new Panel(jPanel);
049: assertTrue(panel.containsLabel("Some text"));
050: assertTrue(panel.containsLabel("text"));
051: assertFalse(panel.containsLabel("unknown"));
052: }
053:
054: public void testGetSpinnerThroughModel() throws Exception {
055: checkGetSpinnerByModel(new SpinnerDateModel(), new Getter() {
056: public UIComponent get(Panel panel) {
057: return panel.getDateSpinner();
058: }
059: });
060: checkGetSpinnerByModel(new SpinnerListModel(), new Getter() {
061: public UIComponent get(Panel panel) {
062: return panel.getListSpinner();
063: }
064: });
065: checkGetSpinnerByModel(new SpinnerNumberModel(), new Getter() {
066: public UIComponent get(Panel panel) {
067: return panel.getNumberSpinner();
068: }
069: });
070: }
071:
072: public void testGetSpinnerThroughModelAndComponentName()
073: throws Exception {
074: checkGetSpinnerByModel(new SpinnerDateModel(), new Getter() {
075: public UIComponent get(Panel panel) {
076: return panel.getDateSpinner("spinner1");
077: }
078: });
079: checkGetSpinnerByModel(new SpinnerListModel(), new Getter() {
080: public UIComponent get(Panel panel) {
081: return panel.getListSpinner("spinner1");
082: }
083: });
084: checkGetSpinnerByModel(new SpinnerNumberModel(), new Getter() {
085: public UIComponent get(Panel panel) {
086: return panel.getNumberSpinner("spinner1");
087: }
088: });
089: }
090:
091: public void testGetSpinnerThroughModelAndMatcher() throws Exception {
092: final ComponentMatcher myMatcher = new ComponentMatcher() {
093: public boolean matches(Component component) {
094: return true;
095: }
096: };
097: checkGetSpinnerByModel(new SpinnerDateModel(), new Getter() {
098: public UIComponent get(Panel panel) {
099: return panel.getDateSpinner(myMatcher);
100: }
101: });
102: checkGetSpinnerByModel(new SpinnerListModel(), new Getter() {
103: public UIComponent get(Panel panel) {
104: return panel.getListSpinner(myMatcher);
105: }
106: });
107: checkGetSpinnerByModel(new SpinnerNumberModel(), new Getter() {
108: public UIComponent get(Panel panel) {
109: return panel.getNumberSpinner(myMatcher);
110: }
111: });
112: }
113:
114: private void checkGetSpinnerByModel(SpinnerModel model,
115: Getter getter) {
116: JPanel jPanel = new JPanel();
117: jPanel.add(new JSpinner(new MySpinnerModel()));
118:
119: Panel panel = new Panel(jPanel);
120: try {
121: getter.get(panel);
122: fail();
123: } catch (ItemNotFoundException e) {
124: assertEquals("No component found", e.getMessage());
125: }
126:
127: JSpinner jSpinner = new JSpinner(model);
128: jSpinner.setName("spinner1");
129: jPanel.add(jSpinner);
130: assertSame(jSpinner, getter.get(panel).getAwtComponent());
131: }
132:
133: private interface Getter {
134: UIComponent get(Panel panel);
135: }
136:
137: private static class MySpinnerModel extends AbstractSpinnerModel {
138: public Object getNextValue() {
139: return null;
140: }
141:
142: public Object getPreviousValue() {
143: return null;
144: }
145:
146: public Object getValue() {
147: return null;
148: }
149:
150: public void setValue(Object value) {
151: }
152: }
153: }
|