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