|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, 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 | +/* |
| 25 | + * @test |
| 26 | + * @key headful |
| 27 | + * @bug 8139581 |
| 28 | + * @summary Verify that components are redrawn after |
| 29 | + * removal and addition to a container |
| 30 | + * @run main ComponentRedrawnTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.BorderLayout; |
| 34 | +import java.awt.Button; |
| 35 | +import java.awt.Color; |
| 36 | +import java.awt.EventQueue; |
| 37 | +import java.awt.Frame; |
| 38 | +import java.awt.Graphics; |
| 39 | +import java.awt.Panel; |
| 40 | +import java.awt.Robot; |
| 41 | +import java.awt.event.ActionEvent; |
| 42 | +import java.awt.event.MouseEvent; |
| 43 | +import java.util.concurrent.atomic.AtomicInteger; |
| 44 | + |
| 45 | +import javax.swing.JButton; |
| 46 | + |
| 47 | +public class ComponentRedrawnTest { |
| 48 | + |
| 49 | + private static Frame frame; |
| 50 | + private static Panel componentPanel; |
| 51 | + private static Button buttonRemove; |
| 52 | + private static Button buttonAdd; |
| 53 | + private static Button awtButton; |
| 54 | + |
| 55 | + private static volatile Robot robot; |
| 56 | + private static volatile int x; |
| 57 | + private static volatile int y; |
| 58 | + private static AtomicInteger awtPainted = new AtomicInteger(); |
| 59 | + private static AtomicInteger swingPainted = new AtomicInteger(); |
| 60 | + |
| 61 | + public static void main(String args[]) throws Exception { |
| 62 | + try { |
| 63 | + EventQueue.invokeAndWait(() -> createGUI()); |
| 64 | + runTest(); |
| 65 | + System.out.println("Test Passed"); |
| 66 | + } finally { |
| 67 | + EventQueue.invokeAndWait(() -> dispose()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static void createGUI() { |
| 72 | + frame = new Frame("ComponentRedrawnTest"); |
| 73 | + frame.setSize(350, 300); |
| 74 | + frame.setBackground(Color.red); |
| 75 | + |
| 76 | + componentPanel = new Panel(); |
| 77 | + componentPanel.setLayout(null); |
| 78 | + componentPanel.setBackground(Color.green); |
| 79 | + |
| 80 | + awtButton = new Button("AWT Button") { |
| 81 | + @Override |
| 82 | + public void paint(Graphics g) { |
| 83 | + super.paint(g); |
| 84 | + awtPainted.incrementAndGet(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + awtButton.setBounds(0, 0, 330, 100); |
| 89 | + componentPanel.add(awtButton); |
| 90 | + |
| 91 | + JButton swingButton = new JButton("Swing JButton") { |
| 92 | + @Override |
| 93 | + public void paint(Graphics g) { |
| 94 | + super.paint(g); |
| 95 | + swingPainted.incrementAndGet(); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + swingButton.setBounds(0, 100, 330, 100); |
| 100 | + componentPanel.add(swingButton); |
| 101 | + frame.add(componentPanel, BorderLayout.CENTER); |
| 102 | + buttonRemove = new Button("remove"); |
| 103 | + buttonRemove.addActionListener(ae -> buttonClicked(ae)); |
| 104 | + |
| 105 | + buttonAdd = new Button("add"); |
| 106 | + buttonAdd.addActionListener(ae -> buttonClicked(ae)); |
| 107 | + |
| 108 | + Panel controlPanel = new Panel(); |
| 109 | + controlPanel.setLayout(new BorderLayout()); |
| 110 | + controlPanel.add(buttonRemove, BorderLayout.NORTH); |
| 111 | + controlPanel.add(buttonAdd, BorderLayout.SOUTH); |
| 112 | + |
| 113 | + frame.add(controlPanel, BorderLayout.SOUTH); |
| 114 | + frame.setLocationRelativeTo(null); |
| 115 | + frame.setVisible(true); |
| 116 | + } |
| 117 | + |
| 118 | + private static void buttonClicked(ActionEvent ae) { |
| 119 | + if (ae.getSource() == buttonRemove) { |
| 120 | + frame.remove(componentPanel); |
| 121 | + } else if (ae.getSource() == buttonAdd) { |
| 122 | + frame.add(componentPanel); |
| 123 | + } |
| 124 | + frame.invalidate(); |
| 125 | + frame.validate(); |
| 126 | + } |
| 127 | + |
| 128 | + private static void runTest() throws Exception { |
| 129 | + EventQueue.invokeAndWait(() -> createGUI()); |
| 130 | + robot = new Robot(); |
| 131 | + robot.setAutoDelay(500); |
| 132 | + awtPainted.set(0); |
| 133 | + swingPainted.set(0); |
| 134 | + |
| 135 | + try { |
| 136 | + EventQueue.invokeAndWait(() -> { |
| 137 | + x = awtButton.getLocationOnScreen().x |
| 138 | + + awtButton.getSize().width / 2; |
| 139 | + y = awtButton.getLocationOnScreen().y |
| 140 | + + awtButton.getSize().height / 2; |
| 141 | + }); |
| 142 | + } catch (Exception e) { |
| 143 | + throw new RuntimeException("Unexpected Exception encountered: " + e); |
| 144 | + } |
| 145 | + |
| 146 | + robot.mouseMove(x, y); |
| 147 | + robot.waitForIdle(); |
| 148 | + robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); |
| 149 | + robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); |
| 150 | + |
| 151 | + try { |
| 152 | + EventQueue.invokeAndWait(() -> { |
| 153 | + x = buttonRemove.getLocationOnScreen().x |
| 154 | + + buttonRemove.getSize().width / 2; |
| 155 | + y = buttonRemove.getLocationOnScreen().y |
| 156 | + + buttonRemove.getSize().height / 2; |
| 157 | + }); |
| 158 | + } catch (Exception e) { |
| 159 | + throw new RuntimeException("Unexpected Exception encountered: " + e); |
| 160 | + } |
| 161 | + |
| 162 | + robot.mouseMove(x, y); |
| 163 | + robot.waitForIdle(); |
| 164 | + robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); |
| 165 | + robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); |
| 166 | + |
| 167 | + try { |
| 168 | + EventQueue.invokeAndWait(() -> { |
| 169 | + x = buttonAdd.getLocationOnScreen().x |
| 170 | + + buttonAdd.getSize().width / 2; |
| 171 | + y = buttonAdd.getLocationOnScreen().y |
| 172 | + + buttonAdd.getSize().height / 2; |
| 173 | + }); |
| 174 | + |
| 175 | + } catch (Exception e) { |
| 176 | + throw new RuntimeException("Unexpected Exception encountered: " + e); |
| 177 | + } |
| 178 | + robot.mouseMove(x, y); |
| 179 | + robot.waitForIdle(); |
| 180 | + robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); |
| 181 | + robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); |
| 182 | + |
| 183 | + if (awtPainted.get() == 0) { |
| 184 | + throw new RuntimeException("AWT button is not painted"); |
| 185 | + } |
| 186 | + if (swingPainted.get() == 0) { |
| 187 | + throw new RuntimeException("Swing button is not painted"); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private static void dispose() { |
| 192 | + if (frame != null) { |
| 193 | + frame.dispose(); |
| 194 | + frame = null; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments