|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 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 java.awt.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.EventQueue; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Point; |
| 29 | +import java.awt.Rectangle; |
| 30 | +import java.awt.Robot; |
| 31 | +import java.awt.Toolkit; |
| 32 | +import java.awt.event.WindowAdapter; |
| 33 | +import java.awt.event.WindowEvent; |
| 34 | +import java.awt.image.BufferedImage; |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import javax.imageio.ImageIO; |
| 40 | + |
| 41 | +import jtreg.SkippedException; |
| 42 | + |
| 43 | +/* |
| 44 | + * @test |
| 45 | + * @key headful |
| 46 | + * @bug 6251941 |
| 47 | + * @summary Undecorated frames should be minimizable. |
| 48 | + * @library /test/lib |
| 49 | + * @build jtreg.SkippedException |
| 50 | + * @run main MinimizeUndecoratedTest |
| 51 | + */ |
| 52 | + |
| 53 | +public class MinimizeUndecoratedTest { |
| 54 | + private static final int SIZE = 300; |
| 55 | + private static final CountDownLatch isMinimized = new CountDownLatch(1); |
| 56 | + |
| 57 | + private static Frame frame; |
| 58 | + private static Robot robot; |
| 59 | + |
| 60 | + private static volatile Point frameLoc; |
| 61 | + |
| 62 | + public static void main(String[] args) throws Exception { |
| 63 | + if (!Toolkit.getDefaultToolkit() |
| 64 | + .isFrameStateSupported(Frame.ICONIFIED)) { |
| 65 | + throw new SkippedException("Test is not applicable as" |
| 66 | + + " the Window manager does not support MINIMIZATION"); |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + robot = new Robot(); |
| 71 | + EventQueue.invokeAndWait(MinimizeUndecoratedTest::createUI); |
| 72 | + robot.waitForIdle(); |
| 73 | + robot.delay(1000); |
| 74 | + |
| 75 | + EventQueue.invokeAndWait(() -> frameLoc = frame.getLocationOnScreen()); |
| 76 | + |
| 77 | + Color beforeColor = robot.getPixelColor(frameLoc.x + SIZE / 2, |
| 78 | + frameLoc.y + SIZE / 2); |
| 79 | + |
| 80 | + EventQueue.invokeAndWait(() -> frame.setExtendedState(Frame.ICONIFIED)); |
| 81 | + robot.waitForIdle(); |
| 82 | + robot.delay(500); |
| 83 | + |
| 84 | + if (!isMinimized.await(8, TimeUnit.SECONDS)) { |
| 85 | + throw new RuntimeException("Window iconified event not received."); |
| 86 | + } |
| 87 | + |
| 88 | + EventQueue.invokeAndWait(() -> System.out.println("Frame state: " |
| 89 | + + frame.getExtendedState())); |
| 90 | + Color afterColor = robot.getPixelColor(frameLoc.x + SIZE / 2, |
| 91 | + frameLoc.y + SIZE / 2); |
| 92 | + |
| 93 | + if (beforeColor.equals(afterColor)) { |
| 94 | + saveScreenCapture(); |
| 95 | + throw new RuntimeException("Color before & after minimization : " |
| 96 | + + beforeColor + " vs " + afterColor + "\n" |
| 97 | + + "Test Failed !! Frame not minimized."); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + EventQueue.invokeAndWait(() -> { |
| 101 | + if (frame != null) { |
| 102 | + frame.setExtendedState(Frame.NORMAL); |
| 103 | + frame.dispose(); |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static void createUI() { |
| 110 | + frame = new Frame("Test Minimization of Frame"); |
| 111 | + frame.setSize(SIZE, SIZE); |
| 112 | + frame.setBackground(Color.GREEN); |
| 113 | + frame.setResizable(true); |
| 114 | + frame.setUndecorated(true); |
| 115 | + frame.addWindowStateListener(new WindowAdapter() { |
| 116 | + @Override |
| 117 | + public void windowStateChanged(WindowEvent e) { |
| 118 | + if (e.getNewState() == Frame.ICONIFIED) { |
| 119 | + System.out.println("Window iconified event received."); |
| 120 | + isMinimized.countDown(); |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + frame.setLocationRelativeTo(null); |
| 126 | + frame.setVisible(true); |
| 127 | + } |
| 128 | + |
| 129 | + private static void saveScreenCapture() { |
| 130 | + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
| 131 | + BufferedImage image = robot.createScreenCapture(new Rectangle(new Point(), |
| 132 | + screenSize)); |
| 133 | + try { |
| 134 | + ImageIO.write(image, "png", new File("MinimizedFrame.png")); |
| 135 | + } catch (IOException e) { |
| 136 | + e.printStackTrace(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments