view src/org/thermostat/qa2/framework/Robot.java @ 170:0d34a379de82

- Added new framework (package org.thermosta.qa2.framework) - Removed hardcoded display in org.thermostat.qa.framework.GuiRobot class
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 10 Mar 2015 13:38:36 +0100
parents
children e78173df9ba0
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.AWTException;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.thermostat.qa.common.Configuration;
import org.thermostat.qa.framework.GuiRobot;
import static org.thermostat.qa2.framework.ThermostatQAConfig.getPatternsDir;
import org.thermostat.qa2.framework.utils.CommonUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
// wrapper of original robot, to be used by gui tests
public class Robot {

    public Robot() throws AWTException {
        Configuration c = new Configuration(null);
        guiRobot = new GuiRobot(c);
    }

    GuiRobot guiRobot;
    long delay = 500;
    long screenshotDelay = 5000;

    public void pressDownKey() {
        guiRobot.pressDownKey();
        CommonUtilities.sleep(delay);
    }

    public void pressLeftKey() {
        guiRobot.pressLeftKey();
        CommonUtilities.sleep(delay);
    }

    public void pressRightKey() {
        guiRobot.pressRightKey();
        CommonUtilities.sleep(delay);
    }

    public void pressEnterKey() {
        guiRobot.pressEnterKey();
        CommonUtilities.sleep(delay);
    }

    public void clickOnRectangle(Rectangle rectangle) {
        guiRobot.clickToRectangle(rectangle);
        CommonUtilities.sleep(delay);
    }

    public void doubleClickOnRectangle(Rectangle rectangle) {
        guiRobot.clickToRectangle(rectangle);
        guiRobot.clickToRectangle(rectangle);
        CommonUtilities.sleep(delay);
    }

    public void resize(Rectangle from, int xd, int yd) {
        guiRobot.resize(from, xd, yd);
        CommonUtilities.sleep(delay);
    }

    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 = guiRobot.findPattern(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);
        }
        guiRobot.highlightRectangle(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");
    }

    public void makeScreenshot(String name) throws IOException {
        CommonUtilities.sleep(screenshotDelay);
        guiRobot.prepareScreenshot(name);
    }

    public void saveScreenshot(String name) throws IOException {
        guiRobot.saveScreenshot(name);
    }

    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() {
        guiRobot.pressFNKey(10);
        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) {
        guiRobot.writeSmallLettersText(text);
    }
    
}