1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.AWTException;
  25 import java.awt.Color;
  26 import java.awt.Component;
  27 import java.awt.Dimension;
  28 import java.awt.Frame;
  29 import java.awt.Graphics;
  30 import java.awt.Point;
  31 import java.awt.Rectangle;
  32 import java.awt.Robot;
  33 import java.awt.Toolkit;
  34 import java.awt.image.BufferedImage;
  35 import java.awt.image.VolatileImage;
  36 
  37 /*
  38  *
  39  *
  40  * Tests that the use of shared memory pixmaps isn't broken:
  41  * create a VolatileImage, fill it with red color, copy it to the screen
  42  * make sure the pixels on the screen are red.
  43  *
  44  * Note that we force the use of shared memory pixmaps in the shell script.
  45  *
  46  * @author Dmitri.Trembovetski
  47  */
  48 
  49 public class SharedMemoryPixmapsTest {
  50     static final int IMAGE_SIZE = 100;
  51     static boolean show = false;
  52     final Frame testFrame;
  53     /** Creates a new instance of SharedMemoryPixmapsTest */
  54     public SharedMemoryPixmapsTest() {
  55         testFrame = new Frame("SharedMemoryPixmapsTest");
  56         testFrame.add(new TestComponent());
  57         testFrame.pack();
  58         testFrame.setVisible(true);
  59     }
  60 
  61     public static void main(String[] args) {
  62         for (String s : args) {
  63             if ("-show".equals(s)) {
  64                 show = true;
  65             } else {
  66                 System.err.println("Usage: SharedMemoryPixmapsTest [-show]");
  67             }
  68         }
  69         new SharedMemoryPixmapsTest();
  70     }
  71 
  72     private class TestComponent extends Component {
  73         VolatileImage vi = null;
  74         boolean tested = false;
  75 
  76         void initVI() {
  77             int res;
  78             if (vi == null) {
  79                 res = VolatileImage.IMAGE_INCOMPATIBLE;
  80             } else {
  81                 res = vi.validate(getGraphicsConfiguration());
  82             }
  83             if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
  84                 if (vi != null) vi.flush();
  85                 vi = createVolatileImage(IMAGE_SIZE, IMAGE_SIZE);
  86                 vi.validate(getGraphicsConfiguration());
  87                 res = VolatileImage.IMAGE_RESTORED;
  88             }
  89             if (res == VolatileImage.IMAGE_RESTORED) {
  90                 Graphics vig = vi.getGraphics();
  91                 vig.setColor(Color.red);
  92                 vig.fillRect(0, 0, vi.getWidth(), vi.getHeight());
  93                 vig.dispose();
  94             }
  95         }
  96 
  97         @Override
  98         public synchronized void paint(Graphics g) {
  99             do {
 100                 g.setColor(Color.green);
 101                 g.fillRect(0, 0, getWidth(), getHeight());
 102 
 103                 initVI();
 104                 g.drawImage(vi, 0, 0, null);
 105             } while (vi.contentsLost());
 106 
 107             Toolkit.getDefaultToolkit().sync();
 108             if (!tested) {
 109                 if (testRendering()) {
 110                     System.err.println("Test Passed");
 111                 } else {
 112                     System.err.println("Test Failed");
 113                 }
 114                 tested = true;
 115             }
 116             if (!show) {
 117                 testFrame.setVisible(false);
 118                 testFrame.dispose();
 119             }
 120         }
 121 
 122         private boolean testRendering() throws RuntimeException {
 123             try {
 124                 Thread.sleep(2000);
 125             } catch (InterruptedException ex) {}
 126             Robot r = null;
 127             try {
 128                 r = new Robot();
 129             } catch (AWTException ex) {
 130                 ex.printStackTrace();
 131                 throw new RuntimeException("Can't create Robot");
 132             }
 133             Point p = getLocationOnScreen();
 134             BufferedImage b =
 135                 r.createScreenCapture(new Rectangle(p, getPreferredSize()));
 136             for (int y = 0; y < b.getHeight(); y++) {
 137                 for (int x = 0; x < b.getWidth(); x++) {
 138                     if (b.getRGB(x, y) != Color.red.getRGB()) {
 139                         System.err.println("Incorrect pixel" + " at "
 140                             + x + "x" + y + " : " +
 141                             Integer.toHexString(b.getRGB(x, y)));
 142                         if (show) {
 143                             return false;
 144                         }
 145                         System.err.println("Test Failed");
 146                         System.exit(1);
 147                     }
 148                 }
 149             }
 150             return true;
 151         }
 152 
 153         @Override
 154         public Dimension getPreferredSize() {
 155             return new Dimension(IMAGE_SIZE, IMAGE_SIZE);
 156         }
 157     }
 158 
 159 }