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