Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
8253543: sanity/client/SwingSet/src/ButtonDemoScreenshotTest.java fai…
Browse files Browse the repository at this point in the history
…led with "AssertionError: All pixels are not black"

Reviewed-by: serb
  • Loading branch information
shurymury committed Sep 30, 2020
1 parent 5310d85 commit ffc97ba
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 44 deletions.
51 changes: 31 additions & 20 deletions test/jdk/sanity/client/SwingSet/src/ButtonDemoScreenshotTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,6 +22,7 @@
*/

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;
Expand All @@ -33,7 +34,11 @@
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;

import javax.swing.UIManager;
Expand Down Expand Up @@ -72,18 +77,32 @@ 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);

new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();

JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE);
waitImageIsStill(rob, mainFrame);
mainFrame.waitComponentShowing(true);

//make sure the frame is already painted
waitChangedImage(rob, () -> rob.createScreenCapture(center),
background, mainFrame.getTimeouts(), "background.png");
//make sure the frame is painted completely
waitStillImage(rob, mainFrame, "frame.png");

// Check all the buttons
for (int i : BUTTONS) {
checkButton(mainFrame, i, rob);
}
}

private void checkButton(JFrameOperator jfo, int i, Robot rob) {
private void checkButton(JFrameOperator jfo, int i, Robot rob) throws InterruptedException {
JButtonOperator button = new JButtonOperator(jfo, i);

//additional instrumentation for JDK-8198920. To be removed after the bug is fixed
Expand All @@ -93,9 +112,8 @@ private void checkButton(JFrameOperator jfo, int i, Robot rob) {

button.moveMouse(button.getCenterX(), button.getCenterY());

BufferedImage initialButtonImage = capture(rob, button);
assertNotBlack(initialButtonImage);
save(initialButtonImage, "button" + i + ".png");
BufferedImage notPressed, pressed = null;
notPressed = waitStillImage(rob, button, "not-pressed-" + i + ".png");

BufferedImage[] pressedImage = new BufferedImage[1];

Expand All @@ -108,22 +126,15 @@ private void checkButton(JFrameOperator jfo, int i, Robot rob) {
//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
button.waitState(new ComponentChooser() {
public boolean checkComponent(Component c) {
pressedImage[0] = capture(rob, button);
assertNotBlack(pressedImage[0]);
return !sComparator.compare(initialButtonImage, pressedImage[0]);
}

public String getDescription() {
return "Button with new image";
}
});
waitChangedImage(rob, () -> capture(rob, button), notPressed,
button.getTimeouts(), "pressed-" + i + ".png");
pressed = waitStillImage(rob, button, "pressed.png");
} finally {
if (pressedImage[0] != null) {
save(pressedImage[0], "button" + i + "_pressed.png");
}
button.releaseMouse();
if(pressed != null) {
waitChangedImage(rob, () -> capture(rob, button), pressed,
button.getTimeouts(), "released-" + i + ".png");
}
//additional instrumentation for JDK-8198920. To be removed after the bug is fixed
button.getOutput().printTrace("JDK-8198920: Button released at " + System.currentTimeMillis());
try {
Expand Down
9 changes: 5 additions & 4 deletions test/jdk/sanity/client/SwingSet/src/EditorPaneDemoTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,8 +23,8 @@

import static com.sun.swingset3.demos.editorpane.EditorPaneDemo.DEMO_TITLE;
import static com.sun.swingset3.demos.editorpane.EditorPaneDemo.SOURCE_FILES;
import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
import static org.jemmy2ext.JemmyExt.assertNotBlack;
import static org.jemmy2ext.JemmyExt.*;
import static org.testng.Assert.assertFalse;

import java.awt.Color;
import java.awt.Dimension;
Expand Down Expand Up @@ -149,7 +149,8 @@ private void checkImage(JEditorPaneOperator editorPaneOperator,
final int xGap = 100, yGap = 40, columns = 2, rows = 5;
editorPaneOperator.waitState(comp -> {
BufferedImage capturedImage = ImageTool.getImage(imageRect);
assertNotBlack(capturedImage);
save(capturedImage, "editor.png");
assertFalse(isBlack(capturedImage), "image blackness");
int x = 0, y = 0, i = 0, j;
for (; i < columns; i++) {
x += xGap;
Expand Down
102 changes: 83 additions & 19 deletions test/jdk/sanity/client/lib/Extensions/src/org/jemmy2ext/JemmyExt.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,6 +40,7 @@
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.IntStream;
Expand All @@ -56,9 +57,11 @@
import org.netbeans.jemmy.DefaultCharBindingMap;
import org.netbeans.jemmy.QueueTool;
import org.netbeans.jemmy.TimeoutExpiredException;
import org.netbeans.jemmy.Timeouts;
import org.netbeans.jemmy.Waitable;
import org.netbeans.jemmy.Waiter;
import org.netbeans.jemmy.drivers.scrolling.JSpinnerDriver;
import org.netbeans.jemmy.image.ImageComparator;
import org.netbeans.jemmy.image.StrictImageComparator;
import org.netbeans.jemmy.operators.ComponentOperator;
import org.netbeans.jemmy.operators.ContainerOperator;
Expand Down Expand Up @@ -89,17 +92,13 @@ public class JemmyExt {
DefaultCharBindingMap.class
};

public static void assertNotBlack(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
try {
assertFalse("All pixels are not black", IntStream.range(0, w).parallel().allMatch(x
-> IntStream.range(0, h).allMatch(y -> (image.getRGB(x, y) & 0xffffff) == 0)
));
} catch (Throwable t) {
save(image, "allPixelsAreBlack.png");
throw t;
}
/**
* Checks if the image is complitely black.
*/
public static boolean isBlack(BufferedImage image) {
return IntStream.range(0, image.getWidth()).parallel()
.allMatch(x-> IntStream.range(0, image.getHeight())
.allMatch(y -> (image.getRGB(x, y) & 0xffffff) == 0));
}

public static void waitArmed(JButtonOperator button) {
Expand Down Expand Up @@ -184,28 +183,93 @@ public static void save(BufferedImage image, String filename) {
}
}

public static void waitImageIsStill(Robot rob, ComponentOperator operator) {
operator.waitState(new ComponentChooser() {
/**
* 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.
* @return last still image
* @throws TimeoutExpiredException if the waiting is unsuccessful
*/
public static BufferedImage waitStillImage(Robot rob, ComponentOperator operator, String imageName) {
operator.getTimeouts().setTimeout("Waiter.TimeDelta", 1000);
class StillImageChooser implements ComponentChooser {
private BufferedImage previousImage = null;
private int index = 0;
private final StrictImageComparator sComparator = new StrictImageComparator();

@Override
public boolean checkComponent(Component comp) {
BufferedImage currentImage = capture(rob, operator);
save(currentImage, "waitImageIsStill" + index + ".png");
index++;
save(currentImage, imageName);
boolean compareResult = previousImage == null ? false : sComparator.compare(currentImage, previousImage);
previousImage = currentImage;
return compareResult;
}

@Override
public String getDescription() {
return "Image of " + operator + " is still";
return "A still image of " + operator;
}
});
}
StillImageChooser chooser = new StillImageChooser();
operator.waitState(chooser);
return chooser.previousImage;
}

/**
* Waits for the displayed image to change.
* @param reference image to compare to
* @return last (changed) image
* @throws TimeoutExpiredException if the waiting is unsuccessful
*/
public static BufferedImage waitChangedImage(Robot rob,
Supplier<BufferedImage> supplier,
BufferedImage reference,
Timeouts timeouts,
String imageName) throws InterruptedException {
ImageComparator comparator = new StrictImageComparator();
class ImageWaitable implements Waitable {
BufferedImage image;

@Override
public Object actionProduced(Object obj) {
image = supplier.get();
save(image, imageName);
return comparator.compare(reference, image) ? null : image;
}

@Override
public String getDescription() {
return "Waiting screen image to change";
}
}
ImageWaitable waitable = new ImageWaitable();
Waiter waiter = new Waiter(waitable);
waiter.setTimeouts(timeouts);
waiter.waitAction(null);
return waitable.image;
}

private static class ThrowableHolder {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -218,6 +218,7 @@ public static void main(String args[]) throws InterruptedException, InvocationTa
JFrame frame = new JFrame(DEMO_TITLE);
frame.add(buttonDemo);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
Expand Down

0 comments on commit ffc97ba

Please sign in to comment.