Path: blob/master/test/jdk/java/awt/Mouse/MaximizedFrameTest/MaximizedFrameTest.java
41155 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@key headful26@bug 6176814 813276627@summary Metalworks frame maximizes after the move28@run main MaximizedFrameTest29*/3031import java.awt.AWTException;32import java.awt.Component;33import java.awt.Point;34import java.awt.Robot;35import java.awt.event.InputEvent;36import java.util.logging.Level;37import java.util.logging.Logger;38import javax.swing.JFrame;39import javax.swing.JLayeredPane;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.UnsupportedLookAndFeelException;4344public class MaximizedFrameTest {4546final static int ITERATIONS_COUNT = 5;47private static JFrame frame;48private static Point tempMousePosition;49private static Component titleComponent;5051public void init() {52JFrame.setDefaultLookAndFeelDecorated(true);5354try {55UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");56} catch (ClassNotFoundException | InstantiationException |57IllegalAccessException | UnsupportedLookAndFeelException ex) {58throw new RuntimeException("Test Failed. MetalLookAndFeel not set "59+ "for frame");60}6162frame = new JFrame("JFrame Maximization Test");63frame.pack();64frame.setSize(450, 260);65frame.setVisible(true);66}6768public void getTitleComponent() throws Exception {6970SwingUtilities.invokeAndWait(new Runnable() {7172@Override73public void run() {74JLayeredPane lPane = frame.getLayeredPane();75boolean titleFound = false;7677for (int j = 0; j < lPane.getComponentsInLayer(78JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {7980titleComponent = lPane.getComponentsInLayer(81JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];8283if (titleComponent.getClass().getName().equals(84"javax.swing.plaf.metal.MetalTitlePane")) {8586titleFound = true;87break;88}89}9091if (!titleFound) {92try {93dispose();94} catch (Exception ex) {95Logger.getLogger(MaximizedFrameTest.class.getName())96.log(Level.SEVERE, null, ex);97}98throw new RuntimeException("Test Failed. Unable to "99+ "determine title component");100}101}102});103}104105public void doMaximizeFrameTest() throws Exception {106107SwingUtilities.invokeAndWait(new Runnable() {108@Override109public void run() {110Point framePosition = frame.getLocationOnScreen();111112tempMousePosition = new Point(framePosition.x113+ frame.getWidth() / 2, framePosition.y114+ titleComponent.getHeight() / 2);115}116});117118try {119Robot robot = new Robot();120robot.mouseMove(tempMousePosition.x, tempMousePosition.y);121robot.waitForIdle();122123for (int iteration = 0; iteration < ITERATIONS_COUNT; iteration++) {124robot.mousePress(InputEvent.BUTTON1_MASK);125robot.waitForIdle();126127// Moving a mouse pointer less than a few pixels128// leads to rising a double click event.129// We have to use exceeded the AWT_MULTICLICK_SMUDGE130// const value (which is 4 by default on GNOME) to test that.131tempMousePosition.x += 5;132robot.mouseMove(tempMousePosition.x, tempMousePosition.y);133robot.waitForIdle();134robot.mouseRelease(InputEvent.BUTTON1_MASK);135robot.waitForIdle();136137if (frame.getExtendedState() != 0) {138dispose();139throw new RuntimeException("Test failed. JFrame was "140+ "maximized. ExtendedState is : "141+ frame.getExtendedState());142}143}144} catch (AWTException e) {145dispose();146throw new RuntimeException("Test Failed. AWTException thrown.");147}148System.out.println("Test passed.");149}150151private void dispose() throws Exception {152SwingUtilities.invokeAndWait(new Runnable() {153@Override154public void run() {155if (null != frame) {156frame.dispose();157}158}159});160}161162public static void main(String[] args) throws Exception {163164MaximizedFrameTest maximizedFrameTest = new MaximizedFrameTest();165maximizedFrameTest.init();166maximizedFrameTest.getTitleComponent();167maximizedFrameTest.doMaximizeFrameTest();168maximizedFrameTest.dispose();169}170}171172173