Path: blob/master/test/jdk/java/awt/Container/ValidateRoot/InvalidateMustRespectValidateRoots.java
41153 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@key headful26@bug 685259227@summary invalidate() must stop when it encounters a validate root28@author [email protected]29@run main/othervm -Djava.awt.smartInvalidate=true InvalidateMustRespectValidateRoots30*/3132import javax.swing.*;33import java.awt.event.*;3435public class InvalidateMustRespectValidateRoots {36private static volatile JRootPane rootPane;3738public static void main(String args[]) throws Exception {39SwingUtilities.invokeAndWait(new Runnable() {40public void run() {41// The JRootPane is a validate root. We'll check if42// invalidate() stops on the root pane, or goes further43// up to the frame.44JFrame frame = new JFrame();45final JButton button = new JButton();4647frame.add(button);4849// To enable running the test manually: use the Ctrl-Shift-F150// to print the component hierarchy to the console51button.addActionListener(new ActionListener() {52public void actionPerformed(ActionEvent ev) {53if (button.isValid()) {54button.invalidate();55} else {56button.revalidate();57}58}59});6061rootPane = frame.getRootPane();6263// Now the component hierarchy looks like:64// frame65// --> rootPane66// --> layered pane67// --> content pane68// --> button6970// Make all components valid first via showing the frame71// We have to make the frame visible. Otherwise revalidate() is72// useless (see RepaintManager.addInvalidComponent()).73frame.pack(); // To enable running this test manually74frame.setVisible(true);7576if (!frame.isValid()) {77throw new RuntimeException(78"setVisible(true) failed to validate the frame");79}8081// Now invalidate the button82button.invalidate();8384// Check if the 'valid' status is what we expect it to be85if (rootPane.isValid()) {86throw new RuntimeException(87"invalidate() failed to invalidate the root pane");88}8990if (!frame.isValid()) {91throw new RuntimeException(92"invalidate() invalidated the frame");93}9495// Now validate the hierarchy again96button.revalidate();9798// Now let the validation happen on the EDT99}100});101102Thread.sleep(1000);103104SwingUtilities.invokeAndWait(new Runnable() {105public void run() {106// Check if the root pane finally became valid107if (!rootPane.isValid()) {108throw new RuntimeException(109"revalidate() failed to validate the hierarchy");110}111}112});113}114}115116117