Path: blob/master/test/jdk/java/awt/Dialog/CloseDialog/CloseDialogTest.java
41153 views
/*1* Copyright (c) 2014, 2017, 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*/2223import java.awt.Dialog;24import java.awt.Frame;25import java.io.*;26import javax.swing.*;27import sun.awt.SunToolkit;28import java.util.concurrent.atomic.AtomicReference;2930/**31* @test32* @key headful33* @bug 804370534* @summary Can't exit color chooser dialog when running as an applet35* @modules java.desktop/sun.awt36* @run main CloseDialogTest37*/3839public class CloseDialogTest {4041private static volatile Frame frame;42private static volatile Dialog dialog;43private static volatile InputStream testErrorStream;44private static final PrintStream systemErrStream = System.err;45private static final AtomicReference<Exception> caughtException46= new AtomicReference<>();4748public static void main(String[] args) throws Exception {4950// redirect System err51PipedOutputStream errorOutputStream = new PipedOutputStream();52testErrorStream = new PipedInputStream(errorOutputStream);53System.setErr(new PrintStream(errorOutputStream));5455ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");56try {57new Thread(swingTG, () -> {58SunToolkit.createNewAppContext();59SwingUtilities.invokeLater(() -> {60frame = new Frame();61frame.setSize(300, 300);62frame.setVisible(true);6364dialog = new Dialog(frame);65dialog.setSize(200, 200);66dialog.setModal(true);67dialog.setVisible(true);68});69}).start();7071Thread.sleep(400);7273Thread disposeThread = new Thread(swingTG, () ->74SwingUtilities.invokeLater(() -> {75try {76while (dialog == null || !dialog.isVisible()) {77Thread.sleep(100);78}79dialog.setVisible(false);80dialog.dispose();81frame.dispose();82} catch (Exception e) {83caughtException.set(e);84}85}));86disposeThread.start();87disposeThread.join();88Thread.sleep(500);8990// read System err91final char[] buffer = new char[2048];92System.err.print("END");93System.setErr(systemErrStream);94try (Reader in = new InputStreamReader(testErrorStream, "UTF-8")) {95int size = in.read(buffer, 0, buffer.length);96String errorString = new String(buffer, 0, size);97if (!errorString.startsWith("END")) {98System.err.println(errorString.99substring(0, errorString.length() - 4));100throw new RuntimeException("Error output is not empty!");101}102}103} finally {104if (caughtException.get() != null) {105throw new RuntimeException("Failed. Caught exception!",106caughtException.get());107}108}109}110111private static ThreadGroup getRootThreadGroup() {112ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();113while (threadGroup.getParent() != null) {114threadGroup = threadGroup.getParent();115}116return threadGroup;117}118}119120121