Path: blob/master/test/jdk/javax/accessibility/SlowPanelIteration/SlowPanelIteration.java
41149 views
/*1* Copyright (c) 2018, 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*/2223import java.awt.BorderLayout;24import java.awt.Color;25import java.awt.Container;26import java.awt.Dimension;27import java.awt.EventQueue;28import java.awt.Point;29import java.awt.Robot;30import java.awt.event.InputEvent;31import java.awt.event.MouseAdapter;32import java.awt.event.MouseEvent;33import java.util.concurrent.CountDownLatch;34import java.util.concurrent.TimeUnit;3536import javax.swing.JFrame;37import javax.swing.JPanel;3839/**40* @test41* @key headful42* @bug 820276843* @summary we should not hang when lots of panels are used44*/45public final class SlowPanelIteration {4647private static JFrame frame;48private static Point center = new Point();49private static volatile CountDownLatch go;5051public static void main(final String[] args) throws Exception {52Robot r = new Robot();53// accessibility tool will need time to react to our clicks54r.setAutoDelay(200);55try {56EventQueue.invokeAndWait(SlowPanelIteration::showUI);57for (int i = 0; i < 10; ++i) {58go = new CountDownLatch(1);59r.mouseMove(center.x, center.y);60r.mousePress(InputEvent.BUTTON1_DOWN_MASK);61r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);62if (!go.await(10, TimeUnit.SECONDS)) {63throw new RuntimeException("Too slow operation");64}65}66} finally {67EventQueue.invokeAndWait(SlowPanelIteration::dispose);68}69}7071private static void showUI() {72frame = new JFrame();73frame.setSize(new Dimension(400, 400));74frame.setLocationRelativeTo(null);7576final Container content = frame.getContentPane();77content.setLayout(new BorderLayout(0, 0));78Container lastPanel = content;79for (int i = 0; i < 500; i++) {80final JPanel p = new JPanel();81p.setLayout(new BorderLayout(0, 0));82lastPanel.add(p);83lastPanel.addMouseListener(new MouseAdapter() {84@Override85public void mouseClicked(MouseEvent e) {86System.out.println("click");87go.countDown();88}89});90lastPanel = p;91}9293lastPanel.setBackground(Color.GREEN);94frame.setVisible(true);9596Point loc = frame.getLocationOnScreen();97center.x = loc.x + frame.getWidth() / 2;98center.y = loc.y + frame.getHeight() / 2;99}100101private static void dispose() {102if (frame != null) {103frame.dispose();104}105}106}107108109