1 /*
   2  * Copyright (c) 2011, 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 7032930
  27  *
  28  * @summary verify the existence of the  method
  29  *           SunGraphicsEnvironment.useAlternateFontforJALocales
  30  *
  31  * @run main/othervm TestSGEuseAlternateFontforJALocales
  32  * @run main/othervm -Dfile.encoding=windows-31j -Duser.language=ja -Duser.country=JA TestSGEuseAlternateFontforJALocales
  33  *
  34  */
  35 
  36 import java.lang.reflect.Method;
  37 import java.nio.charset.Charset;
  38 import java.util.Locale;
  39 import java.awt.Font;
  40 import java.awt.FontMetrics;
  41 import java.awt.Graphics2D;
  42 import java.awt.GraphicsEnvironment;
  43 import java.awt.image.BufferedImage;
  44 
  45 public class TestSGEuseAlternateFontforJALocales {
  46 
  47     public static void main(String args[]) throws Exception {
  48         System.out.println("Default Charset = "
  49             + Charset.defaultCharset().name());
  50         System.out.println("Locale = " + Locale.getDefault());
  51         String os = System.getProperty("os.name");
  52         String encoding = System.getProperty("file.encoding");
  53         /* Want to test the JA locale uses alternate font for DialogInput. */
  54         boolean jaTest = encoding.equalsIgnoreCase("windows-31j");
  55         if (!os.startsWith("Win") && jaTest) {
  56             System.out.println("Skipping Windows only test");
  57             return;
  58         }
  59 
  60         String className = "sun.java2d.SunGraphicsEnvironment";
  61         String methodName = "useAlternateFontforJALocales";
  62         Class sge = Class.forName(className);
  63         Method uafMethod = sge.getMethod(methodName, (Class[])null);
  64         Object ret = uafMethod.invoke(null);
  65         GraphicsEnvironment ge =
  66             GraphicsEnvironment.getLocalGraphicsEnvironment();
  67         ge.preferLocaleFonts();
  68         ge.preferProportionalFonts();
  69         if (jaTest) {
  70             Font msMincho = new Font("MS Mincho", Font.PLAIN, 12);
  71             if (!msMincho.getFamily(Locale.ENGLISH).equals("MS Mincho")) {
  72                  System.out.println("MS Mincho not installed. Skipping test");
  73                  return;
  74             }
  75             Font dialogInput = new Font("DialogInput", Font.PLAIN, 12);
  76             Font courierNew = new Font("Courier New", Font.PLAIN, 12);
  77             Font msGothic = new Font("MS Gothic", Font.PLAIN, 12);
  78             BufferedImage bi = new BufferedImage(1,1,1);
  79             Graphics2D g2d = bi.createGraphics();
  80             FontMetrics cnMetrics = g2d.getFontMetrics(courierNew);
  81             FontMetrics diMetrics = g2d.getFontMetrics(dialogInput);
  82             FontMetrics mmMetrics = g2d.getFontMetrics(msMincho);
  83             FontMetrics mgMetrics = g2d.getFontMetrics(msGothic);
  84             // This tests to make sure we at least have applied
  85             //  "preferLocaleFonts for Japanese
  86             if (cnMetrics.charWidth('A') == diMetrics.charWidth('A')) {
  87                  throw new RuntimeException
  88                        ("Courier New should not be used for DialogInput");
  89             }
  90             // This is supposed to make sure we are using MS Mincho instead
  91             //  of MS Gothic. However they are metrics identical so its
  92             // not definite proof.
  93             if (diMetrics.charWidth('A') != mmMetrics.charWidth('A')) {
  94                  throw new RuntimeException
  95                      ("MS Mincho should be used for DialogInput");
  96             }
  97        }
  98    }
  99 }