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  @bug 6176814 8132766
  27  @summary Metalworks frame maximizes after the move
  28  @run main MaximizedFrameTest
  29  */
  30 
  31 import java.awt.AWTException;
  32 import java.awt.Component;
  33 import java.awt.Point;
  34 import java.awt.Robot;
  35 import java.awt.event.InputEvent;
  36 import java.util.logging.Level;
  37 import java.util.logging.Logger;
  38 import javax.swing.JFrame;
  39 import javax.swing.JLayeredPane;
  40 import javax.swing.SwingUtilities;
  41 import javax.swing.UIManager;
  42 import javax.swing.UnsupportedLookAndFeelException;
  43 
  44 public class MaximizedFrameTest {
  45 
  46     final static int ITERATIONS_COUNT = 5;
  47     private static JFrame frame;
  48     private static Point tempMousePosition;
  49     private static Component titleComponent;
  50 
  51     public void init() {
  52         JFrame.setDefaultLookAndFeelDecorated(true);
  53 
  54         try {
  55             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  56         } catch (ClassNotFoundException | InstantiationException |
  57                 IllegalAccessException | UnsupportedLookAndFeelException ex) {
  58             throw new RuntimeException("Test Failed. MetalLookAndFeel not set "
  59                     + "for frame");
  60         }
  61 
  62         frame = new JFrame("JFrame Maximization Test");
  63         frame.pack();
  64         frame.setSize(450, 260);
  65         frame.setVisible(true);
  66     }
  67 
  68     public void getTitleComponent() throws Exception {
  69 
  70         SwingUtilities.invokeAndWait(new Runnable() {
  71 
  72             @Override
  73             public void run() {
  74                 JLayeredPane lPane = frame.getLayeredPane();
  75                 boolean titleFound = false;
  76 
  77                 for (int j = 0; j < lPane.getComponentsInLayer(
  78                     JLayeredPane.FRAME_CONTENT_LAYER.intValue()).length; j++) {
  79 
  80                     titleComponent = lPane.getComponentsInLayer(
  81                     JLayeredPane.FRAME_CONTENT_LAYER.intValue())[j];
  82 
  83                     if (titleComponent.getClass().getName().equals(
  84                         "javax.swing.plaf.metal.MetalTitlePane")) {
  85 
  86                         titleFound = true;
  87                         break;
  88                     }
  89                 }
  90 
  91                 if (!titleFound) {
  92                     try {
  93                         dispose();
  94                     } catch (Exception ex) {
  95                         Logger.getLogger(MaximizedFrameTest.class.getName())
  96                                 .log(Level.SEVERE, null, ex);
  97                     }
  98                     throw new RuntimeException("Test Failed. Unable to "
  99                             + "determine title component");
 100                 }
 101             }
 102         });
 103     }
 104 
 105     public void doMaximizeFrameTest() throws Exception {
 106 
 107         SwingUtilities.invokeAndWait(new Runnable() {
 108             @Override
 109             public void run() {
 110                 Point framePosition = frame.getLocationOnScreen();
 111 
 112                 tempMousePosition = new Point(framePosition.x
 113                         + frame.getWidth() / 2, framePosition.y
 114                                 + titleComponent.getHeight() / 2);
 115             }
 116         });
 117 
 118         try {
 119             Robot robot = new Robot();
 120             robot.mouseMove(tempMousePosition.x, tempMousePosition.y);
 121             robot.waitForIdle();
 122 
 123             for (int iteration = 0; iteration < ITERATIONS_COUNT; iteration++) {
 124                 robot.mousePress(InputEvent.BUTTON1_MASK);
 125                 robot.waitForIdle();
 126 
 127                 // Moving a mouse pointer less than a few pixels
 128                 // leads to rising a double click event.
 129                 // We have to use exceeded the AWT_MULTICLICK_SMUDGE
 130                 // const value (which is 4 by default on GNOME) to test that.
 131                 tempMousePosition.x += 5;
 132                 robot.mouseMove(tempMousePosition.x, tempMousePosition.y);
 133                 robot.waitForIdle();
 134                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 135                 robot.waitForIdle();
 136 
 137                 if (frame.getExtendedState() != 0) {
 138                     dispose();
 139                     throw new RuntimeException("Test failed. JFrame was "
 140                             + "maximized. ExtendedState is : "
 141                             + frame.getExtendedState());
 142                 }
 143             }
 144         } catch (AWTException e) {
 145             dispose();
 146             throw new RuntimeException("Test Failed. AWTException thrown.");
 147         }
 148         System.out.println("Test passed.");
 149     }
 150 
 151     private void dispose() throws Exception {
 152         SwingUtilities.invokeAndWait(new Runnable() {
 153             @Override
 154             public void run() {
 155                 if (null != frame) {
 156                     frame.dispose();
 157                 }
 158             }
 159         });
 160     }
 161 
 162     public static void main(String[] args) throws Exception {
 163 
 164         MaximizedFrameTest maximizedFrameTest = new MaximizedFrameTest();
 165         maximizedFrameTest.init();
 166         maximizedFrameTest.getTitleComponent();
 167         maximizedFrameTest.doMaximizeFrameTest();
 168         maximizedFrameTest.dispose();
 169     }
 170 }