Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/PrintJob/ConstrainedPrintingTest/ConstrainedPrintingTest.java
38828 views
/*1* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@bug 4116029 430038326@summary verify that child components can draw only inside their27visible bounds28@author [email protected] area=awt.print29@run main/manual=yesno ConstrainedPrintingTest30*/3132// Note there is no @ in front of test above. This is so that the33// harness will not mistake this file as a test file. It should34// only see the html file as a test file. (the harness runs all35// valid test files, so it would run this test twice if this file36// were valid as well as the html file.)37// Also, note the area= after Your Name in the author tag. Here, you38// should put which functional area the test falls in. See the39// AWT-core home page -> test areas and/or -> AWT team for a list of40// areas.41// There are several places where ManualYesNoTest appear. It is42// recommended that these be changed by a global search and replace,43// such as ESC-% in xemacs.44454647/**48* ConstrainedPrintingTest.java49*50* summary: verify that child components can draw only inside their51* visible bounds52*53*/5455import java.applet.Applet;56import java.awt.*;57import java.awt.event.ActionEvent;58import java.awt.event.ActionListener;596061//Manual tests should run as applet tests if possible because they62// get their environments cleaned up, including AWT threads, any63// test created threads, and any system resources used by the test64// such as file descriptors. (This is normally not a problem as65// main tests usually run in a separate VM, however on some platforms66// such as the Mac, separate VMs are not possible and non-applet67// tests will cause problems). Also, you don't have to worry about68// synchronisation stuff in Applet tests the way you do in main69// tests...707172public class ConstrainedPrintingTest implements ActionListener73{74//Declare things used in the test, like buttons and labels here75final Frame frame = new Frame("PrintTest");76final Button button = new Button("Print");77final Panel panel = new Panel();78final Component testComponent = new Component() {79public void paint(Graphics g) {80ConstrainedPrintingTest.paintOutsideBounds(this, g, Color.green);81}82public Dimension getPreferredSize() {83return new Dimension(100, 100);84}85};86final Canvas testCanvas = new Canvas() {87public void paint(Graphics g) {88ConstrainedPrintingTest.paintOutsideBounds(this, g, Color.red);89// The frame is sized so that only the upper part of90// the canvas is visible. We draw on the lower part,91// so that we can verify that the output is clipped92// by the parent container bounds.93Dimension panelSize = panel.getSize();94Rectangle b = getBounds();95g.setColor(Color.red);96g.setClip(null);97for (int i = panelSize.height - b.y; i < b.height; i+= 10) {98g.drawLine(0, i, b.width, i);99}100}101public Dimension getPreferredSize() {102return new Dimension(100, 100);103}104};105106public void init()107{108//Create instructions for the user here, as well as set up109// the environment -- set the layout manager, add buttons,110// etc.111button.addActionListener(this);112113panel.setBackground(Color.white);114panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));115panel.add(testComponent);116panel.add(testCanvas);117118frame.setLayout(new BorderLayout());119frame.add(button, BorderLayout.NORTH);120frame.add(panel, BorderLayout.CENTER);121frame.setSize(200, 250);122frame.validate();123frame.setResizable(false);124125String[] instructions =126{127"1.Look at the frame titled \"PrintTest\". If you see green or",128" red lines on the white area below the \"Print\" button, the",129" test fails. Otherwise go to step 2.",130"2.Press \"Print\" button. The print dialog will appear. Select",131" a printer and proceed. Look at the output. If you see multiple",132" lines outside of the frame bounds or in the white area below",133" the image of the \"Print\" button, the test fails. Otherwise",134" the test passes."135};136Sysout.createDialogWithInstructions( instructions );137138}//End init()139140public void start ()141{142//Get things going. Request focus, set size, et cetera143144frame.setVisible(true);145146//What would normally go into main() will probably go here.147//Use System.out.println for diagnostic messages that you want148// to read after the test is done.149//Use Sysout.println for messages you want the tester to read.150151}// start()152153//The rest of this class is the actions which perform the test...154155//Use Sysout.println to communicate with the user NOT System.out!!156//Sysout.println ("Something Happened!");157158public void stop() {159frame.setVisible(false);160}161162public void destroy() {163frame.dispose();164}165166public void actionPerformed(ActionEvent e) {167PageAttributes pa = new PageAttributes();168pa.setPrinterResolution(36);169PrintJob pjob = frame.getToolkit().getPrintJob(frame, "NewTest",170new JobAttributes(),171pa);172if (pjob != null) {173Graphics pg = pjob.getGraphics();174if (pg != null) {175pg.translate(20, 20);176frame.printAll(pg);177pg.dispose();178}179pjob.end();180}181}182183public static void paintOutsideBounds(Component comp,184Graphics g,185Color color) {186Dimension dim = comp.getSize();187g.setColor(color);188189g.setClip(0, 0, dim.width * 2, dim.height * 2);190for (int i = 0; i < dim.height * 2; i += 10) {191g.drawLine(dim.width, i, dim.width * 2, i);192}193194g.setClip(null);195for (int i = 0; i < dim.width * 2; i += 10) {196g.drawLine(i, dim.height, i, dim.height * 2);197}198199g.setClip(new Rectangle(0, 0, dim.width * 2, dim.height * 2));200for (int i = 0; i < dim.width; i += 10) {201g.drawLine(dim.width * 2 - i, 0, dim.width * 2, i);202}203}204205public static void main(String[] args) {206ConstrainedPrintingTest c = new ConstrainedPrintingTest();207208c.init();209c.start();210}211212}// class ConstrainedPrintingTest213214/* Place other classes related to the test after this line */215216217218219220/****************************************************221Standard Test Machinery222DO NOT modify anything below -- it's a standard223chunk of code whose purpose is to make user224interaction uniform, and thereby make it simpler225to read and understand someone else's test.226****************************************************/227228/**229This is part of the standard test machinery.230It creates a dialog (with the instructions), and is the interface231for sending text messages to the user.232To print the instructions, send an array of strings to Sysout.createDialog233WithInstructions method. Put one line of instructions per array entry.234To display a message for the tester to see, simply call Sysout.println235with the string to be displayed.236This mimics System.out.println but works within the test harness as well237as standalone.238*/239240class Sysout241{242private static TestDialog dialog;243244public static void createDialogWithInstructions( String[] instructions )245{246dialog = new TestDialog( new Frame(), "Instructions" );247dialog.printInstructions( instructions );248dialog.show();249println( "Any messages for the tester will display here." );250}251252public static void createDialog( )253{254dialog = new TestDialog( new Frame(), "Instructions" );255String[] defInstr = { "Instructions will appear here. ", "" } ;256dialog.printInstructions( defInstr );257dialog.show();258println( "Any messages for the tester will display here." );259}260261262public static void printInstructions( String[] instructions )263{264dialog.printInstructions( instructions );265}266267268public static void println( String messageIn )269{270dialog.displayMessage( messageIn );271}272273}// Sysout class274275/**276This is part of the standard test machinery. It provides a place for the277test instructions to be displayed, and a place for interactive messages278to the user to be displayed.279To have the test instructions displayed, see Sysout.280To have a message to the user be displayed, see Sysout.281Do not call anything in this dialog directly.282*/283class TestDialog extends Dialog284{285286TextArea instructionsText;287TextArea messageText;288int maxStringLength = 80;289290//DO NOT call this directly, go through Sysout291public TestDialog( Frame frame, String name )292{293super( frame, name );294int scrollBoth = TextArea.SCROLLBARS_BOTH;295instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );296add( "North", instructionsText );297298messageText = new TextArea( "", 5, maxStringLength, scrollBoth );299add("South", messageText);300301pack();302303show();304}// TestDialog()305306//DO NOT call this directly, go through Sysout307public void printInstructions( String[] instructions )308{309//Clear out any current instructions310instructionsText.setText( "" );311312//Go down array of instruction strings313314String printStr, remainingStr;315for( int i=0; i < instructions.length; i++ )316{317//chop up each into pieces maxSringLength long318remainingStr = instructions[ i ];319while( remainingStr.length() > 0 )320{321//if longer than max then chop off first max chars to print322if( remainingStr.length() >= maxStringLength )323{324//Try to chop on a word boundary325int posOfSpace = remainingStr.326lastIndexOf( ' ', maxStringLength - 1 );327328if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;329330printStr = remainingStr.substring( 0, posOfSpace + 1 );331remainingStr = remainingStr.substring( posOfSpace + 1 );332}333//else just print334else335{336printStr = remainingStr;337remainingStr = "";338}339340instructionsText.append( printStr + "\n" );341342}// while343344}// for345346}//printInstructions()347348//DO NOT call this directly, go through Sysout349public void displayMessage( String messageIn )350{351messageText.append( messageIn + "\n" );352}353354}// TestDialog class355356357