1 /*
   2  * Copyright (c) 2016, 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 8057574
  27  @summary Verify that child Window does not inherit parent's Properties
  28  @run main ChildWindowProperties
  29  */
  30 import java.awt.Color;
  31 import java.awt.Dialog;
  32 import java.awt.Font;
  33 import java.awt.Frame;
  34 import java.awt.Label;
  35 import java.awt.Window;
  36 
  37 public class ChildWindowProperties {
  38 
  39     private Dialog parentDialog;
  40     private Window windowChild;
  41     private Frame parentFrame;
  42     private Window frameChildWindow;
  43     private Label parentLabel;
  44     private Font parentFont;
  45     private Label childLabel;
  46 
  47     private static final int WIDTH = 200;
  48     private static final int HEIGHT = 200;
  49 
  50     public void testChildPropertiesWithDialogAsParent() {
  51 
  52         parentDialog = new Dialog((Dialog) null, "parent Dialog");
  53         parentDialog.setSize(WIDTH, HEIGHT);
  54         parentDialog.setLocation(100, 100);
  55         parentDialog.setBackground(Color.RED);
  56         parentLabel = new Label("ParentForegroundAndFont");
  57         parentFont = new Font("Courier New", Font.ITALIC, 15);
  58         parentDialog.setForeground(Color.BLUE);
  59         parentDialog.setFont(parentFont);
  60         
  61         parentDialog.add(parentLabel);
  62         parentDialog.setVisible(true);
  63 
  64         windowChild = new Window(parentDialog);
  65         windowChild.setSize(WIDTH, HEIGHT);
  66         windowChild.setLocation(WIDTH + 200, 100);
  67         childLabel = new Label("ChildForegroundAndFont");
  68         windowChild.add(childLabel);
  69         windowChild.setVisible(true);
  70 
  71         if (parentDialog.getBackground() == windowChild.getBackground()) {
  72             dispose();
  73             throw new RuntimeException("Child Window Should NOT Inherit "
  74                     + "Parent Dialog's Background Color");
  75         }
  76         if (parentDialog.getForeground() == windowChild.getForeground()) {
  77             dispose();
  78             throw new RuntimeException("Child Window Should NOT Inherit "
  79                     + "Parent Dialog's Foreground Color");
  80         }
  81         if (parentDialog.getFont() == windowChild.getFont()) {
  82             dispose();
  83             throw new RuntimeException("Child Window Should NOT Inherit "
  84                     + "Parent Dialog's Font Color");
  85         }
  86     }
  87 
  88     public void testChildPropertiesWithFrameAsParent() {
  89 
  90         parentFrame = new Frame("parent Frame");
  91         parentFrame.setSize(WIDTH, HEIGHT);
  92         parentFrame.setLocation(100, 400);
  93         parentFrame.setBackground(Color.BLUE);
  94         parentLabel = new Label("ParentForegroundAndFont");
  95         parentFont = new Font("Courier New", Font.ITALIC, 15);
  96         parentFrame.setForeground(Color.RED);
  97         parentFrame.setFont(parentFont);
  98         parentFrame.add(parentLabel);
  99         parentFrame.setVisible(true);
 100 
 101         frameChildWindow = new Window(parentFrame);
 102         frameChildWindow.setSize(WIDTH, HEIGHT);
 103         frameChildWindow.setLocation(WIDTH + 200, 400);
 104         childLabel = new Label("ChildForegroundAndFont");
 105         frameChildWindow.add(childLabel);
 106         frameChildWindow.setVisible(true);
 107 
 108         if (parentFrame.getBackground() == frameChildWindow.getBackground()) {
 109             dispose();
 110             throw new RuntimeException("Child Window Should NOT Inherit "
 111                     + "Parent Frame's Background Color");
 112         }
 113         if (parentDialog.getForeground() == windowChild.getForeground()) {
 114             dispose();
 115             throw new RuntimeException("Child Window Should NOT Inherit "
 116                     + "Parent Frame's Foreground Color");
 117         }
 118         if (parentDialog.getFont() == windowChild.getFont()) {
 119             dispose();
 120             throw new RuntimeException("Child Window Should NOT Inherit "
 121                     + "Parent Frame's Font Color");
 122         }
 123     }
 124 
 125     private void dispose() {
 126 
 127         if (parentDialog != null) {
 128             parentDialog.dispose();
 129         }
 130         if (parentFrame != null) {
 131             parentFrame.dispose();
 132         }
 133     }
 134 
 135     public static void main(String[] args) throws Exception {
 136 
 137         ChildWindowProperties obj = new ChildWindowProperties();
 138         // TestCase1: When Parent is Dialog, Child is Window
 139         obj.testChildPropertiesWithDialogAsParent();
 140         // TestCase2: When Parent is Frame, chis is Window
 141         obj.testChildPropertiesWithFrameAsParent();
 142         obj.dispose();
 143     }
 144 
 145 }