aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-extensions/net/sourceforge/jnlp/awt/AWTHelper.java
blob: 4bf3c0ba47de082c532eb3d3e2ed29b5d3634854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* 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.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
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 = null;        
    private Color appletColor;
    private BufferedImage marker;
    private Point 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 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;
    }
    
    /**
     * 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, Point 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, Point iconPosition, int appletWidth, int appletHeight) throws AWTException{
        this(icon, iconPosition, appletWidth, appletHeight);
        
        this.initStr = initString;
    }
    
    /**
     * 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");

        this.marker = ComponentFinder.defaultIcon;
        this.markerPosition = new Point(0,0);
        this.markerGiven = true;
                
        this.appletWidth = appletWidth;
        this.appletHeight = appletHeight;
        this.appletDimensionGiven = true;
    }
    
    public AWTHelper(String initString, int appletWidth, int appletHeight){
        this(appletWidth, appletHeight);
        this.initStr = initString;
    }
    
    /**
     * 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
     * (if initStr==null, do not check and do not call run)
     * 
     * 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?
        //check and run applet only if initStr is not null
        if ((initStr != null) && !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<String, String> getInitStrAsRule(){
    	if( initStr != null ){
            return new ContainsRule(this.initStr);
    	}else{
    		return new Rule<String, String>(){

				@Override
				public void setRule(String rule) {
				}

				@Override
				public boolean evaluate(String upon) {
					return true;
				}

				@Override
				public String toPassingString() {
					return "nothing to check, initStr is null";
				}

				@Override
				public String toFailingString() {
					return "nothing to check, initStr is null";
				}
    			
    		} ;
    	}
    }
    
    //boolean controls getters
    protected boolean appletIsReady(String content) {
        return this.getInitStrAsRule().evaluate(content);
    }
    
    public boolean isActionStarted() {
        return actionStarted;
    }
    
    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;
    }
       
    public void setMarker(BufferedImage marker, Point 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
     */
    public void captureScreenAndFindAppletByIconTryKTimes(BufferedImage icon, Point iconPosition, int width, int height, int K) throws ComponentNotFoundException {
  
        int count = 0;
        appletFound = false;
        while ((count < K) && !appletFound) {
            robot.delay(defaultWaitForApplet);
            try {
                screenshot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
                initialiseOnScreenshot(icon, iconPosition, width, height, screenshot);
            } catch (ComponentNotFoundException ex) {
                //keeping silent and try more-times
            }
            count++;
        }

        if (ImageSeeker.isRectangleValid(actionArea)) {
            appletFound = true;
        } else {
            throw new ComponentNotFoundException("Object not found in the screenshot!");
        }

    }
    
    public void initialiseOnScreenshot(BufferedImage icon, Point iconPosition, int width, int height, BufferedImage screenshot) throws ComponentNotFoundException {
        Rectangle r = ComponentFinder.findWindowByIcon(icon, iconPosition, width, height, screenshot);
        initialiseOnScreenshotAndArea(screenshot, r);
        
    }
    
    public void initialiseOnScreenshotAndArea(BufferedImage screenshot, Rectangle actionArea) throws ComponentNotFoundException {
        this.screenshot = screenshot;
        screenshotTaken = true;
        this.actionArea = actionArea;
        if (ImageSeeker.isRectangleValid(actionArea)) {
            appletFound = true;
        } else {
            throw new ComponentNotFoundException("set invalid area!");
        }
    }

    
    /**
     * 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 
     */
    public void findAndActivateApplet() throws ComponentNotFoundException, AWTFrameworkException
    {
        captureScreenAndFindAppletByIcon();
        clickInTheMiddleOfApplet();
    }


    //methods for clicking and typing 
    /**
     * method clickInTheMiddleOfApplet focuses the applet by clicking in the
     * middle of its location rectangle
     */
    public void clickInTheMiddleOfApplet() {
        MouseActions.clickInside(this.actionArea, this.robot);
    }
    
    /**
     * Method clickOnIconExact - click in the middle of a rectangle with 
     * given pattern (icon) using specified mouse key.
     * If the applet has not been found yet, the search includes whole screen.
     * 
     * @param icon
     * @param mouseKey
     * @throws ComponentNotFoundException
     */
    public void clickOnIconExact(BufferedImage icon, int mouseKey) throws ComponentNotFoundException{
        Rectangle areaOfSearch;
        if(!appletFound){//searching whole screen, less effective
            areaOfSearch = new Rectangle(0, 0, this.screenshot.getWidth(), this.screenshot.getHeight());
        }else{
            areaOfSearch = this.actionArea;
        }
        Rectangle iconRectangle = ImageSeeker.findExactImage(icon, this.screenshot, areaOfSearch);

        if (ImageSeeker.isRectangleValid(iconRectangle)) {
            MouseActions.clickInside(mouseKey, iconRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Exact icon not found!");
        }
    }

    /**
     * Method clickOnIconBlurred - click in the middle of a rectangle with 
     * given pattern (icon) using specified mouse key.
     * If the applet has not been found yet, the search includes whole screen.
     *  
     * @param icon
     * @param mouseKey
     * @param precision tolerated minimal correlation (see ImageSeeker methods)
     * @throws ComponentNotFoundException
     */
    public void clickOnIconBlurred(BufferedImage icon, int mouseKey, double precision) throws ComponentNotFoundException{
        Rectangle areaOfSearch;
        if(!appletFound){//searching whole screen, less effective
            areaOfSearch = new Rectangle(0, 0, this.screenshot.getWidth(), this.screenshot.getHeight());
        }else{
            areaOfSearch = this.actionArea;
        }    
        Rectangle iconRectangle = ImageSeeker.findBlurredImage(icon, this.screenshot, precision, areaOfSearch);

        if (ImageSeeker.isRectangleValid(iconRectangle)) {
            MouseActions.clickInside(mouseKey, iconRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Blurred icon not found!");
        }
    }
    
    /**
     * Method clickOnColoredRectangle - click in the middle of a rectangle with
     * given color (appletColor must be specified as the background) using
     * specified mouse key.
     * 
     * @param c
     * @param mouseKey
     * @throws ComponentNotFoundException
     * @throws AWTFrameworkException 
     * 
     */
    public void clickOnColoredRectangle(Color c, int mouseKey) throws ComponentNotFoundException, AWTFrameworkException {
        Rectangle buttonRectangle = findColoredRectangle(c);

        if (ImageSeeker.isRectangleValid(buttonRectangle)) {
            MouseActions.clickInside(mouseKey, buttonRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Colored rectangle not found!");
        }
    }

    public void moveToMiddleOfColoredRectangle(Color c) throws ComponentNotFoundException, AWTFrameworkException {

        Rectangle buttonRectangle = findColoredRectangle(c);

        if (ImageSeeker.isRectangleValid(buttonRectangle)) {
            MouseActions.moveMouseToMiddle(buttonRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Colored rectangle not found!");
        }
    }

    public void moveOutsideColoredRectangle(Color c) throws ComponentNotFoundException, AWTFrameworkException {
        Rectangle buttonRectangle = findColoredRectangle(c);

        if (ImageSeeker.isRectangleValid(buttonRectangle)) {
            MouseActions.moveMouseOutside(buttonRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Colored rectangle not found!");
        }
    }

    public void moveInsideColoredRectangle(Color c) throws ComponentNotFoundException, AWTFrameworkException {
        Rectangle buttonRectangle = findColoredRectangle(c);

        if (ImageSeeker.isRectangleValid(buttonRectangle)) {
            MouseActions.moveInsideRectangle(buttonRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Colored rectangle not found!");
        }
    }

    public void dragFromColoredRectangle(Color c) throws ComponentNotFoundException, AWTFrameworkException {
        Rectangle buttonRectangle = findColoredRectangle(c);

        if (ImageSeeker.isRectangleValid(buttonRectangle)) {
            MouseActions.dragFromRectangle(buttonRectangle, this.robot);
        }else{
            throw new ComponentNotFoundException("Colored rectangle not found!");
        }
    }

    public Rectangle findColoredRectangle(Color c) throws AWTFrameworkException {
        if(!appletColorGiven || !appletFound){
            throw new AWTFrameworkException("AWTHelper could not search for colored rectangle, needs appletColor and applet position.");
        }
        
        Rectangle result;

        int gap = 5;
        result = ImageSeeker.findColoredAreaGap(this.screenshot, c, appletColor, this.actionArea.y, this.actionArea.y + this.actionArea.height, gap);

        return result;
    }

    /**
     * method writeText writes string containing small letters and numbers and
     * spaces like the keyboard input (using KeyboardActions so delays are
     * inserted)
     * 
     * @param text
     */
    public void writeText(String text) {
        KeyboardActions.writeText(this.robot, text);
    }

    /**
     * method typeKey writes one key on the keyboard (again using
     * KeyboardActions)
     * 
     * @param key
     */
    public void typeKey(int key) {
        KeyboardActions.typeKey(this.robot, key);
    }
 }