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 PostEventOrderingTest.java
  26  * @bug 4171596 6699589
  27  * @summary Checks that the posting of events between the PostEventQueue
  28  * @summary and the EventQueue maintains proper ordering.
  29  * @run main PostEventOrderingTest
  30  * @author fredx
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import sun.awt.AppContext;
  36 import sun.awt.SunToolkit;
  37 
  38 public class PostEventOrderingTest {
  39     static boolean testPassed = true;
  40 
  41     public static void main(String[] args) throws Throwable {
  42         EventQueue q = Toolkit.getDefaultToolkit().getSystemEventQueue();
  43         for (int i = 0; i < 100; i++) {
  44             for (int j = 0; j < 100; j++) {
  45                 q.postEvent(new PostActionEvent());
  46                 for (int k = 0; k < 10; k++) {
  47                     SunToolkit.postEvent(AppContext.getAppContext(), new PostActionEvent());
  48                 }
  49             }
  50             for (int k = 0; k < 100; k++) {
  51                 SunToolkit.postEvent(AppContext.getAppContext(), new PostActionEvent());
  52             }
  53         }
  54 
  55         for (;;) {
  56             Thread.currentThread().sleep(100);
  57             if (q.peekEvent() == null) {
  58                 Thread.currentThread().sleep(100);
  59                 if (q.peekEvent() == null)
  60                     break;
  61             }
  62         }
  63 
  64         if (!testPassed) {
  65             throw new Exception("PostEventOrderingTest FAILED -- events dispatched out of order.");
  66         } else {
  67             System.out.println("PostEventOrderingTest passed!");
  68         }
  69     }
  70 }
  71 
  72 class PostActionEvent extends ActionEvent implements ActiveEvent {
  73     static int counter = 0;
  74     static int mostRecent = -1;
  75 
  76     int myval;
  77 
  78     public PostActionEvent() {
  79         super("", ACTION_PERFORMED, "" + counter);
  80         myval = counter++;
  81     }
  82 
  83     public void dispatch() {
  84         //System.out.println("myval = "+myval+", mostRecent = "+mostRecent+", diff = "+(myval-mostRecent)+".");
  85         if ((myval - mostRecent) != 1)
  86             PostEventOrderingTest.testPassed = false;
  87         mostRecent = myval;
  88     }
  89 }