Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Graphics2D/DrawString/TextRenderingTest.java
38828 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Color;24import java.awt.Graphics2D;25import java.awt.GraphicsConfiguration;26import java.awt.GraphicsEnvironment;27import java.awt.GradientPaint;28import java.awt.geom.Point2D;2930import java.awt.Font;3132import java.awt.image.BufferedImage;33import java.awt.image.VolatileImage;343536/*37* @test38* @bug 7189452 802476739* @summary Check if source offset for text rendering is handled correctly40* (shouldn't see the text on a similarly colored background).41* @author a.stepanov42* @run main TextRenderingTest43*/4445public class TextRenderingTest {4647private static final int width = 450;48private static final int height = 150;4950public static void main(final String[] args) {5152GraphicsEnvironment ge =53GraphicsEnvironment.getLocalGraphicsEnvironment();54GraphicsConfiguration gc =55ge.getDefaultScreenDevice().getDefaultConfiguration();56VolatileImage vi = gc.createCompatibleVolatileImage(width, height);5758while (true) {59vi.validate(gc);60Graphics2D g2d = vi.createGraphics();61g2d.setColor(Color.white);62g2d.fillRect(0, 0, width, height);6364g2d.setPaint(new GradientPaint(65new Point2D.Float(0, height / 2), Color.white,66new Point2D.Float(width, height / 2), Color.black));67g2d.fillRect(0, 0, width, height);6869String fnt = g2d.getFont().getFamily();70g2d.setFont(new Font(fnt, Font.PLAIN, 100));71g2d.drawString("IIIIIIIIII", 100, 100); // draw text with offset7273g2d.dispose();7475if (vi.validate(gc) != VolatileImage.IMAGE_OK) {76try {77Thread.sleep(100);78} catch (InterruptedException e) {}79continue;80}8182if (vi.contentsLost()) {83try {84Thread.sleep(100);85} catch (InterruptedException e) {}86continue;87}8889break;90}9192BufferedImage bi = vi.getSnapshot();9394// the text shifted shouldn't be visible onto a painted rectangle!95// so the check: color component (blue) must decrease monotonously9697int prev = Integer.MAX_VALUE;98for (int x = 0; x < width; ++x) {99int color = bi.getRGB(x, height / 2);100int b = color & 0xFF;101102if (b > prev) {103throw new RuntimeException("test failed: can see the text rendered!");104}105106prev = b;107}108}109}110111112