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