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 ) { 198 System.out.println( 199 "TEST: passed: Text is selected to the end" 200 + " (expected selection range end is " 201 + desiredSelectionEndString + ")." 202 ); 203 } else { 204 System.out.println( 205 "TEST: FAILED: Text should be selected to the end" 206 + " (selection range end should be " 207 + desiredSelectionEndString + ")." 208 ); 209 throw new RuntimeException( 210 "TEST: FAILED: Text should be selected to the end, but it is not." 211 ); 212 } 213 } 214 }