Path: blob/master/test/jdk/javax/swing/JInternalFrame/NormalBoundsTest.java
41152 views
/*1* Copyright (c) 2016, 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 712682327@summary Verify NormalBounds upon iconify/deiconify sequence28@run main NormalBoundsTest29*/30import java.awt.Point;31import java.awt.Rectangle;32import java.awt.Robot;33import java.awt.event.InputEvent;34import java.beans.PropertyVetoException;35import javax.swing.JDesktopPane;36import javax.swing.JFrame;37import javax.swing.JInternalFrame;38import javax.swing.SwingUtilities;39import javax.swing.UIManager;40import javax.swing.UnsupportedLookAndFeelException;41import javax.swing.WindowConstants;4243public class NormalBoundsTest {4445private static JFrame mainFrame;46private static JInternalFrame internalFrame;47private static Rectangle bounds;4849private static void createUI(String lookAndFeelString) {50internalFrame = new JInternalFrame("Internal", true, true, true, true);51internalFrame.setDefaultCloseOperation(52WindowConstants.DO_NOTHING_ON_CLOSE);53internalFrame.setSize(200, 200);5455JDesktopPane desktopPane = new JDesktopPane();56desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);57desktopPane.add(internalFrame);5859mainFrame = new JFrame(lookAndFeelString);60mainFrame.setSize(640, 480);61mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);62mainFrame.setContentPane(desktopPane);6364mainFrame.setVisible(true);65internalFrame.setVisible(true);6667}6869private static int signWOZero(int i) {70return (i > 0) ? 1 : -1;71}7273private static void mouseMove(Robot robot, Point startPt, Point endPt) {74int dx = endPt.x - startPt.x;75int dy = endPt.y - startPt.y;7677int ax = Math.abs(dx) * 2;78int ay = Math.abs(dy) * 2;7980int sx = signWOZero(dx);81int sy = signWOZero(dy);8283int x = startPt.x;84int y = startPt.y;8586int d = 0;8788if (ax > ay) {89d = ay - ax / 2;90while (true) {91robot.mouseMove(x, y);92robot.delay(50);9394if (x == endPt.x) {95return;96}97if (d >= 0) {98y = y + sy;99d = d - ax;100}101x = x + sx;102d = d + ay;103}104} else {105d = ax - ay / 2;106while (true) {107robot.mouseMove(x, y);108robot.delay(50);109110if (y == endPt.y) {111return;112}113if (d >= 0) {114x = x + sx;115d = d - ay;116}117y = y + sy;118d = d + ax;119}120}121}122123private static void drag(Robot r, Point startPt, Point endPt, int button) {124if (!(button == InputEvent.BUTTON1_MASK125|| button == InputEvent.BUTTON2_MASK126|| button == InputEvent.BUTTON3_MASK)) {127throw new IllegalArgumentException("invalid mouse button");128}129130r.mouseMove(startPt.x, startPt.y);131r.mousePress(button);132try {133mouseMove(r, startPt, endPt);134} finally {135r.mouseRelease(button);136}137}138139private static boolean tryLookAndFeel(String lookAndFeelString) {140try {141UIManager.setLookAndFeel(lookAndFeelString);142return true;143} catch (UnsupportedLookAndFeelException | ClassNotFoundException |144InstantiationException | IllegalAccessException e) {145return false;146}147}148149public static void executeTest(Robot robot) throws Exception {150151// Iconize JInternalFrame152SwingUtilities.invokeAndWait(new Runnable() {153@Override154public void run() {155try {156internalFrame.setIcon(true);157} catch (PropertyVetoException ex) {158mainFrame.dispose();159throw new RuntimeException("Iconize InternalFrame Failed");160}161}162});163robot.waitForIdle();164165// Deiconize JInternalFrame166SwingUtilities.invokeAndWait(new Runnable() {167@Override168public void run() {169try {170internalFrame.setIcon(false);171} catch (PropertyVetoException ex) {172mainFrame.dispose();173throw new RuntimeException("Deiconize InternalFrame"174+ " Failed");175}176}177});178robot.waitForIdle();179180SwingUtilities.invokeAndWait(new Runnable() {181@Override182public void run() {183Point loc = internalFrame.getLocationOnScreen();184// Drag Frame185drag(robot,186new Point((int) loc.x + 80, (int) loc.y + 12),187new Point((int) loc.x + 100, (int) loc.y + 40),188InputEvent.BUTTON1_MASK);189}190});191robot.waitForIdle();192193SwingUtilities.invokeAndWait(new Runnable() {194@Override195public void run() {196bounds = internalFrame.getBounds();197if (!internalFrame.getNormalBounds().equals(bounds)) {198mainFrame.dispose();199throw new RuntimeException("Invalid NormalBounds");200}201}202});203robot.waitForIdle();204205// Regression Test Bug ID: 4424247206// Maximize JInternalFrame207SwingUtilities.invokeAndWait(new Runnable() {208@Override209public void run() {210try {211internalFrame.setMaximum(true);212} catch (PropertyVetoException ex) {213mainFrame.dispose();214throw new RuntimeException("Maximize InternalFrame Failed");215}216}217});218robot.waitForIdle();219220// Iconize JInternalFrame221SwingUtilities.invokeAndWait(new Runnable() {222@Override223public void run() {224try {225internalFrame.setIcon(true);226} catch (PropertyVetoException ex) {227mainFrame.dispose();228throw new RuntimeException("Iconize InternalFrame Failed");229}230}231});232robot.waitForIdle();233234// DeIconize JInternalFrame235SwingUtilities.invokeAndWait(new Runnable() {236@Override237public void run() {238try {239internalFrame.setIcon(false);240} catch (PropertyVetoException ex) {241mainFrame.dispose();242throw new RuntimeException("DeIcoize InternalFrame "243+ " Failed");244}245}246});247robot.waitForIdle();248249// Restore/Undo Maximize JInternalFrame250SwingUtilities.invokeAndWait(new Runnable() {251@Override252public void run() {253try {254internalFrame.setMaximum(false);255} catch (PropertyVetoException ex) {256mainFrame.dispose();257throw new RuntimeException("Restore InternalFrame "258+ " Failed");259}260}261});262robot.waitForIdle();263264SwingUtilities.invokeAndWait(new Runnable() {265@Override266public void run() {267if (!internalFrame.getBounds().equals(bounds)) {268mainFrame.dispose();269throw new RuntimeException("Regression Test Failed");270}271}272});273robot.waitForIdle();274275mainFrame.dispose();276}277278public static void main(String[] args) throws Exception {279Robot robot = new Robot();280UIManager.LookAndFeelInfo[] lookAndFeelArray281= UIManager.getInstalledLookAndFeels();282for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {283String lookAndFeelString = lookAndFeelItem.getClassName();284if (tryLookAndFeel(lookAndFeelString)) {285// create UI286SwingUtilities.invokeAndWait(new Runnable() {287@Override288public void run() {289createUI(lookAndFeelString);290}291});292293robot.waitForIdle();294executeTest(robot);295} else {296throw new RuntimeException("Setting Look and Feel Failed");297}298}299300}301}302303304