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.Panel;
  36 import java.awt.Window;
  37 
  38 public class ChildWindowProperties {
  39 
  40     private Dialog parentDialog;
  41     private Window windowChild;
  42     private Frame parentFrame;
  43     private Window frameChildWindow;
  44     private Label parentLabel;
  45     private Font parentFont;
  46     private Label childLabel;
  47 
  48     private static final int WIDTH = 200;
  49     private static final int HEIGHT = 200;
  50 
  51     public void testChildPropertiesWithDialogAsParent() {
  52 
  53         parentDialog = new Dialog((Dialog) null, "parent Dialog");
  54         parentDialog.setSize(WIDTH, HEIGHT);
  55         parentDialog.setLocation(100, 100);
  56         parentDialog.setBackground(Color.RED);
  57         parentLabel = new Label("ParentForegroundAndFont");
  58         parentFont = new Font("Courier New", Font.ITALIC, 15);
  59         parentDialog.setForeground(Color.BLUE);
  60         parentDialog.setFont(parentFont);
  61 
  62         parentDialog.add(parentLabel);
  63         parentDialog.setVisible(true);
  64 
  65         windowChild = new Window(parentDialog);
  66         windowChild.setSize(WIDTH, HEIGHT);
  67         windowChild.setLocation(WIDTH + 200, 100);
  68         childLabel = new Label("ChildForegroundAndFont");
  69         windowChild.add(childLabel);
  70         windowChild.setVisible(true);
  71 
  72         if (parentDialog.getBackground() == windowChild.getBackground()) {
  73             dispose();
  74             throw new RuntimeException("Child Window Should NOT Inherit "
  75                     + "Parent Dialog's Background Color");
  76         }
  77         if (parentDialog.getForeground() == windowChild.getForeground()) {
  78             dispose();
  79             throw new RuntimeException("Child Window Should NOT Inherit "
  80                     + "Parent Dialog's Foreground Color");
  81         }
  82         if (parentDialog.getFont() == windowChild.getFont()) {
  83             dispose();
  84             throw new RuntimeException("Child Window Should NOT Inherit "
  85                     + "Parent Dialog's Font Color");
  86         }
  87     }
  88 
  89     public void testChildPropertiesWithFrameAsParent() {
  90 
  91         parentFrame = new Frame("parent Frame");
  92         parentFrame.setSize(WIDTH, HEIGHT);
  93         parentFrame.setLocation(100, 400);
  94         parentFrame.setBackground(Color.BLUE);
  95         parentLabel = new Label("ParentForegroundAndFont");
  96         parentFont = new Font("Courier New", Font.ITALIC, 15);
  97         parentFrame.setForeground(Color.RED);
  98         parentFrame.setFont(parentFont);
  99         parentFrame.add(parentLabel);
 100         parentFrame.setVisible(true);
 101 
 102         frameChildWindow = new Window(parentFrame);
 103         frameChildWindow.setSize(WIDTH, HEIGHT);
 104         frameChildWindow.setLocation(WIDTH + 200, 400);
 105         childLabel = new Label("ChildForegroundAndFont");
 106         frameChildWindow.add(childLabel);
 107         frameChildWindow.setVisible(true);
 108 
 109         if (parentFrame.getBackground() == frameChildWindow.getBackground()) {
 110             dispose();
 111             throw new RuntimeException("Child Window Should NOT Inherit "
 112                     + "Parent Frame's Background Color");
 113         }
 114         if (parentDialog.getForeground() == windowChild.getForeground()) {
 115             dispose();
 116             throw new RuntimeException("Child Window Should NOT Inherit "
 117                     + "Parent Frame's Foreground Color");
 118         }
 119         if (parentDialog.getFont() == windowChild.getFont()) {
 120             dispose();
 121             throw new RuntimeException("Child Window Should NOT Inherit "
 122                     + "Parent Frame's Font Color");
 123         }
 124     }
 125 
 126     public void testPanelBackground() {
 127         Window window = new Frame();
 128         window.setBackground(Color.GREEN);
 129         Panel panel = new Panel();
 130         window.add(panel);
 131         window.pack();
 132         window.setVisible(true);
 133         if (panel.getBackground() != Color.GREEN) {
 134             window.dispose();
 135             throw new RuntimeException("Panel Background Color Not Valid");
 136         }
 137         window.dispose();
 138     }
 139 
 140     private void dispose() {
 141 
 142         if (parentDialog != null) {
 143             parentDialog.dispose();
 144         }
 145         if (parentFrame != null) {
 146             parentFrame.dispose();
 147         }
 148     }
 149 
 150     public static void main(String[] args) throws Exception {
 151 
 152         ChildWindowProperties obj = new ChildWindowProperties();
 153         // TestCase1: When Parent is Dialog, Child is Window
 154         obj.testChildPropertiesWithDialogAsParent();
 155         // TestCase2: When Parent is Frame, chis is Window
 156         obj.testChildPropertiesWithFrameAsParent();
 157         // TestCase3: Panel Background Test
 158         obj.testPanelBackground();
 159         obj.dispose();
 160     }
 161 
 162 }
 163