1 /*
   2  * Copyright (c) 2008, 2015 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  @test
  26  @key headful
  27  @bug 6176814 8132766
  28  @summary Metalworks frame maximizes after the move
  29  @run main MaximizedFrameTest
  30  */
  31 
  32 import java.awt.AWTException;
  33 import java.awt.Component;
  34 import java.awt.Point;
  35 import java.awt.Robot;
  36 import java.awt.event.InputEvent;
  37 import java.util.logging.Level;
  38 import java.util.logging.Logger;
  39 import javax.swing.JFrame;
  40 import javax.swing.JLayeredPane;
  41 import javax.swing.SwingUtilities;
  42 import javax.swing.UIManager;
  43 import javax.swing.UnsupportedLookAndFeelException;
  44 
  45 public class MaximizedFrameTest {
  46 
  47     final static int ITERATIONS_COUNT = 5;
  48     private static JFrame frame;
  49     private static Point tempMousePosition;
  50     private static Component titleComponent;
  51 
  52     public void init() {
  53         JFrame.setDefaultLookAndFeelDecorated(true);
  54 
  55         try {
  56             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  57         } catch (ClassNotFoundException | InstantiationException |
  58                 IllegalAccessException | UnsupportedLookAndFeelException ex) {
  59             throw new RuntimeException("Test Failed. MetalLookAndFeel not set "
  60                     + "for frame");
  61         }
  62 
  63         frame = new JFrame("JFrame Maximization Test");
  64         frame.pack();
  65         frame.setSize(450, 260);
  66         frame.setVisible(true);
  67     }
  68 
  69     public void getTitleComponent() throws Exception {
  70 
  71         SwingUtilities.invokeAndWait(new Runnable() {
  72 
  73             @Override
  74             public void run() {
  75                 JLayeredPane lPane = frame.getLayeredPane();
  76                 boolean titleFound = false;
  77 
  78                 for (int j = 0; j < lPane.getComponentsInLayer(
  79                     JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {
  80 
  81                     titleComponent = lPane.getComponentsInLayer(
  82                     JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];
  83 
  84                     if (titleComponent.getClass().getName().equals(
  85                         "javax.swing.plaf.metal.MetalTitlePane")) {
  86 
  87                         titleFound = true;
  88                         break;
  89                     }
  90                 }
  91 
  92                 if (!titleFound) {
  93                     try {
  94                         dispose();
  95                     } catch (Exception ex) {
  96                         Logger.getLogger(MaximizedFrameTest.class.getName())
  97                                 .log(Level.SEVERE, null, ex);
  98                     }
  99                     throw new RuntimeException("Test Failed. Unable to "
 100                             + "determine title component");
 101                 }
 102             }
 103         });
 104     }
 105 
 106     public void doMaximizeFrameTest() throws Exception {
 107 
 108         SwingUtilities.invokeAndWait(new Runnable() {
 109             @Override
 110             public void run() {
 111                 Point framePosition = frame.getLocationOnScreen();
 112 
 113                 tempMousePosition = new Point(framePosition.x
 114                         + frame.getWidth() / 2, framePosition.y
 115                                 + titleComponent.getHeight() / 2);
 116             }
 117         });
 118 
 119         try {
 120             Robot robot = new Robot();
 121             robot.mouseMove(tempMousePosition.x, tempMousePosition.y);
 122             robot.waitForIdle();
 123 
 124             for (int iteration = 0; iteration < ITERATIONS_COUNT; iteration++) {
 125                 robot.mousePress(InputEvent.BUTTON1_MASK);
 126                 robot.waitForIdle();
 127 
 128                 // Moving a mouse pointer less than a few pixels
 129                 // leads to rising a double click event.
 130                 // We have to use exceeded the AWT_MULTICLICK_SMUDGE
 131                 // const value (which is 4 by default on GNOME) to test that.
 132                 tempMousePosition.x += 5;
 133                 robot.mouseMove(tempMousePosition.x, tempMousePosition.y);
 134                 robot.waitForIdle();
 135                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 136                 robot.waitForIdle();
 137 
 138                 if (frame.getExtendedState() != 0) {
 139                     dispose();
 140                     throw new RuntimeException("Test failed. JFrame was "
 141                             + "maximized. ExtendedState is : "
 142                             + frame.getExtendedState());
 143                 }
 144             }
 145         } catch (AWTException e) {
 146             dispose();
 147             throw new RuntimeException("Test Failed. AWTException thrown.");
 148         }
 149         System.out.println("Test passed.");
 150     }
 151 
 152     private void dispose() throws Exception {
 153         SwingUtilities.invokeAndWait(new Runnable() {
 154             @Override
 155             public void run() {
 156                 if (null != frame) {
 157                     frame.dispose();
 158                 }
 159             }
 160         });
 161     }
 162 
 163     public static void main(String[] args) throws Exception {
 164 
 165         MaximizedFrameTest maximizedFrameTest = new MaximizedFrameTest();
 166         maximizedFrameTest.init();
 167         maximizedFrameTest.getTitleComponent();
 168         maximizedFrameTest.doMaximizeFrameTest();
 169         maximizedFrameTest.dispose();
 170     }
 171 }