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