|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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.EventQueue; |
| 25 | +import java.awt.Frame; |
| 26 | +import java.awt.Menu; |
| 27 | +import java.awt.MenuBar; |
| 28 | +import java.awt.MenuItem; |
| 29 | +import java.awt.Point; |
| 30 | +import java.awt.PopupMenu; |
| 31 | +import java.awt.Robot; |
| 32 | +import java.awt.event.InputEvent; |
| 33 | +import java.awt.event.MouseAdapter; |
| 34 | +import java.awt.event.MouseEvent; |
| 35 | +import java.awt.image.BufferedImage; |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import javax.imageio.ImageIO; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @key headful |
| 43 | + * @bug 4234266 |
| 44 | + * @summary To test setLabel functionality on Windows. |
| 45 | + * @requires (os.family == "windows") |
| 46 | + */ |
| 47 | + |
| 48 | +public class SetLabelTest { |
| 49 | + private static Robot robot; |
| 50 | + private static Frame frame; |
| 51 | + private static MenuBar mb; |
| 52 | + private static PopupMenu pm; |
| 53 | + private static Point frameLoc; |
| 54 | + private static final StringBuffer errorLog = new StringBuffer(); |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + try { |
| 58 | + robot = new Robot(); |
| 59 | + |
| 60 | + EventQueue.invokeAndWait(() -> createTestUI()); |
| 61 | + robot.delay(1000); |
| 62 | + EventQueue.invokeAndWait(() -> frameLoc = frame.getLocationOnScreen()); |
| 63 | + |
| 64 | + checkMenu(); |
| 65 | + checkPopupMenu(); |
| 66 | + robot.delay(200); |
| 67 | + if (!errorLog.isEmpty()) { |
| 68 | + throw new RuntimeException("Before & After screenshots are same." + |
| 69 | + " Test fails for the following case(s):\n" + errorLog); |
| 70 | + } |
| 71 | + } finally { |
| 72 | + EventQueue.invokeAndWait(() -> { |
| 73 | + if (frame != null) { |
| 74 | + frame.dispose(); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void createTestUI() { |
| 81 | + frame = new Frame("Menu SetLabel Test"); |
| 82 | + frame.setUndecorated(true); |
| 83 | + mb = new MenuBar(); |
| 84 | + |
| 85 | + //Menu 1 |
| 86 | + Menu menu1 = new Menu("Menu"); |
| 87 | + MenuItem mi1 = new MenuItem("Item-1"); |
| 88 | + menu1.add(mi1); |
| 89 | + menu1.addSeparator(); |
| 90 | + MenuItem mi2 = new MenuItem("Item-2"); |
| 91 | + menu1.add(mi2); |
| 92 | + mb.add(menu1); |
| 93 | + |
| 94 | + //Popup menu |
| 95 | + pm = new PopupMenu("Popup"); |
| 96 | + MenuItem pm1 = new MenuItem("Item-1"); |
| 97 | + pm.add(pm1); |
| 98 | + pm.addSeparator(); |
| 99 | + MenuItem pm2 = new MenuItem("Item-2"); |
| 100 | + pm.add(pm2); |
| 101 | + frame.add(pm); |
| 102 | + |
| 103 | + frame.addMouseListener(new MouseAdapter() { |
| 104 | + @Override |
| 105 | + public void mousePressed(MouseEvent e) { |
| 106 | + showPopup(e); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void mouseReleased(MouseEvent e) { |
| 111 | + showPopup(e); |
| 112 | + } |
| 113 | + |
| 114 | + private void showPopup(MouseEvent e) { |
| 115 | + if (e.isPopupTrigger()) { |
| 116 | + pm.show(e.getComponent(), |
| 117 | + e.getX(), e.getY()); |
| 118 | + } |
| 119 | + } |
| 120 | + }); |
| 121 | + frame.setMenuBar(mb); |
| 122 | + frame.setSize(300, 200); |
| 123 | + frame.setLocation(500,500); |
| 124 | + frame.setVisible(true); |
| 125 | + } |
| 126 | + |
| 127 | + private static void checkMenu() throws IOException { |
| 128 | + robot.mouseMove(frameLoc.x + 8, frameLoc.y + 8); |
| 129 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 130 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 131 | + robot.delay(200); |
| 132 | + BufferedImage beforeImgMenu = robot.createScreenCapture(frame.getBounds()); |
| 133 | + |
| 134 | + //Change Menu & MenuItem label |
| 135 | + Menu m1 = mb.getMenu(0); |
| 136 | + m1.setLabel("New Menu"); |
| 137 | + m1.getItem(0).setLabel("New Item-1"); |
| 138 | + m1.getItem(2).setLabel("New Item-2"); |
| 139 | + robot.delay(200); |
| 140 | + BufferedImage afterImgMenu = robot.createScreenCapture(frame.getBounds()); |
| 141 | + |
| 142 | + if (compareImages(beforeImgMenu, afterImgMenu)) { |
| 143 | + errorLog.append("Menu case\n"); |
| 144 | + ImageIO.write(beforeImgMenu, "png", new File("MenuBefore.png")); |
| 145 | + ImageIO.write(afterImgMenu, "png", new File("MenuAfter.png")); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static void checkPopupMenu() throws IOException { |
| 150 | + robot.mouseMove(frameLoc.x + (frame.getWidth() / 2), |
| 151 | + frameLoc.y + (frame.getHeight() /2)); |
| 152 | + robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); |
| 153 | + robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); |
| 154 | + robot.delay(200); |
| 155 | + BufferedImage beforeImgPopup = robot.createScreenCapture(frame.getBounds()); |
| 156 | + |
| 157 | + //Change popup menu label |
| 158 | + pm.setLabel("Changed Popup"); |
| 159 | + pm.getItem(0).setLabel("New Item-1"); |
| 160 | + pm.getItem(2).setLabel("New Item-2"); |
| 161 | + robot.delay(200); |
| 162 | + BufferedImage afterImgPopup = robot.createScreenCapture(frame.getBounds()); |
| 163 | + robot.mouseMove(frameLoc.x + (frame.getWidth() - 10), |
| 164 | + frameLoc.y + (frame.getHeight() - 10)); |
| 165 | + robot.delay(100); |
| 166 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 167 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 168 | + |
| 169 | + if (compareImages(beforeImgPopup, afterImgPopup)) { |
| 170 | + errorLog.append("Popup case\n"); |
| 171 | + ImageIO.write(beforeImgPopup, "png", new File("PopupBefore.png")); |
| 172 | + ImageIO.write(afterImgPopup, "png", new File("PopupAfter.png")); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static boolean compareImages(BufferedImage beforeImg, BufferedImage afterImg) { |
| 177 | + for (int x = 0; x < beforeImg.getWidth(); x++) { |
| 178 | + for (int y = 0; y < beforeImg.getHeight(); y++) { |
| 179 | + if (beforeImg.getRGB(x, y) != afterImg.getRGB(x, y)) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + return true; |
| 185 | + } |
| 186 | +} |
0 commit comments