test/java/awt/TextField/SelectionInvisibleTest/SelectionInvisibleTest.java

Print this page




  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.Toolkit;
  31 import java.awt.event.InputEvent;
  32 import sun.awt.SunToolkit;
  33 
  34 /**
  35  * @test
  36  * @bug 8036110
  37  * @author Alexander Scherbatiy
  38  * @summary In TextField can only select text visible or to the left
  39  * @run main SelectionInvisibleTest
  40  */
  41 
  42 public class SelectionInvisibleTest {
  43 
  44     private static final String TEXT = "One Two Three Four Five Six Seven Eight Nine ";
  45     private static final String LAST_WORD = "Ten";
  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49         Frame frame = new Frame();
  50         frame.setSize(300, 200);
  51         TextField textField = new TextField(TEXT + LAST_WORD, 30);
  52         Panel panel = new Panel(new FlowLayout());
  53         panel.add(textField);
  54         frame.add(panel);
  55         frame.setVisible(true);
  56 
  57         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  58         toolkit.realSync();
  59 
  60         Robot robot = new Robot();
  61         robot.setAutoDelay(50);
  62 


  63         Point point = textField.getLocationOnScreen();
  64         int x = point.x + textField.getWidth() / 2;
  65         int y = point.y + textField.getHeight() / 2;
  66         robot.mouseMove(x, y);
  67         robot.mousePress(InputEvent.BUTTON1_MASK);
  68         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  69         toolkit.realSync();
  70 
  71         robot.mousePress(InputEvent.BUTTON1_MASK);
  72         int N = 10;
  73         int dx = textField.getWidth() / N;
  74         for (int i = 0; i < N; i++) {
  75             x += dx;
  76             robot.mouseMove(x, y);
  77         }
  78         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  79         toolkit.realSync();
  80 
  81         if (!textField.getSelectedText().endsWith(LAST_WORD)) {
  82             throw new RuntimeException("Last word is not selected!");
  83         }
  84     }
  85 }


  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  * @bug 8036110
  35  * @author Alexander Scherbatiy
  36  * @summary In TextField can only select text visible or to the left
  37  * @run main SelectionInvisibleTest
  38  */
  39 
  40 public class SelectionInvisibleTest {
  41 
  42     private static final String TEXT = "One Two Three Four Five Six Seven Eight Nine ";
  43     private static final String LAST_WORD = "Ten";
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         Frame frame = new Frame();
  48         frame.setSize(300, 200);
  49         TextField textField = new TextField(TEXT + LAST_WORD, 30);
  50         Panel panel = new Panel(new FlowLayout());
  51         panel.add(textField);
  52         frame.add(panel);
  53         frame.setVisible(true);
  54 



  55         Robot robot = new Robot();
  56         robot.setAutoDelay(50);
  57 
  58         robot.waitForIdle();
  59 
  60         Point point = textField.getLocationOnScreen();
  61         int x = point.x + textField.getWidth() / 2;
  62         int y = point.y + textField.getHeight() / 2;
  63         robot.mouseMove(x, y);
  64         robot.mousePress(InputEvent.BUTTON1_MASK);
  65         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  66         robot.waitForIdle();
  67 
  68         robot.mousePress(InputEvent.BUTTON1_MASK);
  69         int N = 10;
  70         int dx = textField.getWidth() / N;
  71         for (int i = 0; i < N; i++) {
  72             x += dx;
  73             robot.mouseMove(x, y);
  74         }
  75         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  76         robot.waitForIdle();
  77 
  78         if (!textField.getSelectedText().endsWith(LAST_WORD)) {
  79             throw new RuntimeException("Last word is not selected!");
  80         }
  81     }
  82 }