Path: blob/master/test/jdk/java/awt/FileDialog/FileDialogOpenDirTest/FileDialogOpenDirTest.java
41153 views
/*1* Copyright (c) 2004, 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 497413526@summary FileDialog should open current directory by default.27@author [email protected] area=awt.filedialog28@run applet/manual=yesno FileDialogOpenDirTest.html29*/3031import java.awt.*;32import java.awt.event.*;33import java.applet.*;3435public class FileDialogOpenDirTest extends Applet {3637public static void main(String[] args) {38Applet a = new FileDialogOpenDirTest();39a.init();40a.start();41}4243public void init()44{45System.setProperty("sun.awt.disableGtkFileDialogs","true");46//Create instructions for the user here, as well as set up47// the environment -- set the layout manager, add buttons,48// etc.49this.setLayout (new BorderLayout ());5051String curdir = System.getProperty("user.dir");5253String[] instructions1 =54{55"After test started you will see 'Test Frame' with a button inside.",56"Click the button to open FileDialog.",57"Verify that the directory opened is current directory, that is:",58curdir,59"If so press PASSED, otherwise FAILED."60};6162String[] instructions2 =63{64"The test is not applicable for current platform. Press PASSED."65};6667Sysout.createDialogWithInstructions(Toolkit.getDefaultToolkit().getClass().getName().68equals("sun.awt.X11.XToolkit") ?69instructions1 : instructions2);70}7172public void start() {73Frame frame = new Frame("Test Frame");74Button open = new Button("Open File Dialog");7576open.addActionListener(new ActionListener() {77public void actionPerformed(ActionEvent e) {78new FileDialog(new Frame()).show();79}80});8182frame.setLayout(new FlowLayout());83frame.add(open);8485int x = 0;86int y = 0;87Component dlg = null;8889if ((dlg = Sysout.getDialog()) != null) {90x = dlg.getBounds().x + dlg.getBounds().width;91y = dlg.getBounds().y;92}93frame.setBounds(x, y, 150, 70);94frame.setVisible(true);95}96}979899/****************************************************100Standard Test Machinery101DO NOT modify anything below -- it's a standard102chunk of code whose purpose is to make user103interaction uniform, and thereby make it simpler104to read and understand someone else's test.105****************************************************/106107/**108This is part of the standard test machinery.109It creates a dialog (with the instructions), and is the interface110for sending text messages to the user.111To print the instructions, send an array of strings to Sysout.createDialog112WithInstructions method. Put one line of instructions per array entry.113To display a message for the tester to see, simply call Sysout.println114with the string to be displayed.115This mimics System.out.println but works within the test harness as well116as standalone.117*/118119class Sysout120{121private static TestDialog dialog;122123public static void createDialogWithInstructions( String[] instructions )124{125dialog = new TestDialog( new Frame(), "Instructions" );126dialog.printInstructions( instructions );127dialog.setVisible(true);128println( "Any messages for the tester will display here." );129}130131public static void createDialog( )132{133dialog = new TestDialog( new Frame(), "Instructions" );134String[] defInstr = { "Instructions will appear here. ", "" } ;135dialog.printInstructions( defInstr );136dialog.setVisible(true);137println( "Any messages for the tester will display here." );138}139140141public static void printInstructions( String[] instructions )142{143dialog.printInstructions( instructions );144}145146147public static void println( String messageIn )148{149dialog.displayMessage( messageIn );150}151152public static Component getDialog() {153return dialog;154}155156}// Sysout class157158/**159This is part of the standard test machinery. It provides a place for the160test instructions to be displayed, and a place for interactive messages161to the user to be displayed.162To have the test instructions displayed, see Sysout.163To have a message to the user be displayed, see Sysout.164Do not call anything in this dialog directly.165*/166class TestDialog extends Dialog167{168169TextArea instructionsText;170TextArea messageText;171int maxStringLength = 80;172173//DO NOT call this directly, go through Sysout174public TestDialog( Frame frame, String name )175{176super( frame, name );177int scrollBoth = TextArea.SCROLLBARS_BOTH;178instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );179add( "North", instructionsText );180181messageText = new TextArea( "", 5, maxStringLength, scrollBoth );182add("Center", messageText);183184pack();185186setVisible(true);187}// TestDialog()188189//DO NOT call this directly, go through Sysout190public void printInstructions( String[] instructions )191{192//Clear out any current instructions193instructionsText.setText( "" );194195//Go down array of instruction strings196197String printStr, remainingStr;198for( int i=0; i < instructions.length; i++ )199{200//chop up each into pieces maxSringLength long201remainingStr = instructions[ i ];202while( remainingStr.length() > 0 )203{204//if longer than max then chop off first max chars to print205if( remainingStr.length() >= maxStringLength )206{207//Try to chop on a word boundary208int posOfSpace = remainingStr.209lastIndexOf( ' ', maxStringLength - 1 );210211if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;212213printStr = remainingStr.substring( 0, posOfSpace + 1 );214remainingStr = remainingStr.substring( posOfSpace + 1 );215}216//else just print217else218{219printStr = remainingStr;220remainingStr = "";221}222223instructionsText.append( printStr + "\n" );224225}// while226227}// for228229}//printInstructions()230231//DO NOT call this directly, go through Sysout232public void displayMessage( String messageIn )233{234messageText.append( messageIn + "\n" );235System.out.println(messageIn);236}237238}// TestDialog class239240241