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 8040322 8060137 28 @library ../../regtesthelpers 29 @build Util 30 @summary Test TextArea APIs replaceRange, insert, append & setText 31 @run main TextAreaEditing 32 */ 33 34 import java.awt.Frame; 35 import java.awt.Robot; 36 import java.awt.TextArea; 37 import java.awt.AWTException; 38 import java.awt.event.KeyEvent; 39 import test.java.awt.regtesthelpers.Util; 40 41 public class TextAreaEditing { 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 TextArea textArea; 50 51 private TextAreaEditing() { 52 testFailMessage = new StringBuilder(); 53 mainFrame = new Frame(); 54 mainFrame.setSize(200, 200); 55 56 textArea = new TextArea(); 57 mainFrame.add(textArea); 58 mainFrame.setVisible(true); 59 } 60 61 private void dispose() { 62 if (mainFrame != null) { 63 mainFrame.dispose(); 64 } 65 } 66 67 public static void main(String[] s) { 68 TextAreaEditing textArea = new TextAreaEditing(); 69 textArea.testReplaceRange(); 70 textArea.testInsert(); 71 textArea.testAppend(); 72 textArea.testSetText(); 73 textArea.checkFailures(); 74 textArea.dispose(); 75 } 76 77 private void testReplaceRange() { 78 textArea.setText(null); 79 textArea.replaceRange("Replace", 0, 0); 80 textArea.setText(null); 81 checkTest(""); 82 83 textArea.setText("SetText"); 84 textArea.replaceRange("Replace", 0, 3); 85 checkTest("ReplaceText"); 86 87 textArea.replaceRange("String", textArea.getText().length(), 88 textArea.getText().length()); 89 checkTest("ReplaceTextString"); 90 91 textArea.replaceRange("String", 0, 0); 92 checkTest("StringReplaceTextString"); 93 94 textArea.replaceRange("replaceRange", 0, textArea.getText().length()); 95 checkTest("replaceRange"); 96 } 97 98 private void testInsert() { 99 textArea.setText(null); 100 textArea.insert("Insert", 0); 101 textArea.setText(""); 102 checkTest(""); 103 104 textArea.setText("SetText"); 105 textArea.insert("Insert", 3); 106 checkTest("SetInsertText"); 107 108 textArea.insert("Insert", 0); 109 checkTest("InsertSetInsertText"); 110 111 textArea.insert("Insert", textArea.getText().length()); 112 checkTest("InsertSetInsertTextInsert"); 113 } 114 115 private void testAppend() { 116 textArea.setText(null); 117 textArea.append("Append"); 118 textArea.setText(null); 119 checkTest(""); 120 121 textArea.setText("SetText"); 122 textArea.append("Append"); 123 checkTest("SetTextAppend"); 124 125 textArea.append(""); 126 checkTest("SetTextAppend"); 127 textArea.setText(""); 128 checkTest(""); 129 } 130 131 private void testSetText() { 132 textArea.setText(null); 133 textArea.requestFocus(); 134 Util.clickOnComp(textArea, robot); 135 Util.waitForIdle(robot); 136 robot.keyPress(KeyEvent.VK_A); 137 robot.delay(5); 138 robot.keyRelease(KeyEvent.VK_A); 139 Util.waitForIdle(robot); 140 textArea.setText(null); 141 checkTest(""); 142 textArea.setText("CaseSensitive"); 143 checkTest("CaseSensitive"); 144 textArea.setText("caseSensitive"); 145 checkTest("caseSensitive"); 146 147 } 148 149 private void checkTest(String str) { 150 if (str != null && !str.equals(textArea.getText())) { 151 testFailMessage.append("TestFail line : "); 152 testFailMessage.append(Thread.currentThread().getStackTrace()[2]. 153 getLineNumber()); 154 testFailMessage.append(" TextArea string : \""); 155 testFailMessage.append(textArea.getText()); 156 testFailMessage.append("\" does not match expected string : \""); 157 testFailMessage.append(str).append("\""); 158 testFailMessage.append(System.getProperty("line.separator")); 159 testFailCount++; 160 isTestFail = true; 161 } 162 } 163 164 private void checkFailures() { 165 if (isTestFail) { 166 testFailMessage.insert(0, "Test Fail count : " + testFailCount 167 + System.getProperty("line.separator")); 168 dispose(); 169 throw new RuntimeException(testFailMessage.toString()); 170 } 171 } 172 }