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 test.java.awt.regtesthelpers.Util;
  44 
  45 public class LWModalTest
  46 {
  47     private static JFrame frame;
  48     private static volatile JInternalFrame internalFrame;
  49 
  50     private static volatile boolean passed = false;
  51 
  52     private static void init()
  53     {
  54         frame = new JFrame("JFrame");
  55         frame.setBounds(100, 100, 320, 240);
  56         frame.setVisible(true);
  57         Util.waitForIdle(null);
  58 
  59         new Thread(new Runnable()
  60         {
  61             public void run()
  62             {
  63                 JOptionPane p = new JOptionPane("Message");
  64                 internalFrame = p.createInternalFrame(frame.getContentPane(), "Title");
  65                 internalFrame.setVisible(true);
  66                 try
  67                 {
  68                     Method m = Container.class.getDeclaredMethod("startLWModal", (Class[])null);
  69                     m.setAccessible(true);
  70                     m.invoke(internalFrame, (Object[])null);
  71                 }
  72                 catch (Exception z)
  73                 {
  74                     z.printStackTrace(System.err);
  75                     LWModalTest.fail(z.getMessage());
  76                     return;
  77                 }
  78                 passed = true;
  79             }
  80         }).start();
  81 
  82         try
  83         {
  84             Thread.sleep(3000);
  85             Util.waitForIdle(null);
  86 
  87             internalFrame.dispose();
  88 
  89             Method m = Container.class.getDeclaredMethod("stopLWModal", (Class[])null);
  90             m.setAccessible(true);
  91             m.invoke(internalFrame, (Object[])null);
  92 
  93             Thread.sleep(3000);
  94             Util.waitForIdle(null);
  95         }
  96         catch (Exception z)
  97         {
  98             z.printStackTrace(System.err);
  99             LWModalTest.fail(z.getMessage());
 100             return;
 101         }
 102 
 103         if (passed)
 104         {
 105             LWModalTest.pass();
 106         }
 107         else
 108         {
 109             LWModalTest.fail("showInternalMessageDialog() has not returned");
 110         }
 111     }
 112 
 113     private static boolean theTestPassed = false;
 114     private static boolean testGeneratedInterrupt = false;
 115     private static String failureMessage = "";
 116 
 117     private static Thread mainThread = null;
 118 
 119     private static int sleepTime = 60000;
 120 
 121     public static void main(String args[])
 122         throws InterruptedException
 123     {
 124         mainThread = Thread.currentThread();
 125         try
 126         {
 127             init();
 128         }
 129         catch (TestPassedException e)
 130         {
 131             return;
 132         }
 133 
 134         try
 135         {
 136             Thread.sleep( sleepTime );
 137             throw new RuntimeException("Timed out after " + sleepTime/1000 + " seconds");
 138         }
 139         catch (InterruptedException e)
 140         {
 141             if(!testGeneratedInterrupt) throw e;
 142 
 143             testGeneratedInterrupt = false;
 144 
 145             if (theTestPassed == false)
 146             {
 147                 throw new RuntimeException(failureMessage);
 148             }
 149         }
 150     }
 151 
 152     public static synchronized void setTimeoutTo(int seconds)
 153     {
 154         sleepTime = seconds * 1000;
 155     }
 156 
 157     public static synchronized void pass()
 158     {
 159         if (mainThread == Thread.currentThread())
 160         {
 161             theTestPassed = true;
 162             throw new TestPassedException();
 163         }
 164         theTestPassed = true;
 165         testGeneratedInterrupt = true;
 166         mainThread.interrupt();
 167     }
 168 
 169     public static synchronized void fail()
 170     {
 171         fail("it just plain failed! :-)");
 172     }
 173 
 174     public static synchronized void fail(String whyFailed)
 175     {
 176         if (mainThread == Thread.currentThread())
 177         {
 178             throw new RuntimeException(whyFailed);
 179         }
 180         theTestPassed = false;
 181         testGeneratedInterrupt = true;
 182         failureMessage = whyFailed;
 183         mainThread.interrupt();
 184     }
 185 }
 186 
 187 class TestPassedException extends RuntimeException
 188 {
 189 }