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