Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/SunGraphicsEnvironment/TestSGEuseAlternateFontforJALocales.java
38840 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* @run main/othervm TestSGEuseAlternateFontforJALocales31* @run main/othervm -Dfile.encoding=windows-31j -Duser.language=ja -Duser.country=JA TestSGEuseAlternateFontforJALocales32*33*/3435import java.lang.reflect.Method;36import java.nio.charset.Charset;37import java.util.Locale;38import java.awt.Font;39import java.awt.FontMetrics;40import java.awt.Graphics2D;41import java.awt.GraphicsEnvironment;42import java.awt.image.BufferedImage;4344public class TestSGEuseAlternateFontforJALocales {4546public static void main(String args[]) throws Exception {47System.out.println("Default Charset = "48+ Charset.defaultCharset().name());49System.out.println("Locale = " + Locale.getDefault());50String os = System.getProperty("os.name");51String encoding = System.getProperty("file.encoding");52/* Want to test the JA locale uses alternate font for DialogInput. */53boolean jaTest = encoding.equalsIgnoreCase("windows-31j");54if (!os.startsWith("Win") && jaTest) {55System.out.println("Skipping Windows only test");56return;57}5859String className = "sun.java2d.SunGraphicsEnvironment";60String methodName = "useAlternateFontforJALocales";61Class sge = Class.forName(className);62Method uafMethod = sge.getMethod(methodName, (Class[])null);63Object ret = uafMethod.invoke(null);64GraphicsEnvironment ge =65GraphicsEnvironment.getLocalGraphicsEnvironment();66ge.preferLocaleFonts();67ge.preferProportionalFonts();68if (jaTest) {69Font msMincho = new Font("MS Mincho", Font.PLAIN, 12);70if (!msMincho.getFamily(Locale.ENGLISH).equals("MS Mincho")) {71System.out.println("MS Mincho not installed. Skipping test");72return;73}74Font dialogInput = new Font("DialogInput", Font.PLAIN, 12);75Font courierNew = new Font("Courier New", Font.PLAIN, 12);76Font msGothic = new Font("MS Gothic", Font.PLAIN, 12);77BufferedImage bi = new BufferedImage(1,1,1);78Graphics2D g2d = bi.createGraphics();79FontMetrics cnMetrics = g2d.getFontMetrics(courierNew);80FontMetrics diMetrics = g2d.getFontMetrics(dialogInput);81FontMetrics mmMetrics = g2d.getFontMetrics(msMincho);82FontMetrics mgMetrics = g2d.getFontMetrics(msGothic);83// This tests to make sure we at least have applied84// "preferLocaleFonts for Japanese85if (cnMetrics.charWidth('A') == diMetrics.charWidth('A')) {86throw new RuntimeException87("Courier New should not be used for DialogInput");88}89// This is supposed to make sure we are using MS Mincho instead90// of MS Gothic. However they are metrics identical so its91// not definite proof.92if (diMetrics.charWidth('A') != mmMetrics.charWidth('A')) {93throw new RuntimeException94("MS Mincho should be used for DialogInput");95}96}97}98}99100101