Added
Link Here
|
1 |
|
2 |
package org.openide.util; |
3 |
|
4 |
import java.awt.*; |
5 |
import java.awt.Color; |
6 |
import java.awt.Graphics; |
7 |
import java.awt.event.KeyEvent; |
8 |
import java.awt.image.*; |
9 |
import javax.swing.*; |
10 |
import javax.swing.JFrame; |
11 |
import javax.swing.JTextField; |
12 |
import javax.swing.KeyStroke; |
13 |
import junit.framework.*; |
14 |
import org.netbeans.junit.*; |
15 |
|
16 |
public class HtmlRenderTest extends NbTestCase { |
17 |
JFrame jf; |
18 |
JTextField jtf; |
19 |
public HtmlRenderTest(java.lang.String testName) { |
20 |
super(testName); |
21 |
} |
22 |
public static void main(java.lang.String[] args) { |
23 |
junit.textui.TestRunner.run(suite()); |
24 |
} |
25 |
public static Test suite() { |
26 |
NbTestSuite suite = new NbTestSuite(HtmlRenderTest.class); |
27 |
return suite; |
28 |
} |
29 |
protected void setUp () { |
30 |
} |
31 |
public void testHTMLLooksNice() throws Exception { |
32 |
assertRender ("Text looks very <b><u>nice</u></b>"); |
33 |
} |
34 |
|
35 |
private static void assertRender (String txt) { |
36 |
BufferedImage img1 = new BufferedImage (500, 200, BufferedImage.TYPE_INT_ARGB); |
37 |
Graphics g = img1.getGraphics(); |
38 |
|
39 |
Font f = new Font ("dialog", Font. PLAIN, 12); |
40 |
|
41 |
Utilities.renderHTML ( |
42 |
txt, |
43 |
g, 2, 20, 480, 400, f, Color.WHITE, Utilities.STYLE_CLIP, true); |
44 |
|
45 |
JLabel l2 = new JLabel ("<html>" + txt); |
46 |
l2.setBackground (Color.BLACK); |
47 |
l2.setForeground (Color.WHITE); |
48 |
l2.setFont (f); |
49 |
l2.setSize(img1.getWidth(), img1.getHeight()); |
50 |
|
51 |
BufferedImage img2 = new BufferedImage (img1.getWidth (), img1.getHeight (), BufferedImage.TYPE_INT_ARGB); |
52 |
l2.paint (img2.getGraphics()); |
53 |
|
54 |
assertImages (img1, img2); |
55 |
} |
56 |
|
57 |
private static void assertImages (final BufferedImage img1, final BufferedImage img2) { |
58 |
Point p1 = firstPoint (img1); |
59 |
Point p2 = firstPoint (img2); |
60 |
|
61 |
for (int line = 0;; line++) { |
62 |
if (line + p1.y >= img1.getHeight()) break; |
63 |
if (line + p2.y >= img2.getHeight()) break; |
64 |
|
65 |
for (int column = 0; ; column++) { |
66 |
if (column + p1.x >= img1.getWidth()) break; |
67 |
if (column + p2.x >= img2.getWidth()) break; |
68 |
|
69 |
int rgb1 = img1.getRGB(p1.x + column, p1.y + line); |
70 |
int rgb2 = img2.getRGB(p2.x + column, p2.y + line); |
71 |
if (rgb1 != rgb2) { |
72 |
if (rgb1 == -16777216 && rgb2 == 0) continue; |
73 |
|
74 |
img1.setRGB(p1.x + column, p1.y + line, Color.RED.getRGB()); |
75 |
img2.setRGB(p2.x + column, p2.y + line, Color.RED.getRGB()); |
76 |
SwingUtilities.invokeLater (new Runnable () { |
77 |
public void run () { |
78 |
showImages (img1, img2); |
79 |
} |
80 |
}); |
81 |
assertEquals ("Different rgb at position: " + column + ", " + line + " offset1: " + p1 + " offset2: " + p2, rgb1, rgb2); |
82 |
} |
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
private static Point firstPoint (BufferedImage img) { |
88 |
for (int i = 0; i < img.getHeight(); i++) { |
89 |
for (int j = 0; j < img.getWidth(); j++) { |
90 |
int rbg = img.getRGB (j, i); |
91 |
if (rbg != 0 && rbg != -16777216) { |
92 |
return new Point (j, i); |
93 |
} |
94 |
} |
95 |
} |
96 |
fail ("No pixel in a picture"); |
97 |
return null; |
98 |
} |
99 |
|
100 |
private static void showImages (BufferedImage img1, BufferedImage img2) { |
101 |
JFrame jf = new JFrame(); |
102 |
jf.setLocation (120,120); |
103 |
jf.getContentPane ().add (BorderLayout.NORTH, new JLabel ("Bellow is the content of images with a red dots marking the place with differences")); |
104 |
jf.getContentPane ().add (BorderLayout.CENTER, new JLabel (new ImageIcon (img1))); |
105 |
jf.getContentPane ().add (BorderLayout.SOUTH, new JLabel (new ImageIcon (img2))); |
106 |
jf.pack (); |
107 |
jf.show (); |
108 |
} |
109 |
} |