Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Dialog/CloseDialog/CloseDialogTest.java
38828 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*/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* @bug 804370533* @summary Can't exit color chooser dialog when running as an applet34* @run main CloseDialogTest35*/36public class CloseDialogTest {3738private static volatile Frame frame;39private static volatile Dialog dialog;40private static volatile InputStream testErrorStream;41private static final PrintStream systemErrStream = System.err;42private static final AtomicReference<Exception> caughtException43= new AtomicReference<>();4445public static void main(String[] args) throws Exception {4647// redirect System err48PipedOutputStream errorOutputStream = new PipedOutputStream();49testErrorStream = new PipedInputStream(errorOutputStream);50System.setErr(new PrintStream(errorOutputStream));5152ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");53try {54new Thread(swingTG, () -> {55SunToolkit.createNewAppContext();56SwingUtilities.invokeLater(() -> {57frame = new Frame();58frame.setSize(300, 300);59frame.setVisible(true);6061dialog = new Dialog(frame);62dialog.setSize(200, 200);63dialog.setModal(true);64dialog.setVisible(true);65});66}).start();6768Thread.sleep(400);6970Thread disposeThread = new Thread(swingTG, () ->71SwingUtilities.invokeLater(() -> {72try {73while (dialog == null || !dialog.isVisible()) {74Thread.sleep(100);75}76dialog.setVisible(false);77dialog.dispose();78frame.dispose();79} catch (Exception e) {80caughtException.set(e);81}82}));83disposeThread.start();84disposeThread.join();85Thread.sleep(500);8687// read System err88final char[] buffer = new char[2048];89System.err.print("END");90System.setErr(systemErrStream);91try (Reader in = new InputStreamReader(testErrorStream, "UTF-8")) {92int size = in.read(buffer, 0, buffer.length);93String errorString = new String(buffer, 0, size);94if (!errorString.startsWith("END")) {95System.err.println(errorString.96substring(0, errorString.length() - 4));97throw new RuntimeException("Error output is not empty!");98}99}100} finally {101if (caughtException.get() != null) {102throw new RuntimeException("Failed. Caught exception!",103caughtException.get());104}105}106}107108private static ThreadGroup getRootThreadGroup() {109ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();110while (threadGroup.getParent() != null) {111threadGroup = threadGroup.getParent();112}113return threadGroup;114}115}116117118