Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/PrintJob/PageSetupDlgBlockingTest/PageSetupDlgBlockingTest.java
38828 views
/*1* Copyright (c) 2003, 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 450758526@summary Native modal dialog shouldn't block event dispatching when called on EventDispatchThread.27@author [email protected]: area=awt.PrintJob28@run main/manual=yesno PageSetupDlgBlockingTest2930*/3132import java.awt.*;33import java.awt.print.*;34import java.awt.event.*;35import javax.swing.*;36import java.applet.*;3738public class PageSetupDlgBlockingTest extends Panel {39public static Frame frame = new TestFrame("Test Frame");4041public static void main(String[] args) {42PageSetupDlgBlockingTest a = new PageSetupDlgBlockingTest();4344a.init();45a.start();46}4748public void init()49{50//Create instructions for the user here, as well as set up51// the environment -- set the layout manager, add buttons,52// etc.53this.setLayout (new BorderLayout ());5455String[] instructions =56{57"This test verifies that native modal 'Page Setup' dialog doesn't block event",58"handling when called on EventDispatchThread.",59" ",60"After test started you will see 'Test Frame' frame which contains",61"one 'Click Me' button.",62"1. Click the button:",63" - 'Page Setup' dialog will appear.",64"2. Drag the dialog over the 'Test Frame' so that to enforce its button redraw:",65" - if you're seeing the button redraw (as long as PAINT events are displayed)",66" the test PASSED else FAILED."67};68Sysout.createDialogWithInstructions(instructions);69}707172public void start() {73JButton button = new JButton("Click Me");74final AWTEventListener listener = new AWTEventListener() {75public void eventDispatched(AWTEvent e) {76if (e.getSource().getClass() == TestFrame.class) {77Sysout.println(e.paramString() + " on <Test Frame>");78}79}80};8182button.addActionListener(new ActionListener() {83public void actionPerformed(ActionEvent e) {8485// Show PAINT events only when the dialog is displayed.86Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.PAINT_EVENT_MASK);8788PrinterJob job = PrinterJob.getPrinterJob();89job.pageDialog(job.defaultPage());9091Toolkit.getDefaultToolkit().removeAWTEventListener(listener);92}93});9495button.setSize(100, 50);9697frame.setLayout(new BorderLayout());98frame.setSize(200, 200);99frame.setLocation(500, 0);100frame.add(button, BorderLayout.CENTER);101frame.setVisible(true);102}103}104105class TestFrame extends Frame {106TestFrame(String title) {107super(title);108}109}110111/****************************************************112Standard Test Machinery113DO NOT modify anything below -- it's a standard114chunk of code whose purpose is to make user115interaction uniform, and thereby make it simpler116to read and understand someone else's test.117****************************************************/118119/**120This is part of the standard test machinery.121It creates a dialog (with the instructions), and is the interface122for sending text messages to the user.123To print the instructions, send an array of strings to Sysout.createDialog124WithInstructions method. Put one line of instructions per array entry.125To display a message for the tester to see, simply call Sysout.println126with the string to be displayed.127This mimics System.out.println but works within the test harness as well128as standalone.129*/130131class Sysout132{133private static TestDialog dialog;134135public static void createDialogWithInstructions( String[] instructions )136{137dialog = new TestDialog( new Frame(), "Instructions" );138dialog.printInstructions( instructions );139dialog.setVisible(true);140println( "Any messages for the tester will display here." );141}142143public static void createDialog( )144{145dialog = new TestDialog( new Frame(), "Instructions" );146String[] defInstr = { "Instructions will appear here. ", "" } ;147dialog.printInstructions( defInstr );148dialog.setVisible(true);149println( "Any messages for the tester will display here." );150}151152153public static void printInstructions( String[] instructions )154{155dialog.printInstructions( instructions );156}157158159public static void println( String messageIn )160{161dialog.displayMessage( messageIn );162}163164}// Sysout class165166/**167This is part of the standard test machinery. It provides a place for the168test instructions to be displayed, and a place for interactive messages169to the user to be displayed.170To have the test instructions displayed, see Sysout.171To have a message to the user be displayed, see Sysout.172Do not call anything in this dialog directly.173*/174class TestDialog extends Dialog175{176177TextArea instructionsText;178TextArea messageText;179int maxStringLength = 80;180181//DO NOT call this directly, go through Sysout182public TestDialog( Frame frame, String name )183{184super( frame, name );185int scrollBoth = TextArea.SCROLLBARS_BOTH;186instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );187add( "North", instructionsText );188189messageText = new TextArea( "", 5, maxStringLength, scrollBoth );190add("Center", messageText);191192pack();193194setVisible(true);195}// TestDialog()196197//DO NOT call this directly, go through Sysout198public void printInstructions( String[] instructions )199{200//Clear out any current instructions201instructionsText.setText( "" );202203//Go down array of instruction strings204205String printStr, remainingStr;206for( int i=0; i < instructions.length; i++ )207{208//chop up each into pieces maxSringLength long209remainingStr = instructions[ i ];210while( remainingStr.length() > 0 )211{212//if longer than max then chop off first max chars to print213if( remainingStr.length() >= maxStringLength )214{215//Try to chop on a word boundary216int posOfSpace = remainingStr.217lastIndexOf( ' ', maxStringLength - 1 );218219if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;220221printStr = remainingStr.substring( 0, posOfSpace + 1 );222remainingStr = remainingStr.substring( posOfSpace + 1 );223}224//else just print225else226{227printStr = remainingStr;228remainingStr = "";229}230231instructionsText.append( printStr + "\n" );232233}// while234235}// for236237}//printInstructions()238239//DO NOT call this directly, go through Sysout240public void displayMessage( String messageIn )241{242messageText.append( messageIn + "\n" );243System.out.println(messageIn);244}245246}// TestDialog class247248249