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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.io.StringReader;
023:
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.SizeRequirements;
026: import javax.swing.text.AttributeSet;
027: import javax.swing.text.Element;
028: import javax.swing.text.SimpleAttributeSet;
029: import javax.swing.text.View;
030: import javax.swing.text.ViewFactory;
031: import javax.swing.text.ViewTestHelpers.ChildrenFactory;
032: import javax.swing.text.html.BlockViewTest.InlineViewFactory;
033:
034: public class ListViewTest extends BasicSwingTestCase {
035: private class ListViewImpl extends ListView {
036: public ListViewImpl(final Element element) {
037: super (element);
038: loadChildren();
039: }
040:
041: public ViewFactory getViewFactory() {
042: return factory;
043: }
044:
045: public void loadChildren() {
046: loadChildren(getViewFactory());
047: }
048: }
049:
050: private HTMLEditorKit kit;
051: private HTMLDocument doc;
052: private Element listU;
053: private ListView view;
054: private ViewFactory factory;
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058: setIgnoreNotImplemented(true);
059: kit = new HTMLEditorKit();
060: doc = (HTMLDocument) kit.createDefaultDocument();
061: StringReader reader = new StringReader("<html><head></head>"
062: + "<body>" + "<ul>" + " <li>first</li>"
063: + " <li>second</li>" + "</ul>" + "</body></html>");
064: kit.read(reader, doc, 0);
065:
066: listU = doc.getDefaultRootElement().getElement(1).getElement(0);
067: // | html | body | ul
068: assertEquals(HTML.Tag.UL.toString(), listU.getName());
069:
070: factory = new InlineViewFactory();
071: view = new ListViewImpl(listU);
072: }
073:
074: public void testListView() {
075: assertEquals(View.Y_AXIS, view.getAxis());
076: assertNotSame(listU.getAttributes(), view.getAttributes());
077: assertEquals(listU.getElementCount(), view.getViewCount());
078: }
079:
080: public void testGetAlignment() {
081: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 0);
082: assertEquals(0.5f, view.getAlignment(View.Y_AXIS), 0);
083: }
084:
085: public void testGetAlignmentFlexible() {
086: factory = new ChildrenFactory();
087: ((ChildrenFactory) factory).makeFlexible();
088: view = new ListViewImpl(listU);
089:
090: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 0);
091: assertEquals(0.5f, view.getAlignment(View.Y_AXIS), 0);
092:
093: SizeRequirements r = view.calculateMajorAxisRequirements(
094: View.Y_AXIS, null);
095: assertEquals(0.5f, r.alignment, 0);
096:
097: r = view.calculateMajorAxisRequirements(View.X_AXIS, r);
098: assertEquals(0.5f, r.alignment, 0);
099: }
100:
101: public void testSetPropertiesFromAttributes() {
102: final Marker listImage = new Marker();
103: final Marker listType = new Marker();
104: view = new ListView(listU) {
105: private AttributeSet attributes;
106:
107: public AttributeSet getAttributes() {
108: if (attributes == null) {
109: attributes = new SimpleAttributeSet(super
110: .getAttributes()) {
111: public Object getAttribute(Object name) {
112: if (name == CSS.Attribute.LIST_STYLE_IMAGE) {
113: listImage.setOccurred();
114: } else if (name == CSS.Attribute.LIST_STYLE_TYPE) {
115: listType.setOccurred();
116: }
117: return super .getAttribute(name);
118: }
119: };
120: }
121: return attributes;
122: }
123: };
124: assertFalse(listImage.isOccurred());
125: assertFalse(listType.isOccurred());
126: view.setPropertiesFromAttributes();
127: assertEquals(!isHarmony(), listImage.isOccurred());
128: assertEquals(!isHarmony(), listType.isOccurred());
129: }
130:
131: public void testSetPropertiesFromAttributesPainter() {
132: final Marker boxMarker = new Marker();
133: final Marker listMarker = new Marker();
134: final StyleSheet ss = new StyleSheet() {
135: public BoxPainter getBoxPainter(final AttributeSet attr) {
136: boxMarker.setOccurred();
137: return super .getBoxPainter(attr);
138: }
139:
140: public ListPainter getListPainter(final AttributeSet attr) {
141: listMarker.setOccurred();
142: return null;
143: }
144: };
145: view = new ListView(listU) {
146: protected StyleSheet getStyleSheet() {
147: return ss;
148: }
149: };
150: assertFalse(boxMarker.isOccurred());
151: assertFalse(listMarker.isOccurred());
152: view.setPropertiesFromAttributes();
153: assertTrue(boxMarker.isOccurred());
154: assertTrue(listMarker.isOccurred());
155: }
156:
157: // public void testPaint() {
158: // }
159:
160: // public void testPaintChild() {
161: // }
162: }
|