1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   test
  26   @bug 4118621 8149636
  27   @summary Test the selection scrolling in TextField.
  28   @run applet/manual=yesno ScrollSelectionTest.html
  29 */
  30 
  31 import java.applet.Applet;
  32 import java.awt.Dialog;
  33 import java.awt.Frame;
  34 import java.awt.TextField;
  35 import java.awt.TextArea;
  36 import java.awt.FlowLayout;
  37 
  38 public class ScrollSelectionTest extends Applet
  39  {
  40 
  41    Frame frame = new Frame("ScrollSelectionTest frame");
  42    TextField tf = new TextField(40);
  43 
  44    public void init()
  45     {
  46       tf.setText("abcdefghijklmnopqrstuvwxyz");
  47       frame.add(tf);
  48       frame.setLayout(new FlowLayout());
  49       tf.select(0, 20);
  50 
  51       String[] instructions = {
  52           "INSTRUCTIONS: There are 3 Tests",
  53           "Test1: Text visibility with Scroll ",
  54           "This is a test for a win32 specific problem",
  55           "If you see all the letters from 'a' to 'z' and",
  56           "letters from 'a' to 't' are selected then test passes.",
  57           "You may have to activate the frame to see the selection",
  58           "highlighted (e.g. by clicking on frame's title).",
  59           ".",
  60           "Test2: Flicker with selection scroll",
  61           "Mouse press on te TextField text",
  62           "Move mouse towards left or right with selecting text",
  63           "Move mouse away outside the bounds of TextField",
  64           "No flicker should be observed.",
  65           ".",
  66           "Test3: Over scroll on right",
  67           "Mouse press on te TextField text",
  68           "Move mouse towards Right",
  69           "Move mouse away outside the bounds of TextField",
  70           "Observe TextField text does not get scrolled towards left",
  71           "as there is no need to scroll",
  72       };
  73       Sysout.createDialogWithInstructions( instructions );
  74 
  75     }// init()
  76 
  77    public void start ()
  78     {
  79       setSize (300, 500);
  80       setVisible(true);
  81 
  82       frame.setVisible(true);
  83       frame.setBounds (400, 0, 400, 300);
  84 
  85     }// start()
  86 
  87  }// class ScrollSelectionTest
  88 
  89 /****************************************************
  90  Standard Test Machinery
  91  DO NOT modify anything below -- it's a standard
  92   chunk of code whose purpose is to make user
  93   interaction uniform, and thereby make it simpler
  94   to read and understand someone else's test.
  95  ****************************************************/
  96 
  97 /**
  98  This is part of the standard test machinery.
  99  It creates a dialog (with the instructions), and is the interface
 100   for sending text messages to the user.
 101  To print the instructions, send an array of strings to Sysout.createDialog
 102   WithInstructions method.  Put one line of instructions per array entry.
 103  To display a message for the tester to see, simply call Sysout.println
 104   with the string to be displayed.
 105  This mimics System.out.println but works within the test harness as well
 106   as standalone.
 107  */
 108 
 109 class Sysout
 110  {
 111    private static TestDialog dialog;
 112 
 113    public static void createDialogWithInstructions( String[] instructions )
 114     {
 115       dialog = new TestDialog( new Frame(), "Instructions" );
 116       dialog.printInstructions( instructions );
 117       dialog.show();
 118       println( "Any messages for the tester will display here." );
 119     }
 120 
 121    public static void createDialog( )
 122     {
 123       dialog = new TestDialog( new Frame(), "Instructions" );
 124       String[] defInstr = { "Instructions will appear here. ", "" } ;
 125       dialog.printInstructions( defInstr );
 126       dialog.show();
 127       println( "Any messages for the tester will display here." );
 128     }
 129 
 130 
 131    public static void printInstructions( String[] instructions )
 132     {
 133       dialog.printInstructions( instructions );
 134     }
 135 
 136 
 137    public static void println( String messageIn )
 138     {
 139       dialog.displayMessage( messageIn );
 140     }
 141 
 142  }// Sysout  class
 143 
 144 /**
 145   This is part of the standard test machinery.  It provides a place for the
 146    test instructions to be displayed, and a place for interactive messages
 147    to the user to be displayed.
 148   To have the test instructions displayed, see Sysout.
 149   To have a message to the user be displayed, see Sysout.
 150   Do not call anything in this dialog directly.
 151   */
 152 class TestDialog extends Dialog
 153  {
 154 
 155    TextArea instructionsText;
 156    TextArea messageText;
 157    int maxStringLength = 80;
 158 
 159    //DO NOT call this directly, go through Sysout
 160    public TestDialog( Frame frame, String name )
 161     {
 162       super( frame, name );
 163       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 164       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 165       add( "North", instructionsText );
 166 
 167       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 168       add("South", messageText);
 169 
 170       pack();
 171 
 172       show();
 173     }// TestDialog()
 174 
 175    //DO NOT call this directly, go through Sysout
 176    public void printInstructions( String[] instructions )
 177     {
 178       //Clear out any current instructions
 179       instructionsText.setText( "" );
 180 
 181       //Go down array of instruction strings
 182 
 183       String printStr, remainingStr;
 184       for( int i=0; i < instructions.length; i++ )
 185        {
 186          //chop up each into pieces maxSringLength long
 187          remainingStr = instructions[ i ];
 188          while( remainingStr.length() > 0 )
 189           {
 190             //if longer than max then chop off first max chars to print
 191             if( remainingStr.length() >= maxStringLength )
 192              {
 193                //Try to chop on a word boundary
 194                int posOfSpace = remainingStr.
 195                   lastIndexOf( ' ', maxStringLength - 1 );
 196 
 197                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 198 
 199                printStr = remainingStr.substring( 0, posOfSpace + 1 );
 200                remainingStr = remainingStr.substring( posOfSpace + 1 );
 201              }
 202             //else just print
 203             else
 204              {
 205                printStr = remainingStr;
 206                remainingStr = "";
 207              }
 208 
 209             instructionsText.append( printStr + "\n" );
 210 
 211           }// while
 212 
 213        }// for
 214 
 215     }//printInstructions()
 216 
 217    //DO NOT call this directly, go through Sysout
 218    public void displayMessage( String messageIn )
 219     {
 220       messageText.append( messageIn + "\n" );
 221     }
 222 
 223  }// TestDialog  class