8335231: [macos] Test java/awt/print/PrinterJob/Cancel/PrinterJobCancel.java failed on macOS because the case didn't get the expected PrintAbortException

Reviewed-by: tr, abhiscxk
This commit is contained in:
Prasanta Sadhukhan 2024-11-25 08:57:54 +00:00
parent 9576546b9c
commit 333a9973f1
3 changed files with 33 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024, 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
@ -71,6 +71,8 @@ public final class CPrinterJob extends RasterPrinterJob {
private String outputBin = null;
private Throwable printerAbortExcpn;
// This is the NSPrintInfo for this PrinterJob. Protect multi thread
// access to it. It is used by the pageDialog, jobDialog, and printLoop.
// This way the state of these items is shared across these calls.
@ -245,7 +247,7 @@ public final class CPrinterJob extends RasterPrinterJob {
}
}
private void completePrintLoop() {
private void completePrintLoop(Throwable excpn) {
Runnable r = new Runnable() { public void run() {
synchronized(this) {
performingPrinting = false;
@ -255,6 +257,10 @@ public final class CPrinterJob extends RasterPrinterJob {
}
}};
if (excpn != null && excpn.toString().contains("PrinterAbortException")) {
printerAbortExcpn = excpn;
}
if (onEventThread) {
try { EventQueue.invokeAndWait(r); } catch (Exception e) { e.printStackTrace(); }
} else {
@ -364,6 +370,9 @@ public final class CPrinterJob extends RasterPrinterJob {
} catch (Exception e) {
e.printStackTrace();
}
if (printerAbortExcpn != null) {
throw (PrinterAbortException) printerAbortExcpn;
}
}
if (++loopi < prMembers.length) {
firstPage = prMembers[loopi][0]-1;
@ -741,15 +750,13 @@ public final class CPrinterJob extends RasterPrinterJob {
// but that will block the AppKit thread against whomever is holding the synchronized lock
boolean cancelled = (performingPrinting && userCancelled);
if (cancelled) {
try {
LWCToolkit.invokeLater(new Runnable() { public void run() {
try {
EventQueue.invokeLater(() -> {
try {
cancelDoc();
} catch (PrinterAbortException pae) {
// no-op, let the native side handle it
}
}}, null);
} catch (java.lang.reflect.InvocationTargetException ite) {}
} catch (PrinterAbortException pae) {
// no-op, let the native side handle it
}
});
}
return cancelled;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024, 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
@ -33,8 +33,10 @@
#import "JNIUtilities.h"
static jclass sjc_CPrinterJob = NULL;
static jclass sjc_PAbortEx = NULL;
#define GET_CPRINTERJOB_CLASS() (sjc_CPrinterJob, "sun/lwawt/macosx/CPrinterJob");
#define GET_CPRINTERJOB_CLASS_RETURN(ret) GET_CLASS_RETURN(sjc_CPrinterJob, "sun/lwawt/macosx/CPrinterJob", ret);
#define GET_PRINERABORTEXCEPTION_CLASS(ret) GET_CLASS_RETURN(sjc_PAbortEx, "java/awt/print/PrinterAbortException", ret);
@implementation PrinterView
@ -260,7 +262,12 @@ static jclass sjc_CPrinterJob = NULL;
DECLARE_METHOD_RETURN(jm_cancelCheck, sjc_CPrinterJob, "cancelCheck", "()Z", NO);
BOOL b = (*env)->CallBooleanMethod(env, fPrinterJob, jm_cancelCheck); // AWT_THREADING Safe (known object)
CHECK_EXCEPTION();
if (b) {
GET_PRINERABORTEXCEPTION_CLASS(b);
(*env)->ThrowNew(env, sjc_PAbortEx, "Printer Job cancelled");
} else {
CHECK_EXCEPTION();
}
return b;
}
@ -269,8 +276,12 @@ static jclass sjc_CPrinterJob = NULL;
{
AWT_ASSERT_NOT_APPKIT_THREAD;
DECLARE_METHOD(jf_completePrintLoop, sjc_CPrinterJob, "completePrintLoop", "()V");
(*env)->CallVoidMethod(env, fPrinterJob, jf_completePrintLoop);
jthrowable excpn = (*env)->ExceptionOccurred(env);
if (excpn != NULL) {
(*env)->ExceptionClear(env);
}
DECLARE_METHOD(jf_completePrintLoop, sjc_CPrinterJob, "completePrintLoop", "(Ljava/lang/Throwable;)V");
(*env)->CallVoidMethod(env, fPrinterJob, jf_completePrintLoop, excpn);
CHECK_EXCEPTION();
// Clean up after ourselves

View File

@ -32,7 +32,7 @@ import java.awt.print.PrinterJob;
/*
* @test
* @bug 4245280
* @bug 4245280 8335231
* @key printer
* @summary PrinterJob not cancelled when PrinterJob.cancel() is used
* @library /java/awt/regtesthelpers