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         mainFrame = new Frame();
  47         mainFrame.setSize(400, 200);
  48         mainFrame.setLocation(200, 200);
  49         mainFrame.setLayout(new FlowLayout());
  50 
  51         textField = new TextField(8);
  52         textField.setSize(300, 100);
  53         textField.setText("123456 789123");
  54         mainFrame.add(textField);
  55         mainFrame.setVisible(true);
  56         textField.requestFocusInWindow();
  57 
  58         try {
  59             robot = new Robot();
  60             robot.setAutoWaitForIdle(true);
  61         } catch (Exception ex) {
  62             dispose();
  63             System.exit(0);
  64             throw new RuntimeException("Robot Creation Failed");
  65         }
  66     }
  67 
  68     public void dispose() {
  69         if (mainFrame != null) {
  70             mainFrame.dispose();
  71         }
  72     }
  73 
  74     public void performTest() {
  75         Point loc = textField.getLocationOnScreen();
  76         Rectangle textFieldBounds = new Rectangle();
  77         textField.getBounds(textFieldBounds);
  78 
  79         // Move mouse at center in first row of TextField.
  80         robot.mouseMove(loc.x + textFieldBounds.width / 2, loc.y + 5);
  81 
  82         // Perform selection by scrolling to right from end of char sequence.
  83         robot.mousePress(InputEvent.BUTTON1_MASK);
  84         for (int i = 0; i < textFieldBounds.width; i += 15) {
  85             robot.mouseMove(i + loc.x + textFieldBounds.width / 2, loc.y + 5);
  86             robot.delay(10);
  87         }
  88         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  89         robot.waitForIdle();
  90 
  91         // Perform double click on beginning word of TextField
  92         robot.mouseMove(loc.x + 5, loc.y + 5);
  93         robot.mousePress(InputEvent.BUTTON1_MASK);
  94         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  95         robot.delay(100);
  96         robot.mousePress(InputEvent.BUTTON1_MASK);
  97         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  98         robot.delay(100);
  99         robot.waitForIdle();
 100 
 101         if (!textField.getSelectedText().contains("123456")) {
 102             dispose();
 103             throw new RuntimeException ("TextField over scrolled towards right"
 104                 + "Expected selected text should contain: '123456'");
 105         }
 106     }
 107 
 108     public static void main(String argv[]) throws RuntimeException {
 109         OverScrollTest test = new OverScrollTest();
 110         test.performTest();
 111         test.dispose();
 112     }
 113 }