jdk/test/java/awt/FileDialog/DefaultFocusOwner/DefaultFocusOwner.java
author ohair
Thu, 17 Sep 2009 13:17:57 -0700
changeset 3790 ab4f91337479
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
  @bug 6579962
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @summary  FileDialog focus is incorrect (Linux)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  @author dmitry.cherepanov area=awt.filedialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  @run main/manual DefaultFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The test verifies that when the file dialog is open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the focus is transfered on the correct component of the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class DefaultFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static void init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        //*** Create instructions for the user here ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String[] instructions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            " 1) there is a frame with a 'show dialog' button, ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            " 2) press the butoton, the dialog will be shown, ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            " 3) the focus will be transfered into the dialog, ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            " 4) check that initially focus owner is the specific text field, ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            "    its label is 'File name' on Windows OS, 'Enter file name' on Linux OS, ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            " 5) if it's true then the test passed, otherwise it failed. "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Sysout.createDialog( );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Sysout.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Frame frame = new Frame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Button button = new Button("show dialog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        button.addActionListener(new ActionListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    FileDialog dialog = new FileDialog(new Frame(), "dialog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        frame.setBounds(200,200,200,200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        frame.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }//End  init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*****************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Standard Test Machinery Section
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * DO NOT modify anything in this section -- it's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * standard chunk of code which has all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * synchronisation necessary for the test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * By keeping it the same in all tests, it is easier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * to read and understand someone else's test, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * well as insuring that all tests behave correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * with the test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * There is a section following this for test-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     ******************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static boolean theTestPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static boolean testGeneratedInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static String failureMessage = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static Thread mainThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static int sleepTime = 300000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main( String args[] ) throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        mainThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        catch( TestPassedException e )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            //The test passed, so just return from main and harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            // interepret this return as a pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        //At this point, neither test passed nor test failed has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // called -- either would have thrown an exception and ended the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // test, so we know we have multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        //Test involves other threads, so sleep and wait for them to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // called pass() or fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            Thread.sleep( sleepTime );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            //Timed out, so fail the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        catch (InterruptedException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if( ! testGeneratedInterrupt ) throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            //reset flag in case hit this code more than once for some reason (just safety)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            testGeneratedInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if ( theTestPassed == false )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                throw new RuntimeException( failureMessage );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }//main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static synchronized void setTimeoutTo( int seconds )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        sleepTime = seconds * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static synchronized void pass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Sysout.println( "The test passed." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //first check if this is executing in main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if ( mainThread == Thread.currentThread() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            //Still in the main thread, so set the flag just for kicks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // and throw a test passed exception which will be caught
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // and end the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            theTestPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new TestPassedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        //pass was called from a different thread, so set the flag and interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // the main thead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        theTestPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        testGeneratedInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }//pass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static synchronized void fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        //test writer didn't specify why test failed, so give generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        fail( "it just plain failed! :-)" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public static synchronized void fail( String whyFailed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Sysout.println( "The test failed: " + whyFailed );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //check if this called from main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if ( mainThread == Thread.currentThread() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            //If main thread, fail now 'cause not sleeping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new RuntimeException( whyFailed );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        theTestPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        testGeneratedInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        failureMessage = whyFailed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }//fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}// class ManualMainTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
//This exception is used to exit from any level of call nesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
// when it's determined that the test has passed, and immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
// end the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
class TestPassedException extends RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
//*********** End Standard Test Machinery Section **********
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
//************ Begin classes defined for the test ****************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
// make listeners in a class defined here, and instantiate them in init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
/* Example of a class which may be written as part of a test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
class NewClass implements anInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
   static int newVar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
   public void eventDispatched(AWTEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      //Counting events to see if we get enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
      eventCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      if( eventCount == 20 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         //got enough events, so pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         ManualMainTest.pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
      else if( tries == 20 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         //tried too many times without getting enough events so fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         ManualMainTest.fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }// eventDispatched()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 }// NewClass class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
//************** End classes defined for the test *******************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
/****************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 Standard Test Machinery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 DO NOT modify anything below -- it's a standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
  chunk of code whose purpose is to make user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
  interaction uniform, and thereby make it simpler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
  to read and understand someone else's test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 ****************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 This is part of the standard test machinery.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 It creates a dialog (with the instructions), and is the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  for sending text messages to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 To print the instructions, send an array of strings to Sysout.createDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
  WithInstructions method.  Put one line of instructions per array entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 To display a message for the tester to see, simply call Sysout.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
  with the string to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 This mimics System.out.println but works within the test harness as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
  as standalone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
class Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private static TestDialog dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public static void createDialogWithInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        dialog = new TestDialog( new Frame(), "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public static void createDialog( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        dialog = new TestDialog( new Frame(), "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        String[] defInstr = { "Instructions will appear here. ", "" } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        dialog.printInstructions( defInstr );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public static void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public static void println( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        dialog.displayMessage( messageIn );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}// Sysout  class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
  This is part of the standard test machinery.  It provides a place for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
   test instructions to be displayed, and a place for interactive messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
   to the user to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  To have the test instructions displayed, see Sysout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  To have a message to the user be displayed, see Sysout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
  Do not call anything in this dialog directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
class TestDialog extends Dialog implements ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    TextArea instructionsText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    TextArea messageText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    int maxStringLength = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    Panel  buttonP = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    Button passB = new Button( "pass" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    Button failB = new Button( "fail" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public TestDialog( Frame frame, String name )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        super( frame, name );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int scrollBoth = TextArea.SCROLLBARS_BOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        add( "North", instructionsText );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        add("Center", messageText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        passB = new Button( "pass" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        passB.setActionCommand( "pass" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        passB.addActionListener( this );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        buttonP.add( "East", passB );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        failB = new Button( "fail" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        failB.setActionCommand( "fail" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        failB.addActionListener( this );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        buttonP.add( "West", failB );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        add( "South", buttonP );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }// TestDialog()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        //Clear out any current instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        instructionsText.setText( "" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        //Go down array of instruction strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        String printStr, remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        for( int i=0; i < instructions.length; i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            //chop up each into pieces maxSringLength long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            remainingStr = instructions[ i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            while( remainingStr.length() > 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                //if longer than max then chop off first max chars to print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if( remainingStr.length() >= maxStringLength )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    //Try to chop on a word boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    int posOfSpace = remainingStr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        lastIndexOf( ' ', maxStringLength - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    remainingStr = remainingStr.substring( posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                //else just print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    printStr = remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    remainingStr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                instructionsText.append( printStr + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }// while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }// for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }//printInstructions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void displayMessage( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        messageText.append( messageIn + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        System.out.println(messageIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    //catch presses of the passed and failed buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    //simply call the standard pass() or fail() static methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    //ManualMainTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public void actionPerformed( ActionEvent e )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if( e.getActionCommand() == "pass" )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            DefaultFocusOwner.pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            DefaultFocusOwner.fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
}// TestDialog  class