/hg/icedtea-web: adding awtframework (without reproducers)
jfabriko at icedtea.classpath.org
jfabriko at icedtea.classpath.org
Fri Apr 26 11:22:40 PDT 2013
changeset d84475508473 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=d84475508473
author: Jana Fabrikova <jfabriko at redhat.com>
date: Fri Apr 26 20:24:55 2013 +0200
adding awtframework (without reproducers)
diffstat:
ChangeLog | 24 +
tests/test-extensions/net/sourceforge/jnlp/awt/AWTFrameworkException.java | 66 +
tests/test-extensions/net/sourceforge/jnlp/awt/AWTHelper.java | 559 ++++++++++
tests/test-extensions/net/sourceforge/jnlp/awt/awtactions/KeyboardActions.java | 111 +
tests/test-extensions/net/sourceforge/jnlp/awt/awtactions/MouseActions.java | 228 ++++
tests/test-extensions/net/sourceforge/jnlp/awt/imagesearch/ComponentFinder.java | 107 +
tests/test-extensions/net/sourceforge/jnlp/awt/imagesearch/ComponentNotFoundException.java | 65 +
tests/test-extensions/net/sourceforge/jnlp/awt/imagesearch/ImageSeeker.java | 325 +++++
tests/test-extensions/net/sourceforge/jnlp/closinglisteners/RulesFolowingClosingListener.java | 4 +
9 files changed, 1489 insertions(+), 0 deletions(-)
diffs (truncated from 1534 to 500 lines):
diff -r 728cc3b2ab34 -r d84475508473 ChangeLog
--- a/ChangeLog Fri Apr 26 20:11:33 2013 +0200
+++ b/ChangeLog Fri Apr 26 20:24:55 2013 +0200
@@ -1,3 +1,27 @@
+2013-04-26 Jana Fabrikova <jfabriko at redhat.com>
+
+ * /tests/test-extensions/net/sourceforge/jnlp/closinglisteners/RulesFolowingClosingListener.java:
+ added a getter method getRules
+ * tests/test-extensions/net/sourceforge/jnlp/awt/AWTHelper.java:
+ the most important class of AWTFramework, combines closing listener and
+ possibility to use mouse and keyboard for input to tests
+ * tests/test-extensions/net/sourceforge/jnlp/awt/AWTFrameworkException.java:
+ exception that is raised in the framework whenever programmer did not
+ provide enough information
+ * tests/test-extensions/net/sourceforge/jnlp/awt/awtactions/KeyboardActions.java:
+ class with utility keyboard methods
+ * tests/test-extensions/net/sourceforge/jnlp/awt/awtactions/MouseActions.java:
+ class with utility mouse methods
+ * tests/test-extensions/net/sourceforge/jnlp/awt/imagesearch/ComponentFinder.java:
+ class for finding components in a screenshot
+ * tests/test-extensions/net/sourceforge/jnlp/awt/imagesearch/ComponentNotFoundException.java:
+ exception that can be raised if an important component could not be found
+ * tests/test-extensions/net/sourceforge/jnlp/awt/imagesearch/ImageSeeker.java:
+ class for general image searching
+ * tests/reproducers/simple/AWTCommonResourcesOnly/resources/marker.png:
+ reproducer with resources only, contains the default icon marking
+ applets
+
2013-04-26 Adam Domurad <adomurad at redhat.com>
* netx/net/sourceforge/jnlp/cache/ResourceTracker.java
diff -r 728cc3b2ab34 -r d84475508473 tests/test-extensions/net/sourceforge/jnlp/awt/AWTFrameworkException.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-extensions/net/sourceforge/jnlp/awt/AWTFrameworkException.java Fri Apr 26 20:24:55 2013 +0200
@@ -0,0 +1,66 @@
+/* AWTFrameworkException.java
+Copyright (C) 2012 Red Hat, Inc.
+
+This file is part of IcedTea.
+
+IcedTea is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 2.
+
+IcedTea is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with IcedTea; see the file COPYING. If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library 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 this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+ */
+
+package net.sourceforge.jnlp.awt;
+
+/**
+ * Class AWTFrameworkException is thrown in the AWTFramework
+ * whenever the framework encounters not enough data specified
+ * to perform an action (for example it is impossible to ascertain
+ * the position of an applet in the screenshot if the width or height
+ * of the applet is not known.
+ *
+ */
+
+public class AWTFrameworkException extends Exception {
+
+ public AWTFrameworkException() {
+ super();
+ }
+
+ public AWTFrameworkException(String s) {
+ super(s);
+ }
+
+ public AWTFrameworkException(String s, Throwable throwable) {
+ super(s, throwable);
+ }
+
+ public AWTFrameworkException(Throwable throwable) {
+ super(throwable);
+ }
+}
diff -r 728cc3b2ab34 -r d84475508473 tests/test-extensions/net/sourceforge/jnlp/awt/AWTHelper.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-extensions/net/sourceforge/jnlp/awt/AWTHelper.java Fri Apr 26 20:24:55 2013 +0200
@@ -0,0 +1,559 @@
+/* AWTHelper.java
+Copyright (C) 2012 Red Hat, Inc.
+
+This file is part of IcedTea.
+
+IcedTea is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 2.
+
+IcedTea is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with IcedTea; see the file COPYING. If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library 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 this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+ */
+package net.sourceforge.jnlp.awt;
+
+import java.awt.AWTException;
+import java.awt.Color;
+import java.awt.Rectangle;
+import java.awt.Robot;
+import java.awt.Toolkit;
+import java.awt.event.InputEvent;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+import net.sourceforge.jnlp.awt.awtactions.KeyboardActions;
+import net.sourceforge.jnlp.awt.awtactions.MouseActions;
+import net.sourceforge.jnlp.awt.imagesearch.ComponentFinder;
+import net.sourceforge.jnlp.awt.imagesearch.ComponentNotFoundException;
+import net.sourceforge.jnlp.awt.imagesearch.ImageSeeker;
+import net.sourceforge.jnlp.closinglisteners.Rule;
+import net.sourceforge.jnlp.closinglisteners.RulesFolowingClosingListener;
+
+public abstract class AWTHelper extends RulesFolowingClosingListener implements Runnable{
+
+ //attributes possibly set by user
+ private String initStr = "";
+ private Color appletColor;
+ private BufferedImage marker;
+ private Rectangle markerPosition;
+ private int appletHeight;
+ private int appletWidth;
+ private int tryKTimes = DEFAULT_K;
+
+ //other
+ protected StringBuilder sb = new StringBuilder();
+ private boolean actionStarted = false;
+ private Rectangle actionArea;
+ private BufferedImage screenshot;
+ private Robot robot;
+ private boolean appletFound = false;
+ private boolean initStrGiven = false; //impossible to find in the output if not given
+ private boolean appletColorGiven = false; //impossible to search for color difference if not given
+ private boolean markerGiven = false; //impossible to find the applet if marker not given
+ private boolean appletDimensionGiven = false;
+ private boolean screenshotTaken = false;
+ private int defaultWaitForApplet = 1000;
+
+ //default number of times the screen is captured and the applet is searched for
+ //in the screenshot
+ public static final int DEFAULT_K = 3;
+
+
+ //several constructors
+ /**
+ * the minimal constructor - use:
+ * - if we do not want to find the bounds of applet area first
+ * - searching for buttons and other components is then done in the whole
+ * screen, confusion with other icons on display is then possible
+ * - less effective, deprecated (better bound the area first)
+ */
+ @Deprecated
+ public AWTHelper() {
+ try {
+ this.robot = new Robot();
+ } catch (AWTException e) {
+ throw new RuntimeException("AWTHelper could not create its Robot instance.",e);
+ }
+ }
+
+ /**
+ * the minimal constructor with initStr - use:
+ * - we want to know from stdout that the applet (or sth else) is ready
+ * - if we do not want to find the bounds of applet area first
+ * - searching for buttons and other components is then done in the whole
+ * screen, confusion with other icons on display is then possible
+ * - less effective, deprecated (better bound the area first)
+ */
+ @Deprecated
+ public AWTHelper(String initStr){
+ this();
+
+ this.initStr = initStr;
+ this.initStrGiven = true;
+ }
+
+ /**
+ * the constructor with icon and its position in applet of given dimension
+ * use:
+ * - we want to find and activate the applet first
+ * - the search for applet will be done via searching for icon
+ * of given position(x,y,w,h) inside applet of given width and height
+ *
+ * @param icon marker by which the applet will be found
+ * @param iconPosition relatively to applet (including icon width and height)
+ * @param appletWidth
+ * @param appletHeight
+ */
+
+ public AWTHelper(BufferedImage icon, Rectangle iconPosition, int appletWidth, int appletHeight){
+ this();
+ this.marker = icon;
+ this.markerPosition = iconPosition;
+ this.markerGiven = true;
+
+ this.appletWidth = appletWidth;
+ this.appletHeight = appletHeight;
+ this.appletDimensionGiven = true;
+ }
+
+ public AWTHelper(String initString, BufferedImage icon, Rectangle iconPosition, int appletWidth, int appletHeight) throws AWTException{
+ this(icon, iconPosition, appletWidth, appletHeight);
+
+ this.initStr = initString;
+ this.initStrGiven = true;
+ }
+
+ /**
+ * the constructor with applet width and height only - use:
+ * - we want to find the applet by finding the default icon
+ * that is located in the upper left corner of applet
+ *
+ * @param appletWidth
+ * @param appletHeight
+ */
+ public AWTHelper(int appletWidth, int appletHeight){
+ this();
+ String test_server_dir_path = System.getProperty("test.server.dir");
+
+ try {
+ this.marker = ImageIO.read(new File(test_server_dir_path + "/marker.png"));
+ this.markerPosition = new Rectangle(0,0,marker.getWidth(),marker.getHeight());
+ this.markerGiven = true;
+ } catch (IOException e) {
+ throw new RuntimeException("AWTHelper could not read marker.png.",e);
+ }
+
+ this.appletWidth = appletWidth;
+ this.appletHeight = appletHeight;
+ this.appletDimensionGiven = true;
+ }
+
+ public AWTHelper(String initString, int appletWidth, int appletHeight){
+ this(appletWidth, appletHeight);
+ this.initStr = initString;
+ this.initStrGiven = true;
+ }
+
+ /**
+ * refers to AWTHelper functioning as RulesFolowingClosingListener
+ *
+ * @param strs array of strings to be added as contains rules
+ */
+ public void addClosingRulesFromStringArray(String [] strs){
+ for(String s : strs){
+ this.addContainsRule(s);
+ }
+ }
+
+ /**
+ * override of method charReaded (from RulesFolowingClosingListener)
+ *
+ * waiting for the applet, when applet is ready run action thread
+ * when all the wanted strings are in the stdout, applet can be closed
+ *
+ * @param ch
+ */
+ @Override
+ public void charReaded(char ch) {
+ sb.append(ch);
+ //is applet ready to start clicking?
+ if (initStrGiven && !actionStarted && appletIsReady(sb.toString())) {
+ try{
+ actionStarted = true;
+ this.findAndActivateApplet();
+ this.run();
+ } catch (ComponentNotFoundException e1) {
+ throw new RuntimeException("AWTHelper problems finding applet.",e1);
+ } catch (AWTFrameworkException e2){
+ throw new RuntimeException("AWTHelper problems with unset attributes.",e2);
+ }
+ }
+ //is all the wanted output in stdout?
+ super.charReaded(ch);
+ }
+
+ /**
+ * method runAWTHelper - we can call run and declared the action as started
+ * without finding out if initStr is in the output, if this method is
+ * called
+ *
+ */
+ public void runAWTHelper(){
+ actionStarted = true;
+ this.run();
+ }
+
+ /**
+ * implementation of AWTHelper should implement the run method
+ */
+ public abstract void run();
+
+
+ /**
+ * method getInitStrAsRule returns the initStr in the form
+ * of Contains rule that can be evaluated on a string
+ *
+ * @return
+ */
+ public Rule getInitStrAsRule(){
+ return new ContainsRule(this.initStr);
+ }
+
+ //boolean controls getters
+ protected boolean appletIsReady(String content) {
+ return (content.contains(initStr));
+ }
+
+ public boolean isActionStarted() {
+ return actionStarted;
+ }
+
+ public boolean isInitStrGiven(){
+ return initStrGiven;
+ }
+
+ public boolean isAppletColorGiven(){
+ return appletColorGiven;
+ }
+
+ public boolean isAppletDimensionGiven(){
+ return appletDimensionGiven;
+ }
+
+ public boolean isMarkerGiven(){
+ return markerGiven;
+ }
+
+ //setters
+ /**
+ * method setDefaultWaitForApplet sets the time (in ms) for which the method
+ * captureScreenAndFindApplet will wait (for the applet to load) before it
+ * gets the screenshot the default time is 1000ms
+ *
+ * @param defaultWaitForApplet
+ */
+ public void setDefaultWaitForApplet(int defaultWaitForApplet) {
+ this.defaultWaitForApplet = defaultWaitForApplet;
+ }
+
+ public void setTryKTimes(int tryKTimes) {
+ this.tryKTimes = tryKTimes;
+ }
+
+ public void setAppletColor(Color appletColor) {
+ this.appletColor = appletColor;
+ this.appletColorGiven = true;
+ }
+
+ public void setInitStr(String initStr) {
+ this.initStr = initStr;
+ this.initStrGiven = true;
+ }
+
+ public void setMarker(BufferedImage marker, Rectangle markerPosition) {
+ this.marker = marker;
+ this.markerPosition = markerPosition;
+ this.markerGiven = true;
+ }
+
+ public void setAppletDimension(int width, int height){
+ this.appletWidth = width;
+ this.appletHeight = height;
+ this.appletDimensionGiven = true;
+ }
+
+
+ //creating screenshots, searching for applet
+ /**
+ * method captureScreenAndFindAppletByIcon
+ * 1. checks that all needed attributes of AWTHelper are given
+ * (marker, its position and applet width and height)
+ * 2. captures screen,
+ * 3. finds the rectangle where applet is and saves it to the attribute
+ * actionArea
+ * 4. sets screenCapture indicator to true (after tryKTimes unsuccessfull
+ * tries an exception "ComponentNotFound" will be raised)
+ *
+ * @throws AWTException
+ * @throws ComponentNotFoundException
+ * @throws AWTFrameworkException
+ */
+ public void captureScreenAndFindAppletByIcon() throws ComponentNotFoundException, AWTFrameworkException {
+ if(!appletDimensionGiven || !markerGiven){
+ throw new AWTFrameworkException("AWTFramework cannot find applet without dimension or marker!");
+ }
+ captureScreenAndFindAppletByIconTryKTimes(marker, markerPosition, appletWidth, appletHeight, tryKTimes);
+ }
+
+ /**
+ ** method captureScreenAndFindAppletByIcon
+ * 1. captures screen,
+ * 2. finds the rectangle where applet is and saves it to the attribute
+ * actionArea
+ * 3. sets screenCapture indicator to true (after tryKTimes unsuccessfull
+ * tries an exception "ComponentNotFound" will be raised)
+ *
+ * @param icon
+ * @param iconPosition
+ * @param width
+ * @param height
+ * @param K
+ * @throws ComponentNotFoundException
+ * @throws AWTFrameworkException
+ */
+ public void captureScreenAndFindAppletByIconTryKTimes(BufferedImage icon, Rectangle iconPosition, int width, int height, int K) throws ComponentNotFoundException, AWTFrameworkException {
+
+ if(!markerGiven){
+ throw new AWTFrameworkException("AWTFramework cannot find applet without marker!");
+ }
+
+ int count = 0;
+ appletFound = false;
+ while ((count < K) && !appletFound) {
+ robot.delay(defaultWaitForApplet);
+ screenshot = robot.createScreenCapture( new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() ) );
+ screenshotTaken = true;
+ actionArea = ComponentFinder.findWindowByIcon(icon, iconPosition, width, height, screenshot);
+ if (ImageSeeker.isRectangleValid(actionArea)) {
+ appletFound = true;
+ }
+ count++;
+ }
+
+ if (ImageSeeker.isRectangleValid(actionArea)) {
+ appletFound = true;
+ } else {
+ throw new ComponentNotFoundException("Applet not found in the screenshot!");
+ }
+
+ }
+
+ /**
+ * auxiliary method writeAppletScreen for writing Buffered image into png
+ *
+ * @param appletScreen
+ * @param filename
+ * @throws IOException
+ */
+ private void writeAppletScreen(BufferedImage appletScreen, String filename) throws IOException {// into png file
+ ImageIO.write(appletScreen, "png", new File(filename+".png"));
+ }
+
+
+ /**
+ * method findAndActivateApplet finds the applet by icon
+ * and clicks in the middle of applet area
+ *
+ * @throws ComponentNotFoundException (applet not found)
+ * @throws AWTFrameworkException
More information about the distro-pkg-dev
mailing list