1 /* 2 * Copyright (c) 2012, 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 4028580 28 * @summary TextArea does not send TextEvent when setText. Does for insert 29 * @author kdm@sparc.spb.su: area= awt.TextAvent 30 * @run main TextEventSequenceTest 31 */ 32 import java.awt.*; 33 import java.awt.event.*; 34 import sun.awt.SunToolkit; 35 36 public class TextEventSequenceTest { 37 38 private static Frame f; 39 private static TextField tf; 40 private static TextArea t; 41 private static int cntEmptyStrings = 0; 42 private static int cntNonEmptyStrings = 0; 43 44 public static void main(String[] args) { 45 46 test("non-empty text string"); 47 test(""); 48 test(null); 49 } 50 51 private static void test(String test) { 52 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 53 54 createAndShowGUI(test); 55 toolkit.realSync(); 56 57 initCounts(); 58 t.setText("Hello "); 59 toolkit.realSync(); 60 t.append("World! !"); 61 toolkit.realSync(); 62 t.insert("from Roger Pham", 13); 63 toolkit.realSync(); 64 t.replaceRange("Java Duke", 18, 28); 65 toolkit.realSync(); 66 checkCounts(0, 4); 67 68 initCounts(); 69 t.setText(""); 70 toolkit.realSync(); 71 t.setText(""); 72 toolkit.realSync(); 73 t.setText(""); 74 toolkit.realSync(); 75 checkCounts(1, 0); 76 77 initCounts(); 78 tf.setText("Hello There!"); 79 toolkit.realSync(); 80 checkCounts(0, 1); 81 82 initCounts(); 83 tf.setText(""); 84 toolkit.realSync(); 85 tf.setText(""); 86 toolkit.realSync(); 87 tf.setText(""); 88 toolkit.realSync(); 89 checkCounts(1, 0); 90 91 f.dispose(); 92 } 93 94 private static void createAndShowGUI(String text) { 95 f = new Frame("TextEventSequenceTest"); 96 f.setLayout(new FlowLayout()); 97 98 TextListener listener = new MyTextListener(); 99 100 tf = new TextField(text); 101 tf.addTextListener(listener); 102 f.add(tf); 103 104 t = new TextArea(text, 10, 30); 105 t.addTextListener(listener); 106 f.add(t); 107 108 f.pack(); 109 f.setVisible(true); 110 } 111 112 static class MyTextListener implements TextListener { 113 114 public synchronized void textValueChanged(TextEvent e) { 115 TextComponent tc = (TextComponent) e.getSource(); 116 String text = tc.getText(); 117 if (text.length() == 0) { 118 cntEmptyStrings++; 119 } else { 120 cntNonEmptyStrings++; 121 } 122 } 123 } 124 125 synchronized static void initCounts() { 126 cntEmptyStrings = 0; 127 cntNonEmptyStrings = 0; 128 } 129 130 synchronized static void checkCounts(int empty, int nonempty) { 131 if (empty != cntEmptyStrings || nonempty != cntNonEmptyStrings) { 132 throw new RuntimeException( 133 String.format("Expected events: empty = %d, nonempty = %d, " 134 + "actual events: empty = %d, nonempty = %d", 135 empty, nonempty, cntEmptyStrings, cntNonEmptyStrings)); 136 } 137 } 138 } 139