0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Roman I. Chernyatchik
0019: * @version $Revision$
0020: */package javax.swing.text.html;
0021:
0022: import java.awt.Component;
0023: import java.awt.Container;
0024: import java.awt.Dimension;
0025: import java.awt.event.ActionEvent;
0026: import java.io.StringReader;
0027:
0028: import javax.swing.Box;
0029: import javax.swing.ButtonModel;
0030: import javax.swing.JButton;
0031: import javax.swing.JCheckBox;
0032: import javax.swing.JComboBox;
0033: import javax.swing.JEditorPane;
0034: import javax.swing.JList;
0035: import javax.swing.JPanel;
0036: import javax.swing.JPasswordField;
0037: import javax.swing.JRadioButton;
0038: import javax.swing.JScrollPane;
0039: import javax.swing.JTextArea;
0040: import javax.swing.JTextField;
0041: import javax.swing.JViewport;
0042: import javax.swing.SwingTestCase;
0043: import javax.swing.text.AttributeSet;
0044: import javax.swing.text.Document;
0045: import javax.swing.text.Element;
0046: import javax.swing.text.MutableAttributeSet;
0047: import javax.swing.text.PlainDocument;
0048: import javax.swing.text.SimpleAttributeSet;
0049: import javax.swing.text.StyleConstants;
0050: import javax.swing.text.View;
0051:
0052: import org.apache.harmony.x.swing.text.html.form.FormAttributes;
0053:
0054: public class FormViewTest extends SwingTestCase {
0055:
0056: private HTMLDocument doc;
0057:
0058: private HTMLEditorKit htmlEditorKit;
0059:
0060: private JEditorPane editorPane;
0061:
0062: private Element elem;
0063:
0064: private FormView formView;
0065:
0066: protected void setUp() throws Exception {
0067: super .setUp();
0068:
0069: setIgnoreNotImplemented(true);
0070:
0071: htmlEditorKit = new HTMLEditorKit();
0072: editorPane = new JEditorPane();
0073: editorPane.setEditorKit(htmlEditorKit);
0074: doc = (HTMLDocument) editorPane.getDocument();
0075:
0076: StringBuffer src = new StringBuffer();
0077: src.append("<HTML>");
0078: src.append("<HEAD></HEAD>");
0079: src.append("<BODY>");
0080: src.append(" Hello word!");
0081: src.append("<FORM ACTION = \"\">");
0082: src
0083: .append(" <INPUT TYPE = \"button\" NAME =\"button_name\" "
0084: + "VALUE = \"button:JButton\" ID=\"button\">");
0085: src
0086: .append(" <INPUT TYPE = \"checkbox\" NAME =\"checkbox_name\" "
0087: + "VALUE = \"checkbox:JCheckBox\" ID=\"checkbox\">");
0088: src
0089: .append(" <INPUT TYPE = \"checkbox\" NAME =\"checked "
0090: + "checkbox_name\" VALUE = \"checked checkbox:JCheckBox\" "
0091: + "CHECKED ID=\"checked checkbox\">");
0092: src.append(" <INPUT TYPE = \"image\" NAME =\"image_name\" "
0093: + "VALUE = \"image:JButton\" ID=\"image\">");
0094: src
0095: .append(" <INPUT TYPE = \"password\" NAME =\"password_name\" "
0096: + "VALUE = \"password:JPasswordField\" ID=\"password\">");
0097: src.append(" <INPUT TYPE = \"radio\" NAME =\"radio1_name\" "
0098: + "value = \"radio:JRadioButton\" ID=\"radio\">");
0099: src.append(" <INPUT TYPE = \"radio\" NAME =\"radio2_name\" "
0100: + "value = \"radio:JRadioButton\" ID=\"radio2\">");
0101: src
0102: .append(" <INPUT TYPE = \"radio\" NAME =\"checked radio_name\" "
0103: + "value = \"checked radio:JRadioButton\" CHECKED "
0104: + "ID=\"checked radio\">");
0105: src.append(" <INPUT TYPE = \"reset\" NAME =\"reset_name\" "
0106: + "VALUE = \"reset:JButton\" ID=\"reset\">");
0107: src
0108: .append(" <INPUT TYPE = \"submit\" NAME =\"submit_name\" "
0109: + "VALUE = \"submit:JButton\" ID=\"submit\">");
0110: src.append(" <INPUT TYPE = \"text\" NAME =\"text_name\" "
0111: + "VALUE = \"text:JTextField\" ID=\"text\">");
0112: src.append(" <INPUT TYPE = \"text\" "
0113: + "NAME =\"disabled_text_name\" disabled VALUE = "
0114: + "\"disabled_text:JTextField\" ID=\"disabled_text\">");
0115: src.append(" <INPUT TYPE = \"file\" NAME =\"file_name\" "
0116: + "VALUE = \"file:JTextField\" ID=\"file\">");
0117: src
0118: .append(" <SELECT NAME = \"select_name\" ID=\"select_0\">");
0119: src.append(" </SELECT>");
0120: src.append(" <SELECT NAME = \"select_name\" ID=\"select\">");
0121: src.append(" <OPTION VALUE = \"case1\" SELECTED> case1");
0122: src.append(" </SELECT>");
0123: src
0124: .append(" <SELECT NAME = \"select_name\" ID=\"select_many\">");
0125: src.append(" <OPTION VALUE = \"case1\"> case1");
0126: src.append(" <OPTION VALUE = \"case2\" SELECTED> case2");
0127: src.append(" <OPTION VALUE = \"case3\" SELECTED> case3");
0128: src.append(" </SELECT>");
0129: src.append(" <SELECT NAME = \"select_name\" MULTIPLE "
0130: + "ID=\"select_multiple\">");
0131: src.append(" <OPTION VALUE = \"case1\"> case1");
0132: src.append(" </SELECT>");
0133: src.append(" <SELECT NAME = \"select_name\" MULTIPLE "
0134: + "ID=\"select_multiple_many\">");
0135: src.append(" <OPTION VALUE = \"case1\"> case1");
0136: src.append(" <OPTION VALUE = \"case2\" SELECTED> case2");
0137: src.append(" <OPTION VALUE = \"case3\" SELECTED> case3");
0138: src.append(" </SELECT>");
0139: src
0140: .append(" <TEXTAREA NAME = \"textarea_name\" WRAP=\"virtual\" "
0141: + "COLS=\"50\" ROWS=\"5\" ID=\"textArea\">");
0142: src.append(" JTextArea in a JScrollPane");
0143: src.append(" </TEXTAREA>");
0144: src
0145: .append(" <INPUT TYPE = \"text\" NAME =\"text_maxlength\" "
0146: + "MAXLENGTH = \"10\" VALUE = \"text:JTextField\" "
0147: + "ID=\"text_maxlength\">");
0148: src
0149: .append(" <INPUT TYPE = \"submit\" NAME =\"submit3_name\" "
0150: + "VALUE = \"submit2:JButton\" ID=\"submit2\">");
0151: src.append("</FORM>");
0152: src
0153: .append(" <INPUT TYPE = \"submit\" NAME =\"submit3_name\" "
0154: + "VALUE = \"submit3:JButton\" ID=\"submit3\">");
0155: src.append("</BODY>");
0156: src.append("</HTML>");
0157: StringReader reader = new StringReader(src.toString());
0158: htmlEditorKit.read(reader, doc, 0);
0159: }
0160:
0161: public void testFormView() throws Exception {
0162:
0163: // Wrong element
0164: elem = createElement();
0165: formView = new FormView(elem);
0166: assertNull(formView.getComponent());
0167: assertSame(elem, formView.getElement());
0168: assertSame(elem.getAttributes(), formView.getAttributes());
0169: assertNull(formView.createComponent());
0170: assertNull(formView.getComponent());
0171: assertNull(getModelAttribute(elem));
0172: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0173:
0174: formView.setParent(editorPane.getUI().getRootView(editorPane));
0175: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0176: assertNull(formView.createComponent());
0177: assertNull(formView.getComponent());
0178: assertNull(getModelAttribute(elem));
0179:
0180: // Wrong element type name
0181: createFormView(FormAttributes.INPUT_TYPE_SUBMIT);
0182: MutableAttributeSet attrs = new SimpleAttributeSet();
0183: attrs.addAttribute(HTML.Attribute.TYPE, "my_type");
0184: doc.setCharacterAttributes(22, 1, attrs, false);
0185: assertEquals("my_type", elem.getAttributes().getAttribute(
0186: HTML.Attribute.TYPE));
0187: assertNull(formView.createComponent());
0188: assertNull(formView.getComponent());
0189: assertNotNull(getModelAttribute(elem));
0190: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0191:
0192: formView.setParent(editorPane.getUI().getRootView(editorPane));
0193: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0194: assertNull(formView.createComponent());
0195: assertNull(formView.getComponent());
0196: assertNotNull(getModelAttribute(elem));
0197:
0198: // Element type "password"
0199: createFormView(FormAttributes.INPUT_TYPE_PASSWORD);
0200: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0201: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0202: Object modelAttribute = getModelAttribute(elem);
0203: assertNull(formView.getComponent());
0204: assertSame(elem, formView.getElement());
0205: assertSame(elem.getAttributes(), formView.getAttributes());
0206: Component component = formView.createComponent();
0207: assertNotNull(component);
0208: assertNull(formView.getComponent());
0209: assertSame(modelAttribute, getModelAttribute(elem));
0210: assertNotSame(component, formView.createComponent());
0211: component = formView.createComponent();
0212:
0213: formView.setParent(editorPane.getUI().getRootView(editorPane));
0214: assertNotNull(component);
0215: Component cashedComponent = formView.getComponent();
0216: assertNotNull(cashedComponent);
0217: assertSame(cashedComponent, formView.getComponent());
0218: assertNotSame(component, cashedComponent);
0219: assertNotSame(component, formView.createComponent());
0220: assertNotSame(formView.getComponent(), formView
0221: .createComponent());
0222: assertSame(modelAttribute, getModelAttribute(elem));
0223: assertSame(cashedComponent, formView.getComponent());
0224:
0225: // Check component attribute
0226: createFormView(FormAttributes.INPUT_TYPE_TEXT);
0227: assertNull(StyleConstants.getComponent(elem.getAttributes()));
0228: JButton button = new JButton();
0229: attrs = new SimpleAttributeSet();
0230: StyleConstants.setComponent(attrs, button);
0231: doc.setCharacterAttributes(23, 1, attrs, false);
0232: assertNotNull(StyleConstants.getComponent(elem.getAttributes()));
0233: assertNull(formView.getComponent());
0234:
0235: formView.setParent(editorPane.getUI().getRootView(editorPane));
0236: component = formView.getComponent();
0237: assertNotSame(button, component);
0238: assertTrue(component instanceof JTextField);
0239: assertNotSame(component, StyleConstants.getComponent(elem
0240: .getAttributes()));
0241:
0242: formView.setParent(null);
0243: assertSame(component, formView.getComponent());
0244: formView.setParent(editorPane.getUI().getRootView(editorPane));
0245: assertSame(component, formView.getComponent());
0246: }
0247:
0248: public void testFormView_Models() throws Exception {
0249: createFormView(FormAttributes.INPUT_TYPE_BUTTON);
0250: if (isHarmony()) {
0251: //TODO uncomment when HTMLDocument would be implemented
0252: /*
0253: * assertNotNull(getModelAttribute(elem));
0254: */
0255: } else {
0256: assertNull(getModelAttribute(elem));
0257: }
0258:
0259: testModel(FormAttributes.INPUT_TYPE_CHECKBOX);
0260: testModel(FormAttributes.INPUT_TYPE_IMAGE);
0261: testModel(FormAttributes.INPUT_TYPE_PASSWORD);
0262: testModel(FormAttributes.INPUT_TYPE_RADIO);
0263: testModel(FormAttributes.INPUT_TYPE_RESET);
0264: testModel(FormAttributes.INPUT_TYPE_SUBMIT);
0265: testModel(FormAttributes.INPUT_TYPE_TEXT);
0266: testModel(FormAttributes.INPUT_TYPE_FILE);
0267: testModel("textArea");
0268: testModel("select_0");
0269: testModel("select");
0270: testModel("select_many");
0271: testModel("select_multiple");
0272: testModel("select_multiple_many");
0273:
0274: //HTML 4.0 innovations
0275: if (isHarmony()) {
0276: //TODO Button
0277: //TODO Select with option groups
0278: }
0279: }
0280:
0281: public void testCreateComponent() throws Exception {
0282: Component component;
0283:
0284: createFormView(FormAttributes.INPUT_TYPE_BUTTON);
0285: if (isHarmony()) {
0286: component = formView.createComponent();
0287: assertTrue(component instanceof JButton);
0288: checkCreateComponent(formView, component);
0289: } else {
0290: assertNull(formView.createComponent());
0291: formView.setParent(editorPane.getUI().getRootView(
0292: editorPane));
0293: assertNull(formView.createComponent());
0294: }
0295:
0296: createFormView(FormAttributes.INPUT_TYPE_CHECKBOX);
0297: component = formView.createComponent();
0298: assertTrue(component instanceof JCheckBox);
0299: checkCreateComponent(formView, component);
0300:
0301: createFormView(FormAttributes.INPUT_TYPE_IMAGE);
0302: component = formView.createComponent();
0303: assertTrue(component instanceof JButton);
0304: checkCreateComponent(formView, component);
0305:
0306: createFormView(FormAttributes.INPUT_TYPE_PASSWORD);
0307: component = formView.createComponent();
0308: assertTrue(component instanceof JPasswordField);
0309: checkCreateComponent(formView, component);
0310:
0311: createFormView(FormAttributes.INPUT_TYPE_RADIO);
0312: component = formView.createComponent();
0313: assertTrue(component instanceof JRadioButton);
0314: checkCreateComponent(formView, component);
0315:
0316: createFormView(FormAttributes.INPUT_TYPE_RESET);
0317: component = formView.createComponent();
0318: assertTrue(component instanceof JButton);
0319: checkCreateComponent(formView, component);
0320:
0321: createFormView(FormAttributes.INPUT_TYPE_SUBMIT);
0322: component = formView.createComponent();
0323: assertTrue(component instanceof JButton);
0324: checkCreateComponent(formView, component);
0325:
0326: createFormView(FormAttributes.INPUT_TYPE_TEXT);
0327: component = formView.createComponent();
0328: assertTrue(component instanceof JTextField);
0329: checkCreateComponent(formView, component);
0330:
0331: createFormView("text_maxlength");
0332: component = formView.createComponent();
0333: assertTrue(component instanceof JTextField);
0334: assertTrue(getModelAttribute(elem) instanceof PlainDocument);
0335:
0336: createFormView(FormAttributes.INPUT_TYPE_FILE);
0337: Container container = (Container) formView.createComponent();
0338: assertTrue(container instanceof Box);
0339: assertEquals(container.getComponentCount(), 3);
0340: assertTrue(container.getComponent(0) instanceof JTextField);
0341: assertTrue(container.getComponent(2) instanceof JButton);
0342: checkCreateComponent(formView, container);
0343:
0344: createFormView("textArea");
0345: container = (Container) formView.createComponent();
0346: assertTrue(container instanceof JScrollPane);
0347: assertEquals(container.getComponentCount(), 3);
0348: assertTrue(container.getComponent(0) instanceof JViewport);
0349: assertTrue(((JViewport) container.getComponent(0))
0350: .getComponent(0) instanceof JTextArea);
0351: checkCreateComponent(formView, container);
0352:
0353: createFormView("select_0");
0354: component = formView.createComponent();
0355: assertTrue(component instanceof JComboBox);
0356: checkCreateComponent(formView, component);
0357:
0358: createFormView("select");
0359: component = formView.createComponent();
0360: assertTrue(component instanceof JComboBox);
0361: checkCreateComponent(formView, component);
0362:
0363: createFormView("select_many");
0364: component = formView.createComponent();
0365: assertTrue(component instanceof JComboBox);
0366: checkCreateComponent(formView, component);
0367:
0368: createFormView("select_multiple");
0369: container = (Container) formView.createComponent();
0370: assertTrue(container instanceof JScrollPane);
0371: assertEquals(container.getComponentCount(), 3);
0372:
0373: assertTrue(container.getComponent(0) instanceof JViewport);
0374: assertTrue(((JViewport) container.getComponent(0))
0375: .getComponent(0) instanceof JList);
0376: checkCreateComponent(formView, container);
0377:
0378: createFormView("select_multiple_many");
0379: container = (Container) formView.createComponent();
0380: assertTrue(container instanceof JScrollPane);
0381: assertEquals(container.getComponentCount(), 3);
0382:
0383: assertTrue(container.getComponent(0) instanceof JViewport);
0384: assertTrue(((JViewport) container.getComponent(0))
0385: .getComponent(0) instanceof JList);
0386: checkCreateComponent(formView, container);
0387: }
0388:
0389: public void testCreateComponent_Models() throws Exception {
0390: Component component;
0391:
0392: createFormView(FormAttributes.INPUT_TYPE_BUTTON);
0393: component = formView.createComponent();
0394: if (isHarmony()) {
0395: assertSame(getModelAttribute(elem), ((JButton) component)
0396: .getModel());
0397:
0398: } else {
0399: assertNull(component);
0400: }
0401:
0402: createFormView(FormAttributes.INPUT_TYPE_CHECKBOX);
0403: component = formView.createComponent();
0404: assertSame(getModelAttribute(elem), ((JCheckBox) component)
0405: .getModel());
0406: checkWithoutlModel(14, FormAttributes.INPUT_TYPE_CHECKBOX);
0407: assertNotNull(((JCheckBox) formView.createComponent())
0408: .getModel());
0409:
0410: createFormView(FormAttributes.INPUT_TYPE_IMAGE);
0411: component = formView.createComponent();
0412: assertSame(getModelAttribute(elem), ((JButton) component)
0413: .getModel());
0414: checkWithoutlModel(16, FormAttributes.INPUT_TYPE_IMAGE);
0415: assertNotNull(((JButton) formView.createComponent()).getModel());
0416:
0417: createFormView(FormAttributes.INPUT_TYPE_PASSWORD);
0418: component = formView.createComponent();
0419: assertSame(getModelAttribute(elem), ((JTextField) component)
0420: .getDocument());
0421: checkWithoutlModel(17, FormAttributes.INPUT_TYPE_PASSWORD);
0422: assertNotNull(((JTextField) formView.createComponent())
0423: .getDocument());
0424:
0425: createFormView(FormAttributes.INPUT_TYPE_RADIO);
0426: component = formView.createComponent();
0427: assertSame(getModelAttribute(elem), ((JRadioButton) component)
0428: .getModel());
0429: checkWithoutlModel(18, FormAttributes.INPUT_TYPE_RADIO);
0430: assertNotNull(((JRadioButton) formView.createComponent())
0431: .getModel());
0432:
0433: createFormView(FormAttributes.INPUT_TYPE_RESET);
0434: component = formView.createComponent();
0435: assertSame(getModelAttribute(elem), ((JButton) component)
0436: .getModel());
0437: checkWithoutlModel(21, FormAttributes.INPUT_TYPE_RESET);
0438: assertNotNull(((JButton) formView.createComponent()).getModel());
0439:
0440: createFormView(FormAttributes.INPUT_TYPE_SUBMIT);
0441: component = formView.createComponent();
0442: assertSame(getModelAttribute(elem), ((JButton) component)
0443: .getModel());
0444: checkWithoutlModel(22, FormAttributes.INPUT_TYPE_SUBMIT);
0445: assertNotNull(((JButton) formView.createComponent()).getModel());
0446:
0447: createFormView(FormAttributes.INPUT_TYPE_TEXT);
0448: component = formView.createComponent();
0449: assertSame(getModelAttribute(elem), ((JTextField) component)
0450: .getDocument());
0451: checkWithoutlModel(23, FormAttributes.INPUT_TYPE_TEXT);
0452: assertNotNull(((JTextField) formView.createComponent())
0453: .getDocument());
0454:
0455: createFormView(FormAttributes.INPUT_TYPE_FILE);
0456: component = formView.createComponent();
0457: assertSame(getModelAttribute(elem),
0458: ((JTextField) ((Container) component).getComponent(0))
0459: .getDocument());
0460: checkWithoutlModel(25, FormAttributes.INPUT_TYPE_FILE);
0461: Component c = ((Box) formView.createComponent())
0462: .getComponent(0);
0463: assertNotNull(((JTextField) c).getDocument());
0464:
0465: createFormView("textArea");
0466: component = formView.createComponent();
0467: assertSame(getModelAttribute(elem),
0468: ((JTextArea) ((Container) ((Container) component)
0469: .getComponent(0)).getComponent(0))
0470: .getDocument());
0471: checkWithoutlModel(31, "textArea", "textarea");
0472: JViewport viewPort = ((JScrollPane) formView.createComponent())
0473: .getViewport();
0474: assertNotNull(((JTextArea) viewPort.getComponent(0))
0475: .getDocument());
0476:
0477: createFormView("select_0");
0478: component = formView.createComponent();
0479: assertSame(getModelAttribute(elem), ((JComboBox) component)
0480: .getModel());
0481: if (isHarmony()) {
0482: checkWithoutlModel(26, "select_0", "select");
0483: } else {
0484: testExceptionalCase(new NullPointerCase() {
0485: public void exceptionalAction() throws Exception {
0486: checkWithoutlModel(26, "select_0", "select");
0487: }
0488: });
0489: }
0490:
0491: createFormView("select");
0492: component = formView.createComponent();
0493: assertSame(getModelAttribute(elem), ((JComboBox) component)
0494: .getModel());
0495:
0496: createFormView("select_many");
0497: component = formView.createComponent();
0498: assertSame(getModelAttribute(elem), ((JComboBox) component)
0499: .getModel());
0500:
0501: createFormView("select_multiple");
0502: formView = new FormView(elem);
0503: component = formView.createComponent();
0504: assertSame(getModelAttribute(elem),
0505: ((JList) ((Container) ((Container) component)
0506: .getComponent(0)).getComponent(0)).getModel());
0507:
0508: createFormView("select_multiple_many");
0509: component = formView.createComponent();
0510: assertSame(getModelAttribute(elem),
0511: ((JList) ((Container) ((Container) component)
0512: .getComponent(0)).getComponent(0)).getModel());
0513: }
0514:
0515: public void testGetMaximumSpan() throws Exception {
0516:
0517: elem = doc.getElement(FormAttributes.INPUT_TYPE_TEXT);
0518: formView = new FormView(elem) {
0519: protected Component createComponent() {
0520: return null;
0521: }
0522: };
0523: assertNull(formView.createComponent());
0524: checkFormViewSpans(0, 0, 0, 0);
0525:
0526: final Marker getPrefferdSpanCalled = new Marker();
0527: elem = doc.getElement(FormAttributes.INPUT_TYPE_RESET);
0528: formView = new FormView(elem) {
0529: protected Component createComponent() {
0530: Component component = new JCheckBox();
0531: ((JCheckBox) component).setModel((ButtonModel) elem
0532: .getAttributes().getAttribute(
0533: StyleConstants.ModelAttribute));
0534: component.setMinimumSize(new Dimension(10, 10));
0535: component.setPreferredSize(new Dimension(20, 20));
0536: component.setMaximumSize(new Dimension(30, 30));
0537: return component;
0538: }
0539:
0540: public float getPreferredSpan(int axis) {
0541: getPrefferdSpanCalled.setOccurred();
0542: return super .getPreferredSpan(axis);
0543: }
0544: };
0545: assertFalse(getPrefferdSpanCalled.isOccurred());
0546: checkFormViewSpans(0, 0, 30, 30);
0547:
0548: // Wrong element type
0549: elem = doc.getElement(FormAttributes.INPUT_TYPE_SUBMIT);
0550: formView = new FormView(elem) {
0551: protected Component createComponent() {
0552: Component component = super .getComponent();
0553: if (component == null) {
0554: return null;
0555: }
0556: component.setMinimumSize(new Dimension(10, 10));
0557: component.setPreferredSize(new Dimension(20, 20));
0558: component.setMaximumSize(new Dimension(30, 30));
0559: return component;
0560: };
0561: };
0562: MutableAttributeSet attrs = new SimpleAttributeSet();
0563: attrs.addAttribute(HTML.Attribute.TYPE, "my_type");
0564: doc.setCharacterAttributes(22, 1, attrs, false);
0565: assertEquals("my_type", elem.getAttributes().getAttribute(
0566: HTML.Attribute.TYPE));
0567: checkFormViewSpans(0, 0, 0, 0);
0568:
0569: elem = doc.getElement(FormAttributes.INPUT_TYPE_RESET);
0570: formView = new FormView(elem) {
0571: private boolean called;
0572:
0573: protected Component createComponent() {
0574: Component component;
0575: if (!called) {
0576: component = super .createComponent();
0577: called = true;
0578: component.setMinimumSize(new Dimension(100, 100));
0579: component
0580: .setPreferredSize(new Dimension(2000, 2000));
0581: component.setMaximumSize(new Dimension(3000, 3000));
0582: } else {
0583: component = new JPanel();
0584: component.setMinimumSize(new Dimension(10, 10));
0585: component.setPreferredSize(new Dimension(20, 20));
0586: component.setMaximumSize(new Dimension(30, 30));
0587: }
0588: return component;
0589: }
0590: };
0591: checkFormViewSpans(0, 0, 2000, 2000);
0592: formView.setParent(null);
0593: checkFormViewSpans(0, 0, 2000, 2000);
0594:
0595: // Wrong element
0596: elem = createElement();
0597: formView = new FormView(elem) {
0598: protected Component createComponent() {
0599: Component component = new JPanel();
0600: component.setMinimumSize(new Dimension(10, 10));
0601: component.setPreferredSize(new Dimension(20, 20));
0602: component.setMaximumSize(new Dimension(30, 30));
0603: return component;
0604: }
0605: };
0606: checkFormViewSpans(0, 0, 30, 30);
0607: }
0608:
0609: public void testGetMaximumSpan_WithValidElements() throws Exception {
0610: createFormView(FormAttributes.INPUT_TYPE_BUTTON);
0611: checkMaxSpan(formView);
0612:
0613: createFormView(FormAttributes.INPUT_TYPE_CHECKBOX);
0614: checkMaxSpan(formView);
0615:
0616: createFormView(FormAttributes.INPUT_TYPE_IMAGE);
0617: checkMaxSpan(formView);
0618:
0619: createFormView(FormAttributes.INPUT_TYPE_PASSWORD);
0620: checkMaxSpan(formView);
0621:
0622: createFormView(FormAttributes.INPUT_TYPE_RADIO);
0623: checkMaxSpan(formView);
0624:
0625: createFormView(FormAttributes.INPUT_TYPE_RESET);
0626: checkMaxSpan(formView);
0627:
0628: createFormView(FormAttributes.INPUT_TYPE_SUBMIT);
0629: checkMaxSpan(formView);
0630:
0631: createFormView(FormAttributes.INPUT_TYPE_TEXT);
0632: checkMaxSpan(formView);
0633:
0634: createFormView(FormAttributes.INPUT_TYPE_FILE);
0635: checkMaxSpan(formView);
0636:
0637: createFormView("textArea");
0638: checkMaxSpan(formView);
0639:
0640: createFormView("select_0");
0641: checkMaxSpan(formView);
0642:
0643: createFormView("select");
0644: checkMaxSpan(formView);
0645:
0646: createFormView("select_many");
0647: checkMaxSpan(formView);
0648:
0649: createFormView("select_multiple");
0650: // getPreferred span doesn't handle
0651: checkFormViewSpans(0, 0);
0652: formView.setParent(editorPane.getUI().getRootView(editorPane));
0653:
0654: boolean notEqual = Math.abs(formView
0655: .getPreferredSpan(View.X_AXIS)
0656: - formView.getMaximumSpan(View.X_AXIS)) > 0.00001;
0657: if (isHarmony()) {
0658: assertFalse(notEqual);
0659: } else {
0660: assertTrue(notEqual);
0661: }
0662: notEqual = Math.abs(formView.getPreferredSpan(View.Y_AXIS)
0663: - formView.getMaximumSpan(View.Y_AXIS)) > 0.00001;
0664: if (isHarmony()) {
0665: assertFalse(notEqual);
0666: } else {
0667: assertTrue(notEqual);
0668: }
0669:
0670: createFormView("select_multiple_many");
0671: // getPreferred span doesn't handle
0672: checkFormViewSpans(0, 0);
0673: formView.setParent(editorPane.getUI().getRootView(editorPane));
0674: notEqual = Math.abs(formView.getPreferredSpan(View.X_AXIS)
0675: - formView.getMaximumSpan(View.X_AXIS)) > 0.00001;
0676: if (isHarmony()) {
0677: assertFalse(notEqual);
0678: } else {
0679: assertTrue(notEqual);
0680: }
0681: notEqual = Math.abs(formView.getPreferredSpan(View.Y_AXIS)
0682: - formView.getMaximumSpan(View.Y_AXIS)) > 0.00001;
0683: if (isHarmony()) {
0684: assertFalse(notEqual);
0685: } else {
0686: assertTrue(notEqual);
0687: }
0688: }
0689:
0690: public void testImageSubmit() throws Exception {
0691: throw new UnsupportedOperationException("Not implemented");
0692: }
0693:
0694: public void testSubmitData() throws Exception {
0695: throw new UnsupportedOperationException("Not implemented");
0696: }
0697:
0698: public void testActionPerformed() throws Exception {
0699: Component component;
0700:
0701: elem = doc.getElement("submit2");
0702: formView = new FormView(elem) {
0703: public void actionPerformed(ActionEvent event) {
0704: super .actionPerformed(event);
0705: }
0706:
0707: protected void submitData(String data) {
0708: super .submitData(data);
0709: }
0710: };
0711: component = formView.createComponent();
0712: JPanel panel = new JPanel() {
0713: public int getComponentCount() {
0714: return super .getComponentCount();
0715: }
0716: };
0717: panel.add(component);
0718: ((JButton) component).doClick();
0719:
0720: throw new UnsupportedOperationException("Not implemented");
0721: }
0722:
0723: public void testIsElementSuccessfull() throws Exception {
0724: StringBuffer src = new StringBuffer();
0725: src.append("<HTML><HEAD></HEAD><BODY> Test");
0726: src.append("<FORM ACTION = \"\">");
0727: src
0728: .append(" <INPUT TYPE = \"checkbox\" NAME =\"checkbox_name\" "
0729: + "VALUE = \"checkbox:JCheckBox\" ID=\"checkbox\">");
0730: src
0731: .append(" <INPUT TYPE = \"checkbox\" NAME =\"checked "
0732: + "checkbox_name\" VALUE = \"checked checkbox:JCheckBox\" "
0733: + "CHECKED ID=\"checked checkbox\">");
0734: src.append("</FORM></BODY></HTML>");
0735: StringReader reader = new StringReader(src.toString());
0736: doc = new HTMLDocument();
0737: editorPane.setDocument(doc);
0738: htmlEditorKit.read(reader, doc, 0);
0739: // 1. Test checkbox button. Successful should be only checked buttons.
0740:
0741: src = new StringBuffer();
0742: src.append("<HTML><HEAD></HEAD><BODY> Test");
0743: src.append("<FORM ACTION = \"\">");
0744: src
0745: .append(" <INPUT TYPE = \"submit\" disabled NAME =\"submit_name\""
0746: + " VALUE = \"submit:JButton\" ID=\"submit\">");
0747: src
0748: .append(" <INPUT TYPE = \"checkbox\" disabled NAME =\"checked "
0749: + "checkbox_name\" VALUE = \"checked checkbox:JCheckBox\" "
0750: + "CHECKED ID=\"checked checkbox\">");
0751: src
0752: .append(" <INPUT TYPE = \"button\" disabled NAME =\"button_name\""
0753: + " VALUE = \"button:JButton\" ID=\"button\">");
0754: src
0755: .append(" <INPUT TYPE = \"file\" disabled NAME =\"file_name\" "
0756: + "VALUE = \"file:JTextField\" ID=\"file\">");
0757: src
0758: .append(" <SELECT NAME = \"select_name\" disabled MULTIPLE "
0759: + "ID=\"select_multiple_many\">");
0760: src.append(" <OPTION VALUE = \"case1\"> case1");
0761: src.append(" <OPTION VALUE = \"case2\" SELECTED> case2");
0762: src.append(" <OPTION VALUE = \"case3\" SELECTED> case3");
0763: src.append(" </SELECT>");
0764: src.append(" <SELECT NAME = \"select_name\" MULTIPLE "
0765: + "ID=\"select_multiple_many\">");
0766: src.append(" <OPTION VALUE = \"case1\"> case1");
0767: src
0768: .append(" <OPTION VALUE = \"case2\" disabled SELECTED> case2");
0769: src.append(" </SELECT>");
0770: src
0771: .append(" <TEXTAREA NAME = \"textarea_name\" disabled WRAP="
0772: + "\"virtual\" COLS=\"50\" ROWS=\"5\" ID=\"textArea\">");
0773: src.append(" JTextArea in a JScrollPane");
0774: src.append(" </TEXTAREA>");
0775: src.append("</FORM></BODY></HTML>");
0776: reader = new StringReader(src.toString());
0777: doc = new HTMLDocument();
0778: editorPane.setDocument(doc);
0779: htmlEditorKit.read(reader, doc, 0);
0780: // TODO
0781: // 1. Test disable attribute in
0782: // a) INPUT
0783: // b) SELECT
0784: // d) OPTION
0785: // c) TEXTAREA
0786:
0787: src = new StringBuffer();
0788: src.append("<HTML><HEAD></HEAD><BODY> Test");
0789: src.append("<FORM ACTION = \"\">");
0790: src
0791: .append(" <INPUT TYPE = \"submit\" NAME =\"submit_name1\" "
0792: + "VALUE = \"submi1t:JButton\" ID=\"submit1\">");
0793: src
0794: .append(" <INPUT TYPE = \"submit\" NAME =\"submit_name2\" "
0795: + "VALUE = \"submit2:JButton\" ID=\"submit2\">");
0796: src.append("</FORM></BODY></HTML>");
0797: reader = new StringReader(src.toString());
0798: doc = new HTMLDocument();
0799: editorPane.setDocument(doc);
0800: htmlEditorKit.read(reader, doc, 0);
0801: // 1. Test submit button. Successful should be only armed button.
0802:
0803: if (isHarmony()) {
0804: /**
0805: * HTML 4.0 specific tags
0806: */
0807: src = new StringBuffer();
0808: src.append("<HTML><HEAD></HEAD><BODY> Test");
0809: src.append("<FORM ACTION = \"\">");
0810: src
0811: .append(" <BUTTON TYPE = \"submit1\" disabled "
0812: + "NAME=\"button_submit1_name\" value=\"submit1:JButton\">");
0813: src.append(" Send");
0814: src.append(" <IMG src=\"\" alt=\"alt\">");
0815: src.append(" </BUTTON>");
0816: src.append(" <SELECT NAME = \"select_name\" MULTIPLE "
0817: + "ID=\"select_multiple_many\">");
0818: src
0819: .append(" <OPTGROUP disabled label=\"Option gr1\">");
0820: src.append(" <OPTION VALUE = \"case1\"> case1");
0821: src
0822: .append(" <OPTION VALUE = \"case2\" disabled "
0823: + "SELECTED> case2");
0824: src.append(" </OPTGROUP>");
0825: src.append(" <OPTGROUP label=\"Option gr2\">");
0826: src.append(" <OPTION VALUE = \"case1\"> case1");
0827: src
0828: .append(" <OPTION VALUE = \"case2\" disabled "
0829: + "SELECTED> case2");
0830: src.append(" </OPTGROUP>");
0831: src.append(" </SELECT>");
0832: src
0833: .append(" <INPUT TYPE = \"submit\" NAME =\"submit_name2\" "
0834: + "VALUE = \"submit2:JButton\" ID=\"submit2\">");
0835: src.append("</FORM></BODY></HTML>");
0836: reader = new StringReader(src.toString());
0837: doc = new HTMLDocument();
0838: editorPane.setDocument(doc);
0839: htmlEditorKit.read(reader, doc, 0);
0840: // TODO
0841: // 1. Test disable attribute in
0842: // a) BUTTON
0843: // b) OPTGROUP
0844: // 2. Test submit button. Successful should be only armed button.
0845: }
0846:
0847: src = new StringBuffer();
0848: src.append("<HTML><HEAD></HEAD><BODY> Test");
0849: src.append("<FORM ACTION = \"\">");
0850: src
0851: .append(" <INPUT TYPE = \"checkbox\" NAME =\"checkbox_name\" "
0852: + "VALUE = \"checkbox:JCheckBox\" ID=\"checkbox\">");
0853: src
0854: .append(" <INPUT TYPE = \"checkbox\" NAME =\"checked "
0855: + "checkbox_name\" VALUE = \"checked checkbox:JCheckBox\" "
0856: + "CHECKED ID=\"checked checkbox\">");
0857: src.append("</FORM></BODY></HTML>");
0858: reader = new StringReader(src.toString());
0859: doc = new HTMLDocument();
0860: editorPane.setDocument(doc);
0861: htmlEditorKit.read(reader, doc, 0);
0862: // TODO
0863: // 1. Test checkbox button. Successful should be only checked buttons.
0864:
0865: src = new StringBuffer();
0866: src.append("<HTML><HEAD></HEAD><BODY> Test");
0867: src.append("<FORM ACTION = \"\">");
0868: src.append(" <INPUT TYPE = \"radio\" NAME =\"radio_name\" "
0869: + "value = \"radio:JRadioButton\" ID=\"radio\">");
0870: src
0871: .append(" <INPUT TYPE = \"radio\" NAME =\"radio_name\" "
0872: + "value = \"radio:JRadioButton\" CHECKED ID=\"radio\">");
0873: src
0874: .append(" <INPUT TYPE = \"radio\" NAME =\"checked radio_name\" "
0875: + "value = \"checked radio:JRadioButton\" ID=\"checked "
0876: + "radio\">");
0877: src.append("</FORM></BODY></HTML>");
0878: reader = new StringReader(src.toString());
0879: doc = new HTMLDocument();
0880: editorPane.setDocument(doc);
0881: htmlEditorKit.read(reader, doc, 0);
0882: // TODO
0883: // 1. Test radioButton.
0884: // a) For buttons with the same name successful should be only selected
0885: // button.
0886: // b) Test Unchecked buttons
0887:
0888: src = new StringBuffer();
0889: src.append("<HTML><HEAD></HEAD><BODY> Test");
0890: src.append("<FORM ACTION = \"\">");
0891: src.append(" <SELECT NAME = \"select_name\" MULTIPLE "
0892: + "ID=\"select_multiple_many\">");
0893: src.append(" <OPTION VALUE = \"case1\"> case1");
0894: src.append(" <OPTION VALUE = \"case1\"> SELECTED case2");
0895: src.append(" <OPTION VALUE = \"case1\"> SELECTED case2");
0896: src
0897: .append(" <OPTION VALUE = \"case2\" disabled SELECTED> case3");
0898: src.append(" </SELECT>");
0899: src.append("</FORM></BODY></HTML>");
0900: reader = new StringReader(src.toString());
0901: doc = new HTMLDocument();
0902: editorPane.setDocument(doc);
0903: htmlEditorKit.read(reader, doc, 0);
0904: // TODO
0905: // 1. Successful should be only selected items.
0906:
0907: src = new StringBuffer();
0908: src.append("<HTML><HEAD></HEAD><BODY> Test");
0909: src.append("<FORM ACTION = \"\">");
0910: src.append(" <INPUT TYPE = \"file\" NAME =\"file_name\" "
0911: + "VALUE = \"file:JTextField\" ID=\"file\">");
0912: src.append("</FORM></BODY></HTML>");
0913: reader = new StringReader(src.toString());
0914: doc = new HTMLDocument();
0915: editorPane.setDocument(doc);
0916: htmlEditorKit.read(reader, doc, 0);
0917: // TODO
0918: // 1. File is successful element
0919:
0920: // TODO
0921: // 1. If current value of element is unset, element may considered as
0922: // not successfull
0923:
0924: src = new StringBuffer();
0925: src.append("<HTML><HEAD></HEAD><BODY> Test");
0926: src.append("<FORM ACTION = \"\">");
0927: src.append(" <INPUT TYPE = \"reset\" NAME =\"reset_name\" "
0928: + "VALUE = \"reset:JButton\" ID=\"reset\">");
0929: src.append("</FORM></BODY></HTML>");
0930: reader = new StringReader(src.toString());
0931: doc = new HTMLDocument();
0932: editorPane.setDocument(doc);
0933: htmlEditorKit.read(reader, doc, 0);
0934: // TODO
0935: // 1. Reset isn't successful element
0936:
0937: src = new StringBuffer();
0938: src.append("<HTML><HEAD></HEAD><BODY> Test");
0939: src.append("<FORM ACTION = \"\">");
0940: src.append(" <INPUT TYPE = \"reset\" NAME =\"reset_name\" "
0941: + "VALUE = \"reset:JButton\" ID=\"reset\">");
0942: src.append("</FORM></BODY></HTML>");
0943: reader = new StringReader(src.toString());
0944: doc = new HTMLDocument();
0945: editorPane.setDocument(doc);
0946: htmlEditorKit.read(reader, doc, 0);
0947: // TODO
0948: // 1. OBJECT with attribute "declare" isn't successful element.
0949:
0950: // TODO
0951: // 1. Tests on invisible elements.
0952: throw new UnsupportedOperationException("Not implemented");
0953: }
0954:
0955: private void checkCreateComponent(final FormView formView,
0956: final Component component) {
0957:
0958: Component comp = component;
0959: assertNotSame(comp, formView.createComponent());
0960: formView.setParent(editorPane.getUI().getRootView(editorPane));
0961: assertNotNull(formView.getComponent());
0962: comp = formView.createComponent();
0963: assertNotSame(comp, formView.getComponent());
0964: assertNotSame(formView.getComponent(), StyleConstants
0965: .getComponent(elem.getAttributes()));
0966: assertNotSame(comp, StyleConstants.getComponent(formView
0967: .getElement().getAttributes()));
0968: assertNull(formView.createComponent().getParent());
0969: assertNotNull(formView.getComponent().getParent());
0970: }
0971:
0972: private void checkFormViewSpans(final double x1, final double y1) {
0973: assertEquals(x1, y1, 0.0001);
0974: }
0975:
0976: private void checkFormViewSpans(final double x1, final double y1,
0977: final double x2, final double y2) {
0978: checkFormViewSpans(x1, y1);
0979: formView.setParent(editorPane.getUI().getRootView(editorPane));
0980: checkFormViewSpans(x2, y2);
0981: }
0982:
0983: private void checkMaxSpan(final FormView formView) {
0984: checkFormViewSpans(formView.getPreferredSpan(View.X_AXIS),
0985: formView.getPreferredSpan(View.X_AXIS));
0986: checkFormViewSpans(formView.getPreferredSpan(View.Y_AXIS),
0987: formView.getPreferredSpan(View.Y_AXIS));
0988:
0989: formView.setParent(editorPane.getUI().getRootView(editorPane));
0990:
0991: checkFormViewSpans(formView.getPreferredSpan(View.X_AXIS),
0992: formView.getPreferredSpan(View.X_AXIS));
0993: checkFormViewSpans(formView.getPreferredSpan(View.Y_AXIS),
0994: formView.getPreferredSpan(View.Y_AXIS));
0995: }
0996:
0997: private void checkWithoutlModel(final int offset, final String id) {
0998: checkWithoutlModel(offset, id, id);
0999: }
1000:
1001: private void checkWithoutlModel(final int offset, final String id,
1002: final String type) {
1003: elem = doc.getElement(id);
1004: MutableAttributeSet attrs = new SimpleAttributeSet();
1005: attrs.addAttributes(elem.getAttributes());
1006: attrs.removeAttribute(StyleConstants.ModelAttribute);
1007: doc.setCharacterAttributes(offset, 1, attrs, true);
1008:
1009: assertNull(elem.getAttributes().getAttribute(
1010: StyleConstants.ModelAttribute));
1011: createFormView(id);
1012: assertNotNull(formView.createComponent());
1013: assertNull(elem.getAttributes().getAttribute(
1014: StyleConstants.ModelAttribute));
1015: }
1016:
1017: private Element createElement() {
1018: return new Element() {
1019:
1020: private MutableAttributeSet attrs = new SimpleAttributeSet();
1021:
1022: public AttributeSet getAttributes() {
1023: return attrs;
1024: }
1025:
1026: public Document getDocument() {
1027: return null;
1028: }
1029:
1030: public Element getElement(int index) {
1031: return null;
1032: }
1033:
1034: public int getElementCount() {
1035: return 0;
1036: }
1037:
1038: public int getElementIndex(int offset) {
1039: return 0;
1040: }
1041:
1042: public int getEndOffset() {
1043: return 2;
1044: }
1045:
1046: public String getName() {
1047: return "element";
1048: }
1049:
1050: public Element getParentElement() {
1051: return null;
1052: }
1053:
1054: public int getStartOffset() {
1055: return 0;
1056: }
1057:
1058: public boolean isLeaf() {
1059: return true;
1060: }
1061: };
1062: }
1063:
1064: private void createFormView(final String id) {
1065: elem = doc.getElement(id);
1066: formView = new FormView(elem);
1067: }
1068:
1069: private Object getModelAttribute(final Element element) {
1070: return element.getAttributes().getAttribute(
1071: StyleConstants.ModelAttribute);
1072: }
1073:
1074: private void testModel(String id) {
1075: createFormView(id);
1076: assertNotNull(getModelAttribute(elem));
1077: }
1078: }
|