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