1 /* 2 * Copyright (c) 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 5003402 8151588 27 @summary TextArea must scroll automatically when calling append and select, 28 even when not in focus. 29 @run main AutoScrollOnSelectAndAppend 30 */ 31 32 import java.awt.Button; 33 import java.awt.FlowLayout; 34 import java.awt.Frame; 35 import java.awt.Point; 36 import java.awt.Robot; 37 import java.awt.TextArea; 38 import java.awt.event.InputEvent; 39 import java.awt.event.MouseAdapter; 40 import java.awt.event.MouseEvent; 41 42 public class AutoScrollOnSelectAndAppend { 43 44 Frame frame; 45 TextArea textArea; 46 Button buttonHoldFocus; 47 Robot robot; 48 int test; 49 int selectScrollPos1; 50 int selectScrollPos2; 51 String selectionText; 52 53 public void composeTextArea() { 54 String filler = ""; 55 // Add 10 rows of text for first selection auto scroll test. 56 for (int i = 0; i < 2; i++) { 57 for (int j = 0; j < 5; j++) { 58 filler = filler + i + i + "\n"; 59 } 60 } 61 selectScrollPos1 = filler.length(); 62 String text = filler + "FirstScroll\n"; 63 64 // Add 10 more rows of text for second selection auto scroll test. 65 filler = ""; 66 for (int i = 2; i < 4; i++) { 67 for (int j = 0; j < 5; j++) { 68 filler = filler + i + i + "\n"; 69 } 70 } 71 text = text + filler; 72 selectScrollPos2 = text.length(); 73 text = text + "SecondScroll\n"; 74 75 // Add 10 more rows of text for append text auto scroll test. 76 filler = ""; 77 for (int i = 4; i < 6; i++) { 78 for (int j = 0; j < 5; j++) { 79 filler = filler + i + i + "\n"; 80 } 81 } 82 text = text + filler; 83 textArea.setText(text); 84 85 textArea.addMouseListener(new MouseAdapter() { 86 @Override 87 public void mouseClicked(MouseEvent e) { 88 if (e.getClickCount() % 2 == 0) { 89 if (!(textArea.getSelectedText().contains(selectionText))) { 90 dispose(); 91 throw new RuntimeException("Test No: " + test + 92 ": TextArea is not auto scrolled to show the" + 93 " select/append text."); 94 } 95 } 96 } 97 }); 98 } 99 100 public AutoScrollOnSelectAndAppend() { 101 try { 102 robot = new Robot(); 103 } catch (Exception ex) { 104 throw new RuntimeException("Robot Creation Failed."); 105 } 106 frame = new Frame(); 107 frame.setSize(200, 200); 108 frame.setLayout(new FlowLayout()); 109 110 textArea = new TextArea(5, 20); 111 composeTextArea(); 112 frame.add(textArea); 113 114 buttonHoldFocus = new Button("HoldFocus"); 115 frame.add(buttonHoldFocus); 116 117 frame.setVisible(true); 118 robot.waitForIdle(); 119 120 // Move mouse cursor on first row of text area. 121 Point loc = textArea.getLocationOnScreen(); 122 robot.mouseMove(loc.x + 8, loc.y + 8); 123 robot.waitForIdle(); 124 } 125 126 public void doubleClick() { 127 // Delay to make sure auto scroll is finished. 128 robot.waitForIdle(); 129 robot.delay(500); 130 robot.mousePress(InputEvent.BUTTON1_MASK); 131 robot.mouseRelease(InputEvent.BUTTON1_MASK); 132 robot.delay(100); 133 robot.mousePress(InputEvent.BUTTON1_MASK); 134 robot.mouseRelease(InputEvent.BUTTON1_MASK); 135 robot.waitForIdle(); 136 } 137 138 public void setFocusOnButton() { 139 buttonHoldFocus.requestFocusInWindow(); 140 robot.waitForIdle(); 141 } 142 143 public void selectAutoScrollTest1() { 144 test = 1; 145 setFocusOnButton(); 146 textArea.select(selectScrollPos1, selectScrollPos1); 147 selectionText = "11"; 148 doubleClick(); 149 } 150 151 public void selectAutoScrollTest2() { 152 test = 2; 153 setFocusOnButton(); 154 textArea.select(selectScrollPos2, selectScrollPos2); 155 selectionText = "33"; 156 doubleClick(); 157 } 158 159 public void appendAutoScrollTest() { 160 test = 3; 161 setFocusOnButton(); 162 selectionText = "55"; 163 textArea.append("appendScroll"); 164 doubleClick(); 165 } 166 167 public void dispose() { 168 frame.dispose(); 169 } 170 171 public static void main(String[] args) { 172 AutoScrollOnSelectAndAppend test = new AutoScrollOnSelectAndAppend(); 173 test.selectAutoScrollTest1(); 174 test.selectAutoScrollTest2(); 175 test.appendAutoScrollTest(); 176 test.dispose(); 177 } 178 }