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