J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 1998-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | @test |
| 26 | @bug 4116029 4300383 |
| 27 | @summary verify that child components can draw only inside their |
| 28 | visible bounds |
| 29 | @author das@sparc.spb.su area=awt.print |
| 30 | @run main/manual=yesno ConstrainedPrintingTest |
| 31 | */ |
| 32 | |
| 33 | // Note there is no @ in front of test above. This is so that the |
| 34 | // harness will not mistake this file as a test file. It should |
| 35 | // only see the html file as a test file. (the harness runs all |
| 36 | // valid test files, so it would run this test twice if this file |
| 37 | // were valid as well as the html file.) |
| 38 | // Also, note the area= after Your Name in the author tag. Here, you |
| 39 | // should put which functional area the test falls in. See the |
| 40 | // AWT-core home page -> test areas and/or -> AWT team for a list of |
| 41 | // areas. |
| 42 | // There are several places where ManualYesNoTest appear. It is |
| 43 | // recommended that these be changed by a global search and replace, |
| 44 | // such as ESC-% in xemacs. |
| 45 | |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * ConstrainedPrintingTest.java |
| 50 | * |
| 51 | * summary: verify that child components can draw only inside their |
| 52 | * visible bounds |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | import java.applet.Applet; |
| 57 | import java.awt.*; |
| 58 | import java.awt.event.ActionEvent; |
| 59 | import java.awt.event.ActionListener; |
| 60 | |
| 61 | |
| 62 | //Manual tests should run as applet tests if possible because they |
| 63 | // get their environments cleaned up, including AWT threads, any |
| 64 | // test created threads, and any system resources used by the test |
| 65 | // such as file descriptors. (This is normally not a problem as |
| 66 | // main tests usually run in a separate VM, however on some platforms |
| 67 | // such as the Mac, separate VMs are not possible and non-applet |
| 68 | // tests will cause problems). Also, you don't have to worry about |
| 69 | // synchronisation stuff in Applet tests the way you do in main |
| 70 | // tests... |
| 71 | |
| 72 | |
| 73 | public class ConstrainedPrintingTest implements ActionListener |
| 74 | { |
| 75 | //Declare things used in the test, like buttons and labels here |
| 76 | final Frame frame = new Frame("PrintTest"); |
| 77 | final Button button = new Button("Print"); |
| 78 | final Panel panel = new Panel(); |
| 79 | final Component testComponent = new Component() { |
| 80 | public void paint(Graphics g) { |
| 81 | ConstrainedPrintingTest.paintOutsideBounds(this, g, Color.green); |
| 82 | } |
| 83 | public Dimension getPreferredSize() { |
| 84 | return new Dimension(100, 100); |
| 85 | } |
| 86 | }; |
| 87 | final Canvas testCanvas = new Canvas() { |
| 88 | public void paint(Graphics g) { |
| 89 | ConstrainedPrintingTest.paintOutsideBounds(this, g, Color.red); |
| 90 | // The frame is sized so that only the upper part of |
| 91 | // the canvas is visible. We draw on the lower part, |
| 92 | // so that we can verify that the output is clipped |
| 93 | // by the parent container bounds. |
| 94 | Dimension panelSize = panel.getSize(); |
| 95 | Rectangle b = getBounds(); |
| 96 | g.setColor(Color.red); |
| 97 | g.setClip(null); |
| 98 | for (int i = panelSize.height - b.y; i < b.height; i+= 10) { |
| 99 | g.drawLine(0, i, b.width, i); |
| 100 | } |
| 101 | } |
| 102 | public Dimension getPreferredSize() { |
| 103 | return new Dimension(100, 100); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | public void init() |
| 108 | { |
| 109 | //Create instructions for the user here, as well as set up |
| 110 | // the environment -- set the layout manager, add buttons, |
| 111 | // etc. |
| 112 | button.addActionListener(this); |
| 113 | |
| 114 | panel.setBackground(Color.white); |
| 115 | panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); |
| 116 | panel.add(testComponent); |
| 117 | panel.add(testCanvas); |
| 118 | |
| 119 | frame.setLayout(new BorderLayout()); |
| 120 | frame.add(button, BorderLayout.NORTH); |
| 121 | frame.add(panel, BorderLayout.CENTER); |
| 122 | frame.setSize(200, 250); |
| 123 | frame.validate(); |
| 124 | frame.setResizable(false); |
| 125 | |
| 126 | String[] instructions = |
| 127 | { |
| 128 | "1.Look at the frame titled \"PrintTest\". If you see green or", |
| 129 | " red lines on the white area below the \"Print\" button, the", |
| 130 | " test fails. Otherwise go to step 2.", |
| 131 | "2.Press \"Print\" button. The print dialog will appear. Select", |
| 132 | " a printer and proceed. Look at the output. If you see multiple", |
| 133 | " lines outside of the frame bounds or in the white area below", |
| 134 | " the image of the \"Print\" button, the test fails. Otherwise", |
| 135 | " the test passes." |
| 136 | }; |
| 137 | Sysout.createDialogWithInstructions( instructions ); |
| 138 | |
| 139 | }//End init() |
| 140 | |
| 141 | public void start () |
| 142 | { |
| 143 | //Get things going. Request focus, set size, et cetera |
| 144 | |
| 145 | frame.setVisible(true); |
| 146 | |
| 147 | //What would normally go into main() will probably go here. |
| 148 | //Use System.out.println for diagnostic messages that you want |
| 149 | // to read after the test is done. |
| 150 | //Use Sysout.println for messages you want the tester to read. |
| 151 | |
| 152 | }// start() |
| 153 | |
| 154 | //The rest of this class is the actions which perform the test... |
| 155 | |
| 156 | //Use Sysout.println to communicate with the user NOT System.out!! |
| 157 | //Sysout.println ("Something Happened!"); |
| 158 | |
| 159 | public void stop() { |
| 160 | frame.setVisible(false); |
| 161 | } |
| 162 | |
| 163 | public void destroy() { |
| 164 | frame.dispose(); |
| 165 | } |
| 166 | |
| 167 | public void actionPerformed(ActionEvent e) { |
| 168 | PageAttributes pa = new PageAttributes(); |
| 169 | pa.setPrinterResolution(36); |
| 170 | PrintJob pjob = frame.getToolkit().getPrintJob(frame, "NewTest", |
| 171 | new JobAttributes(), |
| 172 | pa); |
| 173 | if (pjob != null) { |
| 174 | Graphics pg = pjob.getGraphics(); |
| 175 | if (pg != null) { |
| 176 | pg.translate(20, 20); |
| 177 | frame.printAll(pg); |
| 178 | pg.dispose(); |
| 179 | } |
| 180 | pjob.end(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public static void paintOutsideBounds(Component comp, |
| 185 | Graphics g, |
| 186 | Color color) { |
| 187 | Dimension dim = comp.getSize(); |
| 188 | g.setColor(color); |
| 189 | |
| 190 | g.setClip(0, 0, dim.width * 2, dim.height * 2); |
| 191 | for (int i = 0; i < dim.height * 2; i += 10) { |
| 192 | g.drawLine(dim.width, i, dim.width * 2, i); |
| 193 | } |
| 194 | |
| 195 | g.setClip(null); |
| 196 | for (int i = 0; i < dim.width * 2; i += 10) { |
| 197 | g.drawLine(i, dim.height, i, dim.height * 2); |
| 198 | } |
| 199 | |
| 200 | g.setClip(new Rectangle(0, 0, dim.width * 2, dim.height * 2)); |
| 201 | for (int i = 0; i < dim.width; i += 10) { |
| 202 | g.drawLine(dim.width * 2 - i, 0, dim.width * 2, i); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | public static void main(String[] args) { |
| 207 | ConstrainedPrintingTest c = new ConstrainedPrintingTest(); |
| 208 | |
| 209 | c.init(); |
| 210 | c.start(); |
| 211 | } |
| 212 | |
| 213 | }// class ConstrainedPrintingTest |
| 214 | |
| 215 | /* Place other classes related to the test after this line */ |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | /**************************************************** |
| 222 | Standard Test Machinery |
| 223 | DO NOT modify anything below -- it's a standard |
| 224 | chunk of code whose purpose is to make user |
| 225 | interaction uniform, and thereby make it simpler |
| 226 | to read and understand someone else's test. |
| 227 | ****************************************************/ |
| 228 | |
| 229 | /** |
| 230 | This is part of the standard test machinery. |
| 231 | It creates a dialog (with the instructions), and is the interface |
| 232 | for sending text messages to the user. |
| 233 | To print the instructions, send an array of strings to Sysout.createDialog |
| 234 | WithInstructions method. Put one line of instructions per array entry. |
| 235 | To display a message for the tester to see, simply call Sysout.println |
| 236 | with the string to be displayed. |
| 237 | This mimics System.out.println but works within the test harness as well |
| 238 | as standalone. |
| 239 | */ |
| 240 | |
| 241 | class Sysout |
| 242 | { |
| 243 | private static TestDialog dialog; |
| 244 | |
| 245 | public static void createDialogWithInstructions( String[] instructions ) |
| 246 | { |
| 247 | dialog = new TestDialog( new Frame(), "Instructions" ); |
| 248 | dialog.printInstructions( instructions ); |
| 249 | dialog.show(); |
| 250 | println( "Any messages for the tester will display here." ); |
| 251 | } |
| 252 | |
| 253 | public static void createDialog( ) |
| 254 | { |
| 255 | dialog = new TestDialog( new Frame(), "Instructions" ); |
| 256 | String[] defInstr = { "Instructions will appear here. ", "" } ; |
| 257 | dialog.printInstructions( defInstr ); |
| 258 | dialog.show(); |
| 259 | println( "Any messages for the tester will display here." ); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | public static void printInstructions( String[] instructions ) |
| 264 | { |
| 265 | dialog.printInstructions( instructions ); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | public static void println( String messageIn ) |
| 270 | { |
| 271 | dialog.displayMessage( messageIn ); |
| 272 | } |
| 273 | |
| 274 | }// Sysout class |
| 275 | |
| 276 | /** |
| 277 | This is part of the standard test machinery. It provides a place for the |
| 278 | test instructions to be displayed, and a place for interactive messages |
| 279 | to the user to be displayed. |
| 280 | To have the test instructions displayed, see Sysout. |
| 281 | To have a message to the user be displayed, see Sysout. |
| 282 | Do not call anything in this dialog directly. |
| 283 | */ |
| 284 | class TestDialog extends Dialog |
| 285 | { |
| 286 | |
| 287 | TextArea instructionsText; |
| 288 | TextArea messageText; |
| 289 | int maxStringLength = 80; |
| 290 | |
| 291 | //DO NOT call this directly, go through Sysout |
| 292 | public TestDialog( Frame frame, String name ) |
| 293 | { |
| 294 | super( frame, name ); |
| 295 | int scrollBoth = TextArea.SCROLLBARS_BOTH; |
| 296 | instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); |
| 297 | add( "North", instructionsText ); |
| 298 | |
| 299 | messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); |
| 300 | add("South", messageText); |
| 301 | |
| 302 | pack(); |
| 303 | |
| 304 | show(); |
| 305 | }// TestDialog() |
| 306 | |
| 307 | //DO NOT call this directly, go through Sysout |
| 308 | public void printInstructions( String[] instructions ) |
| 309 | { |
| 310 | //Clear out any current instructions |
| 311 | instructionsText.setText( "" ); |
| 312 | |
| 313 | //Go down array of instruction strings |
| 314 | |
| 315 | String printStr, remainingStr; |
| 316 | for( int i=0; i < instructions.length; i++ ) |
| 317 | { |
| 318 | //chop up each into pieces maxSringLength long |
| 319 | remainingStr = instructions[ i ]; |
| 320 | while( remainingStr.length() > 0 ) |
| 321 | { |
| 322 | //if longer than max then chop off first max chars to print |
| 323 | if( remainingStr.length() >= maxStringLength ) |
| 324 | { |
| 325 | //Try to chop on a word boundary |
| 326 | int posOfSpace = remainingStr. |
| 327 | lastIndexOf( ' ', maxStringLength - 1 ); |
| 328 | |
| 329 | if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; |
| 330 | |
| 331 | printStr = remainingStr.substring( 0, posOfSpace + 1 ); |
| 332 | remainingStr = remainingStr.substring( posOfSpace + 1 ); |
| 333 | } |
| 334 | //else just print |
| 335 | else |
| 336 | { |
| 337 | printStr = remainingStr; |
| 338 | remainingStr = ""; |
| 339 | } |
| 340 | |
| 341 | instructionsText.append( printStr + "\n" ); |
| 342 | |
| 343 | }// while |
| 344 | |
| 345 | }// for |
| 346 | |
| 347 | }//printInstructions() |
| 348 | |
| 349 | //DO NOT call this directly, go through Sysout |
| 350 | public void displayMessage( String messageIn ) |
| 351 | { |
| 352 | messageText.append( messageIn + "\n" ); |
| 353 | } |
| 354 | |
| 355 | }// TestDialog class |