Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/4523758/bug4523758.java
38918 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/* @test24* @bug 452375825* @summary Directly check that torn-off combo works26* @library ../../../../lib/testlibrary27* @build ExtendedRobot28* @run main bug452375829*/30/*31* There is another regression test for this bug created by ssi with a32* fix. It is purely AWT and designed to verify the (absence of) underlying Component issue.33* This functional test does test, well, functionality of the swing control.34*35*/3637import javax.swing.*;38import java.awt.*;39import java.awt.event.*;4041public class bug4523758 {4243private JFrame frame;44private JToolBar tools;45private JComboBox combo;4647private boolean passed = true;48private boolean itemStateChanged = false;49private Object itemLock = new Object();5051private static int delay = 500;52private static final int WAIT_EVENT_DELAY = 60000;5354public bug4523758() {55try {56SwingUtilities.invokeAndWait(new Runnable() {57public void run() {58initializeGUI();59}60});61} catch (Exception e) {62e.printStackTrace();63throw new RuntimeException("Failed to initialize GUI");64}65}6667private void initializeGUI() {68frame = new JFrame("bug4523758");69tools = new JToolBar();70frame.getContentPane().add(tools, BorderLayout.NORTH);71combo = new JComboBox(new Object[] { "Red", "Orange", "Yellow",72"Green", "Blue", "Indigo", "Violet"});73combo.addItemListener(new ItemListener() {74public void itemStateChanged(ItemEvent event) {75itemStateChanged = true;76synchronized (itemLock) {77try {78itemLock.notifyAll();79} catch (Exception e) {80}81}82}83});84tools.add(combo);85frame.setSize(250,400);86frame.setLocation(700, 0);87frame.setVisible(true);88}8990private void doTest() throws Exception {91ExtendedRobot robot = new ExtendedRobot();92robot.waitForIdle(1000);9394final Point cl = combo.getLocationOnScreen();95final Dimension cs = combo.getSize();9697SwingUtilities.invokeAndWait(new Runnable() {98public void run() {99frame.dispose();100}101});102robot.waitForIdle(delay);103SwingUtilities.invokeAndWait(new Runnable() {104public void run() {105frame.setSize((int) cl.x - 700 + cs.width,106(int) cl.y + cs.height - 5);107frame.setVisible(true);108}109});110111robot.waitForIdle(delay*2);112Point comboLocation = combo.getLocationOnScreen();113Dimension comboSize = combo.getSize();114115robot.mouseMove((int) comboLocation.x + comboSize.width / 2,116(int) comboLocation.y + 5);117robot.waitForIdle(delay);118robot.mousePress(InputEvent.BUTTON1_MASK);119robot.delay(100);120robot.mouseRelease(InputEvent.BUTTON1_MASK);121robot.waitForIdle(delay);122123robot.mouseMove((int) comboLocation.x + comboSize.width / 2,124(int) comboLocation.y + 60);125robot.waitForIdle(delay);126robot.mousePress(InputEvent.BUTTON1_MASK);127robot.delay(100);128robot.mouseRelease(InputEvent.BUTTON1_MASK);129robot.waitForIdle(delay);130131if (! itemStateChanged) {132synchronized (itemLock) {133try {134itemLock.wait(WAIT_EVENT_DELAY);135} catch (Exception e) {136}137}138}139if (! itemStateChanged) {140System.err.println("FAIL: ItemEvent not triggered when mouse clicked on combo box drop down");141passed = false;142}143144if (! passed) {145System.err.println("Test failed!");146captureScreenAndSave();147throw new RuntimeException("FAIL");148} else {149System.out.println("Test passed!");150}151}152153public static void main(String[] args) {154try {155bug4523758 test = new bug4523758();156test.doTest();157} catch (Exception e) {158e.printStackTrace();159throw new RuntimeException("FAIL");160}161}162163/**164* Do screen capture and save it as image165*/166private static void captureScreenAndSave() {167168try {169Robot robot = new Robot();170Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();171Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);172System.out.println("About to screen capture - " + rectangle);173java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle);174javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg"));175robot.delay(3000);176} catch (Throwable t) {177System.out.println("WARNING: Exception thrown while screen capture!");178t.printStackTrace();179}180}181}182183184