1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @key headful
  26  * @bug 7161283 8158520
  27  * @summary  Toolkit.getPrintJob throws NPE if no printer available
  28  * @run main PrinterException
  29  */
  30 import java.awt.Frame;
  31 import java.awt.JobAttributes;
  32 import java.awt.PrintJob;
  33 import java.awt.Toolkit;
  34 import java.awt.Robot;
  35 import java.awt.event.KeyEvent;
  36 
  37 public class PrinterException {
  38 
  39     public static void main(String[] args) throws Exception {
  40         Robot robot = new Robot();
  41         Thread t = new Thread (() -> {
  42             robot.waitForIdle();
  43             robot.delay(2000);
  44             robot.keyPress(KeyEvent.VK_ESCAPE);
  45             robot.keyRelease(KeyEvent.VK_ESCAPE);
  46            });
  47         Toolkit tk = Toolkit.getDefaultToolkit();
  48         PrintJob pj = null;
  49 
  50         int[][] pageRange = new int[][]{new int[]{1,1}};
  51         JobAttributes ja = new JobAttributes(1,
  52                 java.awt.JobAttributes.DefaultSelectionType.ALL,
  53                 JobAttributes.DestinationType.FILE, JobAttributes.DialogType.NATIVE,
  54                 "filename.ps", Integer.MAX_VALUE, 1,
  55                 JobAttributes.MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES,
  56                  pageRange, "", JobAttributes.SidesType.ONE_SIDED);
  57 
  58         Frame testFrame = new Frame("print");
  59         try {
  60             if (tk != null) {
  61                 t.start();
  62                 pj = tk.getPrintJob(testFrame, null, ja, null);
  63             }
  64         } finally {
  65             testFrame.dispose();
  66         }
  67     }
  68 
  69 }