8144166: [macosx] Test java/awt/Component/CompEventOnHiddenComponent/CompEventOnHiddenComponent.java fails

Reviewed-by: alexsch
This commit is contained in:
Sergey Bylokhov 2016-04-05 18:11:12 +03:00
parent d5b317c8db
commit 0cd8d72e1d
3 changed files with 159 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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
@ -40,28 +40,28 @@ import sun.swing.SwingUtilities2;
* From MacDockIconUI
*
* A JRSUI L&F implementation of JInternalFrame.JDesktopIcon
* @author
* @version
*/
public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseListener, MouseMotionListener, ComponentListener {
private static final String CACHED_FRAME_ICON_KEY = "apple.laf.internal.frameIcon";
public final class AquaInternalFrameDockIconUI extends DesktopIconUI
implements MouseListener, MouseMotionListener {
protected JInternalFrame.JDesktopIcon fDesktopIcon;
protected JInternalFrame fFrame;
protected ScaledImageLabel fIconPane;
protected DockLabel fDockLabel;
protected boolean fTrackingIcon = false;
private JInternalFrame.JDesktopIcon fDesktopIcon;
private JInternalFrame fFrame;
private ScaledImageLabel fIconPane;
private DockLabel fDockLabel;
private boolean fTrackingIcon;
public static ComponentUI createUI(final JComponent c) {
return new AquaInternalFrameDockIconUI();
}
@Override
public void installUI(final JComponent c) {
fDesktopIcon = (JInternalFrame.JDesktopIcon)c;
installComponents();
installListeners();
}
@Override
public void uninstallUI(final JComponent c) {
uninstallComponents();
uninstallListeners();
@ -69,55 +69,54 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
fFrame = null;
}
protected void installComponents() {
private void installComponents() {
fFrame = fDesktopIcon.getInternalFrame();
fIconPane = new ScaledImageLabel();
fDesktopIcon.setLayout(new BorderLayout());
fDesktopIcon.add(fIconPane, BorderLayout.CENTER);
}
protected void uninstallComponents() {
private void uninstallComponents() {
fDesktopIcon.setLayout(null);
fDesktopIcon.remove(fIconPane);
}
protected void installListeners() {
private void installListeners() {
fDesktopIcon.addMouseListener(this);
fDesktopIcon.addMouseMotionListener(this);
fFrame.addComponentListener(this);
}
protected void uninstallListeners() {
fFrame.removeComponentListener(this);
private void uninstallListeners() {
fDesktopIcon.removeMouseMotionListener(this);
fDesktopIcon.removeMouseListener(this);
}
@Override
public Dimension getMinimumSize(final JComponent c) {
return new Dimension(32, 32);
}
@Override
public Dimension getMaximumSize(final JComponent c) {
return new Dimension(128, 128);
}
@Override
public Dimension getPreferredSize(final JComponent c) {
return new Dimension(64, 64); //$ Dock preferred size
}
public Insets getInsets(final JComponent c) {
return new Insets(0, 0, 0, 0);
}
void updateIcon() {
fIconPane.updateIcon();
}
@Override
public void mousePressed(final MouseEvent e) {
fTrackingIcon = fIconPane.mouseInIcon(e);
if (fTrackingIcon) fIconPane.repaint();
}
@Override
public void mouseReleased(final MouseEvent e) {// only when it's actually in the image
if (fFrame.isIconifiable() && fFrame.isIcon()) {
if (fTrackingIcon) {
@ -137,6 +136,7 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
if (fDockLabel != null && !fIconPane.getBounds().contains(e.getX(), e.getY())) fDockLabel.hide();
}
@Override
public void mouseEntered(final MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) return;
String title = fFrame.getTitle();
@ -145,41 +145,27 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
fDockLabel.show(fDesktopIcon);
}
@Override
public void mouseExited(final MouseEvent e) {
if (fDockLabel != null && (e.getModifiers() & InputEvent.BUTTON1_MASK) == 0) fDockLabel.hide();
}
@Override
public void mouseClicked(final MouseEvent e) { }
@Override
public void mouseDragged(final MouseEvent e) { }
@Override
public void mouseMoved(final MouseEvent e) { }
public void componentHidden(final ComponentEvent e) { }
public void componentMoved(final ComponentEvent e) { }
public void componentResized(final ComponentEvent e) {
fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, null);
}
public void componentShown(final ComponentEvent e) {
fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, null);
}
@SuppressWarnings("serial") // Superclass is not serializable across versions
class ScaledImageLabel extends JLabel {
private final class ScaledImageLabel extends JLabel {
ScaledImageLabel() {
super(null, null, CENTER);
}
void updateIcon() {
final Object priorIcon = fFrame.getClientProperty(CACHED_FRAME_ICON_KEY);
if (priorIcon instanceof ImageIcon) {
setIcon((ImageIcon)priorIcon);
return;
}
int width = fFrame.getWidth();
int height = fFrame.getHeight();
@ -196,11 +182,10 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
final float scale = (float)fDesktopIcon.getWidth() / (float)Math.max(width, height) * 0.89f;
// Sending in -1 for width xor height causes it to maintain aspect ratio
final ImageIcon icon = new ImageIcon(fImage.getScaledInstance((int)(width * scale), -1, Image.SCALE_SMOOTH));
fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, icon);
setIcon(icon);
setIcon(new ImageIcon(fImage.getScaledInstance((int)(width * scale), -1, Image.SCALE_SMOOTH)));
}
@Override
public void paint(final Graphics g) {
if (getIcon() == null) updateIcon();
@ -222,13 +207,14 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
return getBounds().contains(e.getX(), e.getY());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(64, 64); //$ Dock preferred size
}
}
@SuppressWarnings("serial") // Superclass is not serializable across versions
class DockLabel extends JLabel {
private static final class DockLabel extends JLabel {
static final int NUB_HEIGHT = 7;
static final int ROUND_ADDITIONAL_HEIGHT = 8;
static final int ROUND_ADDITIONAL_WIDTH = 12;
@ -243,6 +229,7 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
setSize(SwingUtilities.computeStringWidth(metrics, getText()) + ROUND_ADDITIONAL_WIDTH * 2, metrics.getAscent() + NUB_HEIGHT + ROUND_ADDITIONAL_HEIGHT);
}
@Override
public void paint(final Graphics g) {
final int width = getWidth();
final int height = getHeight();
@ -303,6 +290,7 @@ public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseL
}
}
@Override
@Deprecated
public void hide() {
final Container parent = getParent();

View File

@ -25,7 +25,7 @@
/*
@test
@bug 6383903
@bug 6383903 8144166
@summary REGRESSION: componentMoved is now getting called for some hidden components
@author andrei.dmitriev: area=awt.component
@run main CompEventOnHiddenComponent

View File

@ -0,0 +1,127 @@
/*
* 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.
*/
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
/**
* @test
* @bug 8144166
* @requires (os.family == "mac")
*/
public final class DockIconRepaint {
private static volatile Color color;
private static JFrame frame;
private static JInternalFrame jif;
private static Robot robot;
private static Point iconLoc;
private static Rectangle iconBounds;
public static void main(final String[] args) throws Exception {
robot = new Robot();
EventQueue.invokeAndWait(DockIconRepaint::createUI);
try {
robot.waitForIdle();
color = Color.BLUE;
test();
color = Color.RED;
test();
color = Color.GREEN;
test();
} finally {
frame.dispose();
}
}
private static void test() throws Exception {
// maximize the frame to force repaint
EventQueue.invokeAndWait(() -> {
try {
jif.setIcon(false);
jif.setMaximum(true);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
});
robot.waitForIdle();
Thread.sleep(1000);
// minimize the frame to dock, the icon should be up2date
EventQueue.invokeAndWait(() -> {
try {
jif.setIcon(true);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
iconLoc = jif.getDesktopIcon().getLocationOnScreen();
iconBounds = jif.getDesktopIcon().getBounds();
});
robot.waitForIdle();
Thread.sleep(1000);
final Color c = robot.getPixelColor(iconLoc.x + iconBounds.width / 2,
iconLoc.y + iconBounds.height / 2);
if (c.getRGB() != color.getRGB()) {
System.err.println("Exp: " + Integer.toHexString(color.getRGB()));
System.err.println("Actual: " + Integer.toHexString(c.getRGB()));
throw new RuntimeException("Wrong color.");
}
}
private static void createUI() {
frame = new JFrame();
frame.setUndecorated(true);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
final JDesktopPane pane = new JDesktopPane();
final JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
g.setColor(color);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
jif = new JInternalFrame();
jif.add(panel);
jif.setVisible(true);
jif.setSize(300, 300);
pane.add(jif);
frame.add(pane);
frame.setVisible(true);
}
}