Path: blob/master/test/jdk/javax/swing/JSplitPane/4615365/JSplitPaneDividerLocationTest.java
66646 views
/*1* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.BorderLayout;24import java.awt.Point;25import java.awt.Robot;26import java.awt.event.ActionEvent;27import java.awt.event.ActionListener;28import java.awt.event.InputEvent;29import java.lang.reflect.InvocationTargetException;30import java.util.Arrays;31import java.util.List;32import java.util.concurrent.atomic.AtomicBoolean;33import java.util.concurrent.atomic.AtomicReference;34import java.util.stream.Collectors;35import javax.swing.AbstractButton;36import javax.swing.JButton;37import javax.swing.JFrame;38import javax.swing.JPanel;39import javax.swing.JSplitPane;40import javax.swing.JToggleButton;41import javax.swing.SwingUtilities;42import javax.swing.UIManager;43import javax.swing.UIManager.LookAndFeelInfo;44import javax.swing.UnsupportedLookAndFeelException;4546import static javax.swing.UIManager.getInstalledLookAndFeels;47/*48* @test49* @key headful50* @bug 461536551* @summary This test confirms that the JSplitPane's current and last52* divider positions are correct when realized.53* @run main JSplitPaneDividerLocationTest54*/55public class JSplitPaneDividerLocationTest {5657private static JFrame frame;58private static JPanel panel;59private static JButton leftButton;60private static JToggleButton triggerButton;61private static volatile int currentLoc;62private static volatile int lastLoc;63private static volatile int lastLocExpected;64private static Robot robot;6566public static void main(String[] s) throws Exception {67robot = new Robot();68robot.setAutoWaitForIdle(true);69robot.setAutoDelay(200);70List<String> lafs = Arrays.stream(getInstalledLookAndFeels())71.map(LookAndFeelInfo::getClassName)72.collect(Collectors.toList());73for (final String laf : lafs) {74try {75AtomicBoolean lafSetSuccess = new AtomicBoolean(false);76SwingUtilities.invokeAndWait(() -> {77lafSetSuccess.set(setLookAndFeel(laf));78if (lafSetSuccess.get()) {79createUI();80}81});82if (!lafSetSuccess.get()) {83continue;84}85robot.waitForIdle();8687pressButton(triggerButton);8889// Verifies that JSplitPane current and last divider90// positions are correct and not as per JDK-4615365.91if ((currentLoc == -1) || (lastLoc == 0)) {92throw new RuntimeException(93"Test failed for " + laf + " :- last divider loc:" +94"actual = " + lastLoc + ",expected = -1, current " +95"divider loc:actual=" + currentLoc + ",expected>0");96}97lastLocExpected = currentLoc;9899// Slide the split pane divider slightly to the right side.100final Point leftButtonLoc = getButtonLoc(leftButton);101robot.mouseMove(leftButtonLoc.x + leftButton.getWidth() + 5,102leftButtonLoc.y + 35);103robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);104robot.mouseMove(leftButtonLoc.x + leftButton.getWidth() + 8,105leftButtonLoc.y + 35);106robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);107pressButton(triggerButton);108109// Verifies that JSplitPane current and last divider positions110// reflects the correct positions after a right slide.111if ((lastLoc == lastLocExpected) && (currentLoc > lastLoc)) {112System.out.println("Test Passed.");113} else {114throw new RuntimeException(115"Test failed for " + laf + ", because after a " +116"right " + "slide" + ", last divider " +117"location: " + "actual = " + lastLoc +118", expected = " + lastLocExpected +119", current divider " + "location: actual = " +120currentLoc + ", expected > " + lastLoc);121}122} finally {123SwingUtilities.invokeAndWait(124JSplitPaneDividerLocationTest::disposeFrame);125}126}127}128129private static void pressButton(JToggleButton button) throws Exception {130final Point buttonLoc = getButtonLoc(button);131robot.mouseMove(buttonLoc.x + 8, buttonLoc.y + 8);132robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);133robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);134}135136private static Point getButtonLoc(AbstractButton button)137throws InterruptedException, InvocationTargetException {138final AtomicReference<Point> loc = new AtomicReference<>();139SwingUtilities.invokeAndWait(() -> {140loc.set(button.getLocationOnScreen());141});142final Point buttonLoc = loc.get();143return buttonLoc;144}145146private static void createUI() {147frame = new JFrame();148panel = new JPanel();149panel.setLayout(new BorderLayout());150leftButton = new JButton("Left Button");151JButton rightButton = new JButton("Right Button");152153final JSplitPane splitPane =154new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, leftButton,155rightButton);156panel.add(splitPane, BorderLayout.CENTER);157158splitPane.setDividerSize(10);159160triggerButton = new JToggleButton("Trigger");161triggerButton.addActionListener(new ActionListener() {162public void actionPerformed(ActionEvent event) {163currentLoc = splitPane.getDividerLocation();164lastLoc = splitPane.getLastDividerLocation();165System.out.println(166"currentLoc = " + currentLoc + ", lastLoc = " +167lastLoc);168}169});170panel.add(triggerButton, BorderLayout.SOUTH);171172frame.setContentPane(panel);173frame.setSize(300, 300);174frame.setLocationRelativeTo(null);175frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);176frame.setVisible(true);177}178179private static boolean setLookAndFeel(String lafName) {180try {181UIManager.setLookAndFeel(lafName);182} catch (UnsupportedLookAndFeelException ignored) {183System.out.println("Ignoring Unsupported L&F: " + lafName);184return false;185} catch (ClassNotFoundException | InstantiationException186| IllegalAccessException e) {187throw new RuntimeException(e);188}189return true;190}191192private static void disposeFrame() {193if (frame != null) {194frame.dispose();195frame = null;196}197}198199}200201202