1 /* 2 * Copyright (c) 2018, 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 import java.awt.BorderLayout; 25 import java.awt.Color; 26 import java.awt.Container; 27 import java.awt.Dimension; 28 import java.awt.EventQueue; 29 import java.awt.Point; 30 import java.awt.Robot; 31 import java.awt.event.InputEvent; 32 import java.awt.event.MouseAdapter; 33 import java.awt.event.MouseEvent; 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.TimeUnit; 36 37 import javax.swing.JFrame; 38 import javax.swing.JPanel; 39 40 /** 41 * @test 42 * @key headful 43 * @bug 8202768 44 * @summary we should not hang when lots of panels are used 45 */ 46 public final class SlowPanelIteration { 47 48 private static JFrame frame; 49 private static Point center = new Point(); 50 private static volatile CountDownLatch go; 51 52 public static void main(final String[] args) throws Exception { 53 Robot r = new Robot(); 54 // accessibility tool will need time to react to our clicks 55 r.setAutoDelay(200); 56 try { 57 EventQueue.invokeAndWait(SlowPanelIteration::showUI); 58 for (int i = 0; i < 10; ++i) { 59 go = new CountDownLatch(1); 60 r.mouseMove(center.x, center.y); 61 r.mousePress(InputEvent.BUTTON1_DOWN_MASK); 62 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 63 if (!go.await(10, TimeUnit.SECONDS)) { 64 throw new RuntimeException("Too slow operation"); 65 } 66 } 67 } finally { 68 EventQueue.invokeAndWait(SlowPanelIteration::dispose); 69 } 70 } 71 72 private static void showUI() { 73 frame = new JFrame(); 74 frame.setSize(new Dimension(400, 400)); 75 frame.setLocationRelativeTo(null); 76 77 final Container content = frame.getContentPane(); 78 content.setLayout(new BorderLayout(0, 0)); 79 Container lastPanel = content; 80 for (int i = 0; i < 500; i++) { 81 final JPanel p = new JPanel(); 82 p.setLayout(new BorderLayout(0, 0)); 83 lastPanel.add(p); 84 lastPanel.addMouseListener(new MouseAdapter() { 85 @Override 86 public void mouseClicked(MouseEvent e) { 87 System.out.println("click"); 88 go.countDown(); 89 } 90 }); 91 lastPanel = p; 92 } 93 94 lastPanel.setBackground(Color.GREEN); 95 frame.setVisible(true); 96 97 Point loc = frame.getLocationOnScreen(); 98 center.x = loc.x + frame.getWidth() / 2; 99 center.y = loc.y + frame.getHeight() / 2; 100 } 101 102 private static void dispose() { 103 if (frame != null) { 104 frame.dispose(); 105 } 106 } 107 }