1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.DisplayMode ; 25 import java.awt.FlowLayout ; 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.awt.Graphics ; 29 import java.awt.GraphicsEnvironment ; 30 import java.awt.Image ; 31 import java.awt.Point ; 32 import java.awt.Toolkit ; 33 import java.awt.Transparency ; 34 import java.awt.image.BufferedImage ; 35 import java.awt.image.ColorModel ; 36 import java.net.URL ; 37 import javax.imageio.ImageIO ; 38 import javax.swing.Icon ; 39 import javax.swing.ImageIcon ; 40 import javax.swing.JComponent ; 41 import javax.swing.JFrame ; 42 import javax.swing.JLabel ; 43 import javax.swing.JTable ; 44 import javax.swing.UIManager ; 45 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow; 46 import org.openide.explorer.propertysheet.ExtTestCase.WrapperRunnable; 47 48 51 public class GraphicsTestCase extends ExtTestCase { 52 private static int count=0; 53 54 55 public GraphicsTestCase(String name) { 56 super(name); 57 } 58 59 public void testNothing() { 60 } 62 63 public static void main(String [] args) { 64 ExtTestCase.main(args); 65 System.err.println("Can safely run pixel tests: " + canSafelyRunPixelTests()); 68 } 71 72 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 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 f = UIManager.getFont("controlFont"); 98 if (f == null) { 99 f = new JTable ().getFont(); 100 } 101 Graphics g = GraphicsEnvironment.getLocalGraphicsEnvironment(). 102 getDefaultScreenDevice().getDefaultConfiguration(). 103 createCompatibleImage(10,10).getGraphics(); 104 105 FontMetrics 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 result = false; 114 } 115 } 116 if (result) { 117 result = tryPrototypePixelTest(); 118 } 119 return result; 120 } catch (Exception e) { 121 graphicsTestsSafe = Boolean.FALSE; 122 e.printStackTrace(); 123 return false; 124 } 125 } 126 127 private static Boolean graphicsTestsSafe = null; 128 130 private static boolean tryPrototypePixelTest() throws Exception { 131 if (graphicsTestsSafe != null) { 132 return graphicsTestsSafe.booleanValue(); 133 } 134 try { 135 jf = new JFrame (); 136 final JLabel jl = new JLabel ("Show the dialog"); 137 jl.setOpaque(true); 138 jl.setIcon(new TestIcon()); 139 140 jf.getContentPane().setLayout(new FlowLayout ()); 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 (5,y)); 155 156 final BufferedImage img = 157 loadImage("org/netbeans/core/resources/defaultOpenFolder.gif"); 158 159 if (result) { 160 maybeInvokeLater(new Runnable () { 161 public void run() { 162 try { 163 jl.setIcon(new ImageIcon (img)); 164 } catch (Exception e) { 165 throw new RuntimeException (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 e) { 176 throw new RuntimeException (e); 177 } finally { 178 if (jf != null) { 179 } 182 } 183 } 184 185 private static class TestIcon implements Icon { 186 public int getIconHeight() { 187 return 20; 188 } 189 190 public int getIconWidth() { 191 return 20; 192 } 193 194 public void paintIcon(Component c, Graphics g, int x, int y) { 195 g.setColor(Color.GREEN); 196 g.fillRect(x, y, 20, 20); 197 } 198 } 199 200 201 protected static void assertColorOnComponent(Component c, Color color, Point p) throws Exception { 202 ComponentPixelChecker cpc = new ComponentPixelChecker(c, color, p); 203 click(c); 204 maybeInvokeLater(cpc); 205 cpc.assertMatch(); 206 } 207 208 209 protected static void assertColorOnComponent(Component c, Color color, int x, int y) throws Exception { 210 assertColorOnComponent(c, color, new Point (x,y)); 211 } 212 213 214 216 protected static void assertPixelFromImage(final Image i, final Component c, final int imageX, final int imageY, final int compX, final int compY) throws Exception { 217 ImagePixelChecker pix = new ImagePixelChecker(i, c, imageX, imageY, compX, compY); 218 maybeInvokeLater(pix); 219 pix.assertMatch(); 220 } 221 222 226 private static boolean checkPixelFromImage(final Image i, final Component c, final int imageX, final int imageY, final int compX, final int compY) throws Exception { 227 ImagePixelChecker pix = new ImagePixelChecker(i, c, imageX, imageY, compX, compY); 228 maybeInvokeLater(pix); 229 return pix.compare(); 230 } 231 232 233 237 private static boolean checkColorOnComponent(Component c, Color color, Point p) throws Exception { 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 247 private static class ComponentPixelChecker extends WrapperRunnable { 248 private Component comp; 249 private Point pos; 250 private Color color; 251 public ComponentPixelChecker(Component c, Color color, Point pos) { 252 this.comp = c; 253 this.color = color; 254 this.pos = pos; 255 } 256 257 private Color compColor = null; 258 private Boolean finalResult = null; 259 public Color getCompColor() throws Exception { 260 if (compColor == null) { 261 compare(); 262 } 263 return compColor; 264 } 265 266 public Color getColor() { 267 return color; 268 } 269 270 BufferedImage compImg = null; 271 public boolean compare() throws Exception { 272 if (finalResult != null) { 273 return finalResult.booleanValue(); 274 } 275 BufferedImage img = getImageOfComponent(); 276 int[] cArr = new int[3]; 277 img.getData().getPixel(pos.x, pos.y, cArr); 278 Color compColor = new Color (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 { 286 assertTrue("Component pixel at " + pos + " is " + getCompColor() + " but should be " + getColor(), compare()); 287 } 288 289 public BufferedImage getImageOfComponent() throws Exception { 290 ((JComponent )comp).paintImmediately(0,0,comp.getWidth(), comp.getHeight()); 292 293 sleep(); 294 sleep(); 295 final BufferedImage result = new BufferedImage (comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB); 296 sleep(); 297 sleep(); 298 Toolkit.getDefaultToolkit().sync(); 299 ((JComponent ) comp).paintAll(result.getGraphics()); 300 sleep(); 301 showFrameForImage(result); 302 return result; 303 } 304 305 private void showFrameForImage(final BufferedImage bi) throws Exception { 306 JFrame jf = new JFrame ("assertPixelFromImage " + (count ++) + " (look for the yellow line)") { 307 public void paint(Graphics g) { 308 new ImageIcon (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 320 private static class ImagePixelChecker extends WrapperRunnable { 321 private Component comp; 322 private Image image; 323 private Point imgPoint; 324 private Point compPoint; 325 326 public ImagePixelChecker(Image i, Component c, int imageX, int imageY, int compX, int compY) { 327 this(i, c, new Point (imageX, imageY), new Point (compX, compY)); 328 } 329 330 public ImagePixelChecker(Image i, Component c, Point imgPoint, Point compPoint) { 331 this.image = i; 332 this.comp = c; 333 this.imgPoint = imgPoint; 334 this.compPoint = compPoint; 335 } 336 337 public BufferedImage getImageOfComponent() throws Exception { 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 result = new BufferedImage (comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB); 343 sleep(); 344 sleep(); 345 ((JComponent ) comp).paintAll(result.getGraphics()); 346 sleep(); 347 showFrameForImage(result, compPoint); 348 return result; 349 } 350 351 private void showFrameForImage(final BufferedImage bi, final Point p) throws Exception { 352 JFrame jf = new JFrame ("assertPixelFromImage " + (count ++) + " (look for the yellow line)") { 353 public void paint(Graphics g) { 354 new ImageIcon (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 getImage() throws Exception { 364 sleep(); 365 BufferedImage result = image instanceof BufferedImage ? (BufferedImage ) 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 finalResult = null; 376 377 private Color compColor = null; 378 private Color imgColor = null; 379 380 public Color getCompColor() throws Exception { 381 if (compColor == null) { 382 compare(); 383 } 384 return compColor; 385 } 386 387 public Color getImageColor() throws Exception { 388 if (imgColor == null) { 389 compare(); 390 } 391 return imgColor; 392 } 393 394 public boolean compare() throws Exception { 395 if (finalResult != null) { 396 return finalResult.booleanValue(); 397 } 398 BufferedImage compImg = getImageOfComponent(); 399 BufferedImage img = getImage(); 400 compColor = new Color (compImg.getRGB(compPoint.x, compPoint.y)); 401 imgColor = new Color (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 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 { 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 e){ 425 exception = e; 426 } 427 } 428 } 429 430 431 protected static final BufferedImage loadImage(String resName) throws Exception { 432 URL url = GraphicsTestCase.class.getClassLoader().getResource(resName); 433 return ImageIO.read(url); 434 } 435 436 private static final BufferedImage toBufferedImage(Image img) { 438 new ImageIcon (img); 440 BufferedImage rep = createBufferedImage(img.getWidth(null), img.getHeight(null)); 441 Graphics g = rep.createGraphics(); 442 g.drawImage(img, 0, 0, null); 443 g.dispose(); 444 img.flush(); 445 return rep; 446 } 447 448 449 private static final BufferedImage createBufferedImage(int width, int height) { 450 ColorModel model = GraphicsEnvironment.getLocalGraphicsEnvironment(). 451 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(Transparency.BITMASK); 452 BufferedImage buffImage = new BufferedImage (model, 453 model.createCompatibleWritableRaster(width, height), model.isAlphaPremultiplied(), null); 454 return buffImage; 455 } 456 } 457 | Popular Tags |