1 /*
   2  * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @bug 6178755
  27   @summary The test checks that Container's method startLWModal
  28 and stopLWModal work correctly. The test scenario is very close
  29 to JOptionPane.showInternal*Dialog methods
  30   @author artem.ananiev@...: area=awt.modal
  31   @library ../../regtesthelpers
  32   @build Util
  33   @run main LWModalTest
  34 */
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 
  39 import java.lang.reflect.*;
  40 
  41 import javax.swing.*;
  42 
  43 import sun.awt.*;
  44 
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 public class LWModalTest
  48 {
  49     private static JFrame frame;
  50     private static volatile JInternalFrame internalFrame;
  51 
  52     private static volatile boolean passed = false;
  53 
  54     private static void init()
  55     {
  56         frame = new JFrame("JFrame");
  57         frame.setBounds(100, 100, 320, 240);
  58         frame.setVisible(true);
  59         Util.waitForIdle(null);
  60 
  61         new Thread(new Runnable()
  62         {
  63             public void run()
  64             {
  65                 JOptionPane p = new JOptionPane("Message");
  66                 internalFrame = p.createInternalFrame(frame.getContentPane(), "Title");
  67                 internalFrame.setVisible(true);
  68                 try
  69                 {
  70                     Method m = Container.class.getDeclaredMethod("startLWModal", (Class[])null);
  71                     m.setAccessible(true);
  72                     m.invoke(internalFrame, (Object[])null);
  73                 }
  74                 catch (Exception z)
  75                 {
  76                     z.printStackTrace(System.err);
  77                     LWModalTest.fail(z.getMessage());
  78                     return;
  79                 }
  80                 passed = true;
  81             }
  82         }).start();
  83 
  84         try
  85         {
  86             Thread.sleep(3000);
  87             Util.waitForIdle(null);
  88 
  89             internalFrame.dispose();
  90 
  91             Method m = Container.class.getDeclaredMethod("stopLWModal", (Class[])null);
  92             m.setAccessible(true);
  93             m.invoke(internalFrame, (Object[])null);
  94 
  95             Thread.sleep(3000);
  96             Util.waitForIdle(null);
  97         }
  98         catch (Exception z)
  99         {
 100             z.printStackTrace(System.err);
 101             LWModalTest.fail(z.getMessage());
 102             return;
 103         }
 104 
 105         if (passed)
 106         {
 107             LWModalTest.pass();
 108         }
 109         else
 110         {
 111             LWModalTest.fail("showInternalMessageDialog() has not returned");
 112         }
 113     }
 114 
 115     private static boolean theTestPassed = false;
 116     private static boolean testGeneratedInterrupt = false;
 117     private static String failureMessage = "";
 118 
 119     private static Thread mainThread = null;
 120 
 121     private static int sleepTime = 60000;
 122 
 123     public static void main(String args[])
 124         throws InterruptedException
 125     {
 126         mainThread = Thread.currentThread();
 127         try
 128         {
 129             init();
 130         }
 131         catch (TestPassedException e)
 132         {
 133             return;
 134         }
 135 
 136         try
 137         {
 138             Thread.sleep( sleepTime );
 139             throw new RuntimeException("Timed out after " + sleepTime/1000 + " seconds");
 140         }
 141         catch (InterruptedException e)
 142         {
 143             if(!testGeneratedInterrupt) throw e;
 144 
 145             testGeneratedInterrupt = false;
 146 
 147             if (theTestPassed == false)
 148             {
 149                 throw new RuntimeException(failureMessage);
 150             }
 151         }
 152     }
 153 
 154     public static synchronized void setTimeoutTo(int seconds)
 155     {
 156         sleepTime = seconds * 1000;
 157     }
 158 
 159     public static synchronized void pass()
 160     {
 161         if (mainThread == Thread.currentThread())
 162         {
 163             theTestPassed = true;
 164             throw new TestPassedException();
 165         }
 166         theTestPassed = true;
 167         testGeneratedInterrupt = true;
 168         mainThread.interrupt();
 169     }
 170 
 171     public static synchronized void fail()
 172     {
 173         fail("it just plain failed! :-)");
 174     }
 175 
 176     public static synchronized void fail(String whyFailed)
 177     {
 178         if (mainThread == Thread.currentThread())
 179         {
 180             throw new RuntimeException(whyFailed);
 181         }
 182         theTestPassed = false;
 183         testGeneratedInterrupt = true;
 184         failureMessage = whyFailed;
 185         mainThread.interrupt();
 186     }
 187 }
 188 
 189 class TestPassedException extends RuntimeException
 190 {
 191 }