1 /*
   2  * Copyright (c) 2007, 2008, 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 /**
  25  * @test
  26  * @bug 6705443
  27  * @summary tests that we don't crash during exit if font strikes were disposed
  28  * during the lifetime of the application
  29  *
  30  * @run main/othervm -Dsun.java2d.font.reftype=weak StrikeDisposalCrashTest
  31  * @run main/othervm -Dsun.java2d.font.reftype=weak -Dsun.java2d.noddraw=true StrikeDisposalCrashTest
  32  * @run main/othervm -Dsun.java2d.font.reftype=weak -Dsun.java2d.opengl=True StrikeDisposalCrashTest
  33  */
  34 
  35 import java.awt.Font;
  36 import java.awt.Frame;
  37 import java.awt.Graphics2D;
  38 import java.awt.GraphicsConfiguration;
  39 import java.awt.GraphicsDevice;
  40 import java.awt.GraphicsEnvironment;
  41 import java.awt.RenderingHints;
  42 import java.awt.Toolkit;
  43 import java.awt.image.VolatileImage;
  44 
  45 public class StrikeDisposalCrashTest {
  46 
  47     public static void main(String[] args) {
  48         System.setProperty("sun.java2d.font.reftype", "weak");
  49 
  50         GraphicsDevice gd[] =
  51             GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  52 
  53         Frame frames[] = new Frame[gd.length];
  54         for (int i = 0; i < frames.length; i++) {
  55             GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
  56             Frame f = new Frame("Frame on "+gc, gc);
  57             f.setSize(100, 100);
  58             f.setLocation(gc.getBounds().x, gc.getBounds().y);
  59             f.pack();
  60             frames[i] = f;
  61         }
  62 
  63         Font f1 = new Font("Dialog", Font.PLAIN, 10);
  64         Font f2 = new Font("Dialog", Font.ITALIC, 12);
  65 
  66         for (int i = 0; i < frames.length/2; i++) {
  67             // making sure the glyphs are cached in the accel. cache on
  68             // one frame, then the other
  69             renderText(frames[i], f1);
  70             renderText(frames[frames.length -1 - i], f1);
  71 
  72             // and now the other way around, with different glyphs
  73             renderText(frames[frames.length -1 - i], f2);
  74             renderText(frames[i], f2);
  75         }
  76 
  77         // try to force strike disposal (note that we have to use
  78         // -Dsun.java2d.font.reftype=weak to facilitate the disposal)
  79 
  80         System.gc();
  81         System.runFinalization();
  82         System.gc();
  83         System.runFinalization();
  84 
  85         for (Frame f : frames) {
  86             f.dispose();
  87         }
  88 
  89         System.err.println("Exiting. If the test crashed after this it FAILED");
  90     }
  91 
  92     private static final String text =
  93         "The quick brown fox jumps over the lazy dog 1234567890";
  94     private static void renderText(Frame frame, Font f1) {
  95         VolatileImage vi = frame.createVolatileImage(256, 32);
  96         vi.validate(frame.getGraphicsConfiguration());
  97 
  98         Graphics2D g = vi.createGraphics();
  99         g.setFont(f1);
 100         g.drawString(text, 0, vi.getHeight()/2);
 101         g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 102                            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 103         g.drawString(text, 0, vi.getHeight()/2);
 104         g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 105                            RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
 106         g.drawString(text, 0, vi.getHeight()/2);
 107         Toolkit.getDefaultToolkit().sync();
 108     }
 109 }