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 8149636
  28  * @summary TextArea over scrolls to right when selecting text towards right.
  29  * @requires os.family == "windows"
  30  * @run main OverScrollTest
  31  */
  32 
  33 import java.awt.Frame;
  34 import java.awt.FlowLayout;
  35 import java.awt.Point;
  36 import java.awt.Rectangle;
  37 import java.awt.Robot;
  38 import java.awt.TextArea;
  39 import java.awt.event.InputEvent;
  40 
  41 public class OverScrollTest {
  42     Frame mainFrame;
  43     TextArea textArea;
  44     Robot robot;
  45 
  46     OverScrollTest() {
  47         try {
  48             robot = new Robot();
  49         } catch (Exception ex) {
  50             throw new RuntimeException(ex.getMessage());
  51         }
  52 
  53         mainFrame = new Frame();
  54         mainFrame.setSize(400, 200);
  55         mainFrame.setLocation(200, 200);
  56         mainFrame.setLayout(new FlowLayout());
  57 
  58         textArea = new TextArea(2, 10);
  59         textArea.setSize(300, 100);
  60         textArea.setText("123456 789123");
  61         mainFrame.add(textArea);
  62         mainFrame.setVisible(true);
  63         textArea.requestFocusInWindow();
  64     }
  65 
  66     public void dispose() {
  67         if (mainFrame != null) {
  68             mainFrame.dispose();
  69         }
  70     }
  71 
  72     public void performTest() {
  73         Point loc = textArea.getLocationOnScreen();
  74         Rectangle textAreaBounds = new Rectangle();
  75         textArea.getBounds(textAreaBounds);
  76 
  77         // Move mouse at center in first row of TextArea.
  78         robot.mouseMove(loc.x + textAreaBounds.width / 2, loc.y + 5);
  79 
  80         // Perform selection by scrolling to right from end of char sequence.
  81         robot.mousePress(InputEvent.BUTTON1_MASK);
  82         for (int i = 0; i < textAreaBounds.width; i += 15) {
  83             robot.mouseMove(i + loc.x + textAreaBounds.width / 2, loc.y + 5);
  84             robot.delay(10);
  85         }
  86         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  87         robot.waitForIdle();
  88 
  89         // Perform double click on beginning word of TextArea
  90         robot.mouseMove(loc.x + 5, loc.y + 5);
  91         robot.mousePress(InputEvent.BUTTON1_MASK);
  92         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  93         robot.delay(100);
  94         robot.mousePress(InputEvent.BUTTON1_MASK);
  95         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  96         robot.waitForIdle();
  97 
  98         dispose();
  99         if (!textArea.getSelectedText().contains("123456")) {
 100             throw new RuntimeException ("TextArea over scrolled towards right. "
 101                 + "Selected text should contain: '123456' "
 102                 + "Actual selected test: '" + textArea.getSelectedText() + "'");
 103         }
 104     }
 105 
 106     public static void main(String argv[]) throws RuntimeException {
 107         OverScrollTest test = new OverScrollTest();
 108         test.performTest();
 109     }
 110 }