5240N/A/*
5240N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5240N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5240N/A *
5240N/A * This code is free software; you can redistribute it and/or modify it
5240N/A * under the terms of the GNU General Public License version 2 only, as
5240N/A * published by the Free Software Foundation.
5240N/A *
5240N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5240N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5240N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5240N/A * version 2 for more details (a copy is included in the LICENSE file that
5240N/A * accompanied this code).
5240N/A *
5240N/A * You should have received a copy of the GNU General Public License version
5240N/A * 2 along with this work; if not, write to the Free Software Foundation,
5240N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5240N/A *
5240N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5240N/A * or visit www.oracle.com if you need additional information or have any
5240N/A * questions.
5240N/A */
5240N/A
5240N/A/*
5240N/A * @test
5240N/A * @bug 4028580
5240N/A * @summary TextArea does not send TextEvent when setText. Does for insert
5240N/A * @author kdm@sparc.spb.su: area= awt.TextAvent
5240N/A * @run main TextEventSequenceTest
5240N/A */
5240N/Aimport java.awt.*;
5240N/Aimport java.awt.event.*;
5240N/Aimport sun.awt.SunToolkit;
5240N/A
5240N/Apublic class TextEventSequenceTest {
5240N/A
5240N/A private static Frame f;
5240N/A private static TextField tf;
5240N/A private static TextArea t;
5240N/A private static int cntEmptyStrings = 0;
5240N/A private static int cntNonEmptyStrings = 0;
5240N/A
5240N/A public static void main(String[] args) {
5240N/A
5240N/A test("non-empty text string");
5240N/A test("");
5240N/A test(null);
5240N/A }
5240N/A
5240N/A private static void test(String test) {
5240N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
5240N/A
5240N/A createAndShowGUI(test);
5240N/A toolkit.realSync();
5240N/A
5240N/A initCounts();
5240N/A t.setText("Hello ");
5240N/A toolkit.realSync();
5240N/A t.append("World! !");
5240N/A toolkit.realSync();
5240N/A t.insert("from Roger Pham", 13);
5240N/A toolkit.realSync();
5240N/A t.replaceRange("Java Duke", 18, 28);
5240N/A toolkit.realSync();
5240N/A checkCounts(0, 4);
5240N/A
5240N/A initCounts();
5240N/A t.setText("");
5240N/A toolkit.realSync();
5240N/A t.setText("");
5240N/A toolkit.realSync();
5240N/A t.setText("");
5240N/A toolkit.realSync();
5240N/A checkCounts(1, 0);
5240N/A
5240N/A initCounts();
5240N/A tf.setText("Hello There!");
5240N/A toolkit.realSync();
5240N/A checkCounts(0, 1);
5240N/A
5240N/A initCounts();
5240N/A tf.setText("");
5240N/A toolkit.realSync();
5240N/A tf.setText("");
5240N/A toolkit.realSync();
5240N/A tf.setText("");
5240N/A toolkit.realSync();
5240N/A checkCounts(1, 0);
5240N/A
5240N/A f.dispose();
5240N/A }
5240N/A
5240N/A private static void createAndShowGUI(String text) {
5240N/A f = new Frame("TextEventSequenceTest");
5240N/A f.setLayout(new FlowLayout());
5240N/A
5240N/A TextListener listener = new MyTextListener();
5240N/A
5240N/A tf = new TextField(text);
5240N/A tf.addTextListener(listener);
5240N/A f.add(tf);
5240N/A
5240N/A t = new TextArea(text, 10, 30);
5240N/A t.addTextListener(listener);
5240N/A f.add(t);
5240N/A
5240N/A f.pack();
5240N/A f.setVisible(true);
5240N/A }
5240N/A
5240N/A static class MyTextListener implements TextListener {
5240N/A
5240N/A public synchronized void textValueChanged(TextEvent e) {
5240N/A TextComponent tc = (TextComponent) e.getSource();
5240N/A String text = tc.getText();
5240N/A if (text.length() == 0) {
5240N/A cntEmptyStrings++;
5240N/A } else {
5240N/A cntNonEmptyStrings++;
5240N/A }
5240N/A }
5240N/A }
5240N/A
5240N/A synchronized static void initCounts() {
5240N/A cntEmptyStrings = 0;
5240N/A cntNonEmptyStrings = 0;
5240N/A }
5240N/A
5240N/A synchronized static void checkCounts(int empty, int nonempty) {
5240N/A if (empty != cntEmptyStrings || nonempty != cntNonEmptyStrings) {
5240N/A throw new RuntimeException(
5240N/A String.format("Expected events: empty = %d, nonempty = %d, "
5240N/A + "actual events: empty = %d, nonempty = %d",
5240N/A empty, nonempty, cntEmptyStrings, cntNonEmptyStrings));
5240N/A }
5240N/A }
5240N/A}
5240N/A