Path: blob/master/test/jdk/java/awt/List/ListFlickers/ListFlickers.java
41154 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@bug 647169326@summary Moving the vertical scroll bar of List leads flickering.27@author Dmitry Cherepanov area=awt.list28@run main/manual ListFlickers29*/3031import java.awt.*;32import java.awt.event.*;3334public class ListFlickers35{3637//*** test-writer defined static variables go here ***383940private static void init()41{42//*** Create instructions for the user here ***4344String[] instructions =45{46"drag the scrollbar of the list up and down,",47"if the list flickers then the test fails,",48"otherwise it passes."49};50Sysout.createDialog( );51Sysout.printInstructions( instructions );525354Frame f = new Frame();55List list = new List(10, false);56for (int i = 0; i < 100; i++) {57list.add(" item "+i);58}59f.add(list);60f.setBounds(100,100,300,300);61f.setVisible(true);6263}//End init()64656667/*****************************************************68* Standard Test Machinery Section69* DO NOT modify anything in this section -- it's a70* standard chunk of code which has all of the71* synchronisation necessary for the test harness.72* By keeping it the same in all tests, it is easier73* to read and understand someone else's test, as74* well as insuring that all tests behave correctly75* with the test harness.76* There is a section following this for test-defined77* classes78******************************************************/79private static boolean theTestPassed = false;80private static boolean testGeneratedInterrupt = false;81private static String failureMessage = "";8283private static Thread mainThread = null;8485private static int sleepTime = 300000;8687public static void main( String args[] ) throws InterruptedException88{89mainThread = Thread.currentThread();90try91{92init();93}94catch( TestPassedException e )95{96//The test passed, so just return from main and harness will97// interepret this return as a pass98return;99}100//At this point, neither test passed nor test failed has been101// called -- either would have thrown an exception and ended the102// test, so we know we have multiple threads.103104//Test involves other threads, so sleep and wait for them to105// called pass() or fail()106try107{108Thread.sleep( sleepTime );109//Timed out, so fail the test110throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );111}112catch (InterruptedException e)113{114if( ! testGeneratedInterrupt ) throw e;115116//reset flag in case hit this code more than once for some reason (just safety)117testGeneratedInterrupt = false;118if ( theTestPassed == false )119{120throw new RuntimeException( failureMessage );121}122}123124}//main125126public static synchronized void setTimeoutTo( int seconds )127{128sleepTime = seconds * 1000;129}130131public static synchronized void pass()132{133Sysout.println( "The test passed." );134Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );135//first check if this is executing in main thread136if ( mainThread == Thread.currentThread() )137{138//Still in the main thread, so set the flag just for kicks,139// and throw a test passed exception which will be caught140// and end the test.141theTestPassed = true;142throw new TestPassedException();143}144//pass was called from a different thread, so set the flag and interrupt145// the main thead.146theTestPassed = true;147testGeneratedInterrupt = true;148if (mainThread != null){149mainThread.interrupt();150}151}//pass()152153public static synchronized void fail()154{155//test writer didn't specify why test failed, so give generic156fail( "it just plain failed! :-)" );157}158159public static synchronized void fail( String whyFailed )160{161Sysout.println( "The test failed: " + whyFailed );162Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );163//check if this called from main thread164if ( mainThread == Thread.currentThread() )165{166//If main thread, fail now 'cause not sleeping167throw new RuntimeException( whyFailed );168}169theTestPassed = false;170testGeneratedInterrupt = true;171failureMessage = whyFailed;172mainThread.interrupt();173}//fail()174175}// class ManualMainTest176177//This exception is used to exit from any level of call nesting178// when it's determined that the test has passed, and immediately179// end the test.180class TestPassedException extends RuntimeException181{182}183184//*********** End Standard Test Machinery Section **********185186187//************ Begin classes defined for the test ****************188189// make listeners in a class defined here, and instantiate them in init()190191/* Example of a class which may be written as part of a test192class NewClass implements anInterface193{194static int newVar = 0;195196public void eventDispatched(AWTEvent e)197{198//Counting events to see if we get enough199eventCount++;200201if( eventCount == 20 )202{203//got enough events, so pass204205ManualMainTest.pass();206}207else if( tries == 20 )208{209//tried too many times without getting enough events so fail210211ManualMainTest.fail();212}213214}// eventDispatched()215216}// NewClass class217218*/219220221//************** End classes defined for the test *******************222223224225226/****************************************************227Standard Test Machinery228DO NOT modify anything below -- it's a standard229chunk of code whose purpose is to make user230interaction uniform, and thereby make it simpler231to read and understand someone else's test.232****************************************************/233234/**235This is part of the standard test machinery.236It creates a dialog (with the instructions), and is the interface237for sending text messages to the user.238To print the instructions, send an array of strings to Sysout.createDialog239WithInstructions method. Put one line of instructions per array entry.240To display a message for the tester to see, simply call Sysout.println241with the string to be displayed.242This mimics System.out.println but works within the test harness as well243as standalone.244*/245246class Sysout247{248private static TestDialog dialog;249private static boolean numbering = false;250private static int messageNumber = 0;251252public static void createDialogWithInstructions( String[] instructions )253{254dialog = new TestDialog( new Frame(), "Instructions" );255dialog.printInstructions( instructions );256dialog.setVisible(true);257println( "Any messages for the tester will display here." );258}259260public static void createDialog( )261{262dialog = new TestDialog( new Frame(), "Instructions" );263String[] defInstr = { "Instructions will appear here. ", "" } ;264dialog.printInstructions( defInstr );265dialog.setVisible(true);266println( "Any messages for the tester will display here." );267}268269270/* Enables message counting for the tester. */271public static void enableNumbering(boolean enable){272numbering = enable;273}274275public static void printInstructions( String[] instructions )276{277dialog.printInstructions( instructions );278}279280281public static void println( String messageIn )282{283if (numbering) {284messageIn = "" + messageNumber + " " + messageIn;285messageNumber++;286}287dialog.displayMessage( messageIn );288}289290}// Sysout class291292/**293This is part of the standard test machinery. It provides a place for the294test instructions to be displayed, and a place for interactive messages295to the user to be displayed.296To have the test instructions displayed, see Sysout.297To have a message to the user be displayed, see Sysout.298Do not call anything in this dialog directly.299*/300class TestDialog extends Dialog implements ActionListener301{302303TextArea instructionsText;304TextArea messageText;305int maxStringLength = 80;306Panel buttonP = new Panel();307Button passB = new Button( "pass" );308Button failB = new Button( "fail" );309310//DO NOT call this directly, go through Sysout311public TestDialog( Frame frame, String name )312{313super( frame, name );314int scrollBoth = TextArea.SCROLLBARS_BOTH;315instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );316add( "North", instructionsText );317318messageText = new TextArea( "", 5, maxStringLength, scrollBoth );319add("Center", messageText);320321passB = new Button( "pass" );322passB.setActionCommand( "pass" );323passB.addActionListener( this );324buttonP.add( "East", passB );325326failB = new Button( "fail" );327failB.setActionCommand( "fail" );328failB.addActionListener( this );329buttonP.add( "West", failB );330331add( "South", buttonP );332pack();333334setVisible(true);335}// TestDialog()336337//DO NOT call this directly, go through Sysout338public void printInstructions( String[] instructions )339{340//Clear out any current instructions341instructionsText.setText( "" );342343//Go down array of instruction strings344345String printStr, remainingStr;346for( int i=0; i < instructions.length; i++ )347{348//chop up each into pieces maxSringLength long349remainingStr = instructions[ i ];350while( remainingStr.length() > 0 )351{352//if longer than max then chop off first max chars to print353if( remainingStr.length() >= maxStringLength )354{355//Try to chop on a word boundary356int posOfSpace = remainingStr.357lastIndexOf( ' ', maxStringLength - 1 );358359if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;360361printStr = remainingStr.substring( 0, posOfSpace + 1 );362remainingStr = remainingStr.substring( posOfSpace + 1 );363}364//else just print365else366{367printStr = remainingStr;368remainingStr = "";369}370371instructionsText.append( printStr + "\n" );372373}// while374375}// for376377}//printInstructions()378379//DO NOT call this directly, go through Sysout380public void displayMessage( String messageIn )381{382messageText.append( messageIn + "\n" );383System.out.println(messageIn);384}385386//catch presses of the passed and failed buttons.387//simply call the standard pass() or fail() static methods of388//ManualMainTest389public void actionPerformed( ActionEvent e )390{391if( e.getActionCommand() == "pass" )392{393ListFlickers.pass();394}395else396{397ListFlickers.fail();398}399}400401}// TestDialog class402403404