< prev index next >

test/java/awt/TextArea/UsingWithMouse/SelectionAutoscrollTest.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 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 6497109
  27   @summary TextArea must have selection expanding, and also be autoscrolled, if mouse is dragged from inside.


  28   @author Konstantin Voloshin: area=TextArea
  29   @run applet SelectionAutoscrollTest.html
  30 */
  31 
  32 /**
  33  * SelectionAutoscrollTest.java
  34  *
  35  * summary: TextArea should be auto-scrolled and text should be selected to
  36  *   the end, if mouse is dragged from inside box-for-text to outside it, and
  37  *   is hold pressed there.
  38  */
  39 
  40 
  41 import java.applet.Applet;
  42 import java.awt.Frame;
  43 import java.awt.Panel;
  44 import java.awt.GridLayout;
  45 import java.awt.TextArea;
  46 
  47 import java.awt.Point;
  48 import java.awt.Dimension;
  49 import java.awt.event.MouseEvent;
  50 import java.awt.Robot;
  51 import java.awt.Toolkit;
  52 import test.java.awt.regtesthelpers.Util;
  53 
  54 
  55 public class SelectionAutoscrollTest extends Applet {
  56     TextArea textArea;
  57     Robot robot;
  58     final int desiredSelectionEnd = ('z'-'a'+1)*2;  // 52
  59     final static int SCROLL_DELAY = 10; // ms
  60 
  61     public void start () {
  62         createObjects();
  63         manipulateMouse();
  64         checkResults();


  65     }
  66 
  67     void createObjects() {
  68         textArea = new TextArea( bigString() );
  69         robot = Util.createRobot();
  70 
  71         Panel panel = new Panel();
  72         panel.setLayout( new GridLayout(3,3) );
  73 
  74         for( int y=0; y<3; ++y ) {
  75             for( int x=0; x<3; ++x ) {
  76                 if( x==1 && y==1 ) {
  77                     panel.add( textArea );
  78                 } else {
  79                     panel.add( new Panel() );
  80                 }
  81             }
  82         }
  83 
  84         Frame frame = new Frame( "TextArea cursor icon test" );
  85         frame.setSize( 300, 300 );
  86         frame.add( panel );
  87         frame.setVisible( true );
  88     }
  89 
  90     static String bigString() {
  91         String s = "";
  92         for( char c='a'; c<='z'; ++c ) {
  93             s += c+"\n";
  94         }
  95         return s;
  96     }
  97 
  98     void manipulateMouse() {
  99         moveMouseToCenterOfTextArea();
 100         Util.waitForIdle( robot );
 101 
 102         robot.mousePress( MouseEvent.BUTTON1_MASK );
 103         Util.waitForIdle( robot );
 104 
 105         for( int tremble=0; tremble < desiredSelectionEnd; ++tremble ) {
 106             // Mouse is moved repeatedly here (with conservatively chosen
 107             // ammount of times), to give some time/chance for TextArea to
 108             // autoscroll and for text-selection to expand to the end.
 109             // This is because:
 110             // - On Windows,
 111             //   autoscrolling and selection-expansion happens only once per
 112             //   each mouse-dragged event received, and only for some ammount,
 113             //   not to the end. So, we have to drag mouse repeatedly.
 114             // - on X,
 115             //   only 1 mouse-dragged event is required for autoscrolling/
 116             //   selection-expanding to commence. Once commenced, it will
 117             //   continue to the end of text (provided that mouse-button is
 118             //   hold pressed), but it may take hardly predictable ammount of
 119             //   time. However, repeatedly dragging mouse seems perfectly help
 120             //   here, instead of having to use 'Thread.sleep( ??? )'.
 121             // Note: It's required here to move mouse 2 times to receive the
 122             //   1-st drag-event. After 1-st movement, only mouse-exited event
 123             //   will be generated. If mouse was released after first movement
 124             //   here, we would even get mouse-clicked event (at least for now,
 125             //   and this is probably a bug). But, starting with 2nd iteration,
 126             //   all events received will be mouse-dragged events.
 127 
 128             moveMouseBelowTextArea( tremble%2!=0 );
 129             Util.waitForIdle( robot );
 130             // it is needed to add some small delay on Gnome
 131             waitUntilScrollIsPerformed(robot);
 132         }
 133 
 134         robot.mouseRelease( MouseEvent.BUTTON1_MASK );
 135         Util.waitForIdle( robot );
 136     }
 137 
 138     void moveMouseToCenterOfTextArea() {
 139         Dimension d = textArea.getSize();
 140         Point l = textArea.getLocationOnScreen();
 141         robot.mouseMove( (int)(l.x+d.width*.5), (int)(l.y+d.height*.5) );

 142     }
 143 
 144     void moveMouseBelowTextArea( boolean shift ) {
 145         Dimension d = textArea.getSize();
 146         Point l = textArea.getLocationOnScreen();
 147         int x = (int)(l.x+d.width*.5);
 148         int y = (int)(l.y+d.height*1.5);
 149         if( shift ) y+=15;
 150         robot.mouseMove( x, y );











 151     }
 152 
 153     void waitUntilScrollIsPerformed(Robot robot) {
 154         try {
 155             Thread.sleep( SCROLL_DELAY );
 156         }
 157         catch( Exception e ) {
 158             throw new RuntimeException( e );
 159         }
 160     }
 161 
 162     void checkResults() {
 163         //try { Thread.sleep( 30*1000 ); }
 164         //catch( Exception e ) { throw new RuntimeException( e ); }
 165 
 166         final int currentSelectionEnd = textArea.getSelectionEnd();
 167 
 168         System.out.println(
 169             "TEST: Selection range after test is: ( "
 170             + textArea.getSelectionStart() + ", "
 171             + currentSelectionEnd + " )"
 172         );
 173 
 174         boolean resultOk = ( currentSelectionEnd == desiredSelectionEnd );
 175         String desiredSelectionEndString = "" + desiredSelectionEnd;
 176 
 177         // On Windows, last empty line is surprisingly not selected.
 178         // Even if it's a bug, it's not for this test.
 179         // So, we have 2 acceptable results in this case.
 180         String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
 181         if( toolkitName.equals("sun.awt.windows.WToolkit") ) {
 182             final int desiredSelectionEnd2 = desiredSelectionEnd-1;  // 51
 183             resultOk |= ( currentSelectionEnd == desiredSelectionEnd2 );
 184             desiredSelectionEndString += " or " + desiredSelectionEnd2;
 185         }
 186 
 187         if( resultOk ) {
   1 /*
   2  * Copyright (c) 2007, 2016, 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 6497109 6734341
  27   @summary TextArea must have selection expanding, and also be autoscrolled, if mouse is dragged from inside.
  28   @library ../../regtesthelpers
  29   @build Util
  30   @author Konstantin Voloshin: area=TextArea
  31   @run main SelectionAutoscrollTest
  32 */
  33 
  34 /**
  35  * SelectionAutoscrollTest.java
  36  *
  37  * summary: TextArea should be auto-scrolled and text should be selected to
  38  *   the end, if mouse is dragged from inside box-for-text to outside it, and
  39  *   is hold pressed there.
  40  */
  41 
  42 

  43 import java.awt.Frame;
  44 import java.awt.Panel;
  45 import java.awt.GridLayout;
  46 import java.awt.TextArea;

  47 import java.awt.Point;
  48 import java.awt.Dimension;
  49 import java.awt.event.MouseEvent;
  50 import java.awt.Robot;
  51 import java.awt.Toolkit;
  52 import test.java.awt.regtesthelpers.Util;
  53 
  54 
  55 public class SelectionAutoscrollTest {
  56     TextArea textArea;
  57     Robot robot;
  58     final int desiredSelectionEnd = ('z'-'a'+1)*2;  // 52
  59     final static int SCROLL_DELAY = 10; // ms
  60 
  61     public static void main(String[] args) {
  62         SelectionAutoscrollTest selectionAutoscrollTest
  63                 = new SelectionAutoscrollTest();
  64         selectionAutoscrollTest.createObjects();
  65         selectionAutoscrollTest.manipulateMouse();
  66         selectionAutoscrollTest.checkResults();
  67     }
  68 
  69     void createObjects() {
  70         textArea = new TextArea( bigString() );
  71         robot = Util.createRobot();
  72 
  73         Panel panel = new Panel();
  74         panel.setLayout( new GridLayout(3,3) );
  75 
  76         for( int y=0; y<3; ++y ) {
  77             for( int x=0; x<3; ++x ) {
  78                 if( x==1 && y==1 ) {
  79                     panel.add( textArea );
  80                 } else {
  81                     panel.add( new Panel() );
  82                 }
  83             }
  84         }
  85 
  86         Frame frame = new Frame( "TextArea cursor icon test" );
  87         frame.setSize( 300, 300 );
  88         frame.add( panel );
  89         frame.setVisible( true );
  90     }
  91 
  92     static String bigString() {
  93         String s = "";
  94         for( char c='a'; c<='z'; ++c ) {
  95             s += c+"\n";
  96         }
  97         return s;
  98     }
  99 
 100     void manipulateMouse() {
 101         moveMouseToCenterOfTextArea();
 102         Util.waitForIdle( robot );
 103 
 104         robot.mousePress( MouseEvent.BUTTON1_MASK );
 105         Util.waitForIdle( robot );
 106 
 107         for( int tremble=0; tremble < 10; ++tremble ) {
 108             // Mouse is moved repeatedly here (with conservatively chosen
 109             // ammount of times), to give some time/chance for TextArea to
 110             // autoscroll and for text-selection to expand to the end.
 111             // This is because:
 112             // - On Windows,
 113             //   autoscrolling and selection-expansion happens only once per
 114             //   each mouse-dragged event received, and only for some ammount,
 115             //   not to the end. So, we have to drag mouse repeatedly.
 116             // - on X,
 117             //   only 1 mouse-dragged event is required for autoscrolling/
 118             //   selection-expanding to commence. Once commenced, it will
 119             //   continue to the end of text (provided that mouse-button is
 120             //   hold pressed), but it may take hardly predictable ammount of
 121             //   time. However, repeatedly dragging mouse seems perfectly help
 122             //   here, instead of having to use 'Thread.sleep( ??? )'.
 123             // Note: It's required here to move mouse 2 times to receive the
 124             //   1-st drag-event. After 1-st movement, only mouse-exited event
 125             //   will be generated. If mouse was released after first movement
 126             //   here, we would even get mouse-clicked event (at least for now,
 127             //   and this is probably a bug). But, starting with 2nd iteration,
 128             //   all events received will be mouse-dragged events.
 129 
 130             moveMouseBelowTextArea( tremble );
 131             Util.waitForIdle( robot );
 132             // it is needed to add some small delay on Gnome
 133             waitUntilScrollIsPerformed(robot);
 134         }
 135 
 136         robot.mouseRelease( MouseEvent.BUTTON1_MASK );
 137         Util.waitForIdle( robot );
 138     }
 139 
 140     void moveMouseToCenterOfTextArea() {
 141         Dimension d = textArea.getSize();
 142         Point l = textArea.getLocationOnScreen();
 143         Util.mouseMove(robot, l, new Point((int) (l.x + d.width * .5),
 144                 (int) (l.y + d.height * .5)));
 145     }
 146 
 147     void moveMouseBelowTextArea(int tremble) {
 148         Dimension d = textArea.getSize();
 149         Point l = textArea.getLocationOnScreen();
 150         Point p1;
 151         if (tremble == 0) {
 152             p1 = new Point((int) (l.x + d.width * .5),
 153                     (int) (l.y + d.height * 0.5));
 154         } else {
 155             p1 = new Point((int) (l.x + d.width * .5),
 156                     (int) (l.y + d.height * 1.5));
 157         }
 158         Point p2 = new Point((int) (l.x + d.width * .5),
 159                 (int) (l.y + d.height * 1.5) + 15);
 160         if (tremble % 2 == 0) {
 161             Util.mouseMove(robot, p1, p2);
 162         } else {
 163             Util.mouseMove(robot, p2, p1);
 164         }
 165     }
 166 
 167     void waitUntilScrollIsPerformed(Robot robot) {
 168         try {
 169             Thread.sleep( SCROLL_DELAY );
 170         }
 171         catch( Exception e ) {
 172             throw new RuntimeException( e );
 173         }
 174     }
 175 
 176     void checkResults() {



 177         final int currentSelectionEnd = textArea.getSelectionEnd();

 178         System.out.println(
 179                 "TEST: Selection range after test is: ( "
 180                 + textArea.getSelectionStart() + ", "
 181                 + currentSelectionEnd + " )"
 182         );
 183 
 184         boolean resultOk = ( currentSelectionEnd == desiredSelectionEnd );
 185         String desiredSelectionEndString = "" + desiredSelectionEnd;
 186 
 187         // On Windows, last empty line is surprisingly not selected.
 188         // Even if it's a bug, it's not for this test.
 189         // So, we have 2 acceptable results in this case.
 190         String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
 191         if( toolkitName.equals("sun.awt.windows.WToolkit") ) {
 192             final int desiredSelectionEnd2 = desiredSelectionEnd-1;  // 51
 193             resultOk |= ( currentSelectionEnd == desiredSelectionEnd2 );
 194             desiredSelectionEndString += " or " + desiredSelectionEnd2;
 195         }
 196 
 197         if( resultOk ) {
< prev index next >