1 /*
   2  * Copyright (c) 2015, 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 8060137
  27  @library ../../regtesthelpers
  28  @build Util
  29  @summary Test TextField setText API
  30  @run main TextFieldEditing
  31  */
  32 
  33 import java.awt.Frame;
  34 import java.awt.Robot;
  35 import java.awt.TextField;
  36 import java.awt.AWTException;
  37 import java.awt.event.KeyEvent;
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class TextFieldEditing {
  41 
  42     final static Robot robot = Util.createRobot();
  43     private int testFailCount;
  44     private boolean isTestFail;
  45     private StringBuilder testFailMessage;
  46 
  47     private Frame mainFrame;
  48     private TextField textField;
  49 
  50     private TextFieldEditing() {
  51         testFailMessage = new StringBuilder();
  52         mainFrame = new Frame();
  53         mainFrame.setSize(200, 200);
  54 
  55         textField = new TextField();
  56         mainFrame.add(textField);
  57         mainFrame.setVisible(true);
  58     }
  59 
  60     private void dispose() {
  61         if (mainFrame != null) {
  62             mainFrame.dispose();
  63         }
  64     }
  65 
  66     public static void main(String[] s) {
  67         TextFieldEditing textField = new TextFieldEditing();
  68         textField.testSetText();
  69         textField.checkFailures();
  70         textField.dispose();
  71     }
  72 
  73     private void testSetText() {
  74         textField.setText(null);
  75         textField.requestFocus();
  76         Util.clickOnComp(textField, robot);
  77         Util.waitForIdle(robot);
  78         robot.keyPress(KeyEvent.VK_A);
  79         robot.delay(5);
  80         robot.keyRelease(KeyEvent.VK_A);
  81         Util.waitForIdle(robot);
  82         textField.setText(null);
  83         checkTest("");
  84     }
  85 
  86     private void checkTest(String str) {
  87         if (str != null && !str.equals(textField.getText())) {
  88             testFailMessage.append("TestFail line : ");
  89             testFailMessage.append(Thread.currentThread().getStackTrace()[2].
  90                     getLineNumber());
  91             testFailMessage.append(" TextField string : \"");
  92             testFailMessage.append(textField.getText());
  93             testFailMessage.append("\" does not match expected string : \"");
  94             testFailMessage.append(str).append("\"");
  95             testFailMessage.append(System.getProperty("line.separator"));
  96             testFailCount++;
  97             isTestFail = true;
  98         }
  99     }
 100 
 101     private void checkFailures() {
 102         if (isTestFail) {
 103             testFailMessage.insert(0, "Test Fail count : " + testFailCount
 104                     + System.getProperty("line.separator"));
 105             dispose();
 106             throw new RuntimeException(testFailMessage.toString());
 107         }
 108     }
 109 }