jdk/test/java/text/Bidi/BidiEmbeddingTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2956 931f209e57a9
child 40679 5378433ac23f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2956
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     4
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     8
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    13
 * accompanied this code).
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    14
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2956
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2956
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2956
diff changeset
    21
 * questions.
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    22
 */
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    23
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    24
/*
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    25
 * @test
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
    26
 * @bug 4396492 4396496 4778510 6850113
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    27
 * @summary verify that the embedding values processed by the bidi code use negative values to
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    28
 * indicate overrides, rather than using bit 7.  Also tests Bidi without loading awt classes to
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    29
 * confirm that Bidi can be used without awt. Verify that embedding level 0 is properly mapped
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    30
 * to the base embedding level.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    31
 */
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    32
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    33
import java.awt.Color;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    34
import java.awt.Frame;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    35
import java.awt.font.TextAttribute;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    36
import java.text.AttributedString;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    37
import java.text.Bidi;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    38
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    39
public class BidiEmbeddingTest {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    40
    public static void main(String[] args) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    41
        // to regress embedding test against old fix, call with an arg.  A window will pop
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    42
        // up causing awt lib to be loaded so the vm won't die with the unsatisfied link error.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    43
        if (args.length > 0) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    44
            Frame f = new Frame();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    45
            f.setSize(300, 300);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    46
            f.setBackground(Color.white);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    47
            f.show();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    48
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    49
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    50
        test1();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    51
        test2();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    52
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    53
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    54
    static void test1() {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    55
        String target = "BACK WARDS";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    56
        String str = "If this text is >" + target + "< the test passed.";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    57
        int start = str.indexOf(target);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    58
        int limit = start + target.length();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    59
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    60
        System.out.println("start: " + start + " limit: " + limit);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    61
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    62
        AttributedString astr = new AttributedString(str);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    63
        astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    64
                         new Integer(-1),
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    65
                         start,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    66
                         limit);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    67
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    68
        Bidi bidi = new Bidi(astr.getIterator());
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    69
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    70
        for (int i = 0; i < bidi.getRunCount(); ++i) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    71
            System.out.println("run " + i +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    72
                               " from " + bidi.getRunStart(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    73
                               " to " + bidi.getRunLimit(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    74
                               " at level " + bidi.getRunLevel(i));
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    75
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    76
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    77
        System.out.println(bidi);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    78
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    79
        byte[] embs = new byte[str.length() + 3];
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    80
        for (int i = start + 1; i < limit + 1; ++i) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    81
            embs[i] = -1;
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    82
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    83
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    84
        Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    85
        for (int i = 0; i < bidi2.getRunCount(); ++i) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    86
            System.out.println("run " + i +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    87
                               " from " + bidi2.getRunStart(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    88
                               " to " + bidi2.getRunLimit(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    89
                               " at level " + bidi2.getRunLevel(i));
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    90
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    91
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
    92
        System.out.println(bidi2 + "\n");
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    93
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    94
        if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    95
            throw new Error("Bidi run count incorrect");
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
    96
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
    97
            System.out.println("test1() passed.\n");
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    98
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    99
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   100
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   101
    // make sure BIDI_EMBEDDING values of 0 are mapped to base run direction, instead of flagging an error.
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   102
    static void test2() {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   103
        String target = "BACK WARDS";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   104
        String str = "If this text is >" + target + "< the test passed.";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   105
        int length = str.length();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   106
        int start = str.indexOf(target);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   107
        int limit = start + target.length();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   108
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   109
        System.out.println("start: " + start + " limit: " + limit);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   110
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   111
        AttributedString astr = new AttributedString(str);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   112
        astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   113
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   114
        astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   115
                         new Integer(-3),
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   116
                         start,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   117
                         limit);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   118
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   119
        Bidi bidi = new Bidi(astr.getIterator());
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   120
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   121
        for (int i = 0; i < bidi.getRunCount(); ++i) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   122
            System.out.println("run " + i +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   123
                               " from " + bidi.getRunStart(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   124
                               " to " + bidi.getRunLimit(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   125
                               " at level " + bidi.getRunLevel(i));
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   126
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   127
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
   128
        System.out.println(bidi + "\n");
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   129
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   130
        if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   131
            throw new Error("Bidi embedding processing failed");
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
   132
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents: 1834
diff changeset
   133
            System.out.println("test2() passed.\n");
1834
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   134
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   135
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   136
}