Path: blob/master/test/jdk/javax/swing/JSplitPane/4816114/bug4816114.java
41153 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test25* @key headful26* @bug 4816114 820390427* @summary REGRESSION: Regression in divider location behavior when JSplitPane is resized28* @author Andrey Pikalev29* @run main bug481611430*/3132import java.awt.Robot;33import java.awt.Dimension;34import java.awt.AWTException;35import java.awt.BorderLayout;36import javax.swing.JFrame;37import javax.swing.JButton;38import javax.swing.SwingUtilities;39import javax.swing.JSplitPane;40import javax.swing.BorderFactory;41import java.lang.reflect.InvocationTargetException;4243public class bug4816114 {4445static JFrame fr;46JSplitPane splitPane;4748boolean[] resized = new boolean[] { false, false, false,49false, false, false };50static int step = 0;51boolean h_passed = false;52boolean v_passed = false;5354static bug4816114 test = new bug4816114();5556public static void main(String[] args) throws InterruptedException, InvocationTargetException, AWTException {57try {58SwingUtilities.invokeAndWait(new Runnable() {59public void run() {60test.createAndShowGUI();61}62});63Robot robot = new Robot();64robot.waitForIdle();65Thread.sleep(1000);66Thread.sleep(2000);6768step++;69test.doTest(150, 300);7071step++;72test.doTest(650, 300);7374SwingUtilities.invokeAndWait(new Runnable() {75public void run() {76test.splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);77}78});7980step++;81test.doTest(300, 650);8283step++;84test.doTest(300, 150);8586step++;87test.doTest(300, 650);8889if ( !test.isPassed() ) {90throw new Error("The divider location is wrong.");91}92} finally {93SwingUtilities.invokeAndWait(() -> fr.dispose());94}95}9697public void createAndShowGUI() {98fr = new JFrame("Test");99fr.setUndecorated(true);100101splitPane = new TestSplitPane();102splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);103splitPane.setResizeWeight(0);104splitPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));105106JButton leftButton = new JButton("LEFT");107leftButton.setPreferredSize(new Dimension(300, 300));108leftButton.setMinimumSize(new Dimension(150, 150));109splitPane.setLeftComponent(leftButton);110111JButton rightButton = new JButton("RIGHT");112rightButton.setPreferredSize(new Dimension(300, 300));113rightButton.setMinimumSize(new Dimension(150, 150));114splitPane.setRightComponent(rightButton);115116fr.getContentPane().add(splitPane, BorderLayout.CENTER);117118fr.pack();119fr.setVisible(true);120}121122void doTest(final int width, final int height) throws InterruptedException, InvocationTargetException {123SwingUtilities.invokeAndWait(new Runnable() {124public void run() {125splitPane.setPreferredSize(new Dimension(width, height));126fr.pack();127}128});129130synchronized (bug4816114.this) {131while (!resized[step]) {132bug4816114.this.wait();133}134}135}136137synchronized void setPassed(int orientation, boolean passed) {138if (orientation == JSplitPane.HORIZONTAL_SPLIT) {139this.h_passed = passed;140}141else {142this.v_passed = passed;143}144}145146synchronized boolean isPassed() {147return h_passed && v_passed;148}149150151class TestSplitPane extends JSplitPane {152public void setDividerLocation(int location) {153super.setDividerLocation(location);154155if ( splitPane.getDividerLocation() == 151 ) {156setPassed(getOrientation(), true);157}158159synchronized (bug4816114.this) {160resized[step] = true;161bug4816114.this.notifyAll();162}163}164}165}166167168