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