1 /*
   2  * Copyright (c) 2018, 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 8213183
  28  * @summary verifies InputMethod reconnection
  29  * @run main/manual ReconnectTest
  30  */
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 
  34 public class ReconnectTest {
  35 
  36     private static void init() throws Exception {
  37         String[] instructions
  38             = {
  39                 "This test supports Linux and Japanese/Korean/Chinese",
  40                 "input method(IM). Under other environment, press Pass.",
  41                 "Test steps:",
  42                 "1.Turn on IM and input some Asian characters to TestWindow.",
  43                 "2.kill IM frontend by OS, and restart it.",
  44                 "  e.g. for ibus, $ ibus-daemon -r --xim &",
  45                 "3.Change focus to this Instructions window and back to TestWindow.",
  46                 "4.Confirm IM can be activated."
  47               };
  48 
  49         Sysout.createDialog();
  50         Sysout.printInstructions(instructions);
  51     }
  52 
  53     static Frame mainFrame;
  54 
  55     public static void initTestWindow() {
  56         mainFrame = new Frame();
  57         mainFrame.setTitle("TestWindow");
  58         mainFrame.setLayout(new BorderLayout());
  59         Panel p0 = new Panel();
  60         p0.setLayout(new FlowLayout());
  61         mainFrame.add(p0, BorderLayout.CENTER);
  62         TextArea text = new TextArea();
  63         p0.add(text);
  64 
  65         mainFrame.pack();
  66         mainFrame.setLocation(700, 10);
  67         mainFrame.setVisible(true);
  68     }
  69 
  70     public static void dispose() {
  71         Sysout.dispose();
  72         mainFrame.dispose();
  73     }
  74 
  75     /**
  76      * ***************************************************
  77      * Standard Test Machinery Section DO NOT modify anything in this section --
  78      * it's a standard chunk of code which has all of the synchronisation
  79      * necessary for the test harness. By keeping it the same in all tests, it
  80      * is easier to read and understand someone else's test, as well as insuring
  81      * that all tests behave correctly with the test harness. There is a section
  82      * following this for test-defined classes
  83      * ****************************************************
  84      */
  85     private static boolean theTestPassed = false;
  86     private static boolean testGeneratedInterrupt = false;
  87     private static String failureMessage = "";
  88     private static Thread mainThread = null;
  89     final private static int sleepTime = 300000;
  90 
  91     public static void main(String args[]) throws Exception {
  92         mainThread = Thread.currentThread();
  93         try {
  94             init();
  95             initTestWindow();
  96         } catch (Exception e) {
  97             e.printStackTrace();
  98         }
  99         try {
 100             mainThread.sleep(sleepTime);
 101         } catch (InterruptedException e) {
 102             dispose();
 103             if (testGeneratedInterrupt && !theTestPassed) {
 104                 throw new Exception(failureMessage);
 105             }
 106         }
 107         if (!testGeneratedInterrupt) {
 108             dispose();
 109             throw new RuntimeException("Timed out after " + sleepTime / 1000
 110                     + " seconds");
 111         }
 112     }
 113 
 114     public static synchronized void pass() {
 115         theTestPassed = true;
 116         testGeneratedInterrupt = true;
 117         mainThread.interrupt();
 118     }
 119 
 120     public static synchronized void fail(String whyFailed) {
 121         theTestPassed = false;
 122         testGeneratedInterrupt = true;
 123         failureMessage = whyFailed;
 124         mainThread.interrupt();
 125     }
 126 }
 127 
 128 // *********** End Standard Test Machinery Section **********
 129 /**
 130  * **************************************************
 131  * Standard Test Machinery DO NOT modify anything below -- it's a standard chunk
 132  * of code whose purpose is to make user interaction uniform, and thereby make
 133  * it simpler to read and understand someone else's test.
 134  * **************************************************
 135  */
 136 /**
 137  * This is part of the standard test machinery. It creates a dialog (with the
 138  * instructions), and is the interface for sending text messages to the user. To
 139  * print the instructions, send an array of strings to Sysout.createDialog
 140  * WithInstructions method. Put one line of instructions per array entry. To
 141  * display a message for the tester to see, simply call Sysout.println with the
 142  * string to be displayed. This mimics System.out.println but works within the
 143  * test harness as well as standalone.
 144  */
 145 class Sysout {
 146 
 147     private static TestDialog dialog;
 148     private static Frame frame;
 149 
 150     public static void createDialog() {
 151         frame = new Frame();
 152         dialog = new TestDialog(frame, "Instructions");
 153         String[] defInstr = {"Instructions will appear here. ", ""};
 154         dialog.printInstructions(defInstr);
 155         dialog.show();
 156         println("Any messages for the tester will display here.");
 157     }
 158 
 159     public static void printInstructions(String[] instructions) {
 160         dialog.printInstructions(instructions);
 161     }
 162 
 163     public static void println(String messageIn) {
 164         dialog.displayMessage(messageIn);
 165     }
 166 
 167     public static void dispose() {
 168         dialog.dispose();
 169         frame.dispose();
 170     }
 171 }
 172 
 173 /**
 174  * This is part of the standard test machinery. It provides a place for the test
 175  * instructions to be displayed, and a place for interactive messages to the
 176  * user to be displayed. To have the test instructions displayed, see Sysout. To
 177  * have a message to the user be displayed, see Sysout. Do not call anything in
 178  * this dialog directly.
 179  */
 180 class TestDialog extends Dialog implements ActionListener {
 181 
 182     TextArea instructionsText;
 183     TextArea messageText;
 184     int maxStringLength = 80;
 185     Panel buttonP;
 186     Button failB;
 187     Button passB;
 188 
 189     // DO NOT call this directly, go through Sysout
 190     public TestDialog(Frame frame, String name) {
 191         super(frame, name);
 192         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 193         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
 194         add("North", instructionsText);
 195 
 196         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
 197         add("Center", messageText);
 198 
 199         buttonP = new Panel();
 200         failB = new Button("Fail");
 201         failB.setActionCommand("fail");
 202         failB.addActionListener(this);
 203         passB = new Button("Pass");
 204         buttonP.add(passB);
 205         buttonP.add(failB);
 206         passB.addActionListener(new ActionListener() {
 207             @Override
 208             public void actionPerformed(ActionEvent ae) {
 209                 ReconnectTest.pass();
 210             }
 211         });
 212 
 213         add("South", buttonP);
 214         pack();
 215         setVisible(true);
 216     }
 217 
 218     // DO NOT call this directly, go through Sysout
 219     public void printInstructions(String[] instructions) {
 220         instructionsText.setText("");
 221         String printStr, remainingStr;
 222         for (int i = 0; i < instructions.length; i++) {
 223             remainingStr = instructions[i];
 224             while (remainingStr.length() > 0) {
 225                 if (remainingStr.length() >= maxStringLength) {
 226                     int posOfSpace = remainingStr.
 227                             lastIndexOf(' ', maxStringLength - 1);
 228 
 229                     if (posOfSpace <= 0) {
 230                         posOfSpace = maxStringLength - 1;
 231                     }
 232 
 233                     printStr = remainingStr.substring(0, posOfSpace + 1);
 234                     remainingStr = remainingStr.substring(posOfSpace + 1);
 235                 } else {
 236                     printStr = remainingStr;
 237                     remainingStr = "";
 238                 }
 239                 instructionsText.append(printStr + "\n");
 240             }
 241         }
 242     }
 243 
 244     public void displayMessage(String messageIn) {
 245         messageText.append(messageIn + "\n");
 246     }
 247 
 248     public void actionPerformed(ActionEvent e) {
 249         if (e.getActionCommand() == "fail") {
 250             ReconnectTest.fail("User Clicked Fail");
 251         }
 252     }
 253 }