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