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