|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2022, 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.BorderLayout; |
| 25 | +import java.awt.Point; |
| 26 | +import java.awt.Robot; |
| 27 | +import java.awt.event.ActionEvent; |
| 28 | +import java.awt.event.ActionListener; |
| 29 | +import java.awt.event.InputEvent; |
| 30 | +import java.lang.reflect.InvocationTargetException; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 34 | +import java.util.concurrent.atomic.AtomicReference; |
| 35 | +import java.util.stream.Collectors; |
| 36 | +import javax.swing.AbstractButton; |
| 37 | +import javax.swing.JButton; |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.JPanel; |
| 40 | +import javax.swing.JSplitPane; |
| 41 | +import javax.swing.JToggleButton; |
| 42 | +import javax.swing.SwingUtilities; |
| 43 | +import javax.swing.UIManager; |
| 44 | +import javax.swing.UIManager.LookAndFeelInfo; |
| 45 | +import javax.swing.UnsupportedLookAndFeelException; |
| 46 | + |
| 47 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 48 | +/* |
| 49 | + * @test |
| 50 | + * @key headful |
| 51 | + * @bug 4615365 |
| 52 | + * @summary This test confirms that the JSplitPane's current and last |
| 53 | + * divider positions are correct when realized. |
| 54 | + * @run main JSplitPaneDividerLocationTest |
| 55 | + */ |
| 56 | +public class JSplitPaneDividerLocationTest { |
| 57 | + |
| 58 | + private static JFrame frame; |
| 59 | + private static JPanel panel; |
| 60 | + private static JButton leftButton; |
| 61 | + private static JToggleButton triggerButton; |
| 62 | + private static volatile int currentLoc; |
| 63 | + private static volatile int lastLoc; |
| 64 | + private static volatile int lastLocExpected; |
| 65 | + private static Robot robot; |
| 66 | + |
| 67 | + public static void main(String[] s) throws Exception { |
| 68 | + robot = new Robot(); |
| 69 | + robot.setAutoWaitForIdle(true); |
| 70 | + robot.setAutoDelay(200); |
| 71 | + List<String> lafs = Arrays.stream(getInstalledLookAndFeels()) |
| 72 | + .map(LookAndFeelInfo::getClassName) |
| 73 | + .collect(Collectors.toList()); |
| 74 | + for (final String laf : lafs) { |
| 75 | + try { |
| 76 | + AtomicBoolean lafSetSuccess = new AtomicBoolean(false); |
| 77 | + SwingUtilities.invokeAndWait(() -> { |
| 78 | + lafSetSuccess.set(setLookAndFeel(laf)); |
| 79 | + if (lafSetSuccess.get()) { |
| 80 | + createUI(); |
| 81 | + } |
| 82 | + }); |
| 83 | + if (!lafSetSuccess.get()) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + robot.waitForIdle(); |
| 87 | + |
| 88 | + pressButton(triggerButton); |
| 89 | + |
| 90 | + // Verifies that JSplitPane current and last divider |
| 91 | + // positions are correct and not as per JDK-4615365. |
| 92 | + if ((currentLoc == -1) || (lastLoc == 0)) { |
| 93 | + throw new RuntimeException( |
| 94 | + "Test failed for " + laf + " :- last divider loc:" + |
| 95 | + "actual = " + lastLoc + ",expected = -1, current " + |
| 96 | + "divider loc:actual=" + currentLoc + ",expected>0"); |
| 97 | + } |
| 98 | + lastLocExpected = currentLoc; |
| 99 | + |
| 100 | + // Slide the split pane divider slightly to the right side. |
| 101 | + final Point leftButtonLoc = getButtonLoc(leftButton); |
| 102 | + robot.mouseMove(leftButtonLoc.x + leftButton.getWidth() + 5, |
| 103 | + leftButtonLoc.y + 35); |
| 104 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 105 | + robot.mouseMove(leftButtonLoc.x + leftButton.getWidth() + 8, |
| 106 | + leftButtonLoc.y + 35); |
| 107 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 108 | + pressButton(triggerButton); |
| 109 | + |
| 110 | + // Verifies that JSplitPane current and last divider positions |
| 111 | + // reflects the correct positions after a right slide. |
| 112 | + if ((lastLoc == lastLocExpected) && (currentLoc > lastLoc)) { |
| 113 | + System.out.println("Test Passed."); |
| 114 | + } else { |
| 115 | + throw new RuntimeException( |
| 116 | + "Test failed for " + laf + ", because after a " + |
| 117 | + "right " + "slide" + ", last divider " + |
| 118 | + "location: " + "actual = " + lastLoc + |
| 119 | + ", expected = " + lastLocExpected + |
| 120 | + ", current divider " + "location: actual = " + |
| 121 | + currentLoc + ", expected > " + lastLoc); |
| 122 | + } |
| 123 | + } finally { |
| 124 | + SwingUtilities.invokeAndWait( |
| 125 | + JSplitPaneDividerLocationTest::disposeFrame); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static void pressButton(JToggleButton button) throws Exception { |
| 131 | + final Point buttonLoc = getButtonLoc(button); |
| 132 | + robot.mouseMove(buttonLoc.x + 8, buttonLoc.y + 8); |
| 133 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 134 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 135 | + } |
| 136 | + |
| 137 | + private static Point getButtonLoc(AbstractButton button) |
| 138 | + throws InterruptedException, InvocationTargetException { |
| 139 | + final AtomicReference<Point> loc = new AtomicReference<>(); |
| 140 | + SwingUtilities.invokeAndWait(() -> { |
| 141 | + loc.set(button.getLocationOnScreen()); |
| 142 | + }); |
| 143 | + final Point buttonLoc = loc.get(); |
| 144 | + return buttonLoc; |
| 145 | + } |
| 146 | + |
| 147 | + private static void createUI() { |
| 148 | + frame = new JFrame(); |
| 149 | + panel = new JPanel(); |
| 150 | + panel.setLayout(new BorderLayout()); |
| 151 | + leftButton = new JButton("Left Button"); |
| 152 | + JButton rightButton = new JButton("Right Button"); |
| 153 | + |
| 154 | + final JSplitPane splitPane = |
| 155 | + new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, leftButton, |
| 156 | + rightButton); |
| 157 | + panel.add(splitPane, BorderLayout.CENTER); |
| 158 | + |
| 159 | + splitPane.setDividerSize(10); |
| 160 | + |
| 161 | + triggerButton = new JToggleButton("Trigger"); |
| 162 | + triggerButton.addActionListener(new ActionListener() { |
| 163 | + public void actionPerformed(ActionEvent event) { |
| 164 | + currentLoc = splitPane.getDividerLocation(); |
| 165 | + lastLoc = splitPane.getLastDividerLocation(); |
| 166 | + System.out.println( |
| 167 | + "currentLoc = " + currentLoc + ", lastLoc = " + |
| 168 | + lastLoc); |
| 169 | + } |
| 170 | + }); |
| 171 | + panel.add(triggerButton, BorderLayout.SOUTH); |
| 172 | + |
| 173 | + frame.setContentPane(panel); |
| 174 | + frame.setSize(300, 300); |
| 175 | + frame.setLocationRelativeTo(null); |
| 176 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 177 | + frame.setVisible(true); |
| 178 | + } |
| 179 | + |
| 180 | + private static boolean setLookAndFeel(String lafName) { |
| 181 | + try { |
| 182 | + UIManager.setLookAndFeel(lafName); |
| 183 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 184 | + System.out.println("Ignoring Unsupported L&F: " + lafName); |
| 185 | + return false; |
| 186 | + } catch (ClassNotFoundException | InstantiationException |
| 187 | + | IllegalAccessException e) { |
| 188 | + throw new RuntimeException(e); |
| 189 | + } |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + private static void disposeFrame() { |
| 194 | + if (frame != null) { |
| 195 | + frame.dispose(); |
| 196 | + frame = null; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments