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