jdk/test/javax/swing/text/html/StyleSheet/BackgroundImage/BackgroundImagePosition.java
author serb
Fri, 21 Jul 2017 16:27:35 -0700
changeset 47160 ac5434728c3b
permissions -rw-r--r--
8134256: copy/paste duplicated tests in some condition statements Reviewed-by: azvegint
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47160
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     1
/*
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     4
 *
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     8
 *
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    13
 * accompanied this code).
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    14
 *
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    18
 *
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    21
 * questions.
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    22
 */
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    23
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    24
import java.awt.Color;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    25
import java.awt.EventQueue;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    26
import java.awt.Graphics2D;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    27
import java.awt.image.BufferedImage;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    28
import java.io.File;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    29
import java.io.IOException;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    30
import java.util.List;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    31
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    32
import javax.imageio.ImageIO;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    33
import javax.swing.JEditorPane;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    34
import javax.swing.text.html.HTMLEditorKit;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    35
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    36
/**
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    37
 * @test
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    38
 * @bug 8134256
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    39
 * @summary Different "background-position" should produce different pages
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    40
 */
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    41
public final class BackgroundImagePosition {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    42
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    43
    static final List<String> pos = List.of("", "-2", "-1", "1px", "2px", "3px",
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    44
                                            "1em", "2em", "3em", "10%", "55%",
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    45
                                            "-1em", "-2em", "-3em", "-10%");
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    46
    static final int SIZE = 300;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    47
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    48
    public static void main(final String[] args) throws Exception {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    49
        // The image which we will place on the page
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    50
        final BufferedImage bi = new BufferedImage(50, 50,
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    51
                                                   BufferedImage.TYPE_INT_ARGB);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    52
        final Graphics2D g = bi.createGraphics();
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    53
        g.setColor(Color.GREEN);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    54
        g.fillRect(0, 0, 50, 50);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    55
        g.dispose();
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    56
        final File file = new File("file.png");
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    57
        ImageIO.write(bi, "png", file);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    58
        for (final String x : pos) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    59
            final BufferedImage img1 = test(x, x);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    60
            for (final String y : pos) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    61
                // Different positions should produce different pages
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    62
                if (!x.equals(y)) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    63
                    compareImages(img1, test(x, y));
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    64
                    compareImages(img1, test(y, x));
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    65
                }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    66
            }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    67
        }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    68
    }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    69
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    70
    /**
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    71
     * Throws an exception if the images are the same.
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    72
     */
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    73
    static void compareImages(final BufferedImage img1,
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    74
                              final BufferedImage img2) throws IOException {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    75
        for (int imgX = 0; imgX < SIZE; ++imgX) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    76
            for (int imgY = 0; imgY < SIZE; ++imgY) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    77
                if (img1.getRGB(imgX, imgY) != img2.getRGB(imgX, imgY)) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    78
                    return;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    79
                }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    80
            }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    81
        }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    82
        ImageIO.write(img1, "png", new File("img1.png"));
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    83
        ImageIO.write(img2, "png", new File("img2.png"));
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    84
        throw new RuntimeException("Same images for different size");
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    85
    }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    86
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    87
    static BufferedImage test(final String x, final String y) throws Exception {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    88
        final BufferedImage bi = new BufferedImage(SIZE, SIZE,
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    89
                                                   BufferedImage.TYPE_INT_ARGB);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    90
        EventQueue.invokeAndWait(() -> {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    91
            try {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    92
                final JEditorPane jep = new JEditorPane();
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    93
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    94
                final HTMLEditorKit kit = new HTMLEditorKit();
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    95
                jep.setEditorKit(kit);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    96
                jep.setText(
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    97
                        "<style>body {"
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    98
                                + "background-image: url(file:./file.png);"
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
    99
                                + "background-repeat: no-repeat;"
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   100
                                + "background-position: " + x + " " + y + ";"
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   101
                                + "background-color: #FF0000;"
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   102
                                + "}</style><body></body>");
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   103
                jep.setSize(SIZE, SIZE);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   104
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   105
                final Graphics2D graphics = bi.createGraphics();
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   106
                jep.paint(graphics);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   107
                graphics.dispose();
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   108
            } catch (final Exception e) {
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   109
                throw new RuntimeException(e);
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   110
            }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   111
        });
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   112
        return bi;
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   113
    }
ac5434728c3b 8134256: copy/paste duplicated tests in some condition statements
serb
parents:
diff changeset
   114
}