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