Path: blob/master/test/jdk/java/awt/Modal/LWModalTest/LWModalTest.java
41153 views
/*1* Copyright (c) 2006, 2016, 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 617875527@summary The test checks that Container's method startLWModal28and stopLWModal work correctly. The test scenario is very close29to JOptionPane.showInternal*Dialog methods30@modules java.desktop/java.awt:open31@author artem.ananiev@...: area=awt.modal32@library ../../regtesthelpers33@build Util34@run main LWModalTest35*/3637import java.awt.*;38import java.awt.event.*;3940import java.lang.reflect.*;4142import javax.swing.*;4344import test.java.awt.regtesthelpers.Util;4546public class LWModalTest47{48private static JFrame frame;49private static volatile JInternalFrame internalFrame;5051private static volatile boolean passed = false;5253private static void init()54{55frame = new JFrame("JFrame");56frame.setBounds(100, 100, 320, 240);57frame.setVisible(true);58Util.waitForIdle(null);5960new Thread(new Runnable()61{62public void run()63{64JOptionPane p = new JOptionPane("Message");65internalFrame = p.createInternalFrame(frame.getContentPane(), "Title");66internalFrame.setVisible(true);67try68{69Method m = Container.class.getDeclaredMethod("startLWModal", (Class[])null);70m.setAccessible(true);71m.invoke(internalFrame, (Object[])null);72}73catch (Exception z)74{75z.printStackTrace(System.err);76LWModalTest.fail(z.getMessage());77return;78}79passed = true;80}81}).start();8283try84{85Thread.sleep(3000);86Util.waitForIdle(null);8788internalFrame.dispose();8990Method m = Container.class.getDeclaredMethod("stopLWModal", (Class[])null);91m.setAccessible(true);92m.invoke(internalFrame, (Object[])null);9394Thread.sleep(3000);95Util.waitForIdle(null);96}97catch (Exception z)98{99z.printStackTrace(System.err);100LWModalTest.fail(z.getMessage());101return;102}103104if (passed)105{106LWModalTest.pass();107}108else109{110LWModalTest.fail("showInternalMessageDialog() has not returned");111}112}113114private static boolean theTestPassed = false;115private static boolean testGeneratedInterrupt = false;116private static String failureMessage = "";117118private static Thread mainThread = null;119120private static int sleepTime = 60000;121122public static void main(String args[])123throws InterruptedException124{125mainThread = Thread.currentThread();126try127{128init();129}130catch (TestPassedException e)131{132return;133}134135try136{137Thread.sleep( sleepTime );138throw new RuntimeException("Timed out after " + sleepTime/1000 + " seconds");139}140catch (InterruptedException e)141{142if(!testGeneratedInterrupt) throw e;143144testGeneratedInterrupt = false;145146if (theTestPassed == false)147{148throw new RuntimeException(failureMessage);149}150}151}152153public static synchronized void setTimeoutTo(int seconds)154{155sleepTime = seconds * 1000;156}157158public static synchronized void pass()159{160if (mainThread == Thread.currentThread())161{162theTestPassed = true;163throw new TestPassedException();164}165theTestPassed = true;166testGeneratedInterrupt = true;167mainThread.interrupt();168}169170public static synchronized void fail()171{172fail("it just plain failed! :-)");173}174175public static synchronized void fail(String whyFailed)176{177if (mainThread == Thread.currentThread())178{179throw new RuntimeException(whyFailed);180}181theTestPassed = false;182testGeneratedInterrupt = true;183failureMessage = whyFailed;184mainThread.interrupt();185}186}187188class TestPassedException extends RuntimeException189{190}191192193