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