1 /*
   2  * Copyright (c) 2014, 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 /*
  26   @test
  27   @bug 8041990
  28   @summary Language specific keys does not work in applets when opened outside the browser
  29   @author Petr Pchelko
  30 */
  31 
  32 import sun.awt.SunToolkit;
  33 
  34 import javax.swing.*;
  35 import java.awt.*;
  36 import java.awt.event.InputMethodEvent;
  37 import java.awt.font.TextHitInfo;
  38 import java.text.AttributedString;
  39 import java.util.concurrent.CountDownLatch;
  40 import java.util.concurrent.atomic.AtomicReference;
  41 
  42 public class bug8041990 {
  43     private static JFrame frame;
  44     private static JComponent component;
  45 
  46     public static void main(String[] args) throws Exception {
  47         ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
  48         ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
  49         try {
  50             Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
  51             stubThread.start();
  52             stubThread.join();
  53 
  54             CountDownLatch startSwingLatch = new CountDownLatch(1);
  55             new Thread(swingTG, () -> {
  56                 SunToolkit.createNewAppContext();
  57                 SwingUtilities.invokeLater(() -> {
  58                     frame = new JFrame();
  59                     component = new JLabel("Test Text");
  60                     frame.add(component);
  61                     frame.setBounds(100, 100, 100, 100);
  62                     frame.setVisible(true);
  63                     startSwingLatch.countDown();
  64                 });
  65             }).start();
  66             startSwingLatch.await();
  67 
  68             AtomicReference<Exception> caughtException = new AtomicReference<>();
  69             Thread checkThread = new Thread(getRootThreadGroup(), () -> {
  70                 try {
  71                     // If the bug is present this will throw exception
  72                     new InputMethodEvent(component,
  73                             InputMethodEvent.CARET_POSITION_CHANGED,
  74                             TextHitInfo.leading(0),
  75                             TextHitInfo.trailing(0));
  76                 } catch (Exception e) {
  77                     caughtException.set(e);
  78                 }
  79             });
  80             checkThread.start();
  81             checkThread.join();
  82 
  83             if (caughtException.get() != null) {
  84                 throw new RuntimeException("Failed. Caught exception!", caughtException.get());
  85             }
  86         } finally {
  87             new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
  88                 if (frame != null) {
  89                     frame.dispose();
  90                 }
  91             })).start();
  92         }
  93     }
  94 
  95     private static ThreadGroup getRootThreadGroup() {
  96         ThreadGroup currentTG = Thread.currentThread().getThreadGroup();
  97         ThreadGroup parentTG = currentTG.getParent();
  98         while (parentTG != null) {
  99             currentTG = parentTG;
 100             parentTG = currentTG.getParent();
 101         }
 102         return currentTG;
 103     }
 104 }