Path: blob/master/test/jdk/java/awt/EventQueue/PostEventOrderingTest/PostEventOrderingTest.java
41153 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test PostEventOrderingTest.java25* @bug 4171596 669958926* @summary Checks that the posting of events between the PostEventQueue27* @summary and the EventQueue maintains proper ordering.28* @modules java.desktop/sun.awt29* @run main PostEventOrderingTest30* @author fredx31*/3233import java.awt.*;34import java.awt.event.*;35import sun.awt.AppContext;36import sun.awt.SunToolkit;3738public class PostEventOrderingTest {39static boolean testPassed = true;4041public static void main(String[] args) throws Throwable {42EventQueue q = Toolkit.getDefaultToolkit().getSystemEventQueue();43for (int i = 0; i < 100; i++) {44for (int j = 0; j < 100; j++) {45q.postEvent(new PostActionEvent());46for (int k = 0; k < 10; k++) {47SunToolkit.postEvent(AppContext.getAppContext(), new PostActionEvent());48}49}50for (int k = 0; k < 100; k++) {51SunToolkit.postEvent(AppContext.getAppContext(), new PostActionEvent());52}53}5455for (;;) {56Thread.currentThread().sleep(100);57if (q.peekEvent() == null) {58Thread.currentThread().sleep(100);59if (q.peekEvent() == null)60break;61}62}6364if (!testPassed) {65throw new Exception("PostEventOrderingTest FAILED -- events dispatched out of order.");66} else {67System.out.println("PostEventOrderingTest passed!");68}69}70}7172class PostActionEvent extends ActionEvent implements ActiveEvent {73static int counter = 0;74static int mostRecent = -1;7576int myval;7778public PostActionEvent() {79super("", ACTION_PERFORMED, "" + counter);80myval = counter++;81}8283public void dispatch() {84//System.out.println("myval = "+myval+", mostRecent = "+mostRecent+", diff = "+(myval-mostRecent)+".");85if ((myval - mostRecent) != 1)86PostEventOrderingTest.testPassed = false;87mostRecent = myval;88}89}909192