Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Container/ValidateRoot/InvalidateMustRespectValidateRoots.java
38828 views
/*1* Copyright (c) 2009, 2011, 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@test25@bug 685259226@summary invalidate() must stop when it encounters a validate root27@author [email protected]28@run main/othervm -Djava.awt.smartInvalidate=true InvalidateMustRespectValidateRoots29*/3031import javax.swing.*;32import java.awt.event.*;3334public class InvalidateMustRespectValidateRoots {35private static volatile JRootPane rootPane;3637public static void main(String args[]) throws Exception {38SwingUtilities.invokeAndWait(new Runnable() {39public void run() {40// The JRootPane is a validate root. We'll check if41// invalidate() stops on the root pane, or goes further42// up to the frame.43JFrame frame = new JFrame();44final JButton button = new JButton();4546frame.add(button);4748// To enable running the test manually: use the Ctrl-Shift-F149// to print the component hierarchy to the console50button.addActionListener(new ActionListener() {51public void actionPerformed(ActionEvent ev) {52if (button.isValid()) {53button.invalidate();54} else {55button.revalidate();56}57}58});5960rootPane = frame.getRootPane();6162// Now the component hierarchy looks like:63// frame64// --> rootPane65// --> layered pane66// --> content pane67// --> button6869// Make all components valid first via showing the frame70// We have to make the frame visible. Otherwise revalidate() is71// useless (see RepaintManager.addInvalidComponent()).72frame.pack(); // To enable running this test manually73frame.setVisible(true);7475if (!frame.isValid()) {76throw new RuntimeException(77"setVisible(true) failed to validate the frame");78}7980// Now invalidate the button81button.invalidate();8283// Check if the 'valid' status is what we expect it to be84if (rootPane.isValid()) {85throw new RuntimeException(86"invalidate() failed to invalidate the root pane");87}8889if (!frame.isValid()) {90throw new RuntimeException(91"invalidate() invalidated the frame");92}9394// Now validate the hierarchy again95button.revalidate();9697// Now let the validation happen on the EDT98}99});100101Thread.sleep(1000);102103SwingUtilities.invokeAndWait(new Runnable() {104public void run() {105// Check if the root pane finally became valid106if (!rootPane.isValid()) {107throw new RuntimeException(108"revalidate() failed to validate the hierarchy");109}110}111});112}113}114115116