1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25  * @bug 4523758
  26  * @summary Directly check that torn-off combo works
  27  * @library ../../../../lib/testlibrary
  28  * @build ExtendedRobot
  29  * @run main bug4523758
  30  */
  31 /*
  32  * There is another regression test for this bug created by ssi with a
  33  * fix. It is purely AWT and designed to verify the (absence of) underlying Component issue.
  34  * This functional test does test, well, functionality of the swing control.
  35  *
  36  */
  37 
  38 import javax.swing.*;
  39 import java.awt.*;
  40 import java.awt.event.*;
  41 
  42 public class bug4523758 {
  43 
  44     private JFrame frame;
  45     private JToolBar tools;
  46     private JComboBox combo;
  47 
  48     private boolean passed = true;
  49     private boolean itemStateChanged = false;
  50     private Object itemLock = new Object();
  51 
  52     private static int delay = 500;
  53     private static final int WAIT_EVENT_DELAY = 60000;
  54 
  55     public bug4523758() {
  56         try {
  57             SwingUtilities.invokeAndWait(new Runnable() {
  58                 public void run() {
  59                     initializeGUI();
  60                 }
  61             });
  62         } catch (Exception e) {
  63             e.printStackTrace();
  64             throw new RuntimeException("Failed to initialize GUI");
  65         }
  66     }
  67 
  68     private void initializeGUI() {
  69         frame = new JFrame("bug4523758");
  70         tools = new JToolBar();
  71         frame.getContentPane().add(tools, BorderLayout.NORTH);
  72         combo = new JComboBox(new Object[] { "Red", "Orange", "Yellow",
  73             "Green", "Blue", "Indigo", "Violet"});
  74         combo.addItemListener(new ItemListener() {
  75             public void itemStateChanged(ItemEvent event) {
  76                 itemStateChanged = true;
  77                 synchronized (itemLock) {
  78                     try {
  79                         itemLock.notifyAll();
  80                     } catch (Exception e) {
  81                     }
  82                 }
  83             }
  84         });
  85         tools.add(combo);
  86         frame.setSize(250,400);
  87         frame.setLocation(700, 0);
  88         frame.setVisible(true);
  89     }
  90 
  91     private void doTest() throws Exception {
  92         ExtendedRobot robot = new ExtendedRobot();
  93         robot.waitForIdle(1000);
  94 
  95         final Point cl = combo.getLocationOnScreen();
  96         final Dimension cs = combo.getSize();
  97 
  98         SwingUtilities.invokeAndWait(new Runnable() {
  99             public void run() {
 100                 frame.dispose();
 101             }
 102         });
 103         robot.waitForIdle(delay);
 104         SwingUtilities.invokeAndWait(new Runnable() {
 105             public void run() {
 106                 frame.setSize((int) cl.x - 700 + cs.width,
 107                               (int) cl.y + cs.height - 5);
 108                 frame.setVisible(true);
 109             }
 110         });
 111 
 112         robot.waitForIdle(delay*2);
 113         Point comboLocation = combo.getLocationOnScreen();
 114         Dimension comboSize = combo.getSize();
 115 
 116         robot.mouseMove((int) comboLocation.x + comboSize.width / 2,
 117                         (int) comboLocation.y + 5);
 118         robot.waitForIdle(delay);
 119         robot.mousePress(InputEvent.BUTTON1_MASK);
 120         robot.delay(100);
 121         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 122         robot.waitForIdle(delay);
 123 
 124         robot.mouseMove((int) comboLocation.x + comboSize.width / 2,
 125                         (int) comboLocation.y + 60);
 126         robot.waitForIdle(delay);
 127         robot.mousePress(InputEvent.BUTTON1_MASK);
 128         robot.delay(100);
 129         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 130         robot.waitForIdle(delay);
 131 
 132         if (! itemStateChanged) {
 133             synchronized (itemLock) {
 134                 try {
 135                     itemLock.wait(WAIT_EVENT_DELAY);
 136                 } catch (Exception e) {
 137                 }
 138             }
 139         }
 140         if (! itemStateChanged) {
 141             System.err.println("FAIL: ItemEvent not triggered when mouse clicked on combo box drop down");
 142             passed = false;
 143         }
 144 
 145         if (! passed) {
 146             System.err.println("Test failed!");
 147             captureScreenAndSave();
 148             throw new RuntimeException("FAIL");
 149         } else {
 150             System.out.println("Test passed!");
 151         }
 152     }
 153 
 154     public static void main(String[] args) {
 155         try {
 156             bug4523758 test = new bug4523758();
 157             test.doTest();
 158         } catch (Exception e) {
 159             e.printStackTrace();
 160             throw new RuntimeException("FAIL");
 161         }
 162     }
 163 
 164     /**
 165      * Do screen capture and save it as image
 166      */
 167     private static void captureScreenAndSave() {
 168 
 169         try {
 170             Robot robot = new Robot();
 171             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 172             Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
 173             System.out.println("About to screen capture - " + rectangle);
 174             java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle);
 175             javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg"));
 176             robot.delay(3000);
 177         } catch (Throwable t) {
 178             System.out.println("WARNING: Exception thrown while screen capture!");
 179             t.printStackTrace();
 180         }
 181     }
 182 }