1 /* 2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any 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 int SCROLL_DELAY = 10; // 10ms 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 } 131 132 robot.mouseRelease( MouseEvent.BUTTON1_MASK ); 133 Util.waitForIdle( robot ); 134 } 135 136 void moveMouseToCenterOfTextArea() { 137 Dimension d = textArea.getSize(); 138 Point l = textArea.getLocationOnScreen(); 139 robot.mouseMove( (int)(l.x+d.width*.5), (int)(l.y+d.height*.5) ); 140 } 141 142 void moveMouseBelowTextArea( boolean shift ) { 143 Dimension d = textArea.getSize(); 144 Point l = textArea.getLocationOnScreen(); 145 int x = (int)(l.x+d.width*.5); 146 int y = (int)(l.y+d.height*1.5); 147 if( shift ) y+=15; 148 robot.mouseMove( x, y ); 149 // it is needed to add some small delay on Gnome 150 waitUntilScrollIsPerformed(); 151 } 152 153 void waitUntilScrollIsPerformed() { 154 try { Thread.sleep( SCROLL_DELAY ); } 155 catch( Exception e ) { throw new RuntimeException( e ); } 156 } 157 158 void checkResults() { 159 //try { Thread.sleep( 30*1000 ); } 160 //catch( Exception e ) { throw new RuntimeException( e ); } 161 162 final int currentSelectionEnd = textArea.getSelectionEnd(); 163 164 System.out.println( 165 "TEST: Selection range after test is: ( " 166 + textArea.getSelectionStart() + ", " 167 + currentSelectionEnd + " )" 168 ); 169 170 boolean resultOk = ( currentSelectionEnd == desiredSelectionEnd ); 171 String desiredSelectionEndString = "" + desiredSelectionEnd; 172 173 // On Windows, last empty line is surprisingly not selected. 174 // Even if it's a bug, it's not for this test. 175 // So, we have 2 acceptable results in this case. 176 String toolkitName = Toolkit.getDefaultToolkit().getClass().getName(); 177 if( toolkitName.equals("sun.awt.windows.WToolkit") ) { 178 final int desiredSelectionEnd2 = desiredSelectionEnd-1; // 51 179 resultOk |= ( currentSelectionEnd == desiredSelectionEnd2 ); 180 desiredSelectionEndString += " or " + desiredSelectionEnd2; 181 } 182 183 if( resultOk ) { 184 System.out.println( 185 "TEST: passed: Text is selected to the end" 186 + " (expected selection range end is " 187 + desiredSelectionEndString + ")." 188 ); 189 } else { 190 System.out.println( 191 "TEST: FAILED: Text should be selected to the end" 192 + " (selection range end should be " 193 + desiredSelectionEndString + ")." 194 ); 195 throw new RuntimeException( 196 "TEST: FAILED: Text should be selected to the end, but it is not." 197 ); 198 } 199 } 200 }