8253820: Save test images and dumps with timestamps from client sanity suite

Reviewed-by: serb
This commit is contained in:
Alexandre Iline 2020-11-13 02:27:32 +00:00
parent dff26a489d
commit e32a4ea4ef
3 changed files with 86 additions and 95 deletions

View File

@ -22,22 +22,16 @@
*/
import com.sun.swingset3.demos.button.ButtonDemo;
import org.jemmy2ext.JemmyExt;
import org.jtregext.GuiTestListener;
import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.ComponentChooser;
import org.netbeans.jemmy.image.StrictImageComparator;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
@ -65,25 +59,18 @@ import static org.jemmy2ext.JemmyExt.*;
public class ButtonDemoScreenshotTest {
private static final int[] BUTTONS = {0, 1, 2, 3, 4, 5}; // "open browser" buttons (6, 7) open a browser, so ignore
private static StrictImageComparator sComparator = null;
@BeforeClass
public void init() {
sComparator = new StrictImageComparator();
}
@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
public void test(String lookAndFeel) throws Exception {
UIManager.setLookAndFeel(lookAndFeel);
Robot rob = new Robot();
//capture some of the background
Dimension screeSize = Toolkit.getDefaultToolkit().getScreenSize();
Point screenCenter = new Point(screeSize.width / 2, screeSize.height / 2);
Rectangle center = new Rectangle(
screenCenter.x - 50, screenCenter.y - 50,
screenCenter.x + 50, screenCenter.y + 50);
BufferedImage background = rob.createScreenCapture(center);
100, 100);
BufferedImage background = getRobot().createScreenCapture(center);
new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();
@ -91,18 +78,18 @@ public class ButtonDemoScreenshotTest {
mainFrame.waitComponentShowing(true);
//make sure the frame is already painted
waitChangedImage(rob, () -> rob.createScreenCapture(center),
background, mainFrame.getTimeouts(), "background.png");
waitChangedImage(() -> getRobot().createScreenCapture(center),
background, mainFrame.getTimeouts(), "background");
//make sure the frame is painted completely
waitStillImage(rob, mainFrame, "frame.png");
waitStillImage(mainFrame, "frame");
// Check all the buttons
for (int i : BUTTONS) {
checkButton(mainFrame, i, rob);
checkButton(mainFrame, i);
}
}
private void checkButton(JFrameOperator jfo, int i, Robot rob) throws InterruptedException {
private void checkButton(JFrameOperator jfo, int i) throws InterruptedException {
JButtonOperator button = new JButtonOperator(jfo, i);
//additional instrumentation for JDK-8198920. To be removed after the bug is fixed
@ -113,9 +100,7 @@ public class ButtonDemoScreenshotTest {
button.moveMouse(button.getCenterX(), button.getCenterY());
BufferedImage notPressed, pressed = null;
notPressed = waitStillImage(rob, button, "not-pressed-" + i + ".png");
BufferedImage[] pressedImage = new BufferedImage[1];
notPressed = waitStillImage(button, "not-pressed-" + i);
button.pressMouse();
//additional instrumentation for JDK-8198920. To be removed after the bug is fixed
@ -126,14 +111,14 @@ public class ButtonDemoScreenshotTest {
//additional instrumentation for JDK-8198920. To be removed after the bug is fixed
button.getOutput().printTrace("JDK-8198920: Button press confirmed by " + System.currentTimeMillis());
//end of instrumentation for JDK-8198920
waitChangedImage(rob, () -> capture(rob, button), notPressed,
button.getTimeouts(), "pressed-" + i + ".png");
pressed = waitStillImage(rob, button, "pressed.png");
waitChangedImage(() -> capture(button), notPressed,
button.getTimeouts(), "after-press-" + i);
pressed = waitStillImage(button, "pressed-" + i);
} finally {
button.releaseMouse();
if(pressed != null) {
waitChangedImage(rob, () -> capture(rob, button), pressed,
button.getTimeouts(), "released-" + i + ".png");
waitChangedImage(() -> capture(button), pressed,
button.getTimeouts(), "released-" + i);
}
//additional instrumentation for JDK-8198920. To be removed after the bug is fixed
button.getOutput().printTrace("JDK-8198920: Button released at " + System.currentTimeMillis());

View File

@ -149,7 +149,7 @@ public class EditorPaneDemoTest {
final int xGap = 100, yGap = 40, columns = 2, rows = 5;
editorPaneOperator.waitState(comp -> {
BufferedImage capturedImage = ImageTool.getImage(imageRect);
save(capturedImage, "editor.png");
save(capturedImage, "editor");
assertFalse(isBlack(capturedImage), "image blackness");
int x = 0, y = 0, i = 0, j;
for (; i < columns; i++) {
@ -159,8 +159,7 @@ public class EditorPaneDemoTest {
y += yGap;
if(capturedImage.getRGB(x, y) == Color.WHITE.getRGB()) {
// saving image for failure case
JemmyExt.save(capturedImage, "capturedimage_" + pageName + "_" +
UIManager.getLookAndFeel().getClass().getSimpleName() + ".png");
save(capturedImage, "capturedimage-" + pageName);
return false;
}
}

View File

@ -22,12 +22,14 @@
*/
package org.jemmy2ext;
import java.awt.AWTException;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
@ -36,8 +38,11 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
@ -83,6 +88,19 @@ import static org.testng.AssertJUnit.*;
*/
public class JemmyExt {
private static Robot robot = null;
public static Robot getRobot() {
try {
if(robot == null) {
robot = new Robot();
}
return robot;
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
/**
* Statically referencing all the classes that are needed by tests so that
* they're compiled by jtreg
@ -151,78 +169,62 @@ public class JemmyExt {
});
}
public static void assertEquals(String string, StrictImageComparator comparator, BufferedImage expected, BufferedImage actual) {
try {
assertTrue(string, comparator.compare(expected, actual));
} catch (Error err) {
save(expected, "expected.png");
save(actual, "actual.png");
throw err;
}
private static final DateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private static String timeStamp() {
return timestampFormat.format(new Date());
}
public static void assertNotEquals(String string, StrictImageComparator comparator, BufferedImage notExpected, BufferedImage actual) {
try {
assertFalse(string, comparator.compare(notExpected, actual));
} catch (Error err) {
save(notExpected, "notExpected.png");
save(actual, "actual.png");
throw err;
}
/**
* Constructs filename with a timestamp.
* @param name File name or a path without the extension
* @param extension File extension (without the dot). Could be null,
* in which case timestamp is simply added to the filename (no trailing dot).
* @return file name
*/
public static String timeStamp(String name, String extension) {
return name + "-" + timeStamp() +
((extension != null) ? ("." + extension) : "");
}
public static void save(BufferedImage image, String filename) {
String filepath = filename;
/**
* Saves an image into a file. Filename will be constructed from the given fileID and
* a timestamp.
* @param image
* @param fileID
*/
public static void save(BufferedImage image, String fileID) {
doSave(image, timeStamp(fileID + "-" + lafShortName(), "png"));
}
//Saves an image into a file with the provided filename
private static void doSave(BufferedImage image, String filename) {
try {
filepath = new File(filename).getCanonicalPath();
String filepath = new File(filename).getCanonicalPath();
System.out.println("Saving screenshot to " + filepath);
BufferedOutputStream file = new BufferedOutputStream(new FileOutputStream(filepath));
new PNGEncoder(file, PNGEncoder.COLOR_MODE).encode(image);
} catch (IOException ioe) {
throw new RuntimeException("Failed to save image to " + filepath, ioe);
throw new RuntimeException("Failed to save image to " + filename, ioe);
}
}
/**
* Waits for a screen area taken by a component to not be completely black rectangle.
* @return last (non-black) image
* @throws TimeoutExpiredException if the waiting is unsuccessful
*/
public static BufferedImage waitNotBlack(Robot rob, ComponentOperator operator, String imageName) {
class NonBlackImageChooser implements ComponentChooser {
private BufferedImage image = null;
@Override
public boolean checkComponent(Component comp) {
image = capture(rob, operator);
save(image, imageName);
return !isBlack(image);
}
@Override
public String getDescription() {
return "A non-black Image of " + operator;
}
}
NonBlackImageChooser chooser = new NonBlackImageChooser();
operator.waitState(chooser);
return chooser.image;
}
/**
* Waits for the displayed image to be still.
* @param imageID an image ID with no extension. Timestamp and LAF information is added to the ID when saving.
* @return last still image
* @throws TimeoutExpiredException if the waiting is unsuccessful
*/
public static BufferedImage waitStillImage(Robot rob, ComponentOperator operator, String imageName) {
public static BufferedImage waitStillImage(ComponentOperator operator, String imageID) {
operator.getTimeouts().setTimeout("Waiter.TimeDelta", 1000);
String timestampName = timeStamp(imageID + "-" + lafShortName(), "png");
class StillImageChooser implements ComponentChooser {
private BufferedImage previousImage = null;
private final StrictImageComparator sComparator = new StrictImageComparator();
@Override
public boolean checkComponent(Component comp) {
BufferedImage currentImage = capture(rob, operator);
save(currentImage, imageName);
BufferedImage currentImage = capture(operator);
doSave(currentImage, timestampName);
boolean compareResult = previousImage == null ? false : sComparator.compare(currentImage, previousImage);
previousImage = currentImage;
return compareResult;
@ -241,22 +243,23 @@ public class JemmyExt {
/**
* Waits for the displayed image to change.
* @param reference image to compare to
* @param imageID an image ID with no extension. Timestamp and LAF information is added to the ID when saving.
* @return last (changed) image
* @throws TimeoutExpiredException if the waiting is unsuccessful
*/
public static BufferedImage waitChangedImage(Robot rob,
Supplier<BufferedImage> supplier,
public static BufferedImage waitChangedImage(Supplier<BufferedImage> supplier,
BufferedImage reference,
Timeouts timeouts,
String imageName) throws InterruptedException {
String imageID) throws InterruptedException {
ImageComparator comparator = new StrictImageComparator();
String timestampName = timeStamp(imageID + "-" + lafShortName(), "png");
class ImageWaitable implements Waitable {
BufferedImage image;
@Override
public Object actionProduced(Object obj) {
image = supplier.get();
save(image, imageName);
doSave(image, timestampName);
return comparator.compare(reference, image) ? null : image;
}
@ -316,10 +319,10 @@ public class JemmyExt {
}
}
public static BufferedImage capture(Robot rob, ComponentOperator operator) {
public static BufferedImage capture(ComponentOperator operator) {
Rectangle boundary = new Rectangle(operator.getLocationOnScreen(),
operator.getSize());
return rob.createScreenCapture(boundary);
return getRobot().createScreenCapture(boundary);
}
/**
@ -400,26 +403,26 @@ public class JemmyExt {
}
}
private static String lafShortName() { return UIManager.getLookAndFeel().getClass().getSimpleName(); }
/**
* Trying to capture as much information as possible. Currently it includes
* full dump and a screenshot of the whole screen.
*/
public static void captureAll() {
String lookAndFeelClassName = UIManager.getLookAndFeel().getClass().getSimpleName();
PNGEncoder.captureScreen("failure_" + lookAndFeelClassName + ".png", PNGEncoder.COLOR_MODE);
save(getRobot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())), "failure");
try {
Dumper.dumpAll("dumpAll_" + lookAndFeelClassName + ".xml");
Dumper.dumpAll(timeStamp("dumpAll-" + lafShortName(), "xml"));
} catch (FileNotFoundException ex) {
Logger.getLogger(JemmyExt.class.getName()).log(Level.SEVERE, null, ex);
}
captureWindows(lookAndFeelClassName);
captureWindows();
}
/**
* Captures each showing window image using Window.paint() method.
* @param lookAndFeelClassName
*/
private static void captureWindows(String lookAndFeelClassName) {
private static void captureWindows() {
try {
EventQueue.invokeAndWait(() -> {
Window[] windows = Window.getWindows();
@ -434,10 +437,14 @@ public class JemmyExt {
g.dispose();
try {
ImageIO.write(img, "png", new File("window_" + lookAndFeelClassName
+ "_" + index++ + ".png"));
} catch (IOException e) {
e.printStackTrace();
save(img, "window-" + index++);
} catch (RuntimeException e) {
if (e.getCause() instanceof IOException) {
System.err.println("Failed to save screen images");
e.printStackTrace();
} else {
throw e;
}
}
}
});