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