view src/org/thermostat/qa2/framework/Robot.java @ 177:e78173df9ba0

Added new code to Robot class to replace code from old GuiRobot class, added 2s delay after gnome-keyring startup
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 31 Mar 2015 12:00:22 +0200
parents 0d34a379de82
children
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA give you
 permission to link this code with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import javax.imageio.ImageIO;
import org.thermostat.qa.framework.ImageProcessing;
import static org.thermostat.qa2.framework.ThermostatQAConfig.getPatternsDir;
import org.thermostat.qa2.framework.utils.CommonUtilities;
import org.thermostat.qa2.framework.utils.ProcessUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public class Robot {

    long delay = 500;
    long screenshotDelay = 5000;

    BufferedImage screenCapture;

    public void pressUpKey() throws Exception {
        ProcessUtilities.run("xdotool", "key", "--clearmodifiers", "Up");
        CommonUtilities.sleep(delay);
    }

    public void pressDownKey() throws Exception {
        ProcessUtilities.run("xdotool", "key", "--clearmodifiers", "Down");
        CommonUtilities.sleep(delay);
    }

    public void pressLeftKey() throws Exception {
        ProcessUtilities.run("xdotool", "key", "--clearmodifiers", "Left");
        CommonUtilities.sleep(delay);
    }

    public void pressRightKey() throws Exception {
        ProcessUtilities.run("xdotool", "key", "--clearmodifiers", "Right");
        CommonUtilities.sleep(delay);
    }

    public void pressEnterKey() throws Exception {
        ProcessUtilities.run("xdotool", "key", "--clearmodifiers", "Return");
        CommonUtilities.sleep(delay);
    }

    private void clickRaw(int x, int y) throws Exception {
        ProcessUtilities.run("xdotool", "mousemove", Integer.toString(x), Integer.toString(y));
        ProcessUtilities.run("xdotool", "click", "--clearmodifiers", "1");
    }

    private void clickOnRectangleRaw(Rectangle rectangle) throws Exception {
        int x = rectangle.x + rectangle.width / 2;
        int y = rectangle.y + rectangle.height / 2;
        clickRaw(x, y);
    }

    public void clickOnRectangle(Rectangle rectangle) throws Exception {
        clickOnRectangleRaw(rectangle);
        CommonUtilities.sleep(delay);
    }

    public void doubleClickOnRectangle(Rectangle rectangle) throws Exception {
        clickOnRectangleRaw(rectangle);
        clickOnRectangleRaw(rectangle);
        CommonUtilities.sleep(delay);
    }

    public void resize(Rectangle from, int xd, int yd) throws Exception {
        ProcessUtilities.shellRun("win=`xdotool getactivewindow` ; xdotool windowsize $win " + (from.x + xd) + " " + (from.y + yd));
        CommonUtilities.sleep(delay);
    }

    private Rectangle findPatternRaw(String name) throws IOException {
        BufferedImage marker = ImageIO.read(new File(name));
        return ImageProcessing.findPattern(marker, screenCapture);
    }

    public Rectangle findPattern(String name) throws IOException, Exception {
        System.out.println("INFO: searching for pattern: " + name);
        String patternName = getPatternsDir() + File.separator + name.replace("/", File.separator);
        int count = 0;
        int notExistingCount = 0;
        String patternFile = patternName + ".png";
        do {
            if (new File(patternFile).exists()) {
                Rectangle rect = findPatternRaw(patternFile);
                if (rect != null) {
                    System.out.println("INFO:     pattern: " + patternFile + ": FOUND");
                    return rect;
                }
                System.out.println("INFO:     pattern: " + patternFile + ": NOT FOUND");
                notExistingCount = 0;
            } else {
                ++notExistingCount;
            }
            patternFile = patternName + count++ + ".png";
        } while (notExistingCount < 5);
        return null;
        //throw new Exception("Pattern not found: " + name);
    }

    public Rectangle checkForPattern(String name) throws IOException, Exception {
        Rectangle rect = findPattern(name);
        if (rect == null) {
            throw new AssertionError("Pattern not found: " + name);
        }
        highlightRectangle(screenCapture, rect);
        return rect;

    }

    public Rectangle checkForAnyPattern(String... names) throws IOException, Exception {
        Rectangle rect = null;
        for (String pattern : names) {
            rect = findPattern(pattern);
            if (rect != null) {
                return rect;
            }
        }
        throw new AssertionError("patterns: " + Arrays.toString(names) + "not found");
    }

    private static void highlightRectangle(BufferedImage testImage, Rectangle rect) {
        Graphics2D graphics = (Graphics2D) testImage.getGraphics();
        Rectangle rect1 = getRectangleWithOffset(rect, 2);
        Rectangle rect2 = getRectangleWithOffset(rect, 4);
        graphics.setColor(Color.RED);
        graphics.draw(rect1);
        graphics.setColor(Color.BLACK);
        graphics.draw(rect2);
        graphics.dispose();
    }

    private static Rectangle getRectangleWithOffset(Rectangle rect, int offset) {
        int x = rect.x - offset;
        int y = rect.y - offset;
        int width = rect.width + (offset << 1);
        int height = rect.height + (offset << 1);
        return new Rectangle(x, y, width, height);
    }

    public void makeScreenshot(String name) throws Exception {
        CommonUtilities.sleep(screenshotDelay);
        String path = "screenshots/" + name + ".png";
        System.out.println("INFO: making screenshot: " + path);
        ProcessUtilities.run("import", "-window", "root", path);
        screenCapture = ImageIO.read(new File(path));
    }

    public void saveScreenshot(String name) throws IOException {
        String path = "screenshots/" + name + ".png";
        System.out.println("INFO: saving screenshot: " + path);
        ImageIO.write(screenCapture, "png", new File(path));
    }

    public void clickOnPattern(String name) throws Exception {
        Rectangle rect = checkForPattern(name);
        clickOnRectangle(rect);
    }

    public void clickOnAnyPattern(String... names) throws Exception {
        Rectangle rect = checkForAnyPattern(names);
        clickOnRectangle(rect);
    }

    public void doubleClickOnPattern(String name) throws Exception {
        Rectangle rect = checkForPattern(name);
        doubleClickOnRectangle(rect);
    }

    public void doubleClickOnAnyPattern(String... names) throws Exception {
        Rectangle rect = checkForAnyPattern(names);
        doubleClickOnRectangle(rect);
    }

    public void enterMainMenu() throws Exception {
        ProcessUtilities.run("xdotool", "key", "--clearmodifiers", "F10");
        CommonUtilities.sleep(delay);
    }

    public void clickOnVMView() throws Exception {
        makeScreenshot("GuiStarted1");
        Rectangle r = findPattern("MainWindow/host_icon_with_arrow");
        if (r != null) {
            r.width = 6;
            clickOnRectangle(r);
        }

        makeScreenshot("GuiStarted2");
        clickOnAnyPattern("MainWindow/vm_icon_grey", "MainWindow/vm_icon_whiteblue");

        makeScreenshot("VMViewActive1");
        checkForPattern("MainWindow/vm_icon_blue");
    }

    public void writeText(String text) throws Exception {
        ProcessUtilities.run("xdotool", "type", "--clearmodifiers", text);
    }

}