Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/Mouse/MaximizedFrameTest/MaximizedFrameTest.java
48795 views
/*1* Copyright (c) 2008, 2015 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@bug 6176814 813276626@summary Metalworks frame maximizes after the move27@run main MaximizedFrameTest28*/2930import java.awt.AWTException;31import java.awt.Component;32import java.awt.Point;33import java.awt.Robot;34import java.awt.event.InputEvent;35import java.util.logging.Level;36import java.util.logging.Logger;37import javax.swing.JFrame;38import javax.swing.JLayeredPane;39import javax.swing.SwingUtilities;40import javax.swing.UIManager;41import javax.swing.UnsupportedLookAndFeelException;4243public class MaximizedFrameTest {4445final static int ITERATIONS_COUNT = 5;46private static JFrame frame;47private static Point tempMousePosition;48private static Component titleComponent;4950public void init() {51JFrame.setDefaultLookAndFeelDecorated(true);5253try {54UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");55} catch (ClassNotFoundException | InstantiationException |56IllegalAccessException | UnsupportedLookAndFeelException ex) {57throw new RuntimeException("Test Failed. MetalLookAndFeel not set "58+ "for frame");59}6061frame = new JFrame("JFrame Maximization Test");62frame.pack();63frame.setSize(450, 260);64frame.setVisible(true);65}6667public void getTitleComponent() throws Exception {6869SwingUtilities.invokeAndWait(new Runnable() {7071@Override72public void run() {73JLayeredPane lPane = frame.getLayeredPane();74boolean titleFound = false;7576for (int j = 0; j < lPane.getComponentsInLayer(77JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {7879titleComponent = lPane.getComponentsInLayer(80JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];8182if (titleComponent.getClass().getName().equals(83"javax.swing.plaf.metal.MetalTitlePane")) {8485titleFound = true;86break;87}88}8990if (!titleFound) {91try {92dispose();93} catch (Exception ex) {94Logger.getLogger(MaximizedFrameTest.class.getName())95.log(Level.SEVERE, null, ex);96}97throw new RuntimeException("Test Failed. Unable to "98+ "determine title component");99}100}101});102}103104public void doMaximizeFrameTest() throws Exception {105106SwingUtilities.invokeAndWait(new Runnable() {107@Override108public void run() {109Point framePosition = frame.getLocationOnScreen();110111tempMousePosition = new Point(framePosition.x112+ frame.getWidth() / 2, framePosition.y113+ titleComponent.getHeight() / 2);114}115});116117try {118Robot robot = new Robot();119robot.mouseMove(tempMousePosition.x, tempMousePosition.y);120robot.waitForIdle();121122for (int iteration = 0; iteration < ITERATIONS_COUNT; iteration++) {123robot.mousePress(InputEvent.BUTTON1_MASK);124robot.waitForIdle();125126// Moving a mouse pointer less than a few pixels127// leads to rising a double click event.128// We have to use exceeded the AWT_MULTICLICK_SMUDGE129// const value (which is 4 by default on GNOME) to test that.130tempMousePosition.x += 5;131robot.mouseMove(tempMousePosition.x, tempMousePosition.y);132robot.waitForIdle();133robot.mouseRelease(InputEvent.BUTTON1_MASK);134robot.waitForIdle();135136if (frame.getExtendedState() != 0) {137dispose();138throw new RuntimeException("Test failed. JFrame was "139+ "maximized. ExtendedState is : "140+ frame.getExtendedState());141}142}143} catch (AWTException e) {144dispose();145throw new RuntimeException("Test Failed. AWTException thrown.");146}147System.out.println("Test passed.");148}149150private void dispose() throws Exception {151SwingUtilities.invokeAndWait(new Runnable() {152@Override153public void run() {154if (null != frame) {155frame.dispose();156}157}158});159}160161public static void main(String[] args) throws Exception {162163MaximizedFrameTest maximizedFrameTest = new MaximizedFrameTest();164maximizedFrameTest.init();165maximizedFrameTest.getTitleComponent();166maximizedFrameTest.doMaximizeFrameTest();167maximizedFrameTest.dispose();168}169}170171172