0N/A/*
3261N/A * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/*
0N/A @test
0N/A @bug 4116029 4300383
0N/A @summary verify that child components can draw only inside their
0N/A visible bounds
0N/A @author das@sparc.spb.su area=awt.print
0N/A @run main/manual=yesno ConstrainedPrintingTest
0N/A*/
0N/A
0N/A// Note there is no @ in front of test above. This is so that the
0N/A// harness will not mistake this file as a test file. It should
0N/A// only see the html file as a test file. (the harness runs all
0N/A// valid test files, so it would run this test twice if this file
0N/A// were valid as well as the html file.)
0N/A// Also, note the area= after Your Name in the author tag. Here, you
0N/A// should put which functional area the test falls in. See the
0N/A// AWT-core home page -> test areas and/or -> AWT team for a list of
0N/A// areas.
0N/A// There are several places where ManualYesNoTest appear. It is
0N/A// recommended that these be changed by a global search and replace,
4195N/A// such as ESC-% in xemacs.
4195N/A
0N/A
0N/A
0N/A/**
0N/A * ConstrainedPrintingTest.java
0N/A *
0N/A * summary: verify that child components can draw only inside their
0N/A * visible bounds
1696N/A *
1696N/A */
0N/A
3075N/Aimport java.applet.Applet;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.event.ActionListener;
0N/A
0N/A
0N/A//Manual tests should run as applet tests if possible because they
0N/A// get their environments cleaned up, including AWT threads, any
0N/A// test created threads, and any system resources used by the test
4195N/A// such as file descriptors. (This is normally not a problem as
4195N/A// main tests usually run in a separate VM, however on some platforms
0N/A// such as the Mac, separate VMs are not possible and non-applet
0N/A// tests will cause problems). Also, you don't have to worry about
0N/A// synchronisation stuff in Applet tests the way you do in main
0N/A// tests...
0N/A
0N/A
0N/Apublic class ConstrainedPrintingTest implements ActionListener
0N/A {
0N/A //Declare things used in the test, like buttons and labels here
0N/A final Frame frame = new Frame("PrintTest");
0N/A final Button button = new Button("Print");
0N/A final Panel panel = new Panel();
0N/A final Component testComponent = new Component() {
0N/A public void paint(Graphics g) {
0N/A ConstrainedPrintingTest.paintOutsideBounds(this, g, Color.green);
0N/A }
0N/A public Dimension getPreferredSize() {
0N/A return new Dimension(100, 100);
0N/A }
0N/A };
0N/A final Canvas testCanvas = new Canvas() {
0N/A public void paint(Graphics g) {
0N/A ConstrainedPrintingTest.paintOutsideBounds(this, g, Color.red);
0N/A // The frame is sized so that only the upper part of
0N/A // the canvas is visible. We draw on the lower part,
0N/A // so that we can verify that the output is clipped
1696N/A // by the parent container bounds.
1696N/A Dimension panelSize = panel.getSize();
0N/A Rectangle b = getBounds();
544N/A g.setColor(Color.red);
0N/A g.setClip(null);
0N/A for (int i = panelSize.height - b.y; i < b.height; i+= 10) {
0N/A g.drawLine(0, i, b.width, i);
0N/A }
0N/A }
0N/A public Dimension getPreferredSize() {
544N/A return new Dimension(100, 100);
0N/A }
0N/A };
0N/A
0N/A public void init()
0N/A {
0N/A //Create instructions for the user here, as well as set up
0N/A // the environment -- set the layout manager, add buttons,
0N/A // etc.
0N/A button.addActionListener(this);
0N/A
0N/A panel.setBackground(Color.white);
0N/A panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
0N/A panel.add(testComponent);
0N/A panel.add(testCanvas);
0N/A
0N/A frame.setLayout(new BorderLayout());
0N/A frame.add(button, BorderLayout.NORTH);
0N/A frame.add(panel, BorderLayout.CENTER);
0N/A frame.setSize(200, 250);
0N/A frame.validate();
0N/A frame.setResizable(false);
0N/A
0N/A String[] instructions =
0N/A {
0N/A "1.Look at the frame titled \"PrintTest\". If you see green or",
0N/A " red lines on the white area below the \"Print\" button, the",
0N/A " test fails. Otherwise go to step 2.",
0N/A "2.Press \"Print\" button. The print dialog will appear. Select",
0N/A " a printer and proceed. Look at the output. If you see multiple",
0N/A " lines outside of the frame bounds or in the white area below",
0N/A " the image of the \"Print\" button, the test fails. Otherwise",
0N/A " the test passes."
0N/A };
0N/A Sysout.createDialogWithInstructions( instructions );
0N/A
0N/A }//End init()
0N/A
0N/A public void start ()
0N/A {
0N/A //Get things going. Request focus, set size, et cetera
0N/A
0N/A frame.setVisible(true);
0N/A
0N/A //What would normally go into main() will probably go here.
0N/A //Use System.out.println for diagnostic messages that you want
0N/A // to read after the test is done.
0N/A //Use Sysout.println for messages you want the tester to read.
0N/A
0N/A }// start()
0N/A
0N/A //The rest of this class is the actions which perform the test...
0N/A
0N/A //Use Sysout.println to communicate with the user NOT System.out!!
0N/A //Sysout.println ("Something Happened!");
0N/A
0N/A public void stop() {
0N/A frame.setVisible(false);
0N/A }
0N/A
0N/A public void destroy() {
0N/A frame.dispose();
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A PageAttributes pa = new PageAttributes();
0N/A pa.setPrinterResolution(36);
0N/A PrintJob pjob = frame.getToolkit().getPrintJob(frame, "NewTest",
0N/A new JobAttributes(),
0N/A pa);
0N/A if (pjob != null) {
0N/A Graphics pg = pjob.getGraphics();
1045N/A if (pg != null) {
1045N/A pg.translate(20, 20);
1045N/A frame.printAll(pg);
0N/A pg.dispose();
0N/A }
0N/A pjob.end();
0N/A }
0N/A }
0N/A
0N/A public static void paintOutsideBounds(Component comp,
0N/A Graphics g,
0N/A Color color) {
0N/A Dimension dim = comp.getSize();
0N/A g.setColor(color);
0N/A
0N/A g.setClip(0, 0, dim.width * 2, dim.height * 2);
0N/A for (int i = 0; i < dim.height * 2; i += 10) {
0N/A g.drawLine(dim.width, i, dim.width * 2, i);
0N/A }
0N/A
0N/A g.setClip(null);
0N/A for (int i = 0; i < dim.width * 2; i += 10) {
0N/A g.drawLine(i, dim.height, i, dim.height * 2);
0N/A }
0N/A
0N/A g.setClip(new Rectangle(0, 0, dim.width * 2, dim.height * 2));
0N/A for (int i = 0; i < dim.width; i += 10) {
0N/A g.drawLine(dim.width * 2 - i, 0, dim.width * 2, i);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A ConstrainedPrintingTest c = new ConstrainedPrintingTest();
0N/A
1696N/A c.init();
0N/A c.start();
0N/A }
0N/A
0N/A }// class ConstrainedPrintingTest
0N/A
0N/A/* Place other classes related to the test after this line */
0N/A
0N/A
0N/A
0N/A
0N/A
0N/A/****************************************************
0N/A Standard Test Machinery
0N/A DO NOT modify anything below -- it's a standard
0N/A chunk of code whose purpose is to make user
0N/A interaction uniform, and thereby make it simpler
0N/A to read and understand someone else's test.
0N/A ****************************************************/
0N/A
0N/A/**
0N/A This is part of the standard test machinery.
0N/A It creates a dialog (with the instructions), and is the interface
0N/A for sending text messages to the user.
0N/A To print the instructions, send an array of strings to Sysout.createDialog
0N/A WithInstructions method. Put one line of instructions per array entry.
0N/A To display a message for the tester to see, simply call Sysout.println
0N/A with the string to be displayed.
0N/A This mimics System.out.println but works within the test harness as well
0N/A as standalone.
0N/A */
0N/A
0N/Aclass Sysout
0N/A {
0N/A private static TestDialog dialog;
0N/A
0N/A public static void createDialogWithInstructions( String[] instructions )
0N/A {
0N/A dialog = new TestDialog( new Frame(), "Instructions" );
0N/A dialog.printInstructions( instructions );
0N/A dialog.show();
0N/A println( "Any messages for the tester will display here." );
0N/A }
0N/A
0N/A public static void createDialog( )
0N/A {
3075N/A dialog = new TestDialog( new Frame(), "Instructions" );
3075N/A String[] defInstr = { "Instructions will appear here. ", "" } ;
3075N/A dialog.printInstructions( defInstr );
3075N/A dialog.show();
3075N/A println( "Any messages for the tester will display here." );
3075N/A }
3075N/A
0N/A
0N/A public static void printInstructions( String[] instructions )
0N/A {
0N/A dialog.printInstructions( instructions );
0N/A }
0N/A
0N/A
0N/A public static void println( String messageIn )
0N/A {
0N/A dialog.displayMessage( messageIn );
0N/A }
0N/A
0N/A }// Sysout class
0N/A
0N/A/**
0N/A This is part of the standard test machinery. It provides a place for the
0N/A test instructions to be displayed, and a place for interactive messages
0N/A to the user to be displayed.
0N/A To have the test instructions displayed, see Sysout.
0N/A To have a message to the user be displayed, see Sysout.
0N/A Do not call anything in this dialog directly.
0N/A */
0N/Aclass TestDialog extends Dialog
1057N/A {
1057N/A
1057N/A TextArea instructionsText;
0N/A TextArea messageText;
0N/A int maxStringLength = 80;
0N/A
1057N/A //DO NOT call this directly, go through Sysout
0N/A public TestDialog( Frame frame, String name )
0N/A {
0N/A super( frame, name );
0N/A int scrollBoth = TextArea.SCROLLBARS_BOTH;
0N/A instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
0N/A add( "North", instructionsText );
0N/A
0N/A messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
0N/A add("South", messageText);
0N/A
0N/A pack();
1057N/A
1057N/A show();
1057N/A }// TestDialog()
1057N/A
0N/A //DO NOT call this directly, go through Sysout
0N/A public void printInstructions( String[] instructions )
0N/A {
0N/A //Clear out any current instructions
1057N/A instructionsText.setText( "" );
1057N/A
1057N/A //Go down array of instruction strings
0N/A
0N/A String printStr, remainingStr;
0N/A for( int i=0; i < instructions.length; i++ )
0N/A {
1057N/A //chop up each into pieces maxSringLength long
0N/A remainingStr = instructions[ i ];
0N/A while( remainingStr.length() > 0 )
1057N/A {
1057N/A //if longer than max then chop off first max chars to print
1057N/A if( remainingStr.length() >= maxStringLength )
1057N/A {
544N/A //Try to chop on a word boundary
1057N/A int posOfSpace = remainingStr.
1057N/A lastIndexOf( ' ', maxStringLength - 1 );
0N/A
0N/A if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
0N/A
0N/A printStr = remainingStr.substring( 0, posOfSpace + 1 );
0N/A remainingStr = remainingStr.substring( posOfSpace + 1 );
1057N/A }
1057N/A //else just print
1057N/A else
0N/A {
1057N/A printStr = remainingStr;
0N/A remainingStr = "";
0N/A }
1057N/A
1057N/A instructionsText.append( printStr + "\n" );
1057N/A
0N/A }// while
0N/A
1057N/A }// for
0N/A
0N/A }//printInstructions()
0N/A
0N/A //DO NOT call this directly, go through Sysout
0N/A public void displayMessage( String messageIn )
1057N/A {
1057N/A messageText.append( messageIn + "\n" );
1057N/A }
1057N/A
1057N/A }// TestDialog class
1057N/A