test/jdk/javax/swing/JLayer/8041982/bug8041982.java
author serb
Fri, 16 Feb 2018 12:24:38 -0800
changeset 49090 82c1fe23c469
parent 47216 71c04702a3d5
child 50493 8a3b993c6adc
permissions -rw-r--r--
8196196: Headful tests should not be run in headless mode Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26029
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     1
/*
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     4
 *
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     8
 *
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    13
 * accompanied this code).
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    14
 *
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    18
 *
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    21
 * questions.
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    22
 */
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    23
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    24
/* @test
49090
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
    25
 * @key headful
26029
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    26
 * @bug 8041982
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    27
 * @summary Use of animated icon in JLayer causes CPU spin
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    28
 * @author Alexander Potochkin
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    29
 */
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    30
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    31
import javax.swing.*;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    32
import javax.swing.plaf.LayerUI;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    33
import java.awt.*;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    34
import java.beans.PropertyChangeEvent;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    35
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    36
public class bug8041982 extends JFrame {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    37
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    38
    public bug8041982() {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    39
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    40
        add(new JLayer<>(new JPanel(), new BusyLayer()));
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    41
        setSize(200, 300);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    42
        setVisible(true);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    43
    }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    44
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    45
    public static void main(String... args) throws Exception {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    46
        SwingUtilities.invokeLater(new Runnable() {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    47
            public void run() {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    48
                new bug8041982().setVisible(true);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    49
            }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    50
        });
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    51
        Thread.sleep(5000);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    52
    }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    53
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    54
    private class BusyLayer extends LayerUI<JComponent> {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    55
        private volatile boolean animated = true;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    56
        private Icon icon = new ImageIcon(bug8041982.class.getResource("cupanim.gif"));
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    57
        private int imageUpdateCount;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    58
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    59
        @Override
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    60
        public void paint(Graphics g, JComponent c) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    61
            super.paint(g, c);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    62
            if (isAnimated()) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    63
                icon.paintIcon(c, g, c.getWidth() / 2 - icon.getIconWidth() /
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    64
                        2,
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    65
                        c.getHeight() / 2 - icon.getIconHeight() / 2);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    66
            }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    67
        }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    68
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    69
        public boolean isAnimated() {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    70
            return animated;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    71
        }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    72
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    73
        public void setAnimated(boolean animated) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    74
            if (this.animated != animated) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    75
                this.animated = animated;
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    76
                firePropertyChange("animated", !animated, animated);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    77
            }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    78
        }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    79
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    80
        @Override
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    81
        public void applyPropertyChange(PropertyChangeEvent evt, JLayer l) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    82
            // this will be called when the busy flag is changed
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    83
            l.repaint();
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    84
        }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    85
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    86
        @Override
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    87
        public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h, JLayer<? extends JComponent> l) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    88
            System.out.println("imageUpdate " + imageUpdateCount);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    89
            if (imageUpdateCount++ == 100) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    90
                setAnimated(false);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    91
            } else if (imageUpdateCount > 100) {
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    92
                throw new RuntimeException("Test failed");
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    93
            }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    94
            return isAnimated() && super.imageUpdate(img, infoflags, x, y, w, h, l);
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    95
        }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    96
    }
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    97
}
730f9bc4b85e 8041982: Use of animated icon in JLayer causes CPU spin
alexp
parents:
diff changeset
    98