1 /*
   2  * Copyright (c) 1998, 2016, 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  * @key headful
  27  * @bug     4108453 4778440 6304785
  28  * @summary Test Window.applyResourceBundle orientation support
  29  *
  30  * @build TestBundle TestBundle_es TestBundle_iw
  31  * @build TestBundle1 TestBundle1_ar
  32  * @run main WindowTest
  33  */
  34 
  35 import java.awt.*;
  36 import java.applet.*;
  37 import java.util.Locale;
  38 import java.util.ResourceBundle;
  39 
  40 public class WindowTest extends Applet {
  41     static Exception failure=null;
  42     static Thread mainThread=null;
  43 
  44     public static void main(String args[]) throws Exception {
  45         mainThread = Thread.currentThread();
  46         WindowTest app = new WindowTest();
  47         app.start();
  48         try {
  49             Thread.sleep(300000);
  50         } catch (InterruptedException e) {
  51             if (failure != null) {
  52                 throw failure;
  53             }
  54         }
  55     }
  56 
  57     public void start() {
  58         try {
  59             doTest();
  60         } catch (Exception e) {
  61             failure = e;
  62         }
  63         mainThread.interrupt();
  64     }
  65 
  66     public void doTest() {
  67         System.out.println("WindowTest {");
  68 
  69         ResourceBundle rb;
  70         Frame myFrame;
  71 
  72         // Create a window containing a hierarchy of components.
  73         System.out.println("  Creating component hierarchy...");
  74         myFrame = new Frame();
  75         myFrame.setLayout(new FlowLayout());
  76         Panel panel1 = new Panel();
  77         panel1.setLayout(new BorderLayout());
  78         panel1.add("North", new Button("North"));
  79         panel1.add("South", new Button("South"));
  80         panel1.add("East", new Button("East"));
  81         panel1.add("West", new Button("West"));
  82         panel1.add("Center", new Button("Center"));
  83         myFrame.add(panel1);
  84 
  85         Panel panel2 = new Panel();
  86         panel2.setLayout(new BorderLayout());
  87         panel2.add(BorderLayout.BEFORE_FIRST_LINE, new Button("FirstLine"));
  88         panel2.add(BorderLayout.AFTER_LAST_LINE, new Button("LastLine"));
  89         panel2.add(BorderLayout.BEFORE_LINE_BEGINS, new Button("FirstItem"));
  90         panel2.add(BorderLayout.AFTER_LINE_ENDS, new Button("LastItem"));
  91         panel2.add("Center", new Button("Center"));
  92         myFrame.add(panel2);
  93 
  94         // After construction, all of the components' orientations should be
  95         // set to ComponentOrientation.UNKNOWN.
  96         System.out.println("  Verifying orientation is UNKNOWN...");
  97         verifyOrientation(myFrame, ComponentOrientation.UNKNOWN);
  98 
  99         // This will load TestBundle1 using the default locale and apply
 100         // it to the component hierarchy.  Since the bundle has no Orientation
 101         // specified, this should fall back to the bundle-locale's orientation
 102         System.out.println("  Applying TestBundle1 by name and verifying...");
 103         myFrame.applyResourceBundle("TestBundle1");
 104         verifyOrientation(myFrame,
 105                     ComponentOrientation.getOrientation(
 106                         ResourceBundle.getBundle("TestBundle1", Locale.getDefault())));
 107 
 108         System.out.println("  Applying TestBundle_iw and verifying...");
 109         rb = ResourceBundle.getBundle("TestBundle", new Locale("iw", ""));
 110         myFrame.applyResourceBundle(rb);
 111         verifyOrientation(myFrame, ComponentOrientation.RIGHT_TO_LEFT);
 112 
 113         System.out.println("  Applying TestBundle_es and verifying...");
 114         rb = ResourceBundle.getBundle("TestBundle", new Locale("es", ""));
 115         myFrame.applyResourceBundle(rb);
 116         verifyOrientation(myFrame, ComponentOrientation.LEFT_TO_RIGHT);
 117 
 118 
 119         myFrame.setVisible(false);
 120         myFrame.dispose();
 121         System.out.println("}");
 122     }
 123 
 124     static void verifyOrientation(Component c, ComponentOrientation orient) {
 125 
 126         ComponentOrientation o = c.getComponentOrientation();
 127 
 128         if (o != orient) {
 129             throw new RuntimeException("ERROR: expected " + oString(orient) +
 130                                         ", got " + oString(o) +
 131                                         " on component " + c);
 132         }
 133 
 134         if (c instanceof Container) {
 135             Container cont = (Container) c;
 136             int ncomponents = cont.getComponentCount();
 137 
 138             for (int i = 0 ; i < ncomponents ; ++i) {
 139                 Component comp = cont.getComponent(i);
 140                 verifyOrientation(comp, orient);
 141             }
 142         }
 143     }
 144 
 145     static String oString(ComponentOrientation o) {
 146         if (o == ComponentOrientation.LEFT_TO_RIGHT) {
 147             return "LEFT_TO_RIGHT";
 148         }
 149         else if (o == ComponentOrientation.RIGHT_TO_LEFT) {
 150             return "RIGHT_TO_LEFT";
 151         }
 152         else {
 153             return "UNKNOWN";
 154         }
 155     }
 156 }