1 /*
   2  * Copyright (c) 2011, 2013, 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  @summary Simple test for recieving TextEvents
  27  @summary com.apple.junit.java.awt.Event;
  28  @library ../../regtesthelpers
  29  @build RobotUtilities
  30  @build VisibilityValidator
  31  @build Waypoint
  32  @run juint TextListener01
  33  */
  34 
  35 import junit.framework.*;
  36 
  37 import java.awt.*;
  38 import java.awt.event.*;
  39 
  40 import test.java.awt.regtesthelpers.RobotUtilities;
  41 import test.java.awt.regtesthelpers.VisibilityValidator;
  42 import test.java.awt.regtesthelpers.Waypoint;
  43 
  44 public class TextListener01 extends TestCase implements TextListener {
  45     public static Test suite() {
  46         return new TestSuite(TextListener01.class);
  47     }
  48 
  49     public static void main (String[] args) throws RuntimeException {
  50         TestResult tr = junit.textui.TestRunner.run(suite());
  51         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  52             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  53         }
  54     }
  55 
  56     protected int textValueChangedCount = 0;
  57     final Waypoint didEvent = new Waypoint();
  58 
  59     public void textValueChanged( TextEvent e ) {
  60         textValueChangedCount++;
  61         didEvent.clear();
  62     }
  63 
  64     public void xxtestRepeatedly() throws Exception {
  65         for( int i = 0; i < 1; i++) {
  66             testTextListener();
  67         }
  68     }
  69 
  70     public void testTextListener() throws Exception {
  71         Frame frame;
  72         TextField tf;
  73         frame = new Frame( "TextListener01" );
  74 
  75         try {
  76             frame.setSize( 300, 150 );
  77             frame.setLocation( new Point( 100, 0 ) );
  78             tf = new TextField( "Initial text." );
  79             frame.add( tf, BorderLayout.NORTH );
  80 
  81             VisibilityValidator.setVisibleAndConfirm(frame);
  82 
  83             tf.addTextListener( this );
  84             tf.requestFocus();
  85             tf.setCaretPosition( 0 );
  86             RobotUtilities.delay( 1500 );
  87 
  88             // Generate a textValueChanged event using Robot to generate a key stroke
  89             RobotUtilities.typeKey( KeyEvent.VK_I );
  90 
  91             didEvent.requireClear("Did not receive textValueChanged event.");
  92             didEvent.reset();
  93 
  94             assertEquals("Expected 1 textValueChangedCount event after key press but received " + textValueChangedCount + ".", textValueChangedCount, 1 );
  95 
  96             // Generate a second textValueChanged event by calling setText
  97             tf.setText( "Text changed by setText()" );
  98 
  99             didEvent.requireClear("Did not receive textValueChanged event.");
 100             didEvent.reset();
 101 
 102             assertEquals("Expected 2 textValueChangedCount events but received " + textValueChangedCount + ".", textValueChangedCount, 2 );
 103 
 104         }
 105         finally {
 106             frame.dispose();
 107         }
 108     }
 109 }