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 ) { 188 System.out.println( 189 "TEST: passed: Text is selected to the end" 190 + " (expected selection range end is " 191 + desiredSelectionEndString + ")." 192 ); 193 } else { 194 System.out.println( 195 "TEST: FAILED: Text should be selected to the end" 196 + " (selection range end should be " 197 + desiredSelectionEndString + ")." 198 ); 199 throw new RuntimeException( 200 "TEST: FAILED: Text should be selected to the end, but it is not." 201 ); 202 } 203 } 204 }