|
| 1 | +/* |
| 2 | + * Copyright (c) 1998, 2024, 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 javax.imageio.ImageIO; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.EventQueue; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Menu; |
| 29 | +import java.awt.MenuBar; |
| 30 | +import java.awt.Rectangle; |
| 31 | +import java.awt.Robot; |
| 32 | +import java.awt.image.BufferedImage; |
| 33 | +import java.io.File; |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @key headful |
| 39 | + * @bug 4127271 |
| 40 | + * @summary Tests that disposing of a Frame with MenuBar removes all traces |
| 41 | + * of the Frame from the screen. |
| 42 | + */ |
| 43 | + |
| 44 | +public class DisposeTest { |
| 45 | + private static Frame backgroundFrame; |
| 46 | + private static Frame testedFrame; |
| 47 | + |
| 48 | + private static final Rectangle backgroundFrameBounds = |
| 49 | + new Rectangle(100, 100, 200, 200); |
| 50 | + private static final Rectangle testedFrameBounds = |
| 51 | + new Rectangle(150, 150, 100, 100); |
| 52 | + |
| 53 | + private static Robot robot; |
| 54 | + |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + robot = new Robot(); |
| 57 | + try { |
| 58 | + EventQueue.invokeAndWait(DisposeTest::initAndShowGui); |
| 59 | + robot.waitForIdle(); |
| 60 | + robot.delay(500); |
| 61 | + EventQueue.invokeAndWait(testedFrame::dispose); |
| 62 | + robot.waitForIdle(); |
| 63 | + robot.delay(500); |
| 64 | + test(); |
| 65 | + } finally { |
| 66 | + EventQueue.invokeAndWait(() -> { |
| 67 | + backgroundFrame.dispose(); |
| 68 | + testedFrame.dispose(); |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static void test() { |
| 74 | + BufferedImage bi = robot.createScreenCapture(backgroundFrameBounds); |
| 75 | + int redPix = Color.RED.getRGB(); |
| 76 | + |
| 77 | + for (int x = 0; x < bi.getWidth(); x++) { |
| 78 | + for (int y = 0; y < bi.getHeight(); y++) { |
| 79 | + if (bi.getRGB(x, y) != redPix) { |
| 80 | + try { |
| 81 | + ImageIO.write(bi, "png", |
| 82 | + new File("failure.png")); |
| 83 | + } catch (IOException ignored) {} |
| 84 | + throw new RuntimeException("Test failed"); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void initAndShowGui() { |
| 91 | + backgroundFrame = new Frame("DisposeTest background"); |
| 92 | + backgroundFrame.setUndecorated(true); |
| 93 | + backgroundFrame.setBackground(Color.RED); |
| 94 | + backgroundFrame.setBounds(backgroundFrameBounds); |
| 95 | + backgroundFrame.setVisible(true); |
| 96 | + |
| 97 | + testedFrame = new UglyFrame(); |
| 98 | + } |
| 99 | + |
| 100 | + static class UglyFrame extends Frame { |
| 101 | + public UglyFrame() { |
| 102 | + super("DisposeTest"); |
| 103 | + MenuBar mb = new MenuBar(); |
| 104 | + Menu m = new Menu("menu"); |
| 105 | + mb.add(m); |
| 106 | + setMenuBar(mb); |
| 107 | + setBounds(testedFrameBounds); |
| 108 | + setVisible(true); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments