001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.awt.ComponentOrientation;
023: import java.io.StringReader;
024:
025: import javax.swing.JComponent;
026: import javax.swing.JEditorPane;
027: import javax.swing.JScrollPane;
028: import javax.swing.JTextArea;
029: import javax.swing.SwingTestCase;
030: import javax.swing.text.Element;
031:
032: public class FormView_FormTextAreaElementTest extends SwingTestCase {
033:
034: private static final int DEFAULT_COLS_COUNT = 20;
035: private static final int DEFAULT_ROWS_COUNT = 3;
036:
037: private HTMLDocument document;
038: private HTMLEditorKit htmlEditorKit;
039: private JEditorPane editorPane;
040:
041: private Element elem;
042:
043: private FormView formView;
044:
045: private JScrollPane scrollPane;
046: private JTextArea textArea;
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050: setIgnoreNotImplemented(true);
051:
052: htmlEditorKit = new HTMLEditorKit();
053: editorPane = new JEditorPane();
054: editorPane.setEditorKit(htmlEditorKit);
055: document = (HTMLDocument) editorPane.getDocument();
056: }
057:
058: public void testCreateComponent() throws Exception {
059: createHTMLSample();
060:
061: //ROWS, COLUMNS
062: createFormViewWithParent("textarea");
063: assertEquals(DEFAULT_COLS_COUNT, textArea.getColumns());
064:
065: if (isHarmony()) {
066: assertEquals(DEFAULT_ROWS_COUNT, textArea.getRows());
067: } else {
068: assertEquals(1, textArea.getRows());
069: }
070:
071: if (isHarmony()) {
072: assertEquals(scrollPane.getPreferredSize(), scrollPane
073: .getMaximumSize());
074: assertEquals(scrollPane.getPreferredSize(), scrollPane
075: .getMinimumSize());
076: }
077: assertEquals("", textArea.getText());
078:
079: createFormViewWithParent("textarea_cols");
080: assertEquals(30, textArea.getColumns());
081:
082: createFormViewWithParent("textarea_rows");
083: assertEquals(7, textArea.getRows());
084:
085: if (isHarmony()) {
086: // READONLY
087: createFormViewWithParent("textarea_readonly");
088: assertEquals(30, textArea.getColumns());
089: assertEquals(7, textArea.getRows());
090: assertFalse(textArea.isEditable());
091:
092: // DISABLED
093: createFormViewWithParent("textarea_disabled");
094: assertFalse(textArea.isEnabled());
095:
096: // TITLE
097: createFormViewWithParent("textarea_title");
098: assertEquals("title_title", textArea.getToolTipText());
099:
100: //DIR
101: createFormViewWithParent("textarea_rtl");
102: assertEquals(ComponentOrientation.RIGHT_TO_LEFT, textArea
103: .getComponentOrientation());
104: }
105:
106: //ALIGN
107: createFormViewWithParent("textarea_align_top");
108: assertEquals(JComponent.CENTER_ALIGNMENT, textArea
109: .getAlignmentX(), 0.0001);
110: assertEquals(JComponent.CENTER_ALIGNMENT, scrollPane
111: .getAlignmentX(), 0.0001);
112: assertEquals(JComponent.CENTER_ALIGNMENT, textArea
113: .getAlignmentY(), 0.0001);
114: assertEquals(JComponent.BOTTOM_ALIGNMENT, scrollPane
115: .getAlignmentY(), 0.0001);
116: }
117:
118: private void createFormViewWithParent(final String id) {
119: elem = document.getElement(id);
120: formView = new FormView(elem);
121: formView.setParent(editorPane.getUI().getRootView(editorPane));
122: scrollPane = (JScrollPane) formView.createComponent();
123: textArea = (JTextArea) scrollPane.getViewport().getComponent(0);
124: }
125:
126: private void createHTMLSample() throws Exception {
127: StringBuffer htmlSrc = new StringBuffer();
128: htmlSrc.append("<HTML>");
129: htmlSrc.append("<HEAD></HEAD>");
130: htmlSrc.append("<BODY>");
131: htmlSrc.append(" Hello word!");
132: htmlSrc.append("<FORM ACTION = \"\">");
133: htmlSrc.append(" <TEXTAREA " + "ID=\"textarea\">"
134: + "</TEXTAREA>");
135: htmlSrc.append(" <TEXTAREA "
136: + "NAME =\"textarea_name_only\" "
137: + "ID=\"textarea_name_only\">" + "</TEXTAREA>");
138: htmlSrc.append(" <TEXTAREA " + "NAME =\"textarea_name\" "
139: + "COLS=\"30\"" + "ID=\"textarea_cols\">"
140: + "textarea_cols" + "</TEXTAREA>");
141: htmlSrc.append(" <TEXTAREA " + "NAME =\"textarea_name\" "
142: + "ROWS=\"7\"" + "ID=\"textarea_rows\">"
143: + "textarea_rows" + "</TEXTAREA>");
144: htmlSrc.append(" <TEXTAREA " + "NAME =\"textarea_name\" "
145: + "ROWS=\"7\" COLS=\"30\"" + "ID=\"textarea_default\">"
146: + "textarea_default" + "</TEXTAREA>");
147: htmlSrc.append(" <TEXTAREA " + "READONLY "
148: + "NAME =\"textarea_name\" " + "ROWS=\"7\" COLS=\"30\""
149: + "ID=\"textarea_readonly\">" + "textarea_readonly"
150: + "</TEXTAREA>");
151: htmlSrc.append(" <TEXTAREA " + "DISABLED "
152: + "NAME =\"textarea_name\" " + "ROWS=\"7\" COLS=\"30\""
153: + "ID=\"textarea_disabled\">" + "textarea_disabled"
154: + "</TEXTAREA>");
155: htmlSrc.append(" <TEXTAREA " + "TABINDEX = \"1\" "
156: + "NAME =\"textarea_name\" "
157: + "ID=\"textarea_tabindex\">" + "textarea_tabindex"
158: + "</TEXTAREA>");
159: htmlSrc.append(" <TEXTAREA " + "ACCESSKEY=\"U\" "
160: + "NAME =\"textarea_name\" "
161: + "ID=\"textarea_accesskey\">" + "textarea_accesskey"
162: + "</TEXTAREA>");
163: htmlSrc.append(" <TEXTAREA " + "TITLE = \"title_title\" "
164: + "NAME =\"textarea_name\" " + "ID=\"textarea_title\">"
165: + "textarea_title" + "</TEXTAREA>");
166: htmlSrc.append(" <TEXTAREA " + "DIR = \"rtl\" "
167: + "NAME =\"textarea_name\" " + "ID=\"textarea_rtl\">"
168: + "textarea_rtl" + "</TEXTAREA>");
169: htmlSrc.append(" <TEXTAREA " + "NAME =\"textarea_name\" "
170: + "VALUE = \"textarea_value\" "
171: + "ROWS=\"7\" COLS=\"30\"" + "ID=\"textarea_value\">"
172: + "</TEXTAREA>");
173: htmlSrc.append(" <TEXTAREA "
174: + "NAME =\"textarea_align_top\" "
175: + "VALUE = \"textarea_align_top\" "
176: + "ROWS=\"7\" COLS=\"30\"" + "ALIGN = \"top\""
177: + "ID=\"textarea_align_top\">" + "top" + "</TEXTAREA>");
178: htmlSrc.append("</FORM>");
179: htmlSrc.append("</BODY>");
180: htmlSrc.append("</HTML>");
181:
182: StringReader reader = new StringReader(htmlSrc.toString());
183: htmlEditorKit.read(reader, document, 0);
184: }
185: }
|