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 /java/awt/patchlib /lib/testlibrary
  31  * @modules java.desktop/sun.awt
  32  * @build jdk.testlibrary.Platform
  33  * @build java.desktop/java.awt.Helper
  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().exec(java_home +
  77                                                      "/bin/java -Dawt.toolkit=sun.awt.X11.XToolkit TesterClient "
  78                                                      + test.getName() + " " + window + buf,
  79                                                      enva);
  80             System.err.println("Test for " + test.getName() + " has started.");
  81             log.fine("Test for " + test.getName() + " has started.");
  82             new InputReader(proc.getInputStream());
  83             new InputReader(proc.getErrorStream());
  84             try {
  85                 passed = (proc.waitFor() == 0);
  86             } catch (InterruptedException ie) {
  87             }
  88             log.fine("Test for " + test.getName() + " has finished.");
  89             File logFile = new File("java3.txt");
  90             if (logFile.exists()) {
  91                 logFile.renameTo(new File(test.getName() + ".txt"));
  92             }
  93             return proc;
  94         } catch (IOException ex1) {
  95             ex1.printStackTrace();
  96         }
  97         return null;
  98     }
  99 
 100     public static void main(String[] args) throws Throwable {
 101         if (Platform.isWindows() || Platform.isOSX()) {
 102             return;
 103         }
 104 
 105         // Enabled XEmbed
 106         System.setProperty("sun.awt.xembedserver", "true");
 107 
 108         if (args.length == 1) {
 109             Class cl = Class.forName("sun.awt.X11.XEmbedServerTester");
 110             Method meth = cl.getMethod(args[0], new Class[0]);
 111             System.err.println("Performing single test " + args[0]);
 112             boolean res = performTest(meth);
 113             if (!res) {
 114                 System.err.println("Test " + args[0] + " has failed");
 115             } else {
 116                 System.err.println("Test " + args[0] + " has passed");
 117             }
 118         } else {
 119             Class cl = Class.forName("sun.awt.X11.XEmbedServerTester");
 120             Method[] meths = cl.getMethods();
 121             LinkedList failed = new LinkedList();
 122             for (int i = 0; i < meths.length; i++) {
 123                 Method meth = meths[i];
 124                 if (meth.getReturnType() == Void.TYPE && meth.getName().startsWith("test") && meth.getParameterTypes().length == 0) {
 125                     System.err.println("Performing " + meth.getName());
 126                     boolean res = performTest(meth);
 127                     if (!res) {
 128                         failed.add(meth);
 129                     }
 130                 }
 131             }
 132             log.info("Testing finished.");
 133             if (failed.size() != 0) {
 134                 System.err.println("Some tests have failed:");
 135                 Iterator iter = failed.iterator();
 136                 while(iter.hasNext()) {
 137                     Method meth = (Method)iter.next();
 138                     System.err.println(meth.getName());
 139                 }
 140                 throw new RuntimeException("TestFAILED: some of the testcases are failed");
 141             } else {
 142                 System.err.println("All PASSED");
 143             }
 144         }
 145     }
 146 
 147     private static boolean performTest(Method meth) {
 148         RunTestXEmbed test = new RunTestXEmbed(meth);
 149         test.addClient();
 150         test.dispose();
 151         return test.isPassed();
 152     }
 153 
 154     public boolean isPassed() {
 155         return passed;
 156     }
 157 }
 158 
 159 class InputReader extends Thread {
 160     private InputStream stream;
 161     public InputReader(InputStream stream) {
 162         this.stream = stream;
 163         start();
 164     }
 165     public void run() {
 166         while (!interrupted()) {
 167             try {
 168                 int inp = stream.read();
 169                 if (inp != -1) {
 170                     System.out.write(inp);
 171                 } else {
 172                     try {
 173                         Thread.sleep(100);
 174                     } catch (Exception iie) {
 175                     }
 176                 }
 177             } catch (IOException ie) {
 178                 break;
 179             }
 180         }
 181     }
 182 }