1 /*
   2  * Copyright (c) 2011, 2013, 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  * @summary
  27  * @summary com.apple.junit.java.text;
  28  * @run junit JapaneseTest
  29  */
  30 
  31 import java.awt.*;
  32 import java.awt.image.BufferedImage;
  33 
  34 import javax.swing.JFrame;
  35 import javax.swing.JPanel;
  36 import javax.swing.SwingUtilities;
  37 
  38 import junit.framework.*;
  39 
  40 public class JapaneseTest extends TestCase {
  41 
  42     public void testJapaneseText() {
  43         boolean failed = true;
  44         JFrame f = new JFrame("Japanese Test");
  45         f.setBounds(0, 0, 300, 200);
  46         TextPanel textPanel = new TextPanel();
  47         f.getContentPane().add(textPanel, BorderLayout.CENTER);
  48         f.setVisible(true);
  49         f.toFront();
  50         Robot robot;
  51         try {
  52             robot = new Robot();
  53             Thread.sleep(1000);
  54             Point p = textPanel.getLocationOfText();
  55             SwingUtilities.convertPointToScreen(p, textPanel);
  56             Rectangle r = new Rectangle(p.x - 20, p.y - 20, 100, 60);
  57             BufferedImage screenshot = robot.createScreenCapture(r);
  58             int pixels[] = (int[]) screenshot.getData().getDataElements(0, 0,
  59                     100, 60, null);
  60             int black = Color.BLACK.getRGB();
  61             for (int i = 0; pixels != null && i < pixels.length; i++) {
  62                 if (pixels[i] != black) {
  63                     failed = false;
  64                     break;
  65                 }
  66             }
  67         } catch (Exception e) {
  68             e.printStackTrace();
  69         }
  70         f.dispose();
  71         assertFalse("Japanese Text did not render", failed);
  72     }
  73 
  74     @SuppressWarnings("serial")
  75     class TextPanel extends JPanel {
  76         public TextPanel() {
  77             super();
  78             setOpaque(true);
  79         }
  80 
  81         private Point locationOfText = new Point(20, 60);
  82 
  83         public Point getLocationOfText() {
  84             return locationOfText;
  85         }
  86 
  87         public void paintComponent(Graphics g) {
  88             Rectangle r = getBounds();
  89             g.setColor(Color.BLACK);
  90             g.fillRect(0, 0, r.width, r.height);
  91             g.setColor(Color.WHITE);
  92             g.drawString("\u30a4\u30e1\u30fc\u30b8\u304a\u3088\u3073\u30cb\u30e5\u30fc\u30b9\u691c\u7d22", locationOfText.x, locationOfText.y);
  93         }
  94 
  95     }
  96 
  97     public static Test suite() {
  98         return new TestSuite(JapaneseTest.class);
  99     }
 100 
 101     public static void main (String[] args) throws RuntimeException {
 102         TestResult tr = junit.textui.TestRunner.run(suite());
 103         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 104             throw new RuntimeException("### Unexpected JUnit errors or failures.");
 105         }
 106     }
 107 }