jdk/test/java/awt/Mixing/OpaqueTest.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 31448 1066345d2a8a
child 43724 a272f9475e44
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 31448
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1978
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1978
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1978
diff changeset
    21
 * questions.
2
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
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 31448
diff changeset
    26
  @key headful
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @bug 4811096
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  @summary Tests whether opaque and non-opaque components mix correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  @author anthony.petrov@...: area=awt.mixing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
  @library ../regtesthelpers
31448
1066345d2a8a 8076468: Add @modules to tests in jdk_desktop test group
yan
parents: 5506
diff changeset
    31
  @modules java.desktop/com.sun.awt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  @build Util
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  @run main OpaqueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * OpaqueTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * summary:  OpaqueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import test.java.awt.regtesthelpers.Util;
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
    47
import com.sun.awt.AWTUtilities;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class OpaqueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    //*** test-writer defined static variables go here ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static String testSeq = new String("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    final static String checkSeq = new String("010000101");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static void init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        //*** Create instructions for the user here ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String[] instructions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        {
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
        Sysout.createDialog( );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Sysout.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // Create components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        final Frame f = new Frame("Button-JButton mix test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        final Panel p = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        final Button heavy = new Button("  Heavyweight Button  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        final JButton light = new JButton("  LW Button  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Actions for the buttons add appropriate number to the test sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        heavy.addActionListener(new java.awt.event.ActionListener()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    public void actionPerformed(java.awt.event.ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        p.setComponentZOrder(light, 0);
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
    84
                        f.validate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        testSeq = testSeq + "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        light.addActionListener(new java.awt.event.ActionListener()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    public void actionPerformed(java.awt.event.ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        p.setComponentZOrder(heavy, 0);
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
    94
                        f.validate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        testSeq = testSeq + "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Overlap the buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        heavy.setBounds(30, 30, 200, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        light.setBounds(10, 10, 50, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // Put the components into the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        p.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        p.add(heavy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        p.add(light);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        f.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        f.setBounds(50, 50, 400, 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        f.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Robot robot = Util.createRobot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        robot.setAutoDelay(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // Move the mouse pointer to the position where both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        //    buttons overlap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Point heavyLoc = heavy.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Now perform the click at this point for 9 times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // In the middle of the process toggle the opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // flag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i = 0; i < 9; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (i == 3) {
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
   128
                AWTUtilities.setComponentMixingCutoutShape(light,
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
   129
                        new Rectangle());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (i == 6) {
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
   132
                AWTUtilities.setComponentMixingCutoutShape(light,
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 2
diff changeset
   133
                        null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            robot.mousePress(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // If the buttons are correctly mixed, the test sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // is equal to the check sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (testSeq.equals(checkSeq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            OpaqueTest.pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            OpaqueTest.fail("The components changed their visible Z-order in a wrong sequence: '" + testSeq + "' instead of '" + checkSeq + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }//End  init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*****************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Standard Test Machinery Section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * DO NOT modify anything in this section -- it's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * standard chunk of code which has all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * synchronisation necessary for the test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * By keeping it the same in all tests, it is easier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * to read and understand someone else's test, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * well as insuring that all tests behave correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * with the test harness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * There is a section following this for test-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     ******************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static boolean theTestPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static boolean testGeneratedInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static String failureMessage = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static Thread mainThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static int sleepTime = 300000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // Not sure about what happens if multiple of this test are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    //  instantiated in the same VM.  Being static (and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    //  static vars), it aint gonna work.  Not worrying about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    //  it for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static void main( String args[] ) throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        mainThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        catch( TestPassedException e )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            //The test passed, so just return from main and harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // interepret this return as a pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        //At this point, neither test pass nor test fail has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // called -- either would have thrown an exception and ended the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // test, so we know we have multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        //Test involves other threads, so sleep and wait for them to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // called pass() or fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            Thread.sleep( sleepTime );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            //Timed out, so fail the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        catch (InterruptedException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            //The test harness may have interrupted the test.  If so, rethrow the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // so that the harness gets it and deals with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if( ! testGeneratedInterrupt ) throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            //reset flag in case hit this code more than once for some reason (just safety)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            testGeneratedInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if ( theTestPassed == false )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                throw new RuntimeException( failureMessage );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }//main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public static synchronized void setTimeoutTo( int seconds )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        sleepTime = seconds * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static synchronized void pass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Sysout.println( "The test passed." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        //first check if this is executing in main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if ( mainThread == Thread.currentThread() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            //Still in the main thread, so set the flag just for kicks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // and throw a test passed exception which will be caught
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // and end the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            theTestPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new TestPassedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        theTestPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        testGeneratedInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }//pass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public static synchronized void fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        //test writer didn't specify why test failed, so give generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        fail( "it just plain failed! :-)" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public static synchronized void fail( String whyFailed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Sysout.println( "The test failed: " + whyFailed );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //check if this called from main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if ( mainThread == Thread.currentThread() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            //If main thread, fail now 'cause not sleeping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new RuntimeException( whyFailed );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        theTestPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        testGeneratedInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        failureMessage = whyFailed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }//fail()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}// class OpaqueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
//This exception is used to exit from any level of call nesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
// when it's determined that the test has passed, and immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
// end the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
class TestPassedException extends RuntimeException
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
//*********** End Standard Test Machinery Section **********
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
//************ Begin classes defined for the test ****************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
// if want to make listeners, here is the recommended place for them, then instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
//  them in init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
/* Example of a class which may be written as part of a test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
class NewClass implements anInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
   static int newVar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
   public void eventDispatched(AWTEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      //Counting events to see if we get enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
      eventCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
      if( eventCount == 20 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         //got enough events, so pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         OpaqueTest.pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
      else if( tries == 20 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         //tried too many times without getting enough events so fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         OpaqueTest.fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }// eventDispatched()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 }// NewClass class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
//************** End classes defined for the test *******************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
/****************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 Standard Test Machinery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 DO NOT modify anything below -- it's a standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  chunk of code whose purpose is to make user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  interaction uniform, and thereby make it simpler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  to read and understand someone else's test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 ****************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 This is part of the standard test machinery.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 It creates a dialog (with the instructions), and is the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
  for sending text messages to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 To print the instructions, send an array of strings to Sysout.createDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  WithInstructions method.  Put one line of instructions per array entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 To display a message for the tester to see, simply call Sysout.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  with the string to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 This mimics System.out.println but works within the test harness as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  as standalone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
class Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private static TestDialog dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public static void createDialogWithInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        dialog = new TestDialog( new Frame(), "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public static void createDialog( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        dialog = new TestDialog( new Frame(), "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        String[] defInstr = { "Instructions will appear here. ", "" } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        dialog.printInstructions( defInstr );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public static void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public static void println( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        dialog.displayMessage( messageIn );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        System.out.println(messageIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
}// Sysout  class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
  This is part of the standard test machinery.  It provides a place for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
   test instructions to be displayed, and a place for interactive messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
   to the user to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  To have the test instructions displayed, see Sysout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
  To have a message to the user be displayed, see Sysout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
  Do not call anything in this dialog directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
class TestDialog extends Dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    TextArea instructionsText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    TextArea messageText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    int maxStringLength = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public TestDialog( Frame frame, String name )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        super( frame, name );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int scrollBoth = TextArea.SCROLLBARS_BOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        add( "North", instructionsText );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        add("Center", messageText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }// TestDialog()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        //Clear out any current instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        instructionsText.setText( "" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        //Go down array of instruction strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        String printStr, remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        for( int i=0; i < instructions.length; i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            //chop up each into pieces maxSringLength long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            remainingStr = instructions[ i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            while( remainingStr.length() > 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                //if longer than max then chop off first max chars to print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if( remainingStr.length() >= maxStringLength )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    //Try to chop on a word boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    int posOfSpace = remainingStr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        lastIndexOf( ' ', maxStringLength - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    remainingStr = remainingStr.substring( posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                //else just print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    printStr = remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    remainingStr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                instructionsText.append( printStr + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }// while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }// for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }//printInstructions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    //DO NOT call this directly, go through Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public void displayMessage( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        messageText.append( messageIn + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        System.out.println(messageIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
}// TestDialog  class