1 /*
   2  * Copyright (c) 2007, 2017, 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. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.input.awt;
  26 
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileWriter;
  31 import java.io.IOException;
  32 import java.io.InputStreamReader;
  33 import java.util.logging.Level;
  34 import java.util.logging.Logger;
  35 import org.jemmy.JemmyException;
  36 import org.jemmy.Rectangle;
  37 import org.jemmy.env.Environment;
  38 import org.testng.annotations.AfterClass;
  39 import org.testng.annotations.AfterMethod;
  40 import org.testng.annotations.BeforeClass;
  41 import org.testng.annotations.BeforeMethod;
  42 import org.testng.annotations.Test;
  43 
  44 import static org.testng.Assert.fail;
  45 import static org.testng.AssertJUnit.assertEquals;
  46 import static org.testng.AssertJUnit.assertTrue;
  47 
  48 
  49 /**
  50  * TODO: this test is unstable
  51  * @author Alexander Kouznetsov <mrkam@mail.ru>
  52  */
  53 public class RobotExecutorTest {
  54 
  55 //    public RobotExecutorTest() {
  56 //    }
  57 //
  58 //    static File props;
  59 //
  60 //    @BeforeClass
  61 //    public static void setUpClass() throws Exception {
  62 //        props = File.createTempFile("jemmy", "properties");
  63 //        final FileWriter fileWriter = new FileWriter(props);
  64 //        fileWriter.write(AWTRobotInputFactory.OTHER_VM_CONNECTION_TIMEOUT_PROPERTY + "=" + CONNECTION_TIMEOUT);
  65 //        fileWriter.flush();
  66 //    }
  67 //
  68 //    @AfterClass
  69 //    public static void tearDownClass() throws Exception {
  70 //        props.delete();
  71 //    }
  72 //
  73 //    @BeforeMethod
  74 //    public void setUp() {
  75 //    }
  76 //
  77 //    @AfterMethod
  78 //    public void tearDown() {
  79 //    }
  80 
  81 //    /**
  82 //     * Test of get method, of class RobotExecutor.
  83 //     */
  84 //    @Test
  85 //    public void testGet() {
  86 //        System.out.println("get");
  87 //        RobotExecutor expResult = null;
  88 //        RobotExecutor result = RobotExecutor.get();
  89 //        assertEquals(expResult, result);
  90 //        // TODO review the generated test code and remove the default call to fail.
  91 //        fail("The test case is a prototype.");
  92 //    }
  93 //
  94 //    /**
  95 //     * Test of createScreenCapture method, of class RobotExecutor.
  96 //     */
  97 //    @Test
  98 //    public void testCreateScreenCapture() {
  99 //        System.out.println("createScreenCapture");
 100 //        Rectangle screenRect = null;
 101 //        RobotExecutor instance = new RobotExecutor();
 102 //        Image expResult = null;
 103 //        Image result = instance.createScreenCapture(screenRect);
 104 //        assertEquals(expResult, result);
 105 //        // TODO review the generated test code and remove the default call to fail.
 106 //        fail("The test case is a prototype.");
 107 //    }
 108 //
 109 //    /**
 110 //     * Test of makeAnOperation method, of class RobotExecutor.
 111 //     */
 112 //    @Test
 113 //    public void testMakeAnOperation() {
 114 //        System.out.println("makeAnOperation");
 115 //        String method = "";
 116 //        Object[] params = null;
 117 //        Class[] paramClasses = null;
 118 //        RobotExecutor instance = new RobotExecutor();
 119 //        Object expResult = null;
 120 //        Object result = instance.makeAnOperation(method, params, paramClasses);
 121 //        assertEquals(expResult, result);
 122 //        // TODO review the generated test code and remove the default call to fail.
 123 //        fail("The test case is a prototype.");
 124 //    }
 125 //
 126 //    final static int CONNECTION_TIMEOUT = 30000;
 127 //    final static int DESTROY_TIMEOUT = 8000;
 128 //
 129 //    /**
 130 //     *
 131 //     * @throws IOException
 132 //     */
 133 //    @Test
 134 //    public void testOtherVMConnectionTimout() throws IOException, InterruptedException {
 135 //        System.out.println("testOtherVMConnectionTimout");
 136 //        String ROBOT_TIMEOUT = "123000";
 137 //        Object prevValue = Environment.getEnvironment().setProperty(AWTRobotInputFactory.OTHER_VM_CONNECTION_TIMEOUT_PROPERTY, ROBOT_TIMEOUT);
 138 //        RobotExecutor re = RobotExecutor.get();
 139 //        re.setRunInOtherJVM(true);
 140 //        String timeout = (String)re.getProperty(AWTRobotInputFactory.OTHER_VM_CONNECTION_TIMEOUT_PROPERTY);
 141 //        re.exit();
 142 //        Thread.sleep(DESTROY_TIMEOUT);
 143 //        Environment.getEnvironment().setProperty(AWTRobotInputFactory.OTHER_VM_CONNECTION_TIMEOUT_PROPERTY, prevValue);
 144 //        assertEquals(ROBOT_TIMEOUT, timeout);
 145 //    }
 146 //
 147 //    /**
 148 //     *
 149 //     * @throws IOException
 150 //     */
 151 //    @Test
 152 //    public void testOtherVMConnectionPort() throws IOException, InterruptedException {
 153 //        System.out.println("testOtherVMJemmyProperties");
 154 //        String PORT = "12300";
 155 //        Object prevValue = Environment.getEnvironment().setProperty(AWTRobotInputFactory.OTHER_VM_PORT_PROPERTY, PORT);
 156 //        RobotExecutor re = RobotExecutor.get();
 157 //        re.setRunInOtherJVM(true);
 158 //        String port = (String)re.getProperty(AWTRobotInputFactory.OTHER_VM_PORT_PROPERTY);
 159 //        re.exit();
 160 //        Thread.sleep(DESTROY_TIMEOUT);
 161 //        Environment.getEnvironment().setProperty(AWTRobotInputFactory.OTHER_VM_PORT_PROPERTY, prevValue);
 162 //        assertEquals(PORT, port);
 163 //    }
 164 //
 165 //    /**
 166 //     * Test of exit method, of class RobotExecutor.
 167 //     */
 168 //    @Test
 169 //    public void testExit() {
 170 //        System.out.println("exit");
 171 //        Process pp = null;
 172 //        try {
 173 //            ProcessBuilder pb = new ProcessBuilder("java", "-cp", System.getProperty("java.class.path"), "-D" + Environment.JEMMY_PROPERTIES_FILE_PROPERTY + "=" + props.getCanonicalPath(), RobotExecutor.class.getName());
 174 //            pb.redirectErrorStream(true);
 175 //            final Process p = pb.start();
 176 //            pp = p;
 177 //            new Thread() {
 178 //
 179 //                @Override
 180 //                public void run() {
 181 //                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
 182 //                    while (true) {
 183 //                        try {
 184 //                            String line = br.readLine();
 185 //                            if (line == null) {
 186 //                                break;
 187 //                            }
 188 //                            System.out.println("SERVER: " + line);
 189 //                        } catch (IOException ex) {
 190 //                            throw new JemmyException("Exception during other JVM output processing", ex);
 191 //                        }
 192 //                    }
 193 //                }
 194 //            }.start();
 195 //            RobotExecutor re = RobotExecutor.get();
 196 //            re.setRunInOtherJVM(true);
 197 //            re.exit();
 198 //            final boolean [] result = new boolean[] { false };
 199 //            synchronized (result) {
 200 //                new Thread() {
 201 //
 202 //                    @Override
 203 //                    public void run() {
 204 //                        try {
 205 //                            p.waitFor();
 206 //                            synchronized (result) {
 207 //                                result[0] = true;
 208 //                                result.notify();
 209 //                            }
 210 //                        } catch (InterruptedException ex) {
 211 //                            Logger.getLogger(RobotExecutorTest.class.getName()).log(Level.SEVERE, null, ex);
 212 //                            synchronized (result) {
 213 //                                result[0] = false;
 214 //                                result.notify();
 215 //                            }
 216 //                        }
 217 //                    }
 218 //
 219 //                }.start();
 220 //                try {
 221 //                    result.wait(DESTROY_TIMEOUT * 2);
 222 //                } catch (InterruptedException ex) {
 223 //                    Logger.getLogger(RobotExecutorTest.class.getName()).log(Level.SEVERE, null, ex);
 224 //                }
 225 //                assertTrue("Server process doesn't finish", result[0]);
 226 //            }
 227 //        } catch (IOException ex) {
 228 //            throw new JemmyException("Failed to start other JVM", ex);
 229 //        } finally {
 230 //            if (pp != null) {
 231 //                pp.destroy();
 232 //                try {
 233 //                    Thread.sleep(DESTROY_TIMEOUT);
 234 //                } catch (InterruptedException ex) {
 235 //                    Logger.getLogger(RobotExecutorTest.class.getName()).log(Level.SEVERE, null, ex);
 236 //                }
 237 //            }
 238 //        }
 239 //    }
 240 //
 241 //    /**
 242 //     * Test of main method, of class RobotExecutor.
 243 //     */
 244 //    @Test
 245 //    public void testMain() throws InterruptedException {
 246 //        System.out.println("main");
 247 //        try {
 248 //            final boolean [] result = new boolean[] { false };
 249 //            long start = System.currentTimeMillis();
 250 //            ProcessBuilder pb = new ProcessBuilder("java", "-cp", System.getProperty("java.class.path"), "-D" + Environment.JEMMY_PROPERTIES_FILE_PROPERTY + "=" + props.getCanonicalPath(), RobotExecutor.class.getName());
 251 //            pb.redirectErrorStream(true);
 252 //            final Process p = pb.start();
 253 //            synchronized(result) {
 254 //                new Thread() {
 255 //
 256 //                    @Override
 257 //                    public void run() {
 258 //                        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
 259 //                        while (true) {
 260 //                            try {
 261 //                                String line = br.readLine();
 262 //                                if (line == null) {
 263 //                                    break;
 264 //                                }
 265 //                                System.out.println("SERVER: " + line);
 266 //                                if (line.startsWith("Exiting server as there is no connection for")) {
 267 //                                    synchronized (result) {
 268 //                                        result[0] = true;
 269 //                                        result.notify();
 270 //                                    }
 271 //                                }
 272 //                            } catch (IOException ex) {
 273 //                                throw new JemmyException("Exception during other JVM output processing", ex);
 274 //                            }
 275 //                        }
 276 //                    }
 277 //                }.start();
 278 //                result.wait((int)(CONNECTION_TIMEOUT * 1.1));
 279 //                long end = System.currentTimeMillis();
 280 //                long time = end - start;
 281 //                p.destroy();
 282 //                try {
 283 //                    Thread.sleep(DESTROY_TIMEOUT);
 284 //                } catch (InterruptedException interruptedException) {
 285 //                    Logger.getLogger(RobotExecutorTest.class.getName()).log(Level.SEVERE, null, interruptedException);
 286 //                }
 287 //                if (Math.abs(time - CONNECTION_TIMEOUT) > CONNECTION_TIMEOUT * 0.3) {
 288 //                    fail("Application finished with time (" + time + ") more than 30% different from timeout (" + CONNECTION_TIMEOUT + ")");
 289 //                }
 290 //            }
 291 //        } catch (IOException ex) {
 292 //            throw new JemmyException("Failed to start other JVM", ex);
 293 //        }
 294 //    }
 295 //
 296 //    /**
 297 //     * Test of main method, of class RobotExecutor.
 298 //     */
 299 //    @Test
 300 //    public void testConnectToAlreadyRunningServer() throws InterruptedException {
 301 //        System.out.println("ConnectToAlreadyRunningServer");
 302 //        Process pp = null;
 303 //        try {
 304 //            ProcessBuilder pb = new ProcessBuilder("java", "-cp", System.getProperty("java.class.path"), "-D" + Environment.JEMMY_PROPERTIES_FILE_PROPERTY + "=" + props.getCanonicalPath(), RobotExecutor.class.getName());
 305 //            pb.redirectErrorStream(true);
 306 //            final Process p = pb.start();
 307 //            pp = p;
 308 //            new Thread() {
 309 //
 310 //                @Override
 311 //                public void run() {
 312 //                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
 313 //                    while (true) {
 314 //                        try {
 315 //                            String line = br.readLine();
 316 //                            if (line == null) {
 317 //                                break;
 318 //                            }
 319 //                            System.out.println("SERVER: " + line);
 320 //                        } catch (IOException ex) {
 321 //                            throw new JemmyException("Exception during other JVM output processing", ex);
 322 //                        }
 323 //                    }
 324 //                }
 325 //            }.start();
 326 //            RobotExecutor re = RobotExecutor.get();
 327 //            re.setRunInOtherJVM(true);
 328 //            re.createScreenCapture(new Rectangle(0, 0, 10, 10));
 329 //            re.exit();
 330 //        } catch (IOException ex) {
 331 //            throw new JemmyException("Failed to start other JVM", ex);
 332 //        } finally {
 333 //            if (pp != null) {
 334 //                pp.destroy();
 335 //                Thread.sleep(DESTROY_TIMEOUT);
 336 //            }
 337 //        }
 338 //    }
 339 //
 340 //    /**
 341 //     * Test of synchronizeRobot method, of class RobotExecutor.
 342 //     */
 343 //    @Test
 344 //    public void testSynchronizeRobot() {
 345 //        System.out.println("synchronizeRobot");
 346 //        RobotExecutor instance = new RobotExecutor();
 347 //        instance.synchronizeRobot();
 348 //        // TODO review the generated test code and remove the default call to fail.
 349 //        fail("The test case is a prototype.");
 350 //    }
 351 //
 352 //    /**
 353 //     * Test of setAutoDelay method, of class RobotExecutor.
 354 //     */
 355 //    @Test
 356 //    public void testSetAutoDelay() {
 357 //        System.out.println("setAutoDelay");
 358 //        Timeout autoDelay = null;
 359 //        RobotExecutor instance = new RobotExecutor();
 360 //        instance.setAutoDelay(autoDelay);
 361 //        // TODO review the generated test code and remove the default call to fail.
 362 //        fail("The test case is a prototype.");
 363 //    }
 364 //
 365 //    /**
 366 //     * Test of isRunInOtherJVM method, of class RobotExecutor.
 367 //     */
 368 //    @Test
 369 //    public void testIsRunInSeparateJVM() {
 370 //        System.out.println("isRunInOtherJVM");
 371 //        RobotExecutor instance = new RobotExecutor();
 372 //        boolean expResult = false;
 373 //        boolean result = instance.isRunInOtherJVM();
 374 //        assertEquals(expResult, result);
 375 //        // TODO review the generated test code and remove the default call to fail.
 376 //        fail("The test case is a prototype.");
 377 //    }
 378 //
 379 //    /**
 380 //     * Test of setRunInOtherJVM method, of class RobotExecutor.
 381 //     */
 382 //    @Test
 383 //    public void testSetRunInSeparateJVM() {
 384 //        System.out.println("setRunInOtherJVM");
 385 //        boolean runInSeparateJVM = false;
 386 //        RobotExecutor instance = new RobotExecutor();
 387 //        instance.setRunInOtherJVM(runInSeparateJVM);
 388 //        // TODO review the generated test code and remove the default call to fail.
 389 //        fail("The test case is a prototype.");
 390 //    }
 391 
 392 }