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 6304780 6396378
  27  * @summary Basic tests for java.awt.ComponentOrientation
  28  * @build TestBundle TestBundle_es TestBundle_iw
  29  * @build TestBundle1 TestBundle1_ar
  30  *
  31  * @run main BasicTest
  32  */
  33 /*
  34  * (C) Copyright IBM Corp. 1998 - All Rights Reserved
  35  *
  36  * The original version of this source code and documentation is copyrighted
  37  * and owned by IBM, Inc. These materials are provided under terms of a
  38  * License Agreement between IBM and Sun. This technology is protected by
  39  * multiple US and International patents. This notice and attribution to IBM
  40  * may not be removed.
  41  */
  42 
  43 import java.awt.ComponentOrientation;
  44 import java.util.Locale;
  45 import java.util.ResourceBundle;
  46 
  47 public class BasicTest {
  48     public static void main(String args[]) {
  49         System.out.println("BasicTest {");
  50         TestInvariants();
  51         TestLocale();
  52         TestBundle();
  53 
  54         System.out.println("} Pass");
  55     }
  56 
  57     // TestInvariants
  58     //
  59     // Various no-brainer tests to make sure the constants behave properly
  60     // and so on.
  61     //
  62     static void TestInvariants() {
  63         System.out.println("  TestInvariants {");
  64 
  65         Assert(ComponentOrientation.LEFT_TO_RIGHT.isLeftToRight(),
  66                "LEFT_TO_RIGHT.isLeftToRight()");
  67 
  68         Assert(ComponentOrientation.UNKNOWN.isLeftToRight(),
  69                "UNKNOWN.isLeftToRight()");
  70 
  71         Assert(!ComponentOrientation.RIGHT_TO_LEFT.isLeftToRight(),
  72                "!RIGHT_TO_LEFT.isLeftToRight()");
  73 
  74         Assert(ComponentOrientation.LEFT_TO_RIGHT.isHorizontal(),
  75                "LEFT_TO_RIGHT.isHorizontal()");
  76 
  77         Assert(ComponentOrientation.UNKNOWN.isHorizontal(),
  78                "UNKNOWN.isHorizontal()");
  79 
  80         Assert(ComponentOrientation.RIGHT_TO_LEFT.isHorizontal(),
  81                "RIGHT_TO_LEFT.isHorizontal()");
  82 
  83         System.out.println("  } Pass");
  84     }
  85 
  86     // TestLocale
  87     //
  88     // Make sure that getOrientation(Locale) works, and that the appropriate
  89     // system locales are RIGHT_TO_LEFT
  90     //
  91     static void TestLocale() {
  92         System.out.println("  TestLocale {");
  93 
  94         ComponentOrientation orient = ComponentOrientation.getOrientation(Locale.US);
  95         Assert(orient == ComponentOrientation.LEFT_TO_RIGHT, "US == LEFT_TO_RIGHT");
  96 
  97         orient = ComponentOrientation.getOrientation(new Locale("iw", ""));
  98         Assert(orient == ComponentOrientation.RIGHT_TO_LEFT, "iw == RIGHT_TO_LEFT");
  99 
 100         orient = ComponentOrientation.getOrientation(new Locale("ar", ""));
 101         Assert(orient == ComponentOrientation.RIGHT_TO_LEFT, "ar == RIGHT_TO_LEFT");
 102 
 103         System.out.println("  } Pass");
 104     }
 105 
 106     // TestBundle
 107     //
 108     // Make sure that getOrientation(ResourceBundle) works right, especially
 109     // the fallback mechasm
 110     //
 111     static void TestBundle() {
 112         System.out.println("  TestBundle {");
 113 
 114         // This will fall back to the default locale's bundle or root bundle
 115         ResourceBundle rb = ResourceBundle.getBundle("TestBundle",
 116                                                         new Locale("et", ""));
 117         if (rb.getLocale().getLanguage().equals(new Locale("iw").getLanguage())) {
 118             assertEquals(rb, ComponentOrientation.RIGHT_TO_LEFT, "et == RIGHT_TO_LEFT" );
 119         } else if (rb.getLocale().getLanguage() == "es") {
 120             assertEquals(rb, ComponentOrientation.LEFT_TO_RIGHT, "et == LEFT_TO_RIGHT" );
 121         } else {
 122             assertEquals(rb, ComponentOrientation.UNKNOWN, "et == UNKNOWN" );
 123         }
 124 
 125         // We have actual bundles for "es" and "iw", so it should just fetch
 126         // the orientation object out of them
 127         rb = ResourceBundle.getBundle("TestBundle",new Locale("es", ""));
 128         assertEquals(rb, ComponentOrientation.LEFT_TO_RIGHT, "es == LEFT_TO_RIGHT" );
 129 
 130         rb = ResourceBundle.getBundle("TestBundle", new Locale("iw", "IL"));
 131         assertEquals(rb, ComponentOrientation.RIGHT_TO_LEFT, "iw == RIGHT_TO_LEFT" );
 132 
 133         // This bundle has no orientation setting at all, so we should get
 134         // the system's default orientation for Arabic
 135         rb = ResourceBundle.getBundle("TestBundle1", new Locale("ar", ""));
 136         assertEquals(rb, ComponentOrientation.RIGHT_TO_LEFT, "ar == RIGHT_TO_LEFT" );
 137 
 138         System.out.println("  } Pass");
 139     }
 140 
 141     static void assertEquals(ResourceBundle rb, ComponentOrientation o, String str) {
 142         Assert(ComponentOrientation.getOrientation(rb) == o, str);
 143     }
 144 
 145     static void Assert(boolean condition, String str) {
 146         if (!condition) {
 147             System.err.println("    ASSERT FAILED: " + str);
 148             throw new RuntimeException("Assert Failed: " + str);
 149         }
 150     }
 151 }