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