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