From 3a213c4a3d616afa9a0c8d6512d886e5e48beb50 Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Fri, 21 Jul 2017 16:27:35 -0700 Subject: [PATCH] 8134256: copy/paste duplicated tests in some condition statements Reviewed-by: azvegint --- .../plaf/nimbus/AbstractRegionPainter.java | 4 +- .../javax/swing/text/html/StyleSheet.java | 4 +- .../PaintContextScaleValidation.java | 64 ++++++++++ .../BackgroundImagePosition.java | 114 ++++++++++++++++++ 4 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 jdk/test/javax/swing/plaf/nimbus/AbstractRegionPainter/PaintContextScaleValidation.java create mode 100644 jdk/test/javax/swing/text/html/StyleSheet/BackgroundImage/BackgroundImagePosition.java diff --git a/jdk/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java b/jdk/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java index acc83c4739f..946573c10f3 100644 --- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java +++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, 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 @@ -559,7 +559,7 @@ public abstract class AbstractRegionPainter implements Painter { */ public PaintContext(Insets insets, Dimension canvasSize, boolean inverted, CacheMode cacheMode, double maxH, double maxV) { - if (maxH < 1 || maxH < 1) { + if (maxH < 1 || maxV < 1) { throw new IllegalArgumentException("Both maxH and maxV must be >= 1"); } diff --git a/jdk/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java b/jdk/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java index 07e18dc8765..9f3208c3fe1 100644 --- a/jdk/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java +++ b/jdk/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2017, 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 @@ -2538,7 +2538,7 @@ public class StyleSheet extends StyleContext { if (pos.isHorizontalPositionRelativeToSize()) { flags |= 4; } - else if (pos.isHorizontalPositionRelativeToSize()) { + else if (pos.isHorizontalPositionRelativeToFontSize()) { hPosition *= CSS.getFontSize(a, 12, ss); } if (pos.isVerticalPositionRelativeToSize()) { diff --git a/jdk/test/javax/swing/plaf/nimbus/AbstractRegionPainter/PaintContextScaleValidation.java b/jdk/test/javax/swing/plaf/nimbus/AbstractRegionPainter/PaintContextScaleValidation.java new file mode 100644 index 00000000000..1a665e19c07 --- /dev/null +++ b/jdk/test/javax/swing/plaf/nimbus/AbstractRegionPainter/PaintContextScaleValidation.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017, 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.Dimension; +import java.awt.Graphics2D; +import java.awt.Insets; + +import javax.swing.JComponent; +import javax.swing.plaf.nimbus.AbstractRegionPainter; + +/** + * @test + * @bug 8134256 + */ +public final class PaintContextScaleValidation extends AbstractRegionPainter { + + public static void main(final String[] args) { + final PaintContextScaleValidation t = new PaintContextScaleValidation(); + t.test(0, 0); + t.test(0, 1); + t.test(1, 0); + } + + private void test(final double maxH, final double maxV) { + try { + new PaintContext(new Insets(1, 1, 1, 1), new Dimension(1, 1), false, + null, maxH, maxV); + } catch (final IllegalArgumentException ignored) { + return; // expected exception + } + throw new RuntimeException("IllegalArgumentException was not thrown"); + } + + @Override + protected PaintContext getPaintContext() { + return null; + } + + @Override + protected void doPaint(final Graphics2D g, final JComponent c, + final int width, final int height, + final Object[] extendedCacheKeys) { + } +} diff --git a/jdk/test/javax/swing/text/html/StyleSheet/BackgroundImage/BackgroundImagePosition.java b/jdk/test/javax/swing/text/html/StyleSheet/BackgroundImage/BackgroundImagePosition.java new file mode 100644 index 00000000000..207ac4bafa0 --- /dev/null +++ b/jdk/test/javax/swing/text/html/StyleSheet/BackgroundImage/BackgroundImagePosition.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2017, 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.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.List; + +import javax.imageio.ImageIO; +import javax.swing.JEditorPane; +import javax.swing.text.html.HTMLEditorKit; + +/** + * @test + * @bug 8134256 + * @summary Different "background-position" should produce different pages + */ +public final class BackgroundImagePosition { + + static final List pos = List.of("", "-2", "-1", "1px", "2px", "3px", + "1em", "2em", "3em", "10%", "55%", + "-1em", "-2em", "-3em", "-10%"); + static final int SIZE = 300; + + public static void main(final String[] args) throws Exception { + // The image which we will place on the page + final BufferedImage bi = new BufferedImage(50, 50, + BufferedImage.TYPE_INT_ARGB); + final Graphics2D g = bi.createGraphics(); + g.setColor(Color.GREEN); + g.fillRect(0, 0, 50, 50); + g.dispose(); + final File file = new File("file.png"); + ImageIO.write(bi, "png", file); + for (final String x : pos) { + final BufferedImage img1 = test(x, x); + for (final String y : pos) { + // Different positions should produce different pages + if (!x.equals(y)) { + compareImages(img1, test(x, y)); + compareImages(img1, test(y, x)); + } + } + } + } + + /** + * Throws an exception if the images are the same. + */ + static void compareImages(final BufferedImage img1, + final BufferedImage img2) throws IOException { + for (int imgX = 0; imgX < SIZE; ++imgX) { + for (int imgY = 0; imgY < SIZE; ++imgY) { + if (img1.getRGB(imgX, imgY) != img2.getRGB(imgX, imgY)) { + return; + } + } + } + ImageIO.write(img1, "png", new File("img1.png")); + ImageIO.write(img2, "png", new File("img2.png")); + throw new RuntimeException("Same images for different size"); + } + + static BufferedImage test(final String x, final String y) throws Exception { + final BufferedImage bi = new BufferedImage(SIZE, SIZE, + BufferedImage.TYPE_INT_ARGB); + EventQueue.invokeAndWait(() -> { + try { + final JEditorPane jep = new JEditorPane(); + + final HTMLEditorKit kit = new HTMLEditorKit(); + jep.setEditorKit(kit); + jep.setText( + ""); + jep.setSize(SIZE, SIZE); + + final Graphics2D graphics = bi.createGraphics(); + jep.paint(graphics); + graphics.dispose(); + } catch (final Exception e) { + throw new RuntimeException(e); + } + }); + return bi; + } +}