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