< prev index next >

test/java/awt/print/PrinterJob/MultiMonPrintDlgTest.java

Print this page




  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     Frame primaryFrame = null;
  47     Frame secFrame = null;
  48     GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment().






  49                             getScreenDevices();
  50 
  51     public MultiMonPrintDlgTest() throws Exception {
  52         if (gd.length <= 1) {
  53             System.out.println("This test should be run only on dual-monitor systems. Aborted!!");
  54             return;
  55         }
  56 
  57         String[] instructions =
  58             {
  59              " This test should be running on a dual-monitor setup.",
  60              "A frame will be created on each of the 2 monitor. ",
  61              "Click the Print button on the frame displayed in the non-default monitor.",
  62              "Please verify that page dialog followed by print dialog ",
  63              " is displayed in the same screen",
  64              "where the frame is located ie, in the non-default monitor.",
  65             };
  66 
  67         SwingUtilities.invokeAndWait(() -> {
  68             JOptionPane.showMessageDialog(
  69                     (Component) null,
  70                     instructions,
  71                     "information", JOptionPane.INFORMATION_MESSAGE);
  72         });




  73         GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  74         int x = 0;
  75         Frame f = null;
  76         for (x = 0; x < gd.length; x ++) {
  77             if (gd[x] != defDev) {
  78                 secFrame = new Frame("Screen " + x + " - secondary", gd[x].getDefaultConfiguration());
  79                 f = secFrame;
  80             } else {
  81                 primaryFrame = new Frame("Screen " + x + " - primary", gd[x].getDefaultConfiguration());
  82                 f = primaryFrame;
  83             }
  84             Button b = new Button("Print");
  85             b.addActionListener(this);
  86             f.add("South", b);
  87             f.addWindowListener (new WindowAdapter() {
  88                 public void windowClosing(WindowEvent we) {
  89                     ((Window) we.getSource()).dispose();
  90                 }
  91             });
  92             f.setSize(200, 200);
  93             f.setVisible(true);
  94         }
  95     }
  96 
  97     public void actionPerformed (ActionEvent ae) {
  98         try {
  99             javax.print.attribute.PrintRequestAttributeSet prSet =
 100                   new javax.print.attribute.HashPrintRequestAttributeSet();
 101             java.awt.print.PrinterJob.getPrinterJob().pageDialog(prSet);
 102             Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
 103             int dialogButton = JOptionPane.showConfirmDialog (w,
 104                             "Did the pageDialog shown in non-default monitor?",
 105                             null, JOptionPane.YES_NO_OPTION);
 106             if(dialogButton == JOptionPane.NO_OPTION) {
 107                 throw new RuntimeException("PageDialog is shown in wrong monitor");
 108             }
 109             java.awt.print.PrinterJob.getPrinterJob().printDialog(prSet);
 110             dialogButton = JOptionPane.showConfirmDialog (w,
 111                             "Did the printDialog shown in non-default monitor?",
 112                             null, JOptionPane.YES_NO_OPTION);
 113             if(dialogButton == JOptionPane.NO_OPTION) {
 114                 throw new RuntimeException("PrintDialog is shown in wrong monitor");




 115             }
 116         } finally {

 117             primaryFrame.dispose();
 118             secFrame.dispose();
 119         }












 120     }
 121 
 122     public static void main (String args[]) throws Exception {





 123         MultiMonPrintDlgTest test = new MultiMonPrintDlgTest();















 124     }
 125 }


  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 }
< prev index next >