|
| 1 | +/* |
| 2 | + * Copyright (c) 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 6318027 |
| 27 | + * @key headful |
| 28 | + * @summary Verifies BasicScrollBarUI disables timer when enclosing frame is disabled |
| 29 | + * @run main DisableFrameFromScrollBar |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.FlowLayout; |
| 33 | +import java.awt.Point; |
| 34 | +import java.awt.Rectangle; |
| 35 | +import java.awt.Robot; |
| 36 | +import java.awt.event.AdjustmentEvent; |
| 37 | +import java.awt.event.AdjustmentListener; |
| 38 | +import java.awt.event.InputEvent; |
| 39 | + |
| 40 | +import javax.swing.JFrame; |
| 41 | +import javax.swing.JScrollBar; |
| 42 | +import javax.swing.SwingUtilities; |
| 43 | +import javax.swing.UIManager; |
| 44 | +import javax.swing.UnsupportedLookAndFeelException; |
| 45 | +import javax.swing.event.ChangeEvent; |
| 46 | +import javax.swing.event.ChangeListener; |
| 47 | + |
| 48 | +public class DisableFrameFromScrollBar { |
| 49 | + |
| 50 | + private static JFrame frame; |
| 51 | + private static JScrollBar bar; |
| 52 | + private static int oldValue; |
| 53 | + private static volatile boolean doCheck; |
| 54 | + private static volatile boolean isAdjusting; |
| 55 | + |
| 56 | + private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 57 | + try { |
| 58 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 59 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 60 | + System.out.println("Unsupported LAF: " + laf.getClassName()); |
| 61 | + } catch (ClassNotFoundException | InstantiationException |
| 62 | + | IllegalAccessException e) { |
| 63 | + throw new RuntimeException(e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static void createUI() { |
| 68 | + frame = new JFrame(DisableFrameFromScrollBar.class.getName()); |
| 69 | + bar = new JScrollBar(); |
| 70 | + bar.getModel().addChangeListener(new DisableChangeListener(frame)); |
| 71 | + frame.getContentPane().setLayout(new FlowLayout()); |
| 72 | + frame.add(bar); |
| 73 | + |
| 74 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 75 | + frame.setSize(150, 150); |
| 76 | + frame.setLocationRelativeTo(null); |
| 77 | + frame.setVisible(true); |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String[] args) throws Exception { |
| 81 | + for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { |
| 82 | + System.out.println("Testing LAF : " + laf.getClassName()); |
| 83 | + Robot robot = new Robot(); |
| 84 | + robot.setAutoDelay(100); |
| 85 | + try { |
| 86 | + SwingUtilities.invokeAndWait(() -> { |
| 87 | + setLookAndFeel(laf); |
| 88 | + createUI(); |
| 89 | + }); |
| 90 | + |
| 91 | + robot.waitForIdle(); |
| 92 | + robot.delay(1000); |
| 93 | + Point point = getClickPoint(); |
| 94 | + robot.mouseMove(point.x, point.y); |
| 95 | + robot.waitForIdle(); |
| 96 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 97 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 98 | + |
| 99 | + SwingUtilities.invokeAndWait(() -> { |
| 100 | + oldValue = bar.getValue(); |
| 101 | + bar.addAdjustmentListener(new AdjustmentListener() { |
| 102 | + public void adjustmentValueChanged(AdjustmentEvent e) { |
| 103 | + int curValue = e.getValue(); |
| 104 | + int extent = bar.getMaximum() - bar.getVisibleAmount(); |
| 105 | + if (curValue < extent && curValue != oldValue) { |
| 106 | + oldValue = curValue; |
| 107 | + isAdjusting = true; |
| 108 | + } else { |
| 109 | + doCheck = true; |
| 110 | + isAdjusting = false; |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
| 114 | + }); |
| 115 | + do { |
| 116 | + Thread.sleep(200); |
| 117 | + } while (isAdjusting && !doCheck); |
| 118 | + if (bar.getValue() == (bar.getMaximum() - bar.getVisibleAmount())) { |
| 119 | + throw new RuntimeException("ScrollBar didn't disable timer"); |
| 120 | + } |
| 121 | + } finally { |
| 122 | + SwingUtilities.invokeAndWait(() -> { |
| 123 | + if (frame != null) { |
| 124 | + frame.dispose(); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static Point getClickPoint() throws Exception { |
| 132 | + final Point[] result = new Point[1]; |
| 133 | + |
| 134 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 135 | + |
| 136 | + @Override |
| 137 | + public void run() { |
| 138 | + Point p = bar.getLocationOnScreen(); |
| 139 | + Rectangle rect = bar.getBounds(); |
| 140 | + result[0] = new Point((int) (p.x + rect.width / 2), |
| 141 | + (int) (p.y + rect.height - 10)); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + return result[0]; |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + public static class DisableChangeListener implements ChangeListener { |
| 150 | + private final JFrame m_frame; |
| 151 | + private boolean m_done; |
| 152 | + |
| 153 | + public DisableChangeListener(JFrame p_frame) { |
| 154 | + m_frame = p_frame; |
| 155 | + } |
| 156 | + |
| 157 | + public void stateChanged(ChangeEvent p_e) { |
| 158 | + if (!m_done) { |
| 159 | + m_frame.setEnabled(false); |
| 160 | + Thread t = new Thread(new Enabler(m_frame)); |
| 161 | + t.start(); |
| 162 | + m_done = true; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public static class Enabler implements Runnable { |
| 168 | + private JFrame m_frame; |
| 169 | + |
| 170 | + Enabler(JFrame p_frame) { |
| 171 | + m_frame = p_frame; |
| 172 | + } |
| 173 | + |
| 174 | + public void run() { |
| 175 | + try { |
| 176 | + Thread.sleep(1000); |
| 177 | + } |
| 178 | + catch (InterruptedException e) { |
| 179 | + e.printStackTrace(); |
| 180 | + } |
| 181 | + m_frame.setEnabled(true); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
0 commit comments