/jdk/test/sun/java2d/SunGraphicsEnvironment/TestSGEuseAlternateFontforJALocales.java

https://github.com/Morriar/ProxyJDK · Java · 99 lines · 55 code · 5 blank · 39 comment · 8 complexity · 9c368dfbcb22ac6b464cd7c2f15e2688 MD5 · raw file

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