Path: blob/master/test/jdk/java/awt/Graphics2D/CopyAreaOOB.java
41149 views
/*1* Copyright (c) 2015, 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*/2223/*24* @test25* @key headful26* @bug 6430601 819861327* @summary Verifies that copyArea() works properly when the28* destination parameters are outside the destination bounds.29* @run main/othervm CopyAreaOOB30* @author campbelc31*/3233import java.awt.*;34import java.awt.image.*;3536public class CopyAreaOOB extends Canvas {3738private static boolean done;3940public void paint(Graphics g) {41synchronized (this) {42if (done) {43return;44}45}4647int w = getWidth();48int h = getHeight();4950Graphics2D g2d = (Graphics2D)g;51g2d.setColor(Color.black);52g2d.fillRect(0, 0, w, h);5354g2d.setColor(Color.green);55g2d.fillRect(0, 0, w, 10);5657g2d.setColor(Color.red);58g2d.fillRect(0, 10, 50, h-10);5960// copy the region such that part of it goes below the bottom of the61// destination surface62g2d.copyArea(0, 10, 50, h-10, 60, 10);6364Toolkit.getDefaultToolkit().sync();6566synchronized (this) {67done = true;68notifyAll();69}70}7172public Dimension getPreferredSize() {73return new Dimension(400, 400);74}7576private static void testRegion(BufferedImage bi, String name,77int x1, int y1, int x2, int y2,78int expected)79{80for (int y = y1; y < y2; y++) {81for (int x = x1; x < x2; x++) {82int actual = bi.getRGB(x, y);83if (actual != expected) {84throw new RuntimeException("Test failed for " + name +85" region at x="+x+" y="+y+86" (expected="+87Integer.toHexString(expected) +88" actual="+89Integer.toHexString(actual) +90")");91}92}93}94}9596public static void main(String[] args) {97boolean show = (args.length == 1) && ("-show".equals(args[0]));9899CopyAreaOOB test = new CopyAreaOOB();100Frame frame = new Frame();101frame.setUndecorated(true);102frame.add(test);103frame.pack();104frame.setLocationRelativeTo(null);105frame.setVisible(true);106107// Wait until the component's been painted108synchronized (test) {109while (!done) {110try {111test.wait();112} catch (InterruptedException e) {113throw new RuntimeException("Failed: Interrupted");114}115}116}117118try {119Thread.sleep(2000);120} catch (InterruptedException ex) {}121122// Grab the screen region123BufferedImage capture = null;124try {125Robot robot = new Robot();126Point pt1 = test.getLocationOnScreen();127Rectangle rect = new Rectangle(pt1.x, pt1.y, 400, 400);128capture = robot.createScreenCapture(rect);129} catch (Exception e) {130throw new RuntimeException("Problems creating Robot");131} finally {132if (!show) {133frame.dispose();134}135}136137// Test pixels138testRegion(capture, "green", 0, 0, 400, 10, 0xff00ff00);139testRegion(capture, "original red", 0, 10, 50, 400, 0xffff0000);140testRegion(capture, "background", 50, 10, 60, 400, 0xff000000);141testRegion(capture, "in-between", 60, 10, 110, 20, 0xff000000);142testRegion(capture, "copied red", 60, 20, 110, 400, 0xffff0000);143testRegion(capture, "background", 110, 10, 400, 400, 0xff000000);144}145}146147148