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.awt.FontMetrics;
023: import java.awt.Image;
024: import java.awt.Rectangle;
025: import java.awt.Toolkit;
026: import java.io.File;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.StringReader;
030: import java.net.URL;
031:
032: import javax.swing.BasicSwingTestCase;
033: import javax.swing.Icon;
034: import javax.swing.event.DocumentEvent;
035: import javax.swing.text.AttributeSet;
036: import javax.swing.text.Document;
037: import javax.swing.text.Element;
038: import javax.swing.text.SimpleAttributeSet;
039: import javax.swing.text.View;
040: import javax.swing.text.Position.Bias;
041:
042: @SuppressWarnings({"deprecation","serial"})
043: public class ImageViewTest extends BasicSwingTestCase {
044: private static final String DEFAULT_SRC = "image.jpg";
045: private static final String DEFAULT_ALT = "image description";
046: /**
047: * Natural size of the image in <code>IMAGE_BYTES</code>.
048: */
049: private static final int IMAGE_SIZE = 16;
050: /**
051: * Size specified with attributes, as <code>int</code>.
052: */
053: private static final int SIZE = 20;
054: /**
055: * Size specified with attributes, as <code>String</code>.
056: */
057: private static final String SIZE_VALUE = "20";
058: private static final String WIDTH_HTML = "width=\"20";
059: private static final String HEIGHT_HTML = "height=\"20";
060: private static final String WIDTH_CSS = "width: 20pt";
061: private static final String HEIGHT_CSS = "height: 20pt";
062:
063: /**
064: * Test image in GIF format. It is written to a temporary file and loaded
065: * into the view.
066: */
067: private static final short[] IMAGE_BYTES = { 0x47, 0x49, 0x46,
068: 0x38, 0x39, 0x61, 0x10, 0x00, 0x10, 0x00, 0xf7, 0x00, 0x00,
069: 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80,
070: 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x80,
071: 0x80, 0x80, 0x80, 0x80, 0xc0, 0xc0, 0xc0, 0xff, 0x00, 0x00,
072: 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff,
073: 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
074: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
075: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
076: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
077: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
078: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
079: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
080: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
081: 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x66, 0x00,
082: 0x00, 0x99, 0x00, 0x00, 0xcc, 0x00, 0x00, 0xff, 0x00, 0x33,
083: 0x00, 0x00, 0x33, 0x33, 0x00, 0x33, 0x66, 0x00, 0x33, 0x99,
084: 0x00, 0x33, 0xcc, 0x00, 0x33, 0xff, 0x00, 0x66, 0x00, 0x00,
085: 0x66, 0x33, 0x00, 0x66, 0x66, 0x00, 0x66, 0x99, 0x00, 0x66,
086: 0xcc, 0x00, 0x66, 0xff, 0x00, 0x99, 0x00, 0x00, 0x99, 0x33,
087: 0x00, 0x99, 0x66, 0x00, 0x99, 0x99, 0x00, 0x99, 0xcc, 0x00,
088: 0x99, 0xff, 0x00, 0xcc, 0x00, 0x00, 0xcc, 0x33, 0x00, 0xcc,
089: 0x66, 0x00, 0xcc, 0x99, 0x00, 0xcc, 0xcc, 0x00, 0xcc, 0xff,
090: 0x00, 0xff, 0x00, 0x00, 0xff, 0x33, 0x00, 0xff, 0x66, 0x00,
091: 0xff, 0x99, 0x00, 0xff, 0xcc, 0x00, 0xff, 0xff, 0x33, 0x00,
092: 0x00, 0x33, 0x00, 0x33, 0x33, 0x00, 0x66, 0x33, 0x00, 0x99,
093: 0x33, 0x00, 0xcc, 0x33, 0x00, 0xff, 0x33, 0x33, 0x00, 0x33,
094: 0x33, 0x33, 0x33, 0x33, 0x66, 0x33, 0x33, 0x99, 0x33, 0x33,
095: 0xcc, 0x33, 0x33, 0xff, 0x33, 0x66, 0x00, 0x33, 0x66, 0x33,
096: 0x33, 0x66, 0x66, 0x33, 0x66, 0x99, 0x33, 0x66, 0xcc, 0x33,
097: 0x66, 0xff, 0x33, 0x99, 0x00, 0x33, 0x99, 0x33, 0x33, 0x99,
098: 0x66, 0x33, 0x99, 0x99, 0x33, 0x99, 0xcc, 0x33, 0x99, 0xff,
099: 0x33, 0xcc, 0x00, 0x33, 0xcc, 0x33, 0x33, 0xcc, 0x66, 0x33,
100: 0xcc, 0x99, 0x33, 0xcc, 0xcc, 0x33, 0xcc, 0xff, 0x33, 0xff,
101: 0x00, 0x33, 0xff, 0x33, 0x33, 0xff, 0x66, 0x33, 0xff, 0x99,
102: 0x33, 0xff, 0xcc, 0x33, 0xff, 0xff, 0x66, 0x00, 0x00, 0x66,
103: 0x00, 0x33, 0x66, 0x00, 0x66, 0x66, 0x00, 0x99, 0x66, 0x00,
104: 0xcc, 0x66, 0x00, 0xff, 0x66, 0x33, 0x00, 0x66, 0x33, 0x33,
105: 0x66, 0x33, 0x66, 0x66, 0x33, 0x99, 0x66, 0x33, 0xcc, 0x66,
106: 0x33, 0xff, 0x66, 0x66, 0x00, 0x66, 0x66, 0x33, 0x66, 0x66,
107: 0x66, 0x66, 0x66, 0x99, 0x66, 0x66, 0xcc, 0x66, 0x66, 0xff,
108: 0x66, 0x99, 0x00, 0x66, 0x99, 0x33, 0x66, 0x99, 0x66, 0x66,
109: 0x99, 0x99, 0x66, 0x99, 0xcc, 0x66, 0x99, 0xff, 0x66, 0xcc,
110: 0x00, 0x66, 0xcc, 0x33, 0x66, 0xcc, 0x66, 0x66, 0xcc, 0x99,
111: 0x66, 0xcc, 0xcc, 0x66, 0xcc, 0xff, 0x66, 0xff, 0x00, 0x66,
112: 0xff, 0x33, 0x66, 0xff, 0x66, 0x66, 0xff, 0x99, 0x66, 0xff,
113: 0xcc, 0x66, 0xff, 0xff, 0x99, 0x00, 0x00, 0x99, 0x00, 0x33,
114: 0x99, 0x00, 0x66, 0x99, 0x00, 0x99, 0x99, 0x00, 0xcc, 0x99,
115: 0x00, 0xff, 0x99, 0x33, 0x00, 0x99, 0x33, 0x33, 0x99, 0x33,
116: 0x66, 0x99, 0x33, 0x99, 0x99, 0x33, 0xcc, 0x99, 0x33, 0xff,
117: 0x99, 0x66, 0x00, 0x99, 0x66, 0x33, 0x99, 0x66, 0x66, 0x99,
118: 0x66, 0x99, 0x99, 0x66, 0xcc, 0x99, 0x66, 0xff, 0x99, 0x99,
119: 0x00, 0x99, 0x99, 0x33, 0x99, 0x99, 0x66, 0x99, 0x99, 0x99,
120: 0x99, 0x99, 0xcc, 0x99, 0x99, 0xff, 0x99, 0xcc, 0x00, 0x99,
121: 0xcc, 0x33, 0x99, 0xcc, 0x66, 0x99, 0xcc, 0x99, 0x99, 0xcc,
122: 0xcc, 0x99, 0xcc, 0xff, 0x99, 0xff, 0x00, 0x99, 0xff, 0x33,
123: 0x99, 0xff, 0x66, 0x99, 0xff, 0x99, 0x99, 0xff, 0xcc, 0x99,
124: 0xff, 0xff, 0xcc, 0x00, 0x00, 0xcc, 0x00, 0x33, 0xcc, 0x00,
125: 0x66, 0xcc, 0x00, 0x99, 0xcc, 0x00, 0xcc, 0xcc, 0x00, 0xff,
126: 0xcc, 0x33, 0x00, 0xcc, 0x33, 0x33, 0xcc, 0x33, 0x66, 0xcc,
127: 0x33, 0x99, 0xcc, 0x33, 0xcc, 0xcc, 0x33, 0xff, 0xcc, 0x66,
128: 0x00, 0xcc, 0x66, 0x33, 0xcc, 0x66, 0x66, 0xcc, 0x66, 0x99,
129: 0xcc, 0x66, 0xcc, 0xcc, 0x66, 0xff, 0xcc, 0x99, 0x00, 0xcc,
130: 0x99, 0x33, 0xcc, 0x99, 0x66, 0xcc, 0x99, 0x99, 0xcc, 0x99,
131: 0xcc, 0xcc, 0x99, 0xff, 0xcc, 0xcc, 0x00, 0xcc, 0xcc, 0x33,
132: 0xcc, 0xcc, 0x66, 0xcc, 0xcc, 0x99, 0xcc, 0xcc, 0xcc, 0xcc,
133: 0xcc, 0xff, 0xcc, 0xff, 0x00, 0xcc, 0xff, 0x33, 0xcc, 0xff,
134: 0x66, 0xcc, 0xff, 0x99, 0xcc, 0xff, 0xcc, 0xcc, 0xff, 0xff,
135: 0xff, 0x00, 0x00, 0xff, 0x00, 0x33, 0xff, 0x00, 0x66, 0xff,
136: 0x00, 0x99, 0xff, 0x00, 0xcc, 0xff, 0x00, 0xff, 0xff, 0x33,
137: 0x00, 0xff, 0x33, 0x33, 0xff, 0x33, 0x66, 0xff, 0x33, 0x99,
138: 0xff, 0x33, 0xcc, 0xff, 0x33, 0xff, 0xff, 0x66, 0x00, 0xff,
139: 0x66, 0x33, 0xff, 0x66, 0x66, 0xff, 0x66, 0x99, 0xff, 0x66,
140: 0xcc, 0xff, 0x66, 0xff, 0xff, 0x99, 0x00, 0xff, 0x99, 0x33,
141: 0xff, 0x99, 0x66, 0xff, 0x99, 0x99, 0xff, 0x99, 0xcc, 0xff,
142: 0x99, 0xff, 0xff, 0xcc, 0x00, 0xff, 0xcc, 0x33, 0xff, 0xcc,
143: 0x66, 0xff, 0xcc, 0x99, 0xff, 0xcc, 0xcc, 0xff, 0xcc, 0xff,
144: 0xff, 0xff, 0x00, 0xff, 0xff, 0x33, 0xff, 0xff, 0x66, 0xff,
145: 0xff, 0x99, 0xff, 0xff, 0xcc, 0xff, 0xff, 0xff, 0x21, 0xf9,
146: 0x04, 0x01, 0x00, 0x00, 0x10, 0x00, 0x2c, 0x00, 0x00, 0x00,
147: 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x08, 0x63, 0x00, 0xbf,
148: 0xfd, 0x1b, 0x48, 0x90, 0x5f, 0x3f, 0x82, 0x04, 0xbd, 0xfd,
149: 0x13, 0x88, 0xb0, 0x1f, 0x3f, 0x84, 0xff, 0x14, 0x0e, 0x64,
150: 0x38, 0xd0, 0x20, 0x42, 0x89, 0x04, 0x29, 0x3a, 0x4c, 0x08,
151: 0x71, 0x62, 0xc5, 0x83, 0x11, 0x3b, 0x66, 0xfc, 0xb7, 0x11,
152: 0xa3, 0xc8, 0x85, 0x06, 0x4d, 0x0e, 0xcc, 0xf1, 0x83, 0xa5,
153: 0xcb, 0x6f, 0xde, 0x5a, 0xca, 0x64, 0x39, 0x53, 0xa6, 0xb7,
154: 0x6f, 0x2e, 0x67, 0x76, 0xf4, 0xe6, 0x90, 0xe2, 0x49, 0x85,
155: 0x16, 0x7d, 0x42, 0x94, 0xb8, 0x71, 0xa1, 0x48, 0x8c, 0x16,
156: 0x3d, 0x5e, 0x6c, 0xf8, 0x70, 0xe4, 0x40, 0x95, 0x49, 0x47,
157: 0xaa, 0x24, 0xd9, 0x14, 0xe1, 0xb7, 0x80, 0x00, 0x3b, };
158:
159: private HTMLEditorKit kit;
160: private HTMLDocument doc;
161: private Element img;
162: private ImageView view;
163: private AttributeSet attrs;
164:
165: private Icon noIcon;
166: private int iconWidth;
167: private int iconHeight;
168: private static File imageFile;
169:
170: @Override
171: protected void setUp() throws Exception {
172: super .setUp();
173: setIgnoreNotImplemented(true);
174: init();
175: }
176:
177: @Override
178: protected void tearDown() throws Exception {
179: super .tearDown();
180: if (imageFile != null) {
181: imageFile.delete();
182: }
183: }
184:
185: public void testImageView() {
186: final Marker properties = new Marker();
187: view = new ImageView(img) {
188: @Override
189: protected void setPropertiesFromAttributes() {
190: properties.setOccurred();
191: super .setPropertiesFromAttributes();
192: }
193: };
194: if (isHarmony()) {
195: assertTrue(properties.isOccurred());
196: } else {
197: assertFalse(properties.isOccurred());
198: }
199: assertSame(img, view.getElement());
200: }
201:
202: public void testGetImage() {
203: assertNull(view.getImageURL());
204: assertNull(view.getImage());
205: }
206:
207: public void testGetImage_Image() throws Exception {
208: initImage();
209: view.setLoadsSynchronously(false);
210: assertEquals(imageFile.toURL(), view.getImageURL());
211: assertNotNull(view.getImage());
212: waitForImage();
213: }
214:
215: // URL lacks protocol specification
216: public void testGetImageURL() {
217: assertNull(view.getImageURL());
218: }
219:
220: // HTTP protocol
221: public void testGetImageURL_HTTP() throws Exception {
222: init("http://base.url.test/" + DEFAULT_SRC);
223: assertEquals("http://base.url.test/" + DEFAULT_SRC, view
224: .getImageURL().toString());
225: }
226:
227: // Unknown protocol
228: public void testGetImageURL_IMG() throws Exception {
229: init("img://" + DEFAULT_SRC);
230: assertNull(view.getImageURL());
231: }
232:
233: // doc.getBase() is used to construct absolute URL
234: public void testGetImageURL_BASE01() throws Exception {
235: doc.setBase(new URL("http://base.url.test/"));
236: assertEquals("http://base.url.test/" + DEFAULT_SRC, view
237: .getImageURL().toString());
238: }
239:
240: // doc.getBase() is used to construct absolute URL
241: public void testGetImageURL_BASE02() throws Exception {
242: assertNull(view.getImageURL());
243: assertNull(view.getImage());
244: doc.setBase(new URL("http://base.url.test/"));
245: assertEquals("http://base.url.test/" + DEFAULT_SRC, view
246: .getImageURL().toString());
247: assertNull(view.getImage());
248: }
249:
250: // No src attribute is set on <img>
251: public void testGetImageURL_Null() throws Exception {
252: init(null, null);
253: assertNull(view.getImageURL());
254: }
255:
256: public void testGetLoadingImageIcon() {
257: final Icon icon = view.getLoadingImageIcon();
258: assertNotNull(icon);
259: assertSame(icon, view.getLoadingImageIcon());
260: assertSame(icon, new ImageView(img).getLoadingImageIcon());
261:
262: assertEquals(isHarmony() ? 20 : 38, icon.getIconWidth());
263: assertEquals(isHarmony() ? 18 : 38, icon.getIconHeight());
264: }
265:
266: public void testGetNoImageIcon() {
267: final Icon icon = view.getNoImageIcon();
268: assertNotNull(icon);
269: assertSame(icon, view.getNoImageIcon());
270: assertSame(icon, new ImageView(img).getNoImageIcon());
271:
272: assertEquals(isHarmony() ? 20 : 38, icon.getIconWidth());
273: assertEquals(isHarmony() ? 18 : 38, icon.getIconHeight());
274: }
275:
276: public void testSetLoadsSynchronously() {
277: assertFalse(view.getLoadsSynchronously());
278:
279: view.setLoadsSynchronously(true);
280: assertTrue(view.getLoadsSynchronously());
281:
282: view.setLoadsSynchronously(false);
283: assertFalse(view.getLoadsSynchronously());
284: }
285:
286: public void testGetLoadsSynchronously() {
287: assertFalse(view.getLoadsSynchronously());
288: }
289:
290: // Has null image, alt text is not null
291: public void testGetPreferredSpan01() {
292: assertNull(attrs.getAttribute(CSS.Attribute.WIDTH));
293: assertNull(attrs.getAttribute(CSS.Attribute.HEIGHT));
294:
295: final FontMetrics metrics = Toolkit.getDefaultToolkit()
296: .getFontMetrics(view.getStyleSheet().getFont(attrs));
297: assertEquals(metrics.stringWidth(DEFAULT_ALT) + iconWidth,
298: (int) view.getPreferredSpan(View.X_AXIS));
299: if (isHarmony()) {
300: assertEquals(Math.max(metrics.getHeight(), iconHeight),
301: (int) view.getPreferredSpan(View.Y_AXIS));
302: } else {
303: assertEquals(metrics.getHeight() + iconHeight, (int) view
304: .getPreferredSpan(View.Y_AXIS));
305: }
306: }
307:
308: // Has null image, alt text is null
309: public void testGetPreferredSpan02() throws Exception {
310: init(null, null);
311:
312: assertEquals(iconWidth, (int) view
313: .getPreferredSpan(View.X_AXIS));
314: assertEquals(iconHeight, (int) view
315: .getPreferredSpan(View.Y_AXIS));
316: }
317:
318: // Image has width and height HTML attributes set
319: public void testGetPreferredSpan03() throws Exception {
320: init(DEFAULT_SRC + "\" " + WIDTH_HTML + "\" " + HEIGHT_HTML,
321: null);
322: assertEquals(SIZE_VALUE, img.getAttributes().getAttribute(
323: HTML.Attribute.WIDTH));
324: assertEquals(SIZE_VALUE, img.getAttributes().getAttribute(
325: HTML.Attribute.HEIGHT));
326:
327: assertEquals(iconWidth, (int) view
328: .getPreferredSpan(View.X_AXIS));
329: assertEquals(iconHeight, (int) view
330: .getPreferredSpan(View.Y_AXIS));
331: }
332:
333: // Image has width and height CSS attributes set
334: public void testGetPreferredSpan04() throws Exception {
335: init(DEFAULT_SRC + "\" style=\"" + WIDTH_CSS + "; "
336: + HEIGHT_CSS, null);
337: assertEquals(SIZE_VALUE + "pt", img.getAttributes()
338: .getAttribute(CSS.Attribute.WIDTH).toString());
339: assertEquals(SIZE_VALUE + "pt", img.getAttributes()
340: .getAttribute(CSS.Attribute.HEIGHT).toString());
341:
342: assertEquals(iconWidth, (int) view
343: .getPreferredSpan(View.X_AXIS));
344: assertEquals(iconHeight, (int) view
345: .getPreferredSpan(View.Y_AXIS));
346: }
347:
348: // Loads external image. No attributes specified.
349: // public void testGetPreferredSpan05() throws Exception {
350: // initImage();
351: // waitForImage();
352: // assertNotNull(view.getImage());
353: //
354: // // These assertions should pass but they don't
355: // assertEquals(IMAGE_SIZE,
356: // (int)view.getPreferredSpan(View.X_AXIS));
357: // assertEquals(IMAGE_SIZE,
358: // (int)view.getPreferredSpan(View.Y_AXIS));
359: // }
360:
361: // Loads external image. HTML attributes are set.
362: public void testGetPreferredSpan06() throws Exception {
363: initImage();
364: init(imageFile.toURL().toString() + "\" " + WIDTH_HTML + "\" "
365: + HEIGHT_HTML, null);
366: waitForImage();
367:
368: assertEquals(SIZE_VALUE, img.getAttributes().getAttribute(
369: HTML.Attribute.WIDTH));
370: assertEquals(SIZE_VALUE, img.getAttributes().getAttribute(
371: HTML.Attribute.HEIGHT));
372:
373: assertNotNull(view.getImage());
374:
375: assertEquals(SIZE, (int) view.getPreferredSpan(View.X_AXIS));
376: assertEquals(SIZE, (int) view.getPreferredSpan(View.Y_AXIS));
377: }
378:
379: public void testGetToolTipText01() {
380: assertEquals(view.getAltText(), view.getToolTipText(0, 0,
381: new Rectangle()));
382: }
383:
384: public void testGetToolTipText02() {
385: final Marker marker = new Marker();
386: view = new ImageView(img) {
387: @Override
388: public String getAltText() {
389: marker.setOccurred();
390: return null;
391: }
392: };
393: assertFalse(marker.isOccurred());
394: assertNull(view.getToolTipText(0, 0, new Rectangle()));
395: assertTrue(marker.isOccurred());
396: }
397:
398: public void testGetToolTipText03() {
399: final String alt = view.getAltText();
400: final Rectangle shape = new Rectangle(121, 219, 574, 38);
401:
402: assertEquals(alt, view.getToolTipText(-121, -219, shape));
403: assertEquals(alt, view.getToolTipText(shape.x + shape.width
404: + 41, shape.y + shape.height + 94, shape));
405: }
406:
407: public void testGetAltText01() {
408: assertEquals(DEFAULT_ALT, view.getAltText());
409: }
410:
411: public void testGetAltText02() throws Exception {
412: init(null, null);
413: assertNull(view.getAltText());
414: }
415:
416: public void testGetAltText03() throws Exception {
417: final Marker marker = new Marker();
418: final AttributeSet vas = new SimpleAttributeSet() {
419: @Override
420: public Object getAttribute(final Object key) {
421: marker.setOccurred();
422: return attrs.getAttribute(key);
423: }
424: };
425: view = new ImageView(img) {
426: @Override
427: public AttributeSet getAttributes() {
428: return vas;
429: }
430: };
431: marker.reset();
432: view.getAltText();
433: assertFalse(marker.isOccurred());
434: }
435:
436: public void testGetAltText04() throws Exception {
437: final Marker marker = new Marker();
438:
439: final String value = "attribute value";
440: final AttributeSet eas = new SimpleAttributeSet() {
441: @Override
442: public Object getAttribute(final Object key) {
443: marker.setOccurred();
444: if (key == HTML.Attribute.ALT) {
445: return value;
446: }
447: return attrs.getAttribute(key);
448: }
449: };
450: final Element e = new Element() {
451: public AttributeSet getAttributes() {
452: return eas;
453: }
454:
455: public Document getDocument() {
456: return img.getDocument();
457: }
458:
459: public Element getElement(int index) {
460: return img.getElement(index);
461: }
462:
463: public int getElementCount() {
464: return img.getElementCount();
465: }
466:
467: public int getElementIndex(int offset) {
468: return img.getElementIndex(offset);
469: }
470:
471: public int getEndOffset() {
472: return img.getEndOffset();
473: }
474:
475: public String getName() {
476: return img.getName();
477: }
478:
479: public Element getParentElement() {
480: return img.getParentElement();
481: }
482:
483: public int getStartOffset() {
484: return img.getStartOffset();
485: }
486:
487: public boolean isLeaf() {
488: return img.isLeaf();
489: }
490: };
491:
492: view = new ImageView(e);
493: marker.reset();
494: assertSame(value, view.getAltText());
495: assertTrue(marker.isOccurred());
496: }
497:
498: public void testSetParent() {
499: final Marker properties = new Marker();
500: view = new ImageView(img) {
501: @Override
502: protected void setPropertiesFromAttributes() {
503: properties.setOccurred();
504: super .setPropertiesFromAttributes();
505: }
506: };
507: if (isHarmony()) {
508: assertTrue(properties.isOccurred());
509: } else {
510: assertFalse(properties.isOccurred());
511: }
512:
513: view.setParent(new InlineView(img));
514:
515: if (isHarmony()) {
516: assertTrue(properties.isOccurred());
517: } else {
518: assertFalse(properties.isOccurred());
519: }
520: }
521:
522: public void testSetSize() throws Exception {
523: init(null, null);
524: assertEquals(iconWidth, (int) view
525: .getPreferredSpan(View.X_AXIS));
526: assertEquals(iconHeight, (int) view
527: .getPreferredSpan(View.Y_AXIS));
528:
529: view.setSize(231, 231);
530:
531: assertEquals(iconWidth, (int) view
532: .getPreferredSpan(View.X_AXIS));
533: assertEquals(iconHeight, (int) view
534: .getPreferredSpan(View.Y_AXIS));
535: }
536:
537: // modelToView(int, Shape, Bias)
538: public void testModelToView() throws Exception {
539: init(null, null);
540: assertEquals(iconWidth, (int) view
541: .getPreferredSpan(View.X_AXIS));
542: assertEquals(1, view.getEndOffset() - view.getStartOffset());
543: Rectangle shape = new Rectangle(21, 33, 132, 129);
544: assertEquals(new Rectangle(21, 33, 0, 129), view.modelToView(
545: view.getStartOffset(), shape, Bias.Forward));
546: assertEquals(new Rectangle(21, 33, 0, 129), view.modelToView(
547: view.getStartOffset(), shape, Bias.Backward));
548: assertEquals(new Rectangle(21 + shape.width, 33, 0, 129), view
549: .modelToView(view.getEndOffset(), shape, Bias.Forward));
550: assertEquals(new Rectangle(21 + shape.width, 33, 0, 129), view
551: .modelToView(view.getEndOffset(), shape, Bias.Backward));
552: }
553:
554: public void testModelToViewWithAltText() throws Exception {
555: final FontMetrics metrics = Toolkit.getDefaultToolkit()
556: .getFontMetrics(view.getStyleSheet().getFont(attrs));
557: int textWidth = metrics.stringWidth(view.getAltText());
558: assertEquals(iconWidth + textWidth, (int) view
559: .getPreferredSpan(View.X_AXIS));
560: assertEquals(1, view.getEndOffset() - view.getStartOffset());
561: Rectangle shape = new Rectangle(21, 33, 132, 129);
562: assertEquals(new Rectangle(21, 33, 0, 129), view.modelToView(
563: view.getStartOffset(), shape, Bias.Forward));
564: assertEquals(new Rectangle(21, 33, 0, 129), view.modelToView(
565: view.getStartOffset(), shape, Bias.Backward));
566:
567: assertEquals(new Rectangle(21 + shape.width, 33, 0, 129), view
568: .modelToView(view.getEndOffset(), shape, Bias.Forward));
569: assertEquals(new Rectangle(21 + shape.width, 33, 0, 129), view
570: .modelToView(view.getEndOffset(), shape, Bias.Backward));
571: }
572:
573: // viewToModel(float, float, Shape, Bias[]
574: public void testViewToModel() {
575: Rectangle shape = new Rectangle(21, 33, 132, 129);
576: Bias[] bias = new Bias[1];
577: assertEquals(view.getStartOffset(), view.viewToModel(0, 0,
578: shape, bias));
579: assertSame(Bias.Forward, bias[0]);
580: bias[0] = null;
581: assertEquals(view.getStartOffset(), view.viewToModel(shape.x
582: + shape.width / 2 - 1, shape.y, shape, bias));
583: assertSame(Bias.Forward, bias[0]);
584: bias[0] = null;
585:
586: assertEquals(view.getStartOffset(), view.viewToModel(shape.x
587: + shape.width / 2 + 1, shape.y, shape, bias));
588: assertSame(Bias.Forward, bias[0]);
589: bias[0] = null;
590: assertEquals(view.getEndOffset(), view.viewToModel(shape.x
591: + shape.width + 31, shape.y + shape.height + 33, shape,
592: bias));
593: assertSame(Bias.Backward, bias[0]);
594: bias[0] = null;
595:
596: assertEquals(view.getStartOffset(), view.viewToModel(shape.x
597: + shape.width - 1, shape.y, shape, bias));
598: assertSame(Bias.Forward, bias[0]);
599: bias[0] = null;
600: assertEquals(view.getEndOffset(), view.viewToModel(shape.x
601: + shape.width, shape.y, shape, bias));
602: assertSame(Bias.Backward, bias[0]);
603: bias[0] = null;
604: }
605:
606: public void testGetAlignment() {
607: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 0);
608: assertEquals(1.0f, view.getAlignment(View.Y_AXIS), 0);
609: }
610:
611: public void testGetAttributes() {
612: assertSame(attrs, view.getAttributes());
613: assertNotSame(attrs, view.getElement().getAttributes());
614: }
615:
616: public void testChangedUpdate() {
617: final Marker properties = new Marker(true);
618: view = new ImageView(img) {
619: @Override
620: protected void setPropertiesFromAttributes() {
621: properties.setOccurred();
622: super .setPropertiesFromAttributes();
623: }
624: };
625:
626: if (isHarmony()) {
627: assertTrue(properties.isOccurred());
628: } else {
629: assertFalse(properties.isOccurred());
630: }
631:
632: view.changedUpdate(new DocumentEvent() {
633: public int getOffset() {
634: return img.getStartOffset();
635: }
636:
637: public int getLength() {
638: return img.getEndOffset() - img.getStartOffset();
639: }
640:
641: public Document getDocument() {
642: return doc;
643: }
644:
645: public EventType getType() {
646: return EventType.CHANGE;
647: }
648:
649: public ElementChange getChange(Element elem) {
650: return null;
651: }
652: }, new Rectangle(), null);
653: if (isHarmony()) {
654: assertTrue(properties.isOccurred());
655: } else {
656: assertFalse(properties.isOccurred());
657: }
658: }
659:
660: public void testGetStyleSheet() {
661: assertSame(doc.getStyleSheet(), view.getStyleSheet());
662: }
663:
664: public void testSetPropertiesFromAttributes() {
665: final Marker color = new Marker(true);
666: view = new ImageView(img) {
667: private AttributeSet attributes;
668:
669: @Override
670: public AttributeSet getAttributes() {
671: if (attributes == null) {
672: attributes = new SimpleAttributeSet(super
673: .getAttributes()) {
674: @Override
675: public Object getAttribute(Object key) {
676: if (key == CSS.Attribute.COLOR) {
677: color.setOccurred();
678: }
679: return super .getAttribute(key);
680: }
681: };
682: }
683: return attributes;
684: }
685: };
686: color.reset();
687: view.setPropertiesFromAttributes();
688: assertTrue(color.isOccurred());
689: }
690:
691: // public void testPaint() {
692: //
693: // }
694:
695: private void init() throws Exception {
696: init(DEFAULT_SRC);
697: }
698:
699: private void init(final String src) throws Exception {
700: init(src, DEFAULT_ALT);
701: }
702:
703: private void init(final String src, final String alt)
704: throws Exception {
705: kit = new HTMLEditorKit();
706: doc = (HTMLDocument) kit.createDefaultDocument();
707: StringReader reader = new StringReader("<html><head></head>"
708: + "<body>" + "<img"
709: + (src != null ? " src=\"" + src + "\"" : "")
710: + (alt != null ? " alt=\"" + alt + "\"" : "") + ">"
711: + "</body></html>");
712: kit.read(reader, doc, 0);
713:
714: img = doc.getCharacterElement(1);
715: assertEquals(HTML.Tag.IMG.toString(), img.getName());
716:
717: view = new ImageView(img);
718: attrs = view.getAttributes();
719:
720: noIcon = view.getNoImageIcon();
721: iconWidth = noIcon.getIconWidth();
722: iconHeight = noIcon.getIconHeight();
723: }
724:
725: private void initImage() throws Exception {
726: writeImage();
727: init(imageFile.toURL().toString());
728: }
729:
730: private static void writeImage() throws IOException {
731: imageFile = File.createTempFile("imageViewTest", ".gif");
732: FileOutputStream out = new FileOutputStream(imageFile);
733: for (int i = 0; i < IMAGE_BYTES.length; i++) {
734: out.write(IMAGE_BYTES[i]);
735: }
736: out.close();
737: }
738:
739: private void waitForImage() throws Exception {
740: final Image image = view.getImage();
741:
742: int w = -1;
743: int h = -1;
744: while (w == -1 || h == -1) {
745: w = image.getWidth(null);
746: h = image.getHeight(null);
747: Thread.sleep(1000);
748: }
749: ;
750: assertEquals(IMAGE_SIZE, w);
751: assertEquals(IMAGE_SIZE, h);
752: }
753: }
|