Skip to content

Commit

Permalink
8295324: Adjust the J2DPrinterJobTest
Browse files Browse the repository at this point in the history
The test now also checks for the second race condition around 'jobDone'
flag, which is in the print(Graphics, PageFormat, int) method.
  • Loading branch information
eduardsdv committed Oct 28, 2022
1 parent fdec73d commit 0723d2e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
Expand Up @@ -939,7 +939,7 @@ public void run() {
}
}

private static class PageInfo {
protected static class PageInfo {

private PageLayout pageLayout;
private Node node;
Expand Down Expand Up @@ -1068,12 +1068,7 @@ private boolean waitForNextPage(int pageIndex) {
}

if (currPageInfo != null) {
if (Toolkit.getToolkit().isFxUserThread()) {
currPageInfo.clearScene();
} else {
Application.
invokeAndWait(new ClearSceneRunnable(currPageInfo));
}
clearScene(currPageInfo);
}
currPageInfo = null;
setPageDone(true);
Expand All @@ -1099,6 +1094,15 @@ private boolean waitForNextPage(int pageIndex) {
return true;
}

protected void clearScene(PageInfo pageInfo) {
if (Toolkit.getToolkit().isFxUserThread()) {
pageInfo.clearScene();
} else {
Application.
invokeAndWait(new ClearSceneRunnable(pageInfo));
}
}

private PageFormat getPageFormatFromLayout(PageLayout layout) {
java.awt.print.Paper paper = new java.awt.print.Paper();
double pWid = layout.getPaper().getWidth();
Expand Down Expand Up @@ -1157,13 +1161,13 @@ public int print(Graphics g, PageFormat pf, int pageIndex) {
int y = (int)pf.getImageableY();
int w = (int)pf.getImageableWidth();
int h = (int)pf.getImageableHeight();
Node appNode = currPageInfo.getNode();
g.translate(x, y);
printNode(appNode, g, w, h);
printNode(g, w, h);
return Printable.PAGE_EXISTS;
}

private void printNode(Node node, Graphics g, int w, int h) {
protected void printNode(Graphics g, int w, int h) {
Node node = currPageInfo.getNode();
PrismPrintGraphics ppg =
new PrismPrintGraphics((Graphics2D) g, w, h);
NGNode pgNode = NodeHelper.getPeer(node);
Expand All @@ -1187,10 +1191,6 @@ public Printable getPrintable(int pageIndex) {
return this;
}

protected PageFormat getCurrentPageFormat() {
return currPageFormat;
}

public PageFormat getPageFormat(int pageIndex) {
getPage(pageIndex);
return currPageFormat;
Expand Down
Expand Up @@ -25,13 +25,18 @@

package test.com.sun.prism.j2d.print;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.ByteArrayOutputStream;
import java.util.function.Supplier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.print.DocFlavor;
import javax.print.DocPrintJob;
Expand Down Expand Up @@ -61,16 +66,14 @@
import javafx.scene.ParentShim;
import test.com.sun.javafx.pgstub.StubToolkit;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class J2DPrinterJobTest {

private J2DPrinterJob job;
private PrinterJobMock printerJobMock;
private Supplier<PageFormat> pageFormatAccessor;

@Before
public void setUp() {
Expand All @@ -89,16 +92,11 @@ protected java.awt.print.PrinterJob createPrinterJob() {

@Override
protected J2DPrinterJob.J2DPageable createJ2dPageable() {
J2DPageable pageable = new J2DPageable();

// The printer thread initializes the current page format when it processes a page.
// We use it to check if the page has been printed.
pageFormatAccessor = pageable::getCurrentPageFormat;

return pageable;
return new J2DPageable();
}

class J2DPageable extends J2DPrinterJob.J2DPageable {

@Override
public void setPageDone(boolean pageDone) {
super.setPageDone(pageDone);
Expand All @@ -119,8 +117,13 @@ public void setPageDone(boolean pageDone) {
}

@Override
public PageFormat getCurrentPageFormat() {
return super.getCurrentPageFormat();
protected void printNode(Graphics g, int w, int h) {
// do nothing, as we don't really print in the test
}

@Override
protected void clearScene(J2DPrinterJob.PageInfo pageInfo) {
// not needed in test
}
}
};
Expand All @@ -132,19 +135,20 @@ public void tearDown() {
}

@Test
public void testJobEnd() throws InterruptedException {
public void testJobEnd() {
assertTrue(job.print(PageLayoutShim.createPageLayout(Paper.A4, PageOrientation.PORTRAIT), new ParentShim()));
job.endJob();
printerJobMock.waitUntilPrinted(5);
assertNotNull("The submitted page was not printed.", pageFormatAccessor.get());
assertTrue(job.endJob());
printerJobMock.waitUntilPrinted(10);
assertTrue("It seems that an error is occurred during printing.", job.endJob()); // check the jobError is not set
assertEquals("The submitted page was not printed.", List.of(0), printerJobMock.getPrintedPages());
}

@Test
public void testJobCanceled() throws InterruptedException {
public void testJobCanceled() {
assertTrue(job.print(PageLayoutShim.createPageLayout(Paper.A4, PageOrientation.PORTRAIT), new ParentShim()));
job.cancelJob();
printerJobMock.waitUntilPrinted(5);
assertNull("The page was printed even though the job was canceled.", pageFormatAccessor.get());
printerJobMock.waitUntilPrinted(10);
assertEquals("The page was printed even though the job was canceled.", List.of(), printerJobMock.getPrintedPages());
}

private static class PrintServiceMock extends StreamPrintService {
Expand Down Expand Up @@ -237,6 +241,7 @@ private static class PrinterJobMock extends java.awt.print.PrinterJob {
private PrintService service;
private Pageable pageable;
private volatile boolean printed;
private List<Integer> printedPages = Collections.synchronizedList(new ArrayList<>());

public void waitUntilPrinted(int timeoutInSeconds) {
for (int i = 0; !printed && i < timeoutInSeconds; i++) {
Expand All @@ -252,6 +257,10 @@ public void waitUntilPrinted(int timeoutInSeconds) {
}
}

public List<Integer> getPrintedPages() {
return printedPages;
}

@Override
public void setPrintService(PrintService service) {
this.service = service;
Expand All @@ -268,8 +277,28 @@ public void setPageable(Pageable pageable) throws NullPointerException {

@Override
public void print(PrintRequestAttributeSet attributes) throws PrinterException {
pageable.getPageFormat(0);
printed = true;
try {
int numberOfPages = pageable.getNumberOfPages();
if (numberOfPages == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
numberOfPages = 10; // it should be enough for test purposes
}
for (int i = 0; i < numberOfPages; i++) {
PageFormat pageFormat = pageable.getPageFormat(i);
int printResult = printPage(pageFormat, i);
if (printResult == Printable.NO_SUCH_PAGE) {
break;
}
printedPages.add(i);
}
} finally {
printed = true;
}
}

protected int printPage(PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D graphics = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB).createGraphics();
Printable printable = pageable.getPrintable(pageIndex);
return printable.print(graphics, pageFormat, pageIndex);
}

@Override
Expand Down

0 comments on commit 0723d2e

Please sign in to comment.