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