Index: HtmlRenderTest.java =================================================================== RCS file: HtmlRenderTest.java diff -N HtmlRenderTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ HtmlRenderTest.java 28 Aug 2003 11:07:10 -0000 @@ -0,0 +1,109 @@ + +package org.openide.util; + +import java.awt.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.KeyEvent; +import java.awt.image.*; +import javax.swing.*; +import javax.swing.JFrame; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import junit.framework.*; +import org.netbeans.junit.*; + +public class HtmlRenderTest extends NbTestCase { + JFrame jf; + JTextField jtf; + public HtmlRenderTest(java.lang.String testName) { + super(testName); + } + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(suite()); + } + public static Test suite() { + NbTestSuite suite = new NbTestSuite(HtmlRenderTest.class); + return suite; + } + protected void setUp () { + } + public void testHTMLLooksNice() throws Exception { + assertRender ("Text looks very nice"); + } + + private static void assertRender (String txt) { + BufferedImage img1 = new BufferedImage (500, 200, BufferedImage.TYPE_INT_ARGB); + Graphics g = img1.getGraphics(); + + Font f = new Font ("dialog", Font. PLAIN, 12); + + Utilities.renderHTML ( + txt, + g, 2, 20, 480, 400, f, Color.WHITE, Utilities.STYLE_CLIP, true); + + JLabel l2 = new JLabel ("" + txt); + l2.setBackground (Color.BLACK); + l2.setForeground (Color.WHITE); + l2.setFont (f); + l2.setSize(img1.getWidth(), img1.getHeight()); + + BufferedImage img2 = new BufferedImage (img1.getWidth (), img1.getHeight (), BufferedImage.TYPE_INT_ARGB); + l2.paint (img2.getGraphics()); + + assertImages (img1, img2); + } + + private static void assertImages (final BufferedImage img1, final BufferedImage img2) { + Point p1 = firstPoint (img1); + Point p2 = firstPoint (img2); + + for (int line = 0;; line++) { + if (line + p1.y >= img1.getHeight()) break; + if (line + p2.y >= img2.getHeight()) break; + + for (int column = 0; ; column++) { + if (column + p1.x >= img1.getWidth()) break; + if (column + p2.x >= img2.getWidth()) break; + + int rgb1 = img1.getRGB(p1.x + column, p1.y + line); + int rgb2 = img2.getRGB(p2.x + column, p2.y + line); + if (rgb1 != rgb2) { + if (rgb1 == -16777216 && rgb2 == 0) continue; + + img1.setRGB(p1.x + column, p1.y + line, Color.RED.getRGB()); + img2.setRGB(p2.x + column, p2.y + line, Color.RED.getRGB()); + SwingUtilities.invokeLater (new Runnable () { + public void run () { + showImages (img1, img2); + } + }); + assertEquals ("Different rgb at position: " + column + ", " + line + " offset1: " + p1 + " offset2: " + p2, rgb1, rgb2); + } + } + } + } + + private static Point firstPoint (BufferedImage img) { + for (int i = 0; i < img.getHeight(); i++) { + for (int j = 0; j < img.getWidth(); j++) { + int rbg = img.getRGB (j, i); + if (rbg != 0 && rbg != -16777216) { + return new Point (j, i); + } + } + } + fail ("No pixel in a picture"); + return null; + } + + private static void showImages (BufferedImage img1, BufferedImage img2) { + JFrame jf = new JFrame(); + jf.setLocation (120,120); + jf.getContentPane ().add (BorderLayout.NORTH, new JLabel ("Bellow is the content of images with a red dots marking the place with differences")); + jf.getContentPane ().add (BorderLayout.CENTER, new JLabel (new ImageIcon (img1))); + jf.getContentPane ().add (BorderLayout.SOUTH, new JLabel (new ImageIcon (img2))); + jf.pack (); + jf.show (); + } +}