Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/NonFocusableBlockedOwnerTest/NonFocusableBlockedOwnerTest.java
47854 views
/*1* Copyright (c) 2005, 2015, 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/*24test25@bug 627232426@summary Modal excluded Window which decorated parent is blocked should be non-focusable.27@author [email protected]: area=awt.focus28@run applet NonFocusableBlockedOwnerTest.html29*/3031import java.applet.Applet;32import java.awt.*;33import java.awt.event.*;34import java.lang.reflect.*;3536public class NonFocusableBlockedOwnerTest extends Applet {37Robot robot;38Frame frame = new Frame("Modal Blocked Frame");39Dialog dialog = new Dialog(frame, "Modal Dialog", true);40Window excluded = new Window(frame);41Button button = new Button("button");4243public static void main(String[] args) {44NonFocusableBlockedOwnerTest app = new NonFocusableBlockedOwnerTest();45app.init();46app.start();47}4849public void init() {50try {51robot = new Robot();52} catch (AWTException e) {53throw new RuntimeException("Error: unable to create robot", e);54}55// Create instructions for the user here, as well as set up56// the environment -- set the layout manager, add buttons,57// etc.58this.setLayout (new BorderLayout ());59Sysout.createDialogWithInstructions(new String[]60{"This is an AUTOMATIC test", "simply wait until it is done"});61}6263public void start() {6465if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {66Sysout.println("No testing on MToolkit.");67return;68}6970try {71EventQueue.invokeLater(new Runnable() {72public void run() {73frame.setSize(300, 200);74frame.setVisible(true);7576excluded.setSize(300, 200);77excluded.setLocation(0, 400);78excluded.setModalExclusionType(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);79excluded.setLayout(new FlowLayout());80excluded.add(button);81excluded.setVisible(true);8283dialog.setSize(200, 100);84dialog.setLocation(0, 250);85dialog.setVisible(true);86}87});88} catch (Exception e) {89e.printStackTrace();90}9192waitTillShown(dialog);93clickOn(button);94if (frame == KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()) {95throw new RuntimeException("Test failed!");96}97if (excluded == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()) {98throw new RuntimeException("Test failed!");99}100if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {101throw new RuntimeException("Test failed!");102}103Sysout.println("Test passed.");104}105106void clickOn(Component c) {107Point p = c.getLocationOnScreen();108Dimension d = c.getSize();109110Sysout.println("Clicking " + c);111112if (c instanceof Frame) {113robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);114} else {115robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));116}117robot.mousePress(InputEvent.BUTTON1_MASK);118robot.mouseRelease(InputEvent.BUTTON1_MASK);119waitForIdle();120}121122void waitTillShown(Component c) {123while (true) {124try {125Thread.sleep(100);126c.getLocationOnScreen();127break;128} catch (InterruptedException e) {129throw new RuntimeException(e);130} catch (IllegalComponentStateException e) {}131}132}133void waitForIdle() {134try {135robot.waitForIdle();136EventQueue.invokeAndWait( new Runnable() {137public void run() {} // Dummy implementation138});139} catch(InterruptedException ie) {140Sysout.println("waitForIdle, non-fatal exception caught:");141ie.printStackTrace();142} catch(InvocationTargetException ite) {143Sysout.println("waitForIdle, non-fatal exception caught:");144ite.printStackTrace();145}146147// wait longer...148robot.delay(200);149}150}151152/****************************************************153Standard Test Machinery154DO NOT modify anything below -- it's a standard155chunk of code whose purpose is to make user156interaction uniform, and thereby make it simpler157to read and understand someone else's test.158****************************************************/159160/**161This is part of the standard test machinery.162It creates a dialog (with the instructions), and is the interface163for sending text messages to the user.164To print the instructions, send an array of strings to Sysout.createDialog165WithInstructions method. Put one line of instructions per array entry.166To display a message for the tester to see, simply call Sysout.println167with the string to be displayed.168This mimics System.out.println but works within the test harness as well169as standalone.170*/171172class Sysout173{174static TestDialog dialog;175176public static void createDialogWithInstructions( String[] instructions )177{178dialog = new TestDialog( new Frame(), "Instructions" );179dialog.printInstructions( instructions );180dialog.setVisible(true);181println( "Any messages for the tester will display here." );182}183184public static void createDialog( )185{186dialog = new TestDialog( new Frame(), "Instructions" );187String[] defInstr = { "Instructions will appear here. ", "" } ;188dialog.printInstructions( defInstr );189dialog.setVisible(true);190println( "Any messages for the tester will display here." );191}192193194public static void printInstructions( String[] instructions )195{196dialog.printInstructions( instructions );197}198199200public static void println( String messageIn )201{202dialog.displayMessage( messageIn );203}204205}// Sysout class206207/**208This is part of the standard test machinery. It provides a place for the209test instructions to be displayed, and a place for interactive messages210to the user to be displayed.211To have the test instructions displayed, see Sysout.212To have a message to the user be displayed, see Sysout.213Do not call anything in this dialog directly.214*/215class TestDialog extends Dialog216{217218TextArea instructionsText;219TextArea messageText;220int maxStringLength = 80;221222//DO NOT call this directly, go through Sysout223public TestDialog( Frame frame, String name )224{225super( frame, name );226int scrollBoth = TextArea.SCROLLBARS_BOTH;227instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );228add( "North", instructionsText );229230messageText = new TextArea( "", 5, maxStringLength, scrollBoth );231add("Center", messageText);232233pack();234235setVisible(true);236}// TestDialog()237238//DO NOT call this directly, go through Sysout239public void printInstructions( String[] instructions )240{241//Clear out any current instructions242instructionsText.setText( "" );243244//Go down array of instruction strings245246String printStr, remainingStr;247for( int i=0; i < instructions.length; i++ )248{249//chop up each into pieces maxSringLength long250remainingStr = instructions[ i ];251while( remainingStr.length() > 0 )252{253//if longer than max then chop off first max chars to print254if( remainingStr.length() >= maxStringLength )255{256//Try to chop on a word boundary257int posOfSpace = remainingStr.258lastIndexOf( ' ', maxStringLength - 1 );259260if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;261262printStr = remainingStr.substring( 0, posOfSpace + 1 );263remainingStr = remainingStr.substring( posOfSpace + 1 );264}265//else just print266else267{268printStr = remainingStr;269remainingStr = "";270}271272instructionsText.append( printStr + "\n" );273274}// while275276}// for277278}//printInstructions()279280//DO NOT call this directly, go through Sysout281public void displayMessage( String messageIn )282{283messageText.append( messageIn + "\n" );284System.out.println(messageIn);285}286287}// TestDialog class288289290