1 /*
   2  * Copyright (c) 2004, 2015, 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  * @bug 4931668 7146533
  27  * @summary Tests XEmbed server/client functionality
  28  * @author Denis Mikhalkin: area=awt.xembed
  29  * @requires (!(os.family=="mac") & !(os.family=="windows"))
  30  * @library /lib/testlibrary
  31  * @build jdk.testlibrary.Platform
  32  * @modules java.desktop/sun.awt
  33  * @compile JavaClient.java TesterClient.java TestXEmbedServer.java
  34  * @run main/timeout=6000 RunTestXEmbed
  35  */
  36 
  37 import java.awt.Rectangle;
  38 import java.lang.reflect.Method;
  39 import java.util.logging.*;
  40 import java.util.*;
  41 import java.io.*;
  42 
  43 public class RunTestXEmbed extends TestXEmbedServer {
  44     private static final Logger log = Logger.getLogger("test.xembed");
  45     private Method test;
  46     private boolean passed = false;
  47     public RunTestXEmbed(Method test) {
  48         super(false);
  49         this.test = test;
  50     }
  51 
  52     public Process startClient(Rectangle bounds[], long window) {
  53         try {
  54             String java_home = System.getProperty("java.home");
  55             StringBuilder buf = new StringBuilder();
  56             for (int i = 0; i < bounds.length; i++) {
  57                 buf.append(" " + bounds[i].x);
  58                 buf.append(" " + bounds[i].y);
  59                 buf.append(" " + bounds[i].width);
  60                 buf.append(" " + bounds[i].height);
  61             }
  62             Map envs = System.getenv();
  63             String enva[] = new String[envs.size()];
  64             int ind = 0;
  65             Iterator iter = envs.entrySet().iterator();
  66             while (iter.hasNext()) {
  67                 Map.Entry entry = (Map.Entry)iter.next();
  68                 if (!"AWT_TOOLKIT".equals(entry.getKey())) {
  69                     enva[ind++] = entry.getKey() + "=" + entry.getValue();
  70                 } else {
  71                     enva[ind++] = "AWT_TOOLKIT=sun.awt.X11.XToolkit";
  72                 }
  73             }
  74             Process proc = Runtime.getRuntime().exec(java_home +
  75                                                      "/bin/java -Dawt.toolkit=sun.awt.X11.XToolkit TesterClient "
  76                                                      + test.getName() + " " + window + buf,
  77                                                      enva);
  78             System.err.println("Test for " + test.getName() + " has started.");
  79             log.fine("Test for " + test.getName() + " has started.");
  80             new InputReader(proc.getInputStream());
  81             new InputReader(proc.getErrorStream());
  82             try {
  83                 passed = (proc.waitFor() == 0);
  84             } catch (InterruptedException ie) {
  85             }
  86             log.fine("Test for " + test.getName() + " has finished.");
  87             File logFile = new File("java3.txt");
  88             if (logFile.exists()) {
  89                 logFile.renameTo(new File(test.getName() + ".txt"));
  90             }
  91             return proc;
  92         } catch (IOException ex1) {
  93             ex1.printStackTrace();
  94         }
  95         return null;
  96     }
  97 
  98     public static void main(String[] args) throws Throwable {
  99         if (Platform.isWindows() || Platform.isOSX()) {
 100             return;
 101         }
 102 
 103         // Enabled XEmbed
 104         System.setProperty("sun.awt.xembedserver", "true");
 105 
 106         if (args.length == 1) {
 107             Class cl = Class.forName("sun.awt.X11.XEmbedServerTester");
 108             Method meth = cl.getMethod(args[0], new Class[0]);
 109             System.err.println("Performing single test " + args[0]);
 110             boolean res = performTest(meth);
 111             if (!res) {
 112                 System.err.println("Test " + args[0] + " has failed");
 113             } else {
 114                 System.err.println("Test " + args[0] + " has passed");
 115             }
 116         } else {
 117             Class cl = Class.forName("sun.awt.X11.XEmbedServerTester");
 118             Method[] meths = cl.getMethods();
 119             LinkedList failed = new LinkedList();
 120             for (int i = 0; i < meths.length; i++) {
 121                 Method meth = meths[i];
 122                 if (meth.getReturnType() == Void.TYPE && meth.getName().startsWith("test") && meth.getParameterTypes().length == 0) {
 123                     System.err.println("Performing " + meth.getName());
 124                     boolean res = performTest(meth);
 125                     if (!res) {
 126                         failed.add(meth);
 127                     }
 128                 }
 129             }
 130             log.info("Testing finished.");
 131             if (failed.size() != 0) {
 132                 System.err.println("Some tests have failed:");
 133                 Iterator iter = failed.iterator();
 134                 while(iter.hasNext()) {
 135                     Method meth = (Method)iter.next();
 136                     System.err.println(meth.getName());
 137                 }
 138                 throw new RuntimeException("TestFAILED: some of the testcases are failed");
 139             } else {
 140                 System.err.println("All PASSED");
 141             }
 142         }
 143     }
 144 
 145     private static boolean performTest(Method meth) {
 146         RunTestXEmbed test = new RunTestXEmbed(meth);
 147         test.addClient();
 148         test.dispose();
 149         return test.isPassed();
 150     }
 151 
 152     public boolean isPassed() {
 153         return passed;
 154     }
 155 }
 156 
 157 class InputReader extends Thread {
 158     private InputStream stream;
 159     public InputReader(InputStream stream) {
 160         this.stream = stream;
 161         start();
 162     }
 163     public void run() {
 164         while (!interrupted()) {
 165             try {
 166                 int inp = stream.read();
 167                 if (inp != -1) {
 168                     System.out.write(inp);
 169                 } else {
 170                     try {
 171                         Thread.sleep(100);
 172                     } catch (Exception iie) {
 173                     }
 174                 }
 175             } catch (IOException ie) {
 176                 break;
 177             }
 178         }
 179     }
 180 }