1 /*
2  * Copyright (c) 2007, 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 /**
25  * @test InvalidPage.java
26  * @bug 4671634 6506286
27  * @summary Invalid page format can crash win32 JRE
28  * @author prr
29  * @run main/manual InvalidPage
30  */
31 
32 import java.awt.*;
33 import java.awt.event.*;
34 import java.awt.print.*;
35 
36 public class InvalidPage extends Frame implements Printable {
37 
38   PrinterJob pJob;
39   PageFormat pf;
40 
InvalidPage()41   public InvalidPage() {
42     super ("Validate Page Test");
43     pJob = PrinterJob.getPrinterJob();
44     pf = pJob.defaultPage();
45     Paper p = pf.getPaper();
46     p.setImageableArea(0,0,p.getWidth(), p.getHeight());
47     pf.setPaper(p);
48     setLayout(new FlowLayout());
49     Panel panel = new Panel();
50     Button printButton = new Button ("Print");
51     printButton.addActionListener(new ActionListener() {
52                 public void actionPerformed (ActionEvent e) {
53                     try {
54                          if (pJob.printDialog()) {
55                              pJob.setPrintable(InvalidPage.this, pf);
56                              pJob.print();
57                     }
58                     } catch (PrinterException pe ) {
59                     }
60                 }
61     });
62     panel.add (printButton);
63     add(panel);
64 
65     addWindowListener (new WindowAdapter() {
66          public void windowClosing (WindowEvent e) {
67             dispose();
68             System.exit (0);
69          }
70 
71       });
72       setSize (200, 200);
73       setVisible (true);
74   }
75 
print(Graphics graphics, PageFormat pageFormat, int pageIndex)76   public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
77 
78      if (pageIndex > 1) {
79         return Printable.NO_SUCH_PAGE;
80      }
81 
82      Graphics2D g2d = (Graphics2D)graphics;
83 
84      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
85      g2d.drawString("ORIGIN", 30, 30);
86      g2d.drawString("X THIS WAY", 200, 50);
87      g2d.drawString("Y THIS WAY", 60 , 200);
88      g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(),
89                       (int)pageFormat.getImageableHeight());
90      if (pageIndex == 0) {
91         g2d.setColor(Color.black);
92      } else {
93         g2d.setColor(new Color(0,0,0,128));
94      }
95      g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2,
96                       (int)pageFormat.getImageableHeight()-2);
97 
98      g2d.drawLine(0,0,
99                   (int)pageFormat.getImageableWidth(),
100                   (int)pageFormat.getImageableHeight());
101      g2d.drawLine((int)pageFormat.getImageableWidth(),0,
102                    0,(int)pageFormat.getImageableHeight());
103      return  Printable.PAGE_EXISTS;
104   }
105 
main( String[] args)106   public static void main( String[] args) {
107   String[] instructions =
108         {
109          "You must have a printer available to perform this test",
110          "Press the print button, which brings up a print dialog and",
111          "in the dialog select a printer and press the print button",
112          "in the dialog. Repeat for as many printers as you have installed",
113          "On solaris and linux just one printer is sufficient",
114          "Collect the output and examine it, each print job has two pages",
115          "of very similar output, except that the 2nd page of the job may",
116          "appear in a different colour, and the output near the edge of",
117          "the page may be clipped. This is OK. Hold up both pieces of paper",
118          "to the light and confirm that the lines and text (where present)",
119          "are positioned identically on both pages",
120          "The test fails if the JRE crashes, or if the output from the two",
121          "pages of a job is aligned differently"
122        };
123       Sysout.createDialog( );
124       Sysout.printInstructions( instructions );
125 
126      new InvalidPage();
127   }
128 
129 }
130 
131 class Sysout {
132    private static TestDialog dialog;
133 
createDialogWithInstructions( String[] instructions )134    public static void createDialogWithInstructions( String[] instructions )
135     {
136       dialog = new TestDialog( new Frame(), "Instructions" );
137       dialog.printInstructions( instructions );
138       dialog.show();
139       println( "Any messages for the tester will display here." );
140     }
141 
createDialog( )142    public static void createDialog( )
143     {
144       dialog = new TestDialog( new Frame(), "Instructions" );
145       String[] defInstr = { "Instructions will appear here. ", "" } ;
146       dialog.printInstructions( defInstr );
147       dialog.show();
148       println( "Any messages for the tester will display here." );
149     }
150 
151 
printInstructions( String[] instructions )152    public static void printInstructions( String[] instructions )
153     {
154       dialog.printInstructions( instructions );
155     }
156 
157 
println( String messageIn )158    public static void println( String messageIn )
159     {
160       dialog.displayMessage( messageIn );
161     }
162 
163 }// Sysout  class
164 
165 /**
166   This is part of the standard test machinery.  It provides a place for the
167    test instructions to be displayed, and a place for interactive messages
168    to the user to be displayed.
169   To have the test instructions displayed, see Sysout.
170   To have a message to the user be displayed, see Sysout.
171   Do not call anything in this dialog directly.
172   */
173 class TestDialog extends Dialog {
174 
175    TextArea instructionsText;
176    TextArea messageText;
177    int maxStringLength = 80;
178 
179    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )180    public TestDialog( Frame frame, String name )
181     {
182       super( frame, name );
183       int scrollBoth = TextArea.SCROLLBARS_BOTH;
184       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
185       add( "North", instructionsText );
186 
187       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
188       add("Center", messageText);
189 
190       pack();
191 
192       show();
193     }// TestDialog()
194 
195    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )196    public void printInstructions( String[] instructions )
197     {
198       //Clear out any current instructions
199       instructionsText.setText( "" );
200 
201       //Go down array of instruction strings
202 
203       String printStr, remainingStr;
204       for( int i=0; i < instructions.length; i++ )
205        {
206          //chop up each into pieces maxSringLength long
207          remainingStr = instructions[ i ];
208          while( remainingStr.length() > 0 )
209           {
210             //if longer than max then chop off first max chars to print
211             if( remainingStr.length() >= maxStringLength )
212              {
213                //Try to chop on a word boundary
214                int posOfSpace = remainingStr.
215                   lastIndexOf( ' ', maxStringLength - 1 );
216 
217                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
218 
219                printStr = remainingStr.substring( 0, posOfSpace + 1 );
220                remainingStr = remainingStr.substring( posOfSpace + 1 );
221              }
222             //else just print
223             else
224              {
225                printStr = remainingStr;
226                remainingStr = "";
227              }
228 
229             instructionsText.append( printStr + "\n" );
230 
231           }// while
232 
233        }// for
234 
235     }//printInstructions()
236 
237    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )238    public void displayMessage( String messageIn )
239     {
240       messageText.append( messageIn + "\n" );
241     }
242 
243  }// TestDialog  class
244