From 282908c590da995d081775aa92384827ec539bb0 Mon Sep 17 00:00:00 2001 From: Prem Balakrishnan Date: Tue, 24 May 2016 14:19:53 +0530 Subject: [PATCH] 8057574: inconsistent behavior for setBackground (Windows/Linux) Reviewed-by: serb, arapte --- .../unix/classes/sun/awt/X11/XWindowPeer.java | 33 ++-- .../classes/sun/awt/windows/WDialogPeer.java | 19 +- .../classes/sun/awt/windows/WWindowPeer.java | 15 +- .../native/libawt/windows/awt_Dialog.cpp | 12 +- .../ChildDialogProperties.java | 151 ++++++++++++++++ .../ChildWindowProperties.java | 163 ++++++++++++++++++ 6 files changed, 341 insertions(+), 52 deletions(-) create mode 100644 jdk/test/java/awt/Dialog/ChildProperties/ChildDialogProperties.java create mode 100644 jdk/test/java/awt/Window/ChildProperties/ChildWindowProperties.java diff --git a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindowPeer.java b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindowPeer.java index 3dad9f30728..97676f20646 100644 --- a/jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindowPeer.java +++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11/XWindowPeer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -174,27 +174,24 @@ class XWindowPeer extends XPanelPeer implements WindowPeer, cachedFocusableWindow = isFocusableWindow(); - Font f = target.getFont(); - if (f == null) { - f = XWindow.getDefaultFont(); - target.setFont(f); - // we should not call setFont because it will call a repaint - // which the peer may not be ready to do yet. + if (!target.isFontSet()) { + target.setFont(XWindow.getDefaultFont()); + // we should not call setFont because it will call a repaint + // which the peer may not be ready to do yet. } - Color c = target.getBackground(); - if (c == null) { - Color background = SystemColor.window; - target.setBackground(background); - // we should not call setBackGround because it will call a repaint - // which the peer may not be ready to do yet. + if (!target.isBackgroundSet()) { + target.setBackground(SystemColor.window); + // we should not call setBackGround because it will call a repaint + // which the peer may not be ready to do yet. + } - c = target.getForeground(); - if (c == null) { - target.setForeground(SystemColor.windowText); - // we should not call setForeGround because it will call a repaint - // which the peer may not be ready to do yet. + if (!target.isForegroundSet()) { + target.setForeground(SystemColor.windowText); + // we should not call setForeGround because it will call a repaint + // which the peer may not be ready to do yet. } + alwaysOnTop = ((Window)target).isAlwaysOnTop() && ((Window)target).isAlwaysOnTopSupported(); GraphicsConfiguration gc = getGraphicsConfiguration(); diff --git a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WDialogPeer.java b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WDialogPeer.java index 86c708006c1..a497dffb75f 100644 --- a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WDialogPeer.java +++ b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WDialogPeer.java @@ -37,10 +37,6 @@ final class WDialogPeer extends WWindowPeer implements DialogPeer { // target has none explicitly specified. static final Color defaultBackground = SystemColor.control; - // If target doesn't have its background color set, we set its - // background to platform default. - boolean needDefaultBackground; - WDialogPeer(Dialog target) { super(target); @@ -67,7 +63,7 @@ final class WDialogPeer extends WWindowPeer implements DialogPeer { Dialog target = (Dialog)this.target; // Need to set target's background to default _before_ a call // to super.initialize. - if (needDefaultBackground) { + if (!target.isBackgroundSet()) { target.setBackground(defaultBackground); } @@ -133,19 +129,6 @@ final class WDialogPeer extends WWindowPeer implements DialogPeer { } } - /* Native create() peeks at target's background and if it's null - * calls this method to arrage for default background to be set on - * target. Can't make the check in Java, since getBackground will - * return owner's background if target has none set. - */ - private void setDefaultColor() { - // Can't call target.setBackground directly, since we are - // called on toolkit thread. Can't schedule a Runnable on the - // EventHandlerThread because of the race condition. So just - // set a flag and call target.setBackground in initialize. - needDefaultBackground = true; - } - native void pSetIMMOption(String option); void notifyIMMOptionChange(){ InputMethodManager.getInstance().notifyChangeRequest((Component)target); diff --git a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java index 2e8e81262d4..a776c741174 100644 --- a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java +++ b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java @@ -177,12 +177,17 @@ public class WWindowPeer extends WPanelPeer implements WindowPeer, updateInsets(insets_); - Font f = ((Window)target).getFont(); - if (f == null) { - f = defaultFont; - ((Window)target).setFont(f); - setFont(f); + if (!((Window) target).isFontSet()) { + ((Window) target).setFont(defaultFont); + setFont(defaultFont); } + if (!((Window) target).isForegroundSet()) { + ((Window) target).setForeground(SystemColor.windowText); + } + if (!((Window) target).isBackgroundSet()) { + ((Window) target).setBackground(SystemColor.window); + } + // Express our interest in display changes GraphicsConfiguration gc = getGraphicsConfiguration(); ((Win32GraphicsDevice)gc.getDevice()).addDisplayChangedListener(this); diff --git a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Dialog.cpp b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Dialog.cpp index 7bb201b49af..fb65b85a519 100644 --- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Dialog.cpp +++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Dialog.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -100,7 +100,6 @@ AwtDialog* AwtDialog::Create(jobject peer, jobject parent) { JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); - jobject background = NULL; jobject target = NULL; AwtDialog* dialog = NULL; @@ -181,22 +180,13 @@ AwtDialog* AwtDialog::Create(jobject peer, jobject parent) } dialog->DoUpdateIcon(); - - background = env->GetObjectField(target, - AwtComponent::backgroundID); - if (background == NULL) { - JNU_CallMethodByName(env, NULL, - peer, "setDefaultColor", "()V"); - } } } catch (...) { - env->DeleteLocalRef(background); env->DeleteLocalRef(target); throw; } done: - env->DeleteLocalRef(background); env->DeleteLocalRef(target); return dialog; diff --git a/jdk/test/java/awt/Dialog/ChildProperties/ChildDialogProperties.java b/jdk/test/java/awt/Dialog/ChildProperties/ChildDialogProperties.java new file mode 100644 index 00000000000..1a963aa9898 --- /dev/null +++ b/jdk/test/java/awt/Dialog/ChildProperties/ChildDialogProperties.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + /* + @test + @bug 8057574 + @summary Verify that child Dialog does not inherit parent's Properties + @run main ChildDialogProperties + */ +import java.awt.Color; +import java.awt.Dialog; +import java.awt.Font; +import java.awt.Frame; +import java.awt.Label; + +public class ChildDialogProperties { + + private Dialog parentDialog; + private Dialog dialogChild; + private Frame parentFrame; + private Dialog frameChildDialog; + private Label parentLabel; + private Font parentFont; + private Label childLabel; + + private static final int WIDTH = 200; + private static final int HEIGHT = 200; + + public void testChildPropertiesWithDialogAsParent() { + + parentDialog = new Dialog((Dialog) null, "parent Dialog"); + parentDialog.setSize(WIDTH, HEIGHT); + parentDialog.setLocation(100, 100); + parentDialog.setBackground(Color.RED); + + parentLabel = new Label("ParentForegroundAndFont"); + parentFont = new Font("Courier New", Font.ITALIC, 15); + parentDialog.setForeground(Color.BLUE); + parentDialog.setFont(parentFont); + + parentDialog.add(parentLabel); + parentDialog.setVisible(true); + + dialogChild = new Dialog(parentDialog, "Dialog's child"); + dialogChild.setSize(WIDTH, HEIGHT); + dialogChild.setLocation(WIDTH + 200, 100); + childLabel = new Label("ChildForegroundAndFont"); + dialogChild.add(childLabel); + + dialogChild.setVisible(true); + + if (parentDialog.getBackground() == dialogChild.getBackground()) { + dispose(); + throw new RuntimeException("Child Dialog Should NOT Inherit " + + "Parent Dialog's Background Color"); + } + + if (parentDialog.getForeground() == dialogChild.getForeground()) { + dispose(); + throw new RuntimeException("Child Dialog Should NOT Inherit " + + "Parent Dialog's Foreground Color"); + } + + if (parentDialog.getFont() == dialogChild.getFont()) { + dispose(); + throw new RuntimeException("Child Dialog Should NOT Inherit " + + "Parent Dialog's Font Style/Color"); + } + + } + + public void testChildPropertiesWithFrameAsParent() { + + parentFrame = new Frame("parent Frame"); + parentFrame.setSize(WIDTH, HEIGHT); + parentFrame.setLocation(100, 400); + parentFrame.setBackground(Color.BLUE); + parentLabel = new Label("ParentForegroundAndFont"); + parentFont = new Font("Courier New", Font.ITALIC, 15); + parentFrame.setForeground(Color.RED); + parentFrame.setFont(parentFont); + parentFrame.add(parentLabel); + parentFrame.setVisible(true); + + frameChildDialog = new Dialog(parentFrame, "Frame's child"); + frameChildDialog.setSize(WIDTH, HEIGHT); + frameChildDialog.setLocation(WIDTH + 200, 400); + childLabel = new Label("ChildForegroundAndFont"); + frameChildDialog.add(childLabel); + frameChildDialog.setVisible(true); + + if (parentFrame.getBackground() == frameChildDialog.getBackground()) { + dispose(); + throw new RuntimeException("Child Dialog Should NOT Inherit " + + "Parent Frame's Background Color"); + } + + if (parentFrame.getForeground() == frameChildDialog.getForeground()) { + dispose(); + throw new RuntimeException("Child Dialog Should NOT Inherit " + + "Parent Frame's Foreground Color"); + } + + if (parentFrame.getFont() == frameChildDialog.getFont()) { + dispose(); + throw new RuntimeException("Child Dialog Should NOT Inherit " + + "Parent Frame's Font Style/Color"); + } + } + + private void dispose() { + + if (parentDialog != null) { + parentDialog.dispose(); + } + if (parentFrame != null) { + parentFrame.dispose(); + } + } + + public static void main(String[] args) throws Exception { + + ChildDialogProperties obj = new ChildDialogProperties(); + // TestCase1: When Parent is Dialog, Child is Dialog + obj.testChildPropertiesWithDialogAsParent(); + // TestCase2: When Parent is Frame, chis is Dialog + obj.testChildPropertiesWithFrameAsParent(); + obj.dispose(); + } + +} diff --git a/jdk/test/java/awt/Window/ChildProperties/ChildWindowProperties.java b/jdk/test/java/awt/Window/ChildProperties/ChildWindowProperties.java new file mode 100644 index 00000000000..a6a23184d39 --- /dev/null +++ b/jdk/test/java/awt/Window/ChildProperties/ChildWindowProperties.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + /* + @test + @bug 8057574 + @summary Verify that child Window does not inherit parent's Properties + @run main ChildWindowProperties + */ +import java.awt.Color; +import java.awt.Dialog; +import java.awt.Font; +import java.awt.Frame; +import java.awt.Label; +import java.awt.Panel; +import java.awt.Window; + +public class ChildWindowProperties { + + private Dialog parentDialog; + private Window windowChild; + private Frame parentFrame; + private Window frameChildWindow; + private Label parentLabel; + private Font parentFont; + private Label childLabel; + + private static final int WIDTH = 200; + private static final int HEIGHT = 200; + + public void testChildPropertiesWithDialogAsParent() { + + parentDialog = new Dialog((Dialog) null, "parent Dialog"); + parentDialog.setSize(WIDTH, HEIGHT); + parentDialog.setLocation(100, 100); + parentDialog.setBackground(Color.RED); + parentLabel = new Label("ParentForegroundAndFont"); + parentFont = new Font("Courier New", Font.ITALIC, 15); + parentDialog.setForeground(Color.BLUE); + parentDialog.setFont(parentFont); + + parentDialog.add(parentLabel); + parentDialog.setVisible(true); + + windowChild = new Window(parentDialog); + windowChild.setSize(WIDTH, HEIGHT); + windowChild.setLocation(WIDTH + 200, 100); + childLabel = new Label("ChildForegroundAndFont"); + windowChild.add(childLabel); + windowChild.setVisible(true); + + if (parentDialog.getBackground() == windowChild.getBackground()) { + dispose(); + throw new RuntimeException("Child Window Should NOT Inherit " + + "Parent Dialog's Background Color"); + } + if (parentDialog.getForeground() == windowChild.getForeground()) { + dispose(); + throw new RuntimeException("Child Window Should NOT Inherit " + + "Parent Dialog's Foreground Color"); + } + if (parentDialog.getFont() == windowChild.getFont()) { + dispose(); + throw new RuntimeException("Child Window Should NOT Inherit " + + "Parent Dialog's Font Color"); + } + } + + public void testChildPropertiesWithFrameAsParent() { + + parentFrame = new Frame("parent Frame"); + parentFrame.setSize(WIDTH, HEIGHT); + parentFrame.setLocation(100, 400); + parentFrame.setBackground(Color.BLUE); + parentLabel = new Label("ParentForegroundAndFont"); + parentFont = new Font("Courier New", Font.ITALIC, 15); + parentFrame.setForeground(Color.RED); + parentFrame.setFont(parentFont); + parentFrame.add(parentLabel); + parentFrame.setVisible(true); + + frameChildWindow = new Window(parentFrame); + frameChildWindow.setSize(WIDTH, HEIGHT); + frameChildWindow.setLocation(WIDTH + 200, 400); + childLabel = new Label("ChildForegroundAndFont"); + frameChildWindow.add(childLabel); + frameChildWindow.setVisible(true); + + if (parentFrame.getBackground() == frameChildWindow.getBackground()) { + dispose(); + throw new RuntimeException("Child Window Should NOT Inherit " + + "Parent Frame's Background Color"); + } + if (parentDialog.getForeground() == windowChild.getForeground()) { + dispose(); + throw new RuntimeException("Child Window Should NOT Inherit " + + "Parent Frame's Foreground Color"); + } + if (parentDialog.getFont() == windowChild.getFont()) { + dispose(); + throw new RuntimeException("Child Window Should NOT Inherit " + + "Parent Frame's Font Color"); + } + } + + public void testPanelBackground() { + Window window = new Frame(); + window.setBackground(Color.GREEN); + Panel panel = new Panel(); + window.add(panel); + window.pack(); + window.setVisible(true); + if (panel.getBackground() != Color.GREEN) { + window.dispose(); + throw new RuntimeException("Panel Background Color Not Valid"); + } + window.dispose(); + } + + private void dispose() { + + if (parentDialog != null) { + parentDialog.dispose(); + } + if (parentFrame != null) { + parentFrame.dispose(); + } + } + + public static void main(String[] args) throws Exception { + + ChildWindowProperties obj = new ChildWindowProperties(); + // TestCase1: When Parent is Dialog, Child is Window + obj.testChildPropertiesWithDialogAsParent(); + // TestCase2: When Parent is Frame, chis is Window + obj.testChildPropertiesWithFrameAsParent(); + // TestCase3: Panel Background Test + obj.testPanelBackground(); + obj.dispose(); + } + +} +