jdk/test/java/text/Bidi/BidiEmbeddingTest.java
author peytoia
Fri, 17 Oct 2008 13:34:03 +0900
changeset 1834 6eefd0d6804d
child 2956 931f209e57a9
permissions -rw-r--r--
6759521: Move Bidi test programs from closed to open. Reviewed-by: okutsu
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
/*
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
     2
 * Copyright (c) 2008 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    21
 * have any questions.
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
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    26
 * @bug 4396492 4396496 4778510
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
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    92
        System.out.println(bidi2);
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");
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    96
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
    97
    }
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
    // 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
   100
    static void test2() {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   101
        String target = "BACK WARDS";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   102
        String str = "If this text is >" + target + "< the test passed.";
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   103
        int length = str.length();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   104
        int start = str.indexOf(target);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   105
        int limit = start + target.length();
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   106
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   107
        System.out.println("start: " + start + " limit: " + limit);
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
        AttributedString astr = new AttributedString(str);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   110
        astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   111
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   112
        astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   113
                         new Integer(-3),
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   114
                         start,
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   115
                         limit);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   116
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   117
        Bidi bidi = new Bidi(astr.getIterator());
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
        for (int i = 0; i < bidi.getRunCount(); ++i) {
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   120
            System.out.println("run " + i +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   121
                               " from " + bidi.getRunStart(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   122
                               " to " + bidi.getRunLimit(i) +
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   123
                               " at level " + bidi.getRunLevel(i));
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   124
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   125
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   126
        System.out.println(bidi);
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   127
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   128
        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
   129
            throw new Error("Bidi embedding processing failed");
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   130
        }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   131
    }
6eefd0d6804d 6759521: Move Bidi test programs from closed to open.
peytoia
parents:
diff changeset
   132
}