jdk/test/java/text/Bidi/BidiEmbeddingTest.java
changeset 1834 6eefd0d6804d
child 2956 931f209e57a9
equal deleted inserted replaced
1833:e4d8baed5336 1834:6eefd0d6804d
       
     1 /*
       
     2  * Copyright (c) 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4396492 4396496 4778510
       
    27  * @summary verify that the embedding values processed by the bidi code use negative values to
       
    28  * indicate overrides, rather than using bit 7.  Also tests Bidi without loading awt classes to
       
    29  * confirm that Bidi can be used without awt. Verify that embedding level 0 is properly mapped
       
    30  * to the base embedding level.
       
    31  */
       
    32 
       
    33 import java.awt.Color;
       
    34 import java.awt.Frame;
       
    35 import java.awt.font.TextAttribute;
       
    36 import java.text.AttributedString;
       
    37 import java.text.Bidi;
       
    38 
       
    39 public class BidiEmbeddingTest {
       
    40     public static void main(String[] args) {
       
    41         // to regress embedding test against old fix, call with an arg.  A window will pop
       
    42         // up causing awt lib to be loaded so the vm won't die with the unsatisfied link error.
       
    43         if (args.length > 0) {
       
    44             Frame f = new Frame();
       
    45             f.setSize(300, 300);
       
    46             f.setBackground(Color.white);
       
    47             f.show();
       
    48         }
       
    49 
       
    50         test1();
       
    51         test2();
       
    52     }
       
    53 
       
    54     static void test1() {
       
    55         String target = "BACK WARDS";
       
    56         String str = "If this text is >" + target + "< the test passed.";
       
    57         int start = str.indexOf(target);
       
    58         int limit = start + target.length();
       
    59 
       
    60         System.out.println("start: " + start + " limit: " + limit);
       
    61 
       
    62         AttributedString astr = new AttributedString(str);
       
    63         astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
       
    64                          new Integer(-1),
       
    65                          start,
       
    66                          limit);
       
    67 
       
    68         Bidi bidi = new Bidi(astr.getIterator());
       
    69 
       
    70         for (int i = 0; i < bidi.getRunCount(); ++i) {
       
    71             System.out.println("run " + i +
       
    72                                " from " + bidi.getRunStart(i) +
       
    73                                " to " + bidi.getRunLimit(i) +
       
    74                                " at level " + bidi.getRunLevel(i));
       
    75         }
       
    76 
       
    77         System.out.println(bidi);
       
    78 
       
    79         byte[] embs = new byte[str.length() + 3];
       
    80         for (int i = start + 1; i < limit + 1; ++i) {
       
    81             embs[i] = -1;
       
    82         }
       
    83 
       
    84         Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
       
    85         for (int i = 0; i < bidi2.getRunCount(); ++i) {
       
    86             System.out.println("run " + i +
       
    87                                " from " + bidi2.getRunStart(i) +
       
    88                                " to " + bidi2.getRunLimit(i) +
       
    89                                " at level " + bidi2.getRunLevel(i));
       
    90         }
       
    91 
       
    92         System.out.println(bidi2);
       
    93 
       
    94         if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
       
    95             throw new Error("Bidi run count incorrect");
       
    96         }
       
    97     }
       
    98 
       
    99     // make sure BIDI_EMBEDDING values of 0 are mapped to base run direction, instead of flagging an error.
       
   100     static void test2() {
       
   101         String target = "BACK WARDS";
       
   102         String str = "If this text is >" + target + "< the test passed.";
       
   103         int length = str.length();
       
   104         int start = str.indexOf(target);
       
   105         int limit = start + target.length();
       
   106 
       
   107         System.out.println("start: " + start + " limit: " + limit);
       
   108 
       
   109         AttributedString astr = new AttributedString(str);
       
   110         astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
       
   111 
       
   112         astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
       
   113                          new Integer(-3),
       
   114                          start,
       
   115                          limit);
       
   116 
       
   117         Bidi bidi = new Bidi(astr.getIterator());
       
   118 
       
   119         for (int i = 0; i < bidi.getRunCount(); ++i) {
       
   120             System.out.println("run " + i +
       
   121                                " from " + bidi.getRunStart(i) +
       
   122                                " to " + bidi.getRunLimit(i) +
       
   123                                " at level " + bidi.getRunLevel(i));
       
   124         }
       
   125 
       
   126         System.out.println(bidi);
       
   127 
       
   128         if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1
       
   129             throw new Error("Bidi embedding processing failed");
       
   130         }
       
   131     }
       
   132 }