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