Skip to content

Commit 333a997

Browse files
committed
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
1 parent 9576546 commit 333a997

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -71,6 +71,8 @@ public final class CPrinterJob extends RasterPrinterJob {
7171

7272
private String outputBin = null;
7373

74+
private Throwable printerAbortExcpn;
75+
7476
// This is the NSPrintInfo for this PrinterJob. Protect multi thread
7577
// access to it. It is used by the pageDialog, jobDialog, and printLoop.
7678
// This way the state of these items is shared across these calls.
@@ -245,7 +247,7 @@ protected void cancelDoc() throws PrinterAbortException {
245247
}
246248
}
247249

248-
private void completePrintLoop() {
250+
private void completePrintLoop(Throwable excpn) {
249251
Runnable r = new Runnable() { public void run() {
250252
synchronized(this) {
251253
performingPrinting = false;
@@ -255,6 +257,10 @@ private void completePrintLoop() {
255257
}
256258
}};
257259

260+
if (excpn != null && excpn.toString().contains("PrinterAbortException")) {
261+
printerAbortExcpn = excpn;
262+
}
263+
258264
if (onEventThread) {
259265
try { EventQueue.invokeAndWait(r); } catch (Exception e) { e.printStackTrace(); }
260266
} else {
@@ -364,6 +370,9 @@ public void print(PrintRequestAttributeSet attributes) throws PrinterException {
364370
} catch (Exception e) {
365371
e.printStackTrace();
366372
}
373+
if (printerAbortExcpn != null) {
374+
throw (PrinterAbortException) printerAbortExcpn;
375+
}
367376
}
368377
if (++loopi < prMembers.length) {
369378
firstPage = prMembers[loopi][0]-1;
@@ -741,15 +750,13 @@ private boolean cancelCheck() {
741750
// but that will block the AppKit thread against whomever is holding the synchronized lock
742751
boolean cancelled = (performingPrinting && userCancelled);
743752
if (cancelled) {
744-
try {
745-
LWCToolkit.invokeLater(new Runnable() { public void run() {
746-
try {
753+
EventQueue.invokeLater(() -> {
754+
try {
747755
cancelDoc();
748-
} catch (PrinterAbortException pae) {
749-
// no-op, let the native side handle it
750-
}
751-
}}, null);
752-
} catch (java.lang.reflect.InvocationTargetException ite) {}
756+
} catch (PrinterAbortException pae) {
757+
// no-op, let the native side handle it
758+
}
759+
});
753760
}
754761
return cancelled;
755762
}

src/java.desktop/macosx/native/libawt_lwawt/awt/PrinterView.m

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,8 +33,10 @@
3333
#import "JNIUtilities.h"
3434

3535
static jclass sjc_CPrinterJob = NULL;
36+
static jclass sjc_PAbortEx = NULL;
3637
#define GET_CPRINTERJOB_CLASS() (sjc_CPrinterJob, "sun/lwawt/macosx/CPrinterJob");
3738
#define GET_CPRINTERJOB_CLASS_RETURN(ret) GET_CLASS_RETURN(sjc_CPrinterJob, "sun/lwawt/macosx/CPrinterJob", ret);
39+
#define GET_PRINERABORTEXCEPTION_CLASS(ret) GET_CLASS_RETURN(sjc_PAbortEx, "java/awt/print/PrinterAbortException", ret);
3840

3941
@implementation PrinterView
4042

@@ -260,7 +262,12 @@ - (BOOL)cancelCheck:(JNIEnv*)env
260262
DECLARE_METHOD_RETURN(jm_cancelCheck, sjc_CPrinterJob, "cancelCheck", "()Z", NO);
261263

262264
BOOL b = (*env)->CallBooleanMethod(env, fPrinterJob, jm_cancelCheck); // AWT_THREADING Safe (known object)
263-
CHECK_EXCEPTION();
265+
if (b) {
266+
GET_PRINERABORTEXCEPTION_CLASS(b);
267+
(*env)->ThrowNew(env, sjc_PAbortEx, "Printer Job cancelled");
268+
} else {
269+
CHECK_EXCEPTION();
270+
}
264271
return b;
265272
}
266273

@@ -269,8 +276,12 @@ - (void)complete:(JNIEnv*)env
269276
{
270277
AWT_ASSERT_NOT_APPKIT_THREAD;
271278

272-
DECLARE_METHOD(jf_completePrintLoop, sjc_CPrinterJob, "completePrintLoop", "()V");
273-
(*env)->CallVoidMethod(env, fPrinterJob, jf_completePrintLoop);
279+
jthrowable excpn = (*env)->ExceptionOccurred(env);
280+
if (excpn != NULL) {
281+
(*env)->ExceptionClear(env);
282+
}
283+
DECLARE_METHOD(jf_completePrintLoop, sjc_CPrinterJob, "completePrintLoop", "(Ljava/lang/Throwable;)V");
284+
(*env)->CallVoidMethod(env, fPrinterJob, jf_completePrintLoop, excpn);
274285
CHECK_EXCEPTION();
275286

276287
// Clean up after ourselves

test/jdk/java/awt/print/PrinterJob/Cancel/PrinterJobCancel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/*
3434
* @test
35-
* @bug 4245280
35+
* @bug 4245280 8335231
3636
* @key printer
3737
* @summary PrinterJob not cancelled when PrinterJob.cancel() is used
3838
* @library /java/awt/regtesthelpers

0 commit comments

Comments
 (0)