KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > GraphicsTestCase


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer.propertysheet;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.DisplayMode JavaDoc;
25 import java.awt.FlowLayout JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.GraphicsEnvironment JavaDoc;
30 import java.awt.Image JavaDoc;
31 import java.awt.Point JavaDoc;
32 import java.awt.Toolkit JavaDoc;
33 import java.awt.Transparency JavaDoc;
34 import java.awt.image.BufferedImage JavaDoc;
35 import java.awt.image.ColorModel JavaDoc;
36 import java.net.URL JavaDoc;
37 import javax.imageio.ImageIO JavaDoc;
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.ImageIcon JavaDoc;
40 import javax.swing.JComponent JavaDoc;
41 import javax.swing.JFrame JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JTable JavaDoc;
44 import javax.swing.UIManager JavaDoc;
45 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow;
46 import org.openide.explorer.propertysheet.ExtTestCase.WrapperRunnable;
47
48 /**
49  * @author Tim Boudreau
50  */

51 public class GraphicsTestCase extends ExtTestCase {
52     private static int count=0;
53     
54     /** Creates a new instance of GraphicsExtTestCase */
55     public GraphicsTestCase(String JavaDoc name) {
56         super(name);
57     }
58     
59     public void testNothing() {
60         //do nothing - method just here to keep JUnit happy
61
}
62     
63     public static void main(String JavaDoc[] args) {
64         ExtTestCase.main(args);
65         // SwingUtilities.invokeLater (new Runnable() {
66
// public void run() {
67
System.err.println("Can safely run pixel tests: " + canSafelyRunPixelTests());
68         // }
69
// });
70
}
71     
72     /** Determine if the graphics environment is 24 bit and has standard fonts,
73      * so pixel position and color tests will not erroneously fail. This method is
74      * thread safe.
75      */

76     public static boolean canSafelyRunPixelTests() {
77         if (graphicsTestsSafe != null) {
78             return graphicsTestsSafe.booleanValue();
79         }
80         
81         try {
82             boolean result = false;
83             if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) {
84                 System.err.println("Cannot run test in a headless environment");
85                 graphicsTestsSafe = Boolean.FALSE;
86                 return false;
87             }
88             
89             DisplayMode JavaDoc dm =
90                     GraphicsEnvironment.getLocalGraphicsEnvironment().
91                     getDefaultScreenDevice().getDisplayMode();
92             
93             int i = dm.getBitDepth();
94             if (i == dm.BIT_DEPTH_MULTI || i >= 16) {
95                 result = true;
96                 
97                 Font JavaDoc f = UIManager.getFont("controlFont");
98                 if (f == null) {
99                     f = new JTable JavaDoc().getFont();
100                 }
101                 Graphics JavaDoc g = GraphicsEnvironment.getLocalGraphicsEnvironment().
102                         getDefaultScreenDevice().getDefaultConfiguration().
103                         createCompatibleImage(10,10).getGraphics();
104                 
105                 FontMetrics JavaDoc fm = g.getFontMetrics(f);
106                 if (fm.getHeight() != 16) {
107                     System.err.println("Cannot run this test - default font size is not " + 16 + " pixels in height - could lead to false fails");
108                     System.err.println("Basic font size is " + fm.getHeight());
109                     //Some environments, such as Mandrake linux or Windows with
110
//large fonts will supply fonts bigger than the error icon,
111
//causing the pixel tests to fail due to icon positioning
112
//differences
113
result = false;
114                 }
115             }
116             if (result) {
117                 result = tryPrototypePixelTest();
118             }
119             return result;
120         } catch (Exception JavaDoc e) {
121             graphicsTestsSafe = Boolean.FALSE;
122             e.printStackTrace();
123             return false;
124         }
125     }
126     
127     private static Boolean JavaDoc graphicsTestsSafe = null;
128     /** Does a slightly more involved test, displaying actual icons and images
129      * that should always match and seeing if they do. */

130     private static boolean tryPrototypePixelTest() throws Exception JavaDoc {
131         if (graphicsTestsSafe != null) {
132             return graphicsTestsSafe.booleanValue();
133         }
134         try {
135             jf = new JFrame JavaDoc();
136             final JLabel JavaDoc jl = new JLabel JavaDoc("Show the dialog");
137             jl.setOpaque(true);
138             jl.setIcon(new TestIcon());
139             
140             jf.getContentPane().setLayout(new FlowLayout JavaDoc());
141             jf.getContentPane().add(jl);
142             jf.setBounds(20,20, 100,100);
143             new WaitWindow(jf);
144             jf.pack();
145             
146             boolean frameGotFocus = checkFocusedContainer(jf);
147             
148             sleep();
149             Toolkit.getDefaultToolkit().sync();
150             
151             int y = jl.getHeight() / 2;
152             int xoff = jl.getLocation().x;
153             
154             boolean result = checkColorOnComponent(jl, Color.GREEN, new Point JavaDoc(5,y));
155             
156             final BufferedImage JavaDoc img =
157                     loadImage("org/netbeans/core/resources/defaultOpenFolder.gif");
158             
159             if (result) {
160                 maybeInvokeLater(new Runnable JavaDoc() {
161                     public void run() {
162                         try {
163                             jl.setIcon(new ImageIcon JavaDoc(img));
164                         } catch (Exception JavaDoc e) {
165                             throw new RuntimeException JavaDoc(e);
166                         }
167                     }
168                 });
169                 result = checkPixelFromImage(img, jl, 5, 8, 5, 8);
170             }
171             
172             graphicsTestsSafe = result ? Boolean.TRUE : Boolean.FALSE;
173             
174             return result;
175         } catch (Exception JavaDoc e) {
176             throw new RuntimeException JavaDoc(e);
177         } finally {
178             if (jf != null) {
179                 // jf.hide();
180
// jf.dispose();
181
}
182         }
183     }
184     
185     private static class TestIcon implements Icon JavaDoc {
186         public int getIconHeight() {
187             return 20;
188         }
189         
190         public int getIconWidth() {
191             return 20;
192         }
193         
194         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
195             g.setColor(Color.GREEN);
196             g.fillRect(x, y, 20, 20);
197         }
198     }
199     
200     /** Assert that the pixel at the specified point is of the color <code>color</code> */
201     protected static void assertColorOnComponent(Component JavaDoc c, Color JavaDoc color, Point JavaDoc p) throws Exception JavaDoc {
202         ComponentPixelChecker cpc = new ComponentPixelChecker(c, color, p);
203         click(c);
204         maybeInvokeLater(cpc);
205         cpc.assertMatch();
206     }
207     
208     /** Assert that the pixel at the specified coordinates is of the color <code>color</code> */
209     protected static void assertColorOnComponent(Component JavaDoc c, Color JavaDoc color, int x, int y) throws Exception JavaDoc {
210         assertColorOnComponent(c, color, new Point JavaDoc(x,y));
211     }
212     
213     
214     /** Asserts that a pixel at a given position in an image matches a
215      * pixel in a given position in a component */

216     protected static void assertPixelFromImage(final Image JavaDoc i, final Component JavaDoc c, final int imageX, final int imageY, final int compX, final int compY) throws Exception JavaDoc {
217         ImagePixelChecker pix = new ImagePixelChecker(i, c, imageX, imageY, compX, compY);
218         maybeInvokeLater(pix);
219         pix.assertMatch();
220     }
221     
222     /** Check that the pixel at compX, compY on the component is of the same
223      * color as the pixel in the passed image at imageX, imageY. Used by the
224      * generic tests that should always pass to decide if it is safe to run
225      * pixel tests. */

226     private static boolean checkPixelFromImage(final Image JavaDoc i, final Component JavaDoc c, final int imageX, final int imageY, final int compX, final int compY) throws Exception JavaDoc {
227         ImagePixelChecker pix = new ImagePixelChecker(i, c, imageX, imageY, compX, compY);
228         maybeInvokeLater(pix);
229         return pix.compare();
230     }
231     
232     
233     /** Check that the pixel at compX, compY on the component is of the passed
234      * color. Used by the
235      * generic tests that should always pass to decide if it is safe to run
236      * pixel tests. */

237     private static boolean checkColorOnComponent(Component JavaDoc c, Color JavaDoc color, Point JavaDoc p) throws Exception JavaDoc {
238         System.err.println("Checking for color " + color);
239         click(c);
240         ComponentPixelChecker cpc = new ComponentPixelChecker(c, color, p);
241         maybeInvokeLater(cpc);
242         return cpc.compare();
243     }
244     
245     /** Runnable class that ensures a component is drawn and compares a
246      * pixel at some coordinates on it with a color. */

247     private static class ComponentPixelChecker extends WrapperRunnable {
248         private Component JavaDoc comp;
249         private Point JavaDoc pos;
250         private Color JavaDoc color;
251         public ComponentPixelChecker(Component JavaDoc c, Color JavaDoc color, Point JavaDoc pos) {
252             this.comp = c;
253             this.color = color;
254             this.pos = pos;
255         }
256         
257         private Color JavaDoc compColor = null;
258         private Boolean JavaDoc finalResult = null;
259         public Color JavaDoc getCompColor() throws Exception JavaDoc {
260             if (compColor == null) {
261                 compare();
262             }
263             return compColor;
264         }
265         
266         public Color JavaDoc getColor() {
267             return color;
268         }
269         
270         BufferedImage JavaDoc compImg = null;
271         public boolean compare() throws Exception JavaDoc {
272             if (finalResult != null) {
273                 return finalResult.booleanValue();
274             }
275             BufferedImage JavaDoc img = getImageOfComponent();
276             int[] cArr = new int[3];
277             img.getData().getPixel(pos.x, pos.y, cArr);
278             Color JavaDoc compColor = new Color JavaDoc(cArr[0], cArr[1], cArr[2]);
279             
280             System.err.println("Comparing " + compColor + " with " + getColor());
281             
282             return getColor().equals(compColor);
283         }
284         
285         public void assertMatch() throws Exception JavaDoc {
286             assertTrue("Component pixel at " + pos + " is " + getCompColor() + " but should be " + getColor(), compare());
287         }
288         
289         public BufferedImage JavaDoc getImageOfComponent() throws Exception JavaDoc {
290             //Block to force graphics initialization
291
((JComponent JavaDoc)comp).paintImmediately(0,0,comp.getWidth(), comp.getHeight());
292             
293             sleep();
294             sleep();
295             final BufferedImage JavaDoc result = new BufferedImage JavaDoc(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
296             sleep();
297             sleep();
298             Toolkit.getDefaultToolkit().sync();
299             ((JComponent JavaDoc) comp).paintAll(result.getGraphics());
300             sleep();
301             showFrameForImage(result);
302             return result;
303         }
304         
305         private void showFrameForImage(final BufferedImage JavaDoc bi) throws Exception JavaDoc {
306             JFrame JavaDoc jf = new JFrame JavaDoc("assertPixelFromImage " + (count ++) + " (look for the yellow line)") {
307                 public void paint(Graphics JavaDoc g) {
308                     new ImageIcon JavaDoc(bi).paintIcon(this, g, 25, 25);
309                     g.setColor(Color.YELLOW);
310                     g.drawLine(pos.x+20, pos.y+25, pos.x+25, pos.y+25);
311                 }
312             };
313             jf.setBounds(100,100, pos.x + 100, pos.y + 100);
314             new WaitWindow(jf);
315         }
316     }
317     
318     /** Runnable class that compares a pixel on an image and a pixel on a
319      * component. */

320     private static class ImagePixelChecker extends WrapperRunnable {
321         private Component JavaDoc comp;
322         private Image JavaDoc image;
323         private Point JavaDoc imgPoint;
324         private Point JavaDoc compPoint;
325         
326         public ImagePixelChecker(Image JavaDoc i, Component JavaDoc c, int imageX, int imageY, int compX, int compY) {
327             this(i, c, new Point JavaDoc(imageX, imageY), new Point JavaDoc(compX, compY));
328         }
329         
330         public ImagePixelChecker(Image JavaDoc i, Component JavaDoc c, Point JavaDoc imgPoint, Point JavaDoc compPoint) {
331             this.image = i;
332             this.comp = c;
333             this.imgPoint = imgPoint;
334             this.compPoint = compPoint;
335         }
336         
337         public BufferedImage JavaDoc getImageOfComponent() throws Exception JavaDoc {
338             sleep();
339             sleep();
340             int maxX = Math.max(imgPoint.x, compPoint.x) + 20;
341             int maxY = Math.max(imgPoint.y, compPoint.y) + 20;
342             final BufferedImage JavaDoc result = new BufferedImage JavaDoc(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
343             sleep();
344             sleep();
345             ((JComponent JavaDoc) comp).paintAll(result.getGraphics());
346             sleep();
347             showFrameForImage(result, compPoint);
348             return result;
349         }
350         
351         private void showFrameForImage(final BufferedImage JavaDoc bi, final Point JavaDoc p) throws Exception JavaDoc {
352             JFrame JavaDoc jf = new JFrame JavaDoc("assertPixelFromImage " + (count ++) + " (look for the yellow line)") {
353                 public void paint(Graphics JavaDoc g) {
354                     new ImageIcon JavaDoc(bi).paintIcon(this, g, 25, 25);
355                     g.setColor(Color.YELLOW);
356                     g.drawLine(p.x+20, p.y+25, p.x+25, p.y+25);
357                 }
358             };
359             jf.setBounds(100,100, p.x + 100, p.y + 100);
360             new WaitWindow(jf);
361         }
362         
363         public BufferedImage JavaDoc getImage() throws Exception JavaDoc {
364             sleep();
365             BufferedImage JavaDoc result = image instanceof BufferedImage JavaDoc ? (BufferedImage JavaDoc) image
366                     : toBufferedImage(image);
367             
368             result.getSampleModel().getNumBands();
369             showFrameForImage(result, imgPoint);
370             sleep();
371             sleep();
372             return result;
373         }
374         
375         private Boolean JavaDoc finalResult = null;
376         
377         private Color JavaDoc compColor = null;
378         private Color JavaDoc imgColor = null;
379         
380         public Color JavaDoc getCompColor() throws Exception JavaDoc {
381             if (compColor == null) {
382                 compare();
383             }
384             return compColor;
385         }
386         
387         public Color JavaDoc getImageColor() throws Exception JavaDoc {
388             if (imgColor == null) {
389                 compare();
390             }
391             return imgColor;
392         }
393         
394         public boolean compare() throws Exception JavaDoc {
395             if (finalResult != null) {
396                 return finalResult.booleanValue();
397             }
398             BufferedImage JavaDoc compImg = getImageOfComponent();
399             BufferedImage JavaDoc img = getImage();
400             compColor = new Color JavaDoc(compImg.getRGB(compPoint.x, compPoint.y));
401             imgColor = new Color JavaDoc(img.getRGB(imgPoint.x, imgPoint.y));
402             
403             finalResult = imgColor.equals(compColor) ? Boolean.TRUE :
404                 Boolean.FALSE;
405             
406             System.err.println("Comparing " + compColor + " at " + compPoint + " with " + imgColor + " at " + imgPoint);
407             
408             Graphics JavaDoc g = comp.getGraphics();
409             
410             g.setColor(Color.YELLOW);
411             g.drawLine(compPoint.x-5, compPoint.y, compPoint.x, compPoint.y);
412             
413             System.err.println("Pixel comparison returns " + finalResult);
414             return finalResult.booleanValue();
415         }
416         
417         public void assertMatch() throws Exception JavaDoc {
418             assertTrue("Image pixel at " + imgPoint + " is " + getImageColor() + " and component should match at point " + compPoint + " but component color at those coordinates is " + getCompColor(), compare());
419         }
420         
421         public void run() {
422             try {
423                 compare();
424             } catch (Exception JavaDoc e){
425                 exception = e;
426             }
427         }
428     }
429     
430     /** Just a handy routine to load images. */
431     protected static final BufferedImage JavaDoc loadImage(String JavaDoc resName) throws Exception JavaDoc {
432         URL JavaDoc url = GraphicsTestCase.class.getClassLoader().getResource(resName);
433         return ImageIO.read(url);
434     }
435     
436     //Shamelessly stolen from util.IconManager
437
private static final BufferedImage JavaDoc toBufferedImage(Image JavaDoc img) {
438         // load the image
439
new ImageIcon JavaDoc(img);
440         BufferedImage JavaDoc rep = createBufferedImage(img.getWidth(null), img.getHeight(null));
441         Graphics JavaDoc g = rep.createGraphics();
442         g.drawImage(img, 0, 0, null);
443         g.dispose();
444         img.flush();
445         return rep;
446     }
447     
448     /** Creates BufferedImage 16x16 and Transparency.BITMASK */
449     private static final BufferedImage JavaDoc createBufferedImage(int width, int height) {
450         ColorModel JavaDoc model = GraphicsEnvironment.getLocalGraphicsEnvironment().
451                 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(Transparency.BITMASK);
452         BufferedImage JavaDoc buffImage = new BufferedImage JavaDoc(model,
453                 model.createCompatibleWritableRaster(width, height), model.isAlphaPremultiplied(), null);
454         return buffImage;
455     }
456 }
457
Popular Tags