1 /*
   2  * Copyright (c) 2015, 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 import java.awt.Button;
  24 import java.awt.Component;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsDevice;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.KeyboardFocusManager;
  29 import java.awt.Window;
  30 import java.awt.event.ActionEvent;
  31 import java.awt.event.ActionListener;
  32 import java.awt.event.WindowAdapter;
  33 import java.awt.event.WindowEvent;
  34 import javax.swing.JOptionPane;
  35 import javax.swing.SwingUtilities;
  36 
  37 /**
  38  * @test
  39  * @bug 8138749
  40  * @summary PrinterJob.printDialog() does not support multi-mon,
  41  *           always displayed on primary
  42  * @run main/manual MultiMonPrintDlgTest
  43  */
  44 public class MultiMonPrintDlgTest implements ActionListener {
  45 
  46     private static boolean testPassed;
  47     private static Thread mainThread;
  48     private static boolean testGeneratedInterrupt;
  49     private static int sleepTime = 30000;
  50     private static String message = "User has not executed the test";
  51 
  52     static Frame primaryFrame = null;
  53     static Frame secFrame = null;
  54     static GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment().
  55                             getScreenDevices();
  56 
  57     private static void init() throws Exception {
  58 
  59         String[] instructions =
  60             {
  61              " This test should be running on a dual-monitor setup.",
  62              "A frame will be created on each of the 2 monitor. ",
  63              "Click the Print button on the frame displayed in the non-default monitor.",
  64              "Please verify that page dialog followed by print dialog ",
  65              " is displayed in the same screen",
  66              "where the frame is located ie, in the non-default monitor.",
  67             };
  68 
  69         SwingUtilities.invokeAndWait(() -> {
  70             JOptionPane.showMessageDialog(
  71                     (Component) null,
  72                     instructions,
  73                     "information", JOptionPane.INFORMATION_MESSAGE);
  74         });
  75     }
  76 
  77     private void executeTest() {
  78 
  79         GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  80         int x = 0;
  81         Frame f = null;
  82         for (x = 0; x < gd.length; x ++) {
  83             if (gd[x] != defDev) {
  84                 secFrame = new Frame("Screen " + x + " - secondary", gd[x].getDefaultConfiguration());
  85                 f = secFrame;
  86             } else {
  87                 primaryFrame = new Frame("Screen " + x + " - primary", gd[x].getDefaultConfiguration());
  88                 f = primaryFrame;
  89             }
  90             Button b = new Button("Print");
  91             b.addActionListener(this);
  92             f.add("South", b);
  93             f.addWindowListener (new WindowAdapter() {
  94                 public void windowClosing(WindowEvent we) {
  95                     ((Window) we.getSource()).dispose();
  96                 }
  97             });
  98             f.setSize(200, 200);
  99             f.setVisible(true);
 100         }
 101     }
 102 
 103     public void actionPerformed (ActionEvent ae) {
 104         javax.print.attribute.PrintRequestAttributeSet prSet =
 105               new javax.print.attribute.HashPrintRequestAttributeSet();
 106         java.awt.print.PrinterJob.getPrinterJob().pageDialog(prSet);
 107         Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
 108         int dialogButton = JOptionPane.showConfirmDialog (w,
 109                         "Did the pageDialog shown in non-default monitor?",
 110                         null, JOptionPane.YES_NO_OPTION);
 111         if(dialogButton == JOptionPane.NO_OPTION) {
 112             fail("PageDialog is shown in wrong monitor");
 113         } else { 
 114             java.awt.print.PrinterJob.getPrinterJob().printDialog(prSet);
 115             dialogButton = JOptionPane.showConfirmDialog (w,
 116                         "Did the printDialog shown in non-default monitor?",
 117                         null, JOptionPane.YES_NO_OPTION);
 118             if(dialogButton == JOptionPane.NO_OPTION) {
 119                 fail("PrintDialog is shown in wrong monitor");
 120             } else {
 121                 pass();
 122             }
 123         }
 124     }
 125 
 126     private static void dispose() {
 127         primaryFrame.dispose();
 128         secFrame.dispose();
 129     }
 130 
 131     public static synchronized void pass() {
 132         testPassed = true; 
 133         testGeneratedInterrupt = true;
 134         mainThread.interrupt();
 135     }
 136 
 137     public static synchronized void fail(String msg) {
 138         testPassed = false;
 139         message = msg; 
 140         testGeneratedInterrupt = true;
 141         mainThread.interrupt();
 142     }
 143 
 144     public static void main (String args[]) throws Exception {
 145         if (gd.length <= 1) {
 146             System.out.println("This test should be run only on dual-monitor systems. Aborted!!");
 147             return;
 148         }
 149         init();
 150         MultiMonPrintDlgTest test = new MultiMonPrintDlgTest();
 151         test.executeTest();
 152         mainThread = Thread.currentThread();
 153 
 154         try {
 155             mainThread.sleep(sleepTime);
 156         } catch (InterruptedException ex) {
 157             dispose();
 158             if (!testPassed && testGeneratedInterrupt) {
 159                 throw new RuntimeException(message);
 160             }
 161         }
 162         if (!testGeneratedInterrupt) {
 163             dispose(); 
 164             throw new RuntimeException(message);
 165         }
 166     }
 167 }