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