1 /*
   2  * Copyright (c) 2009, 2011, 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 /*
  25   @test
  26   @bug 6852592
  27   @summary invalidate() must stop when it encounters a validate root
  28   @author anthony.petrov@sun.com
  29   @run main/othervm -Djava.awt.smartInvalidate=true InvalidateMustRespectValidateRoots
  30 */
  31 
  32 import javax.swing.*;
  33 import java.awt.event.*;
  34 
  35 public class InvalidateMustRespectValidateRoots {
  36     private static volatile JRootPane rootPane;
  37 
  38     public static void main(String args[]) throws Exception {
  39         SwingUtilities.invokeAndWait(new Runnable() {
  40             public void run() {
  41                 // The JRootPane is a validate root. We'll check if
  42                 // invalidate() stops on the root pane, or goes further
  43                 // up to the frame.
  44                 JFrame frame = new JFrame();
  45                 final JButton button = new JButton();
  46 
  47                 frame.add(button);
  48 
  49                 // To enable running the test manually: use the Ctrl-Shift-F1
  50                 // to print the component hierarchy to the console
  51                 button.addActionListener(new ActionListener() {
  52                     public void actionPerformed(ActionEvent ev) {
  53                         if (button.isValid()) {
  54                             button.invalidate();
  55                         } else {
  56                             button.revalidate();
  57                         }
  58                     }
  59                 });
  60 
  61                 rootPane = frame.getRootPane();
  62 
  63                 // Now the component hierarchy looks like:
  64                 // frame
  65                 // --> rootPane
  66                 //     --> layered pane
  67                 //         --> content pane
  68                 //             --> button
  69 
  70                 // Make all components valid first via showing the frame
  71                 // We have to make the frame visible. Otherwise revalidate() is
  72                 // useless (see RepaintManager.addInvalidComponent()).
  73                 frame.pack(); // To enable running this test manually
  74                 frame.setVisible(true);
  75 
  76                 if (!frame.isValid()) {
  77                     throw new RuntimeException(
  78                             "setVisible(true) failed to validate the frame");
  79                 }
  80 
  81                 // Now invalidate the button
  82                 button.invalidate();
  83 
  84                 // Check if the 'valid' status is what we expect it to be
  85                 if (rootPane.isValid()) {
  86                     throw new RuntimeException(
  87                             "invalidate() failed to invalidate the root pane");
  88                 }
  89 
  90                 if (!frame.isValid()) {
  91                     throw new RuntimeException(
  92                             "invalidate() invalidated the frame");
  93                 }
  94 
  95                 // Now validate the hierarchy again
  96                 button.revalidate();
  97 
  98                 // Now let the validation happen on the EDT
  99             }
 100         });
 101 
 102         Thread.sleep(1000);
 103 
 104         SwingUtilities.invokeAndWait(new Runnable() {
 105             public void run() {
 106                 // Check if the root pane finally became valid
 107                 if (!rootPane.isValid()) {
 108                     throw new RuntimeException(
 109                             "revalidate() failed to validate the hierarchy");
 110                 }
 111             }
 112         });
 113     }
 114 }