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 defect: <rdar://problem/3627947> Events: PaintEvents not delivered
  27  @summary com.apple.junit.java.awt.Event;
  28  @library ../../../regtesthelpers
  29  @build VisibilityValidator
  30  @run main PaintEventTest
  31  */
  32 
  33 import junit.framework.*;
  34 
  35 import java.awt.*;
  36 import java.awt.event.AWTEventListener;
  37 import java.awt.event.ActionEvent;
  38 
  39 import test.java.awt.regtesthelpers.VisibilityValidator;
  40 
  41 public class PaintEventTest extends TestCase {
  42     static final int LOOP = 100;
  43 
  44     class PaintEventCounter {
  45         static final int TIMEOUT = 5000; // Wait up to five seconds
  46 
  47         private int counter = 0;
  48         private int buttonCounter = 0;
  49         private int panelCounter = 0;
  50         private int frameCounter = 0;
  51         private AWTEventListener listener = null;
  52 
  53         public PaintEventCounter() {
  54             listener = new AWTEventListener() {
  55                 public void eventDispatched( AWTEvent e ) {
  56                     doEvent(e);
  57                 }
  58             };
  59             Toolkit.getDefaultToolkit().addAWTEventListener( listener , AWTEvent.PAINT_EVENT_MASK );
  60         }
  61 
  62         public void dispose() {
  63             Toolkit.getDefaultToolkit().removeAWTEventListener( listener );
  64         }
  65 
  66         synchronized public boolean requireEvents( int total ) {
  67             long endtime = System.currentTimeMillis() + TIMEOUT;
  68             try {
  69                 while (counter < total) {
  70                     if (System.currentTimeMillis() < endtime) {
  71                         wait( TIMEOUT / 10 );
  72                     }
  73                     else {
  74                         break;
  75                     }
  76                 }
  77             }
  78             catch (InterruptedException ix) {
  79             }
  80             return (counter == total);
  81         }
  82 
  83         synchronized void doEvent(AWTEvent e) {
  84             counter++;
  85 
  86             // System.out.println(e);
  87 
  88             if ( e.getSource() instanceof Button) {
  89                 buttonCounter++;
  90             }
  91 
  92             if ( e.getSource() instanceof Panel) {
  93                 panelCounter++;
  94             }
  95 
  96             if ( e.getSource() instanceof Frame) {
  97                 frameCounter++;
  98             }
  99             notify();
 100         }
 101 
 102         public int getCounter() {
 103             return( counter );
 104         }
 105 
 106         public int getButtonCounter() {
 107             return( buttonCounter );
 108         }
 109 
 110         public int getPanelCounter() {
 111             return( panelCounter );
 112         }
 113 
 114         public int getFrameCounter() {
 115             return( frameCounter );
 116         }
 117 
 118 
 119     }
 120 
 121     public void testPaintEvents01() throws Exception {
 122         Frame frame = null;
 123        PaintEventCounter collecter = null;
 124 
 125         try {
 126             // Thread.currentThread().setName( "PaintEventTest Thread" );
 127 
 128             // Bring up a test frame
 129             frame = new Frame( "PaintEventTest " );
 130             frame.setLayout( new FlowLayout() );
 131             frame.setSize( 300, 400 );
 132 
 133             // Add components and count the events
 134             collecter = new PaintEventCounter();
 135 
 136             assertTrue( "Nothing visible yet, no events expected", collecter.requireEvents( 0 ) );
 137 
 138             Button but = new Button("Do Action");
 139             but.addActionListener(new java.awt.event.ActionListener() {
 140                 public void actionPerformed(ActionEvent e) {
 141                     System.out.println("\tStartFrame action.");
 142                 }
 143             });
 144             Panel  pan = new Panel();
 145             pan.add(but);
 146             frame.add(pan);
 147 
 148             VisibilityValidator.setVisibleAndConfirm(frame);
 149 
 150             //
 151             //    We are getting too many paint events.  We need at least three to continue...
 152             //
 153             //    Put assert below back in when we have dealt with:
 154             //    <rdar://problem/3132190> Painting: We paint twice on startup
 155             //    <rdar://problem/3535283> Java does about 5x too much painting for each repaint request
 156             //
 157             //    Then delete the thread.sleep();
 158 
 159             // assertTrue( 3 + " events expected, but got " + collecter.getCounter() + " events", collecter.requireEvents( 3 ) );
 160             // assertEquals( "Single frame event expected", collecter.getFrameCounter(), 1 );
 161             // assertEquals( "Single panel event expected", collecter.getPanelCounter(), 1 );
 162             // assertEquals( "Single button event expected", collecter.getButtonCounter(), 1 );
 163 
 164 
 165             collecter.requireEvents(3);
 166             Thread.sleep(500);
 167 
 168             assertTrue( "At least one frame event expected", collecter.getFrameCounter() >= 1 );
 169             assertTrue( "At least one panel event expected", collecter.getPanelCounter() >= 1 );
 170             assertTrue( "At least one button event expected", collecter.getButtonCounter() >= 1 );
 171         }
 172         finally {
 173             if ( frame != null) {
 174                 frame.setVisible( false );
 175                 frame.dispose();
 176             }
 177 
 178             if ( collecter != null) {
 179                 collecter.dispose();
 180             }
 181 
 182         }
 183     }
 184 
 185     public static Test suite() {
 186         return new TestSuite( PaintEventTest.class);
 187     }
 188 
 189     public static void main (String[] args) throws RuntimeException {
 190         TestResult tr = junit.textui.TestRunner.run(suite());
 191         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 192             throw new RuntimeException("### Unexpected JUnit errors or failures.");
 193         }
 194     }
 195 }