Path: blob/master/test/jdk/java/awt/ComponentOrientation/WindowTest.java
41149 views
/*1* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 4108453 4778440 630478527* @summary Test Window.applyResourceBundle orientation support28*29* @build TestBundle TestBundle_es TestBundle_iw30* @build TestBundle1 TestBundle1_ar31* @run main WindowTest32*/3334import java.awt.Frame;35import java.awt.Panel;36import java.awt.FlowLayout;37import java.awt.BorderLayout;38import java.awt.Button;39import java.awt.Component;40import java.awt.ComponentOrientation;41import java.awt.Container;42import java.util.Locale;43import java.util.ResourceBundle;4445public class WindowTest {4647public static void main(String args[]) throws Exception {48Frame frame = new Frame();49frame.setSize(200,200);50frame.setVisible(true);51try {52doTest(frame);53} finally {54frame.setVisible(false);55frame.dispose();56}57}5859public static void doTest (Frame myFrame) throws Exception{60System.out.println("WindowTest {");6162ResourceBundle rb;6364// Create a window containing a hierarchy of components.65System.out.println(" Creating component hierarchy...");66myFrame.setLayout(new FlowLayout());67Panel panel1 = new Panel();68panel1.setLayout(new BorderLayout());69panel1.add("North", new Button("North"));70panel1.add("South", new Button("South"));71panel1.add("East", new Button("East"));72panel1.add("West", new Button("West"));73panel1.add("Center", new Button("Center"));74myFrame.add(panel1);7576Panel panel2 = new Panel();77panel2.setLayout(new BorderLayout());78panel2.add(BorderLayout.BEFORE_FIRST_LINE, new Button("FirstLine"));79panel2.add(BorderLayout.AFTER_LAST_LINE, new Button("LastLine"));80panel2.add(BorderLayout.BEFORE_LINE_BEGINS, new Button("FirstItem"));81panel2.add(BorderLayout.AFTER_LINE_ENDS, new Button("LastItem"));82panel2.add("Center", new Button("Center"));83myFrame.add(panel2);8485// After construction, all of the components' orientations should be86// set to ComponentOrientation.UNKNOWN.87System.out.println(" Verifying orientation is UNKNOWN...");88verifyOrientation(myFrame, ComponentOrientation.UNKNOWN);8990// This will load TestBundle1 using the default locale and apply91// it to the component hierarchy. Since the bundle has no Orientation92// specified, this should fall back to the bundle-locale's orientation93System.out.println(" Applying TestBundle1 by name and verifying...");94myFrame.applyResourceBundle("TestBundle1");95verifyOrientation(myFrame,96ComponentOrientation.getOrientation(97ResourceBundle.getBundle("TestBundle1", Locale.getDefault())));9899System.out.println(" Applying TestBundle_iw and verifying...");100rb = ResourceBundle.getBundle("TestBundle", new Locale("iw", ""));101myFrame.applyResourceBundle(rb);102verifyOrientation(myFrame, ComponentOrientation.RIGHT_TO_LEFT);103104System.out.println(" Applying TestBundle_es and verifying...");105rb = ResourceBundle.getBundle("TestBundle", new Locale("es", ""));106myFrame.applyResourceBundle(rb);107verifyOrientation(myFrame, ComponentOrientation.LEFT_TO_RIGHT);108109System.out.println("}");110}111112static void verifyOrientation(Component c, ComponentOrientation orient) {113114ComponentOrientation o = c.getComponentOrientation();115116if (o != orient) {117throw new RuntimeException("ERROR: expected " + oString(orient) +118", got " + oString(o) +119" on component " + c);120}121122if (c instanceof Container) {123Container cont = (Container) c;124int ncomponents = cont.getComponentCount();125126for (int i = 0 ; i < ncomponents ; ++i) {127Component comp = cont.getComponent(i);128verifyOrientation(comp, orient);129}130}131}132133static String oString(ComponentOrientation o) {134if (o == ComponentOrientation.LEFT_TO_RIGHT) {135return "LEFT_TO_RIGHT";136}137else if (o == ComponentOrientation.RIGHT_TO_LEFT) {138return "RIGHT_TO_LEFT";139}140else {141return "UNKNOWN";142}143}144}145146147