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