Path: blob/master/test/jdk/sun/java2d/DirectX/StrikeDisposalCrashTest/StrikeDisposalCrashTest.java
41153 views
/*1* Copyright (c) 2007, 2018, 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*/2223/**24* @test25* @key headful26* @bug 6705443 819861327* @summary tests that we don't crash during exit if font strikes were disposed28* during the lifetime of the application29*30* @run main/othervm -Dsun.java2d.font.reftype=weak StrikeDisposalCrashTest31* @run main/othervm -Dsun.java2d.font.reftype=weak -Dsun.java2d.noddraw=true StrikeDisposalCrashTest32*/3334import java.awt.Font;35import java.awt.Frame;36import java.awt.Graphics2D;37import java.awt.GraphicsConfiguration;38import java.awt.GraphicsDevice;39import java.awt.GraphicsEnvironment;40import java.awt.RenderingHints;41import java.awt.Toolkit;42import java.awt.image.VolatileImage;4344public class StrikeDisposalCrashTest {4546public static void main(String[] args) {47System.setProperty("sun.java2d.font.reftype", "weak");4849GraphicsDevice gd[] =50GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();5152Frame frames[] = new Frame[gd.length];53for (int i = 0; i < frames.length; i++) {54GraphicsConfiguration gc = gd[i].getDefaultConfiguration();55Frame f = new Frame("Frame on "+gc, gc);56f.setSize(100, 100);57f.setLocation(gc.getBounds().x, gc.getBounds().y);58f.pack();59frames[i] = f;60}6162Font f1 = new Font("Dialog", Font.PLAIN, 10);63Font f2 = new Font("Dialog", Font.ITALIC, 12);6465for (int i = 0; i < frames.length/2; i++) {66// making sure the glyphs are cached in the accel. cache on67// one frame, then the other68renderText(frames[i], f1);69renderText(frames[frames.length -1 - i], f1);7071// and now the other way around, with different glyphs72renderText(frames[frames.length -1 - i], f2);73renderText(frames[i], f2);74}7576// try to force strike disposal (note that we have to use77// -Dsun.java2d.font.reftype=weak to facilitate the disposal)7879System.gc();80System.runFinalization();81System.gc();82System.runFinalization();8384for (Frame f : frames) {85f.dispose();86}8788System.err.println("Exiting. If the test crashed after this it FAILED");89}9091private static final String text =92"The quick brown fox jumps over the lazy dog 1234567890";93private static void renderText(Frame frame, Font f1) {94VolatileImage vi = frame.createVolatileImage(256, 32);95vi.validate(frame.getGraphicsConfiguration());9697Graphics2D g = vi.createGraphics();98g.setFont(f1);99g.drawString(text, 0, vi.getHeight()/2);100g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,101RenderingHints.VALUE_TEXT_ANTIALIAS_ON);102g.drawString(text, 0, vi.getHeight()/2);103g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,104RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);105g.drawString(text, 0, vi.getHeight()/2);106Toolkit.getDefaultToolkit().sync();107}108}109110111