|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
40 | 40 | import java.util.Collections;
|
41 | 41 | import java.util.List;
|
42 | 42 | import java.util.function.Function;
|
| 43 | +import java.util.function.Supplier; |
43 | 44 | import java.util.logging.Level;
|
44 | 45 | import java.util.logging.Logger;
|
45 | 46 | import java.util.stream.IntStream;
|
|
56 | 57 | import org.netbeans.jemmy.DefaultCharBindingMap;
|
57 | 58 | import org.netbeans.jemmy.QueueTool;
|
58 | 59 | import org.netbeans.jemmy.TimeoutExpiredException;
|
| 60 | +import org.netbeans.jemmy.Timeouts; |
59 | 61 | import org.netbeans.jemmy.Waitable;
|
60 | 62 | import org.netbeans.jemmy.Waiter;
|
61 | 63 | import org.netbeans.jemmy.drivers.scrolling.JSpinnerDriver;
|
| 64 | +import org.netbeans.jemmy.image.ImageComparator; |
62 | 65 | import org.netbeans.jemmy.image.StrictImageComparator;
|
63 | 66 | import org.netbeans.jemmy.operators.ComponentOperator;
|
64 | 67 | import org.netbeans.jemmy.operators.ContainerOperator;
|
@@ -89,17 +92,13 @@ public class JemmyExt {
|
89 | 92 | DefaultCharBindingMap.class
|
90 | 93 | };
|
91 | 94 |
|
92 |
| - public static void assertNotBlack(BufferedImage image) { |
93 |
| - int w = image.getWidth(); |
94 |
| - int h = image.getHeight(); |
95 |
| - try { |
96 |
| - assertFalse("All pixels are not black", IntStream.range(0, w).parallel().allMatch(x |
97 |
| - -> IntStream.range(0, h).allMatch(y -> (image.getRGB(x, y) & 0xffffff) == 0) |
98 |
| - )); |
99 |
| - } catch (Throwable t) { |
100 |
| - save(image, "allPixelsAreBlack.png"); |
101 |
| - throw t; |
102 |
| - } |
| 95 | + /** |
| 96 | + * Checks if the image is complitely black. |
| 97 | + */ |
| 98 | + public static boolean isBlack(BufferedImage image) { |
| 99 | + return IntStream.range(0, image.getWidth()).parallel() |
| 100 | + .allMatch(x-> IntStream.range(0, image.getHeight()) |
| 101 | + .allMatch(y -> (image.getRGB(x, y) & 0xffffff) == 0)); |
103 | 102 | }
|
104 | 103 |
|
105 | 104 | public static void waitArmed(JButtonOperator button) {
|
@@ -184,28 +183,93 @@ public static void save(BufferedImage image, String filename) {
|
184 | 183 | }
|
185 | 184 | }
|
186 | 185 |
|
187 |
| - public static void waitImageIsStill(Robot rob, ComponentOperator operator) { |
188 |
| - operator.waitState(new ComponentChooser() { |
| 186 | + /** |
| 187 | + * Waits for a screen area taken by a component to not be completely black rectangle. |
| 188 | + * @return last (non-black) image |
| 189 | + * @throws TimeoutExpiredException if the waiting is unsuccessful |
| 190 | + */ |
| 191 | + public static BufferedImage waitNotBlack(Robot rob, ComponentOperator operator, String imageName) { |
| 192 | + class NonBlackImageChooser implements ComponentChooser { |
| 193 | + private BufferedImage image = null; |
| 194 | + @Override |
| 195 | + public boolean checkComponent(Component comp) { |
| 196 | + image = capture(rob, operator); |
| 197 | + save(image, imageName); |
| 198 | + return !isBlack(image); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public String getDescription() { |
| 203 | + return "A non-black Image of " + operator; |
| 204 | + } |
| 205 | + } |
| 206 | + NonBlackImageChooser chooser = new NonBlackImageChooser(); |
| 207 | + operator.waitState(chooser); |
| 208 | + return chooser.image; |
| 209 | + } |
189 | 210 |
|
| 211 | + /** |
| 212 | + * Waits for the displayed image to be still. |
| 213 | + * @return last still image |
| 214 | + * @throws TimeoutExpiredException if the waiting is unsuccessful |
| 215 | + */ |
| 216 | + public static BufferedImage waitStillImage(Robot rob, ComponentOperator operator, String imageName) { |
| 217 | + operator.getTimeouts().setTimeout("Waiter.TimeDelta", 1000); |
| 218 | + class StillImageChooser implements ComponentChooser { |
190 | 219 | private BufferedImage previousImage = null;
|
191 |
| - private int index = 0; |
192 | 220 | private final StrictImageComparator sComparator = new StrictImageComparator();
|
193 | 221 |
|
194 | 222 | @Override
|
195 | 223 | public boolean checkComponent(Component comp) {
|
196 | 224 | BufferedImage currentImage = capture(rob, operator);
|
197 |
| - save(currentImage, "waitImageIsStill" + index + ".png"); |
198 |
| - index++; |
| 225 | + save(currentImage, imageName); |
199 | 226 | boolean compareResult = previousImage == null ? false : sComparator.compare(currentImage, previousImage);
|
200 | 227 | previousImage = currentImage;
|
201 | 228 | return compareResult;
|
202 | 229 | }
|
203 | 230 |
|
204 | 231 | @Override
|
205 | 232 | public String getDescription() {
|
206 |
| - return "Image of " + operator + " is still"; |
| 233 | + return "A still image of " + operator; |
207 | 234 | }
|
208 |
| - }); |
| 235 | + } |
| 236 | + StillImageChooser chooser = new StillImageChooser(); |
| 237 | + operator.waitState(chooser); |
| 238 | + return chooser.previousImage; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Waits for the displayed image to change. |
| 243 | + * @param reference image to compare to |
| 244 | + * @return last (changed) image |
| 245 | + * @throws TimeoutExpiredException if the waiting is unsuccessful |
| 246 | + */ |
| 247 | + public static BufferedImage waitChangedImage(Robot rob, |
| 248 | + Supplier<BufferedImage> supplier, |
| 249 | + BufferedImage reference, |
| 250 | + Timeouts timeouts, |
| 251 | + String imageName) throws InterruptedException { |
| 252 | + ImageComparator comparator = new StrictImageComparator(); |
| 253 | + class ImageWaitable implements Waitable { |
| 254 | + BufferedImage image; |
| 255 | + |
| 256 | + @Override |
| 257 | + public Object actionProduced(Object obj) { |
| 258 | + image = supplier.get(); |
| 259 | + save(image, imageName); |
| 260 | + return comparator.compare(reference, image) ? null : image; |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + public String getDescription() { |
| 265 | + return "Waiting screen image to change"; |
| 266 | + } |
| 267 | + } |
| 268 | + ImageWaitable waitable = new ImageWaitable(); |
| 269 | + Waiter waiter = new Waiter(waitable); |
| 270 | + waiter.setTimeouts(timeouts); |
| 271 | + waiter.waitAction(null); |
| 272 | + return waitable.image; |
209 | 273 | }
|
210 | 274 |
|
211 | 275 | private static class ThrowableHolder {
|
|
0 commit comments