jdk/test/java/awt/grab/EmbeddedFrameTest1/EmbeddedFrameTest1.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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 2006 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 6359129
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @summary REGRESSION: Popup menus dont respond to selections when extend outside Applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  @author oleg.sukhodolsky area=awt.grab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  @library ../../regtesthelpers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
  @build Util
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  @run main EmbeddedFrameTest1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * EmbeddedFrameTest1.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * summary: REGRESSION: Popup menus dont respond to selections when extend outside Applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.BorderLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Panel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.Robot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.TextArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.swing.JButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.swing.JPopupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import test.java.awt.regtesthelpers.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class EmbeddedFrameTest1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static void init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        //*** Create instructions for the user here ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        String[] instructions = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            "This is an AUTOMATIC test, simply wait until it is done.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            "The result (passed or failed) will be shown in the",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            "message window below."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Sysout.createDialog( );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Sysout.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SunToolkit tk = (SunToolkit) Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if ("sun.awt.motif.MToolkit".equals(tk.getClass().getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            System.out.println("We shouldn't test MToolkit.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            EmbeddedFrameTest1.pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            final Frame frame = new Frame("AWT Frame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            frame.setSize(200,200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            final Frame embedded_frame = Util.createEmbeddedFrame(frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            embedded_frame.setSize(200, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            Sysout.println("embedded_frame = " + embedded_frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            final JPopupMenu menu = new JPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            JButton item = new JButton("A button in popup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            item.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        System.out.println("Button pressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            menu.add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            final JButton btn = new JButton("Press me to see popup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            btn.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        menu.show(btn, 0, btn.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            final Panel p = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            p.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            embedded_frame.add(p,BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            embedded_frame.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            p.add(btn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            p.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            Robot robot = new Robot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            tk.realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Util.clickOnComp(btn, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            tk.realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            Util.clickOnComp(item, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            tk.realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (item.getMousePosition() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                throw new RuntimeException("Popup was not closed (mouse above it)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            embedded_frame.remove(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            embedded_frame.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            frame.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (Throwable thr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            thr.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            EmbeddedFrameTest1.fail("TEST FAILED: " + thr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        EmbeddedFrameTest1.pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }//End  init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*****************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Standard Test Machinery Section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * DO NOT modify anything in this section -- it's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * standard chunk of code which has all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * synchronisation necessary for the test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * By keeping it the same in all tests, it is easier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * to read and understand someone else's test, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * well as insuring that all tests behave correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * with the test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * There is a section following this for test-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     ******************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static boolean theTestPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static boolean testGeneratedInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static String failureMessage = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static Thread mainThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static int sleepTime = 300000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // Not sure about what happens if multiple of this test are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    //  instantiated in the same VM.  Being static (and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    //  static vars), it aint gonna work.  Not worrying about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    //  it for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static void main( String args[] ) throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        mainThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        catch( TestPassedException e )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            //The test passed, so just return from main and harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // interepret this return as a pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //At this point, neither test pass nor test fail has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // called -- either would have thrown an exception and ended the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // test, so we know we have multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //Test involves other threads, so sleep and wait for them to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // called pass() or fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            Thread.sleep( sleepTime );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            //Timed out, so fail the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        catch (InterruptedException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            //The test harness may have interrupted the test.  If so, rethrow the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // so that the harness gets it and deals with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if( ! testGeneratedInterrupt ) throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            //reset flag in case hit this code more than once for some reason (just safety)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            testGeneratedInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if ( theTestPassed == false )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                throw new RuntimeException( failureMessage );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }//main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static synchronized void setTimeoutTo( int seconds )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        sleepTime = seconds * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static synchronized void pass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Sysout.println( "The test passed." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        //first check if this is executing in main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if ( mainThread == Thread.currentThread() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            //Still in the main thread, so set the flag just for kicks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // and throw a test passed exception which will be caught
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // and end the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            theTestPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new TestPassedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        theTestPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        testGeneratedInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }//pass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static synchronized void fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        //test writer didn't specify why test failed, so give generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        fail( "it just plain failed! :-)" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static synchronized void fail( String whyFailed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Sysout.println( "The test failed: " + whyFailed );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //check if this called from main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if ( mainThread == Thread.currentThread() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            //If main thread, fail now 'cause not sleeping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new RuntimeException( whyFailed );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        theTestPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        testGeneratedInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        failureMessage = whyFailed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }//fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}// class EmbeddedFrameTest1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
//This exception is used to exit from any level of call nesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
// when it's determined that the test has passed, and immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
// end the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
class TestPassedException extends RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
//*********** End Standard Test Machinery Section **********
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
/****************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 Standard Test Machinery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 DO NOT modify anything below -- it's a standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  chunk of code whose purpose is to make user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
  interaction uniform, and thereby make it simpler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  to read and understand someone else's test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 ****************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 This is part of the standard test machinery.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 It creates a dialog (with the instructions), and is the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  for sending text messages to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 To print the instructions, send an array of strings to Sysout.createDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
  WithInstructions method.  Put one line of instructions per array entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 To display a message for the tester to see, simply call Sysout.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
  with the string to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 This mimics System.out.println but works within the test harness as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
  as standalone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
class Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private static TestDialog dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public static void createDialogWithInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        dialog = new TestDialog( new Frame(), "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public static void createDialog( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        dialog = new TestDialog( new Frame(), "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        String[] defInstr = { "Instructions will appear here. ", "" } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        dialog.printInstructions( defInstr );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public static void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public static void println( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        dialog.displayMessage( messageIn );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        System.out.println(messageIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}// Sysout  class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
  This is part of the standard test machinery.  It provides a place for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
   test instructions to be displayed, and a place for interactive messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
   to the user to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
  To have the test instructions displayed, see Sysout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  To have a message to the user be displayed, see Sysout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
  Do not call anything in this dialog directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
class TestDialog extends Dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    TextArea instructionsText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    TextArea messageText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    int maxStringLength = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public TestDialog( Frame frame, String name )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        super( frame, name );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        int scrollBoth = TextArea.SCROLLBARS_BOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        add( "North", instructionsText );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        add("Center", messageText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }// TestDialog()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        //Clear out any current instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        instructionsText.setText( "" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        //Go down array of instruction strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        String printStr, remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        for( int i=0; i < instructions.length; i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            //chop up each into pieces maxSringLength long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            remainingStr = instructions[ i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            while( remainingStr.length() > 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                //if longer than max then chop off first max chars to print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if( remainingStr.length() >= maxStringLength )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    //Try to chop on a word boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    int posOfSpace = remainingStr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        lastIndexOf( ' ', maxStringLength - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    remainingStr = remainingStr.substring( posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                //else just print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    printStr = remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    remainingStr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                instructionsText.append( printStr + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }// while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }// for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }//printInstructions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public void displayMessage( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        messageText.append( messageIn + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        System.out.println(messageIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}// TestDialog  class