1 /*
   2  * Copyright (c) 2014, 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 import java.awt.FlowLayout;
  25 import java.awt.Frame;
  26 import java.awt.Panel;
  27 import java.awt.Point;
  28 import java.awt.Robot;
  29 import java.awt.TextField;
  30 import java.awt.event.InputEvent;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 8036110
  36  * @author Alexander Scherbatiy
  37  * @summary In TextField can only select text visible or to the left
  38  * @run main SelectionInvisibleTest
  39  */
  40 
  41 public class SelectionInvisibleTest {
  42 
  43     private static final String TEXT = "One Two Three Four Five Six Seven Eight Nine ";
  44     private static final String LAST_WORD = "Ten";
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         Frame frame = new Frame();
  49         frame.setSize(300, 200);
  50         TextField textField = new TextField(TEXT + LAST_WORD, 30);
  51         Panel panel = new Panel(new FlowLayout());
  52         panel.add(textField);
  53         frame.add(panel);
  54         frame.setVisible(true);
  55 
  56         Robot robot = new Robot();
  57         robot.setAutoDelay(50);
  58 
  59         robot.waitForIdle();
  60 
  61         Point point = textField.getLocationOnScreen();
  62         int x = point.x + textField.getWidth() / 2;
  63         int y = point.y + textField.getHeight() / 2;
  64         robot.mouseMove(x, y);
  65         robot.mousePress(InputEvent.BUTTON1_MASK);
  66         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  67         robot.waitForIdle();
  68 
  69         robot.mousePress(InputEvent.BUTTON1_MASK);
  70         int N = 10;
  71         int dx = textField.getWidth() / N;
  72         for (int i = 0; i < N; i++) {
  73             x += dx;
  74             robot.mouseMove(x, y);
  75         }
  76         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  77         robot.waitForIdle();
  78 
  79         if (!textField.getSelectedText().endsWith(LAST_WORD)) {
  80             throw new RuntimeException("Last word is not selected!");
  81         }
  82     }
  83 }