test/jdk/javax/swing/JCheckBox/8032667/bug8032667_image_diff.java
author coleenp
Mon, 30 Sep 2019 13:10:11 -0400
changeset 58409 a595e67d6683
parent 51637 e9177e7749e7
permissions -rw-r--r--
8184732: Deadlock detection improvements for 'special' locks Summary: Assert that special ranked locks cannot safepoint and allow_vm_block and remove locks from the exceptional lock list in no_safepoint_verifier. Reviewed-by: dholmes, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24138
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     1
/*
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
24138
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     4
 *
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     7
 * published by the Free Software Foundation.
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     8
 *
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    13
 * accompanied this code).
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    14
 *
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    18
 *
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    21
 * questions.
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    22
 */
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    23
import java.awt.Dimension;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    24
import java.awt.Graphics;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    25
import java.awt.Graphics2D;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    26
import java.awt.Image;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    27
import java.awt.image.BufferedImage;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    28
import javax.swing.JCheckBox;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    29
import javax.swing.JComponent;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    30
import javax.swing.SwingUtilities;
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 47216
diff changeset
    31
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 47216
diff changeset
    32
import jdk.test.lib.Platform;
24138
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    33
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    34
/* @test
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    35
 * @bug 8032667
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    36
 * @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 47216
diff changeset
    37
 * @library /test/lib
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 47216
diff changeset
    38
 * @build jdk.test.lib.Platform
24138
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    39
 * @run main bug8032667_image_diff
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    40
 */
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    41
public class bug8032667_image_diff {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    42
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    43
    static final int IMAGE_WIDTH = 130;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    44
    static final int IMAGE_HEIGHT = 50;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    45
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    47
51637
e9177e7749e7 8210039: move OSInfo to top level testlibrary
iignatyev
parents: 47216
diff changeset
    48
        if (!Platform.isOSX()) {
24138
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    49
            return;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    50
        }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    51
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    52
        SwingUtilities.invokeAndWait(new Runnable() {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    53
            @Override
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    54
            public void run() {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    55
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    56
                JCheckBox checkBox = new JCheckBox();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    57
                checkBox.setSelected(true);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    58
                checkBox.setSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    59
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    60
                final BufferedImage image1 = getHiDPIImage(checkBox);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    61
                final BufferedImage image2 = getScaledImage(checkBox);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    62
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    63
                if(equal(image1, image2)){
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    64
                    throw new RuntimeException("2x image equals to non smooth image");
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    65
                }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    66
            }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    67
        });
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    68
    }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    69
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    70
    static boolean equal(BufferedImage image1, BufferedImage image2) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    71
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    72
        int w = image1.getWidth();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    73
        int h = image1.getHeight();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    74
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    75
        if (w != image2.getWidth() || h != image2.getHeight()) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    76
            return false;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    77
        }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    78
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    79
        for (int i = 0; i < w; i++) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    80
            for (int j = 0; j < h; j++) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    81
                int color1 = image1.getRGB(i, j);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    82
                int color2 = image2.getRGB(i, j);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    83
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    84
                if (color1 != color2) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    85
                    return false;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    86
                }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    87
            }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    88
        }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    89
        return true;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    90
    }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    91
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    92
    static BufferedImage getHiDPIImage(JComponent component) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    93
        return getImage(component, 2, IMAGE_WIDTH, IMAGE_HEIGHT);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    94
    }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    95
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    96
    static BufferedImage getScaledImage(JComponent component) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    97
        Image image1x = getImage(component, 1, IMAGE_WIDTH, IMAGE_HEIGHT);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    98
        final BufferedImage image2x = new BufferedImage(
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
    99
                2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   100
        final Graphics g = image2x.getGraphics();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   101
        ((Graphics2D) g).scale(2, 2);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   102
        g.drawImage(image1x, 0, 0, null);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   103
        g.dispose();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   104
        return image2x;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   105
    }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   106
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   107
    static BufferedImage getImage(JComponent component, int scale, int width, int height) {
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   108
        final BufferedImage image = new BufferedImage(
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   109
                scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   110
        final Graphics g = image.getGraphics();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   111
        ((Graphics2D) g).scale(scale, scale);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   112
        component.paint(g);
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   113
        g.dispose();
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   114
        return image;
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   115
    }
93734b110121 8032667: [macosx] Components cannot be rendered in HiDPI to BufferedImage
alexsch
parents:
diff changeset
   116
}