Path: blob/master/test/jdk/javax/swing/JComboBox/4523758/bug4523758.java
41153 views
/*1* Copyright (c) 2014, 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 452375827* @summary Directly check that torn-off combo works28* @library /lib/client29* @build ExtendedRobot30* @run main bug452375831*/32/*33* There is another regression test for this bug created by ssi with a34* fix. It is purely AWT and designed to verify the (absence of) underlying Component issue.35* This functional test does test, well, functionality of the swing control.36*37*/3839import javax.swing.*;40import java.awt.*;41import java.awt.event.*;4243public class bug4523758 {4445private static JFrame frame;46private JToolBar tools;47private JComboBox combo;4849private boolean passed = true;50private boolean itemStateChanged = false;51private Object itemLock = new Object();5253private static int delay = 500;54private static final int WAIT_EVENT_DELAY = 60000;5556public bug4523758() {57try {58SwingUtilities.invokeAndWait(new Runnable() {59public void run() {60initializeGUI();61}62});63} catch (Exception e) {64e.printStackTrace();65throw new RuntimeException("Failed to initialize GUI");66}67}6869private void initializeGUI() {70frame = new JFrame("bug4523758");71tools = new JToolBar();72frame.getContentPane().add(tools, BorderLayout.NORTH);73combo = new JComboBox(new Object[] { "Red", "Orange", "Yellow",74"Green", "Blue", "Indigo", "Violet"});75combo.addItemListener(new ItemListener() {76public void itemStateChanged(ItemEvent event) {77itemStateChanged = true;78synchronized (itemLock) {79try {80itemLock.notifyAll();81} catch (Exception e) {82}83}84}85});86tools.add(combo);87frame.setSize(250,400);88frame.setLocation(700, 0);89frame.setVisible(true);90}9192private void doTest() throws Exception {93ExtendedRobot robot = new ExtendedRobot();94robot.waitForIdle(1000);9596final Point cl = combo.getLocationOnScreen();97final Dimension cs = combo.getSize();9899SwingUtilities.invokeAndWait(new Runnable() {100public void run() {101frame.dispose();102}103});104robot.waitForIdle(delay);105SwingUtilities.invokeAndWait(new Runnable() {106public void run() {107frame.setSize((int) cl.x - 700 + cs.width,108(int) cl.y + cs.height - 5);109frame.setVisible(true);110}111});112113robot.waitForIdle(delay*2);114Point comboLocation = combo.getLocationOnScreen();115Dimension comboSize = combo.getSize();116117robot.mouseMove((int) comboLocation.x + comboSize.width / 2,118(int) comboLocation.y + 5);119robot.waitForIdle(delay);120robot.mousePress(InputEvent.BUTTON1_MASK);121robot.delay(100);122robot.mouseRelease(InputEvent.BUTTON1_MASK);123robot.waitForIdle(delay);124125robot.mouseMove((int) comboLocation.x + comboSize.width / 2,126(int) comboLocation.y + 60);127robot.waitForIdle(delay);128robot.mousePress(InputEvent.BUTTON1_MASK);129robot.delay(100);130robot.mouseRelease(InputEvent.BUTTON1_MASK);131robot.waitForIdle(delay);132133if (! itemStateChanged) {134synchronized (itemLock) {135try {136itemLock.wait(WAIT_EVENT_DELAY);137} catch (Exception e) {138}139}140}141if (! itemStateChanged) {142System.err.println("FAIL: ItemEvent not triggered when mouse clicked on combo box drop down");143passed = false;144}145146if (! passed) {147System.err.println("Test failed!");148captureScreenAndSave();149throw new RuntimeException("FAIL");150} else {151System.out.println("Test passed!");152}153}154155public static void main(String[] args) throws Exception {156try {157bug4523758 test = new bug4523758();158test.doTest();159} catch (Exception e) {160e.printStackTrace();161throw new RuntimeException("FAIL");162} finally {163if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());164}165}166167/**168* Do screen capture and save it as image169*/170private static void captureScreenAndSave() {171172try {173Robot robot = new Robot();174Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();175Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);176System.out.println("About to screen capture - " + rectangle);177java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle);178javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg"));179robot.delay(3000);180} catch (Throwable t) {181System.out.println("WARNING: Exception thrown while screen capture!");182t.printStackTrace();183}184}185}186187188