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.JComboBox;
026: import javax.swing.JEditorPane;
027: import javax.swing.JList;
028: import javax.swing.JScrollPane;
029: import javax.swing.SwingTestCase;
030: import javax.swing.text.Element;
031:
032: public class FormView_FormSelectElementTest extends SwingTestCase {
033:
034: private HTMLDocument document;
035: private HTMLEditorKit htmlEditorKit;
036: private JEditorPane editorPane;
037:
038: private Element elem;
039:
040: private FormView formView;
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044:
045: setIgnoreNotImplemented(true);
046:
047: htmlEditorKit = new HTMLEditorKit();
048: editorPane = new JEditorPane();
049: editorPane.setEditorKit(htmlEditorKit);
050: document = (HTMLDocument) editorPane.getDocument();
051: }
052:
053: public void testModelAttributes() throws Exception {
054: createHTMLSample();
055:
056: }
057:
058: public void testCreateComponent() throws Exception {
059: createHTMLSample();
060: createFormViewWithParent("option_disabled_attribute");
061: JComboBox comboBox = (JComboBox) formView.getComponent();
062: assertFalse(comboBox.isEditable());
063:
064: createFormViewWithParent("select_with_dir_title");
065: comboBox = (JComboBox) formView.getComponent();
066:
067: if (isHarmony()) {
068: assertEquals("TITLE", comboBox.getToolTipText());
069: assertEquals(ComponentOrientation.RIGHT_TO_LEFT, comboBox
070: .getComponentOrientation());
071: }
072:
073: }
074:
075: public void testCreateMultipleSelectionComponent() throws Exception {
076: createHTMLSample();
077:
078: createFormViewWithParent("select_multiple_name_only");
079: JScrollPane pane = (JScrollPane) formView.getComponent();
080: JList list = (JList) pane.getViewport().getComponent(0);
081: int[] selectedIndices = list.getSelectedIndices();
082: assertEquals(0, selectedIndices.length);
083:
084: createFormViewWithParent("select_multiple");
085: pane = (JScrollPane) formView.getComponent();
086: list = (JList) pane.getViewport().getComponent(0);
087: selectedIndices = list.getSelectedIndices();
088: assertEquals(3, selectedIndices.length);
089: assertEquals(0, selectedIndices[0]);
090: assertEquals(1, selectedIndices[1]);
091: assertEquals(4, selectedIndices[2]);
092:
093: //HTML 4.0 feature
094: if (isHarmony()) {
095: createFormViewWithParent("select_optiongroup");
096: pane = (JScrollPane) formView.getComponent();
097: list = (JList) pane.getViewport().getComponent(0);
098: selectedIndices = list.getSelectedIndices();
099: assertEquals(2, selectedIndices.length);
100: assertEquals(0, selectedIndices[0]);
101: assertEquals(2, selectedIndices[1]);
102: }
103: }
104:
105: public void testCreateSimpleSelectionComponent() throws Exception {
106: createHTMLSample();
107:
108: createFormViewWithParent("select");
109: JComboBox comboBox1 = (JComboBox) formView.getComponent();
110: createFormViewWithParent("select_name_only");
111: JComboBox comboBox2 = (JComboBox) formView.getComponent();
112:
113: assertEquals(comboBox1.getMinimumSize(), comboBox1
114: .getPreferredSize());
115: if (isHarmony()) {
116: assertEquals(comboBox1.getMaximumSize(), comboBox1
117: .getPreferredSize());
118: }
119:
120: assertEquals(comboBox1.getPreferredSize(), comboBox2
121: .getPreferredSize());
122:
123: createFormViewWithParent("select_simple_options");
124: comboBox1 = (JComboBox) formView.getComponent();
125: assertEquals(comboBox1.getSelectedIndex(), 0);
126:
127: createFormViewWithParent("options_label_attr");
128: comboBox1 = (JComboBox) formView.getComponent();
129: assertEquals(comboBox1.getSelectedIndex(), 0);
130:
131: createFormViewWithParent("selected_disabled_options1");
132: comboBox1 = (JComboBox) formView.getComponent();
133: assertEquals(comboBox1.getSelectedIndex(), 1);
134:
135: createFormViewWithParent("selected_disabled_options2");
136: comboBox1 = (JComboBox) formView.getComponent();
137: assertEquals(comboBox1.getSelectedIndex(), 1);
138:
139: if (isHarmony()) {
140: createFormViewWithParent("option_disabled_attribute");
141: comboBox1 = (JComboBox) formView.getComponent();
142: assertEquals(comboBox1.getSelectedIndex(), 4);
143: }
144: }
145:
146: private void createFormViewWithParent(final String id) {
147: elem = document.getElement(id);
148: formView = new FormView(elem);
149: formView.setParent(editorPane.getUI().getRootView(editorPane));
150: }
151:
152: private void createHTMLSample() throws Exception {
153: StringBuffer htmlSrc = new StringBuffer();
154: htmlSrc.append("<HTML>");
155: htmlSrc.append("<HEAD></HEAD>");
156: htmlSrc.append("<BODY>");
157: htmlSrc.append(" Hello word!");
158: htmlSrc.append("<FORM ACTION = \"\">");
159: htmlSrc.append(" <SELECT " + "ID=\"select\">" + "</SELECT>");
160: htmlSrc.append(" <SELECT " + "NAME =\"select_name_only\" "
161: + "ID=\"select_name_only\">" + "</SELECT>");
162: htmlSrc.append(" <SELECT multiple "
163: + "NAME =\"select_multiple_name_only\" "
164: + "ID=\"select_multiple_name_only\">" + "</SELECT>");
165: htmlSrc.append(" <SELECT size = 1 "
166: + "NAME =\"select_size1_name_only\" "
167: + "ID=\"select_size1_name_only\">" + "</SELECT>");
168: htmlSrc.append(" <SELECT size = 5 "
169: + "NAME =\"select_size5_name_only\" "
170: + "ID=\"select_size5_name_only\">" + "</SELECT>");
171: htmlSrc.append(" <SELECT " + "SIZE =\"10\" "
172: + "ID=\"select_size_only\">" + "</SELECT>");
173: htmlSrc.append(" <SELECT "
174: + "NAME =\"select_simple_options\" "
175: + "ID=\"select_simple_options\">" + " <OPTION>"
176: + " text1" + " </OPTION>"
177: + " <OPTION> text2" + " </OPTION>"
178: + " <OPTION> text3" + " </OPTION>"
179: + "</SELECT>");
180: htmlSrc.append(" <SELECT dir =\"RTL\" title = \"TITLE\" "
181: + "NAME =\"select_with_dir_title\" "
182: + "ID=\"select_with_dir_title\">" + " <OPTION>"
183: + " text1" + " </OPTION>"
184: + " <OPTION> text2" + " </OPTION>"
185: + " <OPTION> text3" + " </OPTION>"
186: + "</SELECT>");
187: htmlSrc.append(" <SELECT "
188: + "NAME =\"options_label_attr\" "
189: + "ID=\"options_label_attr\">"
190: + " <OPTION value = \"val1\" label = \"lab1\">"
191: + " text1" + " </OPTION>"
192: + " <OPTION value = \"val2\" label = \"lab2\"> text2"
193: + " </OPTION>"
194: + " <OPTION label = \"lab3\"> text3"
195: + " </OPTION>" + "</SELECT>");
196: htmlSrc
197: .append(" <SELECT "
198: + "NAME =\"selected_disabled_options1\" "
199: + "ID=\"selected_disabled_options1\">"
200: + " <OPTION disabled value = \"val1\" label = \"lab1\">"
201: + " text1"
202: + " </OPTION>"
203: + " <OPTION disabled selected value = \"val2\", "
204: + " label = \"lab2\"> text2"
205: + " </OPTION>"
206: + " <OPTION label = \"lab3\"> text3"
207: + " </OPTION>" + "</SELECT>");
208: htmlSrc
209: .append(" <SELECT "
210: + "NAME =\"selected_disabled_options2\" "
211: + "ID=\"selected_disabled_options2\">"
212: + " <OPTION disabled selected = \"val1\" label = \"lab1\">"
213: + " text1"
214: + " </OPTION>"
215: + " <OPTION disabled selected value = \"val2\" "
216: + " label = \"lab2\"> text2"
217: + " </OPTION>"
218: + " <OPTION label = \"lab3\"> text3"
219: + " </OPTION>" + "</SELECT>");
220: htmlSrc
221: .append(" <SELECT "
222: + "NAME =\"incorrect_selection\" "
223: + "ID=\"incorrect_selection\">"
224: + " <OPTION selected value = \"val1\" label = \"lab1\">"
225: + " text1" + " </OPTION>"
226: + " <OPTION selected value = \"val2\" "
227: + " label = \"lab2\"> text2"
228: + " </OPTION>"
229: + " <OPTION label = \"lab3\"> text3"
230: + " </OPTION>" + "</SELECT>");
231: htmlSrc.append(" <SELECT "
232: + "NAME =\"option_dir_attribute\" "
233: + "ID=\"option_dir_attribute\">"
234: + " <OPTION value = \"val1\" label = \"lab1\" "
235: + " dir = \"rtl\">" + " text1"
236: + " </OPTION>"
237: + " <OPTION value = \"val1\" dir = \"rtl\">"
238: + " text1" + " </OPTION>"
239: + " <OPTION value = \"val2\" dir = \"rtl\" "
240: + " label = \"lab2\"> text2"
241: + " </OPTION>"
242: + " <OPTION value = \"val2\" dir = \"rtl\"> text2"
243: + " </OPTION>"
244: + " <OPTION dir = \"rtl\"> text3"
245: + " </OPTION>" + "</SELECT>");
246: htmlSrc
247: .append(" <SELECT "
248: + "NAME =\"option_title_attribute\" "
249: + "ID=\"option_title_attribute\">"
250: + " <OPTION value = \"val1\" label = \"lab1\" "
251: + " title = \"title1\">"
252: + " text1"
253: + " </OPTION>"
254: + " <OPTION value = \"val1\" title = \"title2\">"
255: + " text1"
256: + " </OPTION>"
257: + " <OPTION value = \"val2\" title = \"title3\" "
258: + " label = \"lab2\"> text2"
259: + " </OPTION>"
260: + " <OPTION value = \"val2\" title = \"title4\"> text2"
261: + " </OPTION>" + "</SELECT>");
262: htmlSrc.append(" <SELECT "
263: + "NAME =\"option_disabled_attribute\" "
264: + "ID=\"option_disabled_attribute\">"
265: + " <OPTION value = \"val1\" label = \"lab1\" "
266: + " title = \"title1\" disabled>"
267: + " text1" + " </OPTION>"
268: + " <OPTION value = \"val2\" disabled>"
269: + " text1" + " </OPTION>"
270: + " <OPTION value = \"val3\" title = \"title3\" "
271: + " disabled label = \"lab2\"> text2"
272: + " </OPTION>"
273: + " <OPTION value = \"val4\" disabled> text2"
274: + " </OPTION>" + " <OPTION> text3"
275: + " </OPTION>" + "</SELECT>");
276: htmlSrc.append(" <SELECT disabled "
277: + "NAME =\"select_disabled\" "
278: + "ID=\"select_disabled\">"
279: + " <OPTION value = \"val1\" label = \"lab1\">"
280: + " text1" + " </OPTION>"
281: + " <OPTION value = \"val2\" label = \"lab2\"> text2"
282: + " </OPTION>"
283: + " <OPTION label = \"lab3\"> text3"
284: + " </OPTION>" + "</SELECT>");
285: htmlSrc
286: .append(" <SELECT multiple "
287: + "NAME =\"select_multiple\" "
288: + "ID=\"select_multiple\">"
289: + " <OPTION selected value = \"val1\" label = \"lab1\">"
290: + " text1"
291: + " </OPTION>"
292: + " <OPTION selected value = \"val2\" label = \"lab2\">"
293: + " text2" + " </OPTION>"
294: + " <OPTION label = \"lab3\"> text4"
295: + " </OPTION>"
296: + " <OPTION label = \"lab3\"> text5"
297: + " </OPTION>"
298: + " <OPTION selected > very_long_text"
299: + " </OPTION>" + "</SELECT>");
300: htmlSrc
301: .append(" <SELECT multiple "
302: + "NAME =\"select_multiple\" "
303: + "ID=\"select_optiongroup\">"
304: + " <OPTION selected value = \"val1\" label = \"lab1\">"
305: + " text1"
306: + " </OPTION>"
307: + " <OPTIONGROUP selected value = \"val2\" label = \"lab2\">"
308: + " <OPTION label = \"lab3\"> text4 </OPTION>"
309: + " <OPTION label = \"lab3\" selected> text5 </OPTION>"
310: + " </OPTIONGROUP>" + "</SELECT>");
311: htmlSrc
312: .append(" <SELECT size = 1 "
313: + "NAME =\"incorrect_select_size1\" "
314: + "ID=\"incorrect_select_size1\">"
315: + " <OPTION selected value = \"val1\" label = \"lab1\">"
316: + " text1"
317: + " </OPTION>"
318: + " <OPTION selected value = \"val2\" label = \"lab2\"> text2"
319: + " </OPTION>"
320: + " <OPTION selected label = \"lab3\"> text3"
321: + " </OPTION>" + "</SELECT>");
322: htmlSrc.append(" <SELECT size = 1 "
323: + "NAME =\"select_size1\" " + "ID=\"select_size1\">"
324: + " <OPTION value = \"val1\" label = \"lab1\">"
325: + " text1" + " </OPTION>"
326: + " <OPTION value = \"val2\" label = \"lab2\"> text2"
327: + " </OPTION>"
328: + " <OPTION label = \"lab3\"> text3"
329: + " </OPTION>" + "</SELECT>");
330: htmlSrc
331: .append(" <SELECT "
332: + "NAME =\"complex_content\" "
333: + "ID=\"complex_content\">"
334: + " <OPTION value = \"val1\" >"
335: + " <TABLE>"
336: + " <TR><TH>text1<TD>val1"
337: + " <TR><TH>text2<TD>val2"
338: + " </TABLE>"
339: + " </OPTION>"
340: + " <OPTION value = \"val1\" label = \"lab1\">"
341: + " <TABLE>"
342: + " <TR><TH>text1<TD>val1"
343: + " <TR><TH>text2<TD>val2"
344: + " </TABLE>"
345: + " </OPTION>"
346: + " <OPTION value = \"val2\"> +"
347: + " <TEXTAREA ID=\"textarea\">textarea</TEXTAREA>"
348: + " </OPTION>"
349: + " <OPTION value = \"val2\" label = \"lab2\">"
350: + " <TEXTAREA ID=\"textarea\">textarea</TEXTAREA>"
351: + " </OPTION>"
352: + " <OPTION label = \"lab3\"> <b> bold <b>"
353: + " </OPTION>" + "</SELECT>");
354: htmlSrc.append("</FORM>");
355: htmlSrc.append("</BODY>");
356: htmlSrc.append("</HTML>");
357:
358: StringReader reader = new StringReader(htmlSrc.toString());
359: htmlEditorKit.read(reader, document, 0);
360: }
361: }
|