Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/text/Bidi/BidiEmbeddingTest.java
38813 views
/*1* Copyright (c) 2008, 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* @bug 4396492 4396496 4778510 685011326* @summary verify that the embedding values processed by the bidi code use negative values to27* indicate overrides, rather than using bit 7. Also tests Bidi without loading awt classes to28* confirm that Bidi can be used without awt. Verify that embedding level 0 is properly mapped29* to the base embedding level.30*/3132import java.awt.Color;33import java.awt.Frame;34import java.awt.font.TextAttribute;35import java.text.AttributedString;36import java.text.Bidi;3738public class BidiEmbeddingTest {39public static void main(String[] args) {40// to regress embedding test against old fix, call with an arg. A window will pop41// up causing awt lib to be loaded so the vm won't die with the unsatisfied link error.42if (args.length > 0) {43Frame f = new Frame();44f.setSize(300, 300);45f.setBackground(Color.white);46f.show();47}4849test1();50test2();51}5253static void test1() {54String target = "BACK WARDS";55String str = "If this text is >" + target + "< the test passed.";56int start = str.indexOf(target);57int limit = start + target.length();5859System.out.println("start: " + start + " limit: " + limit);6061AttributedString astr = new AttributedString(str);62astr.addAttribute(TextAttribute.BIDI_EMBEDDING,63new Integer(-1),64start,65limit);6667Bidi bidi = new Bidi(astr.getIterator());6869for (int i = 0; i < bidi.getRunCount(); ++i) {70System.out.println("run " + i +71" from " + bidi.getRunStart(i) +72" to " + bidi.getRunLimit(i) +73" at level " + bidi.getRunLevel(i));74}7576System.out.println(bidi);7778byte[] embs = new byte[str.length() + 3];79for (int i = start + 1; i < limit + 1; ++i) {80embs[i] = -1;81}8283Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);84for (int i = 0; i < bidi2.getRunCount(); ++i) {85System.out.println("run " + i +86" from " + bidi2.getRunStart(i) +87" to " + bidi2.getRunLimit(i) +88" at level " + bidi2.getRunLevel(i));89}9091System.out.println(bidi2 + "\n");9293if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {94throw new Error("Bidi run count incorrect");95} else {96System.out.println("test1() passed.\n");97}98}99100// make sure BIDI_EMBEDDING values of 0 are mapped to base run direction, instead of flagging an error.101static void test2() {102String target = "BACK WARDS";103String str = "If this text is >" + target + "< the test passed.";104int length = str.length();105int start = str.indexOf(target);106int limit = start + target.length();107108System.out.println("start: " + start + " limit: " + limit);109110AttributedString astr = new AttributedString(str);111astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);112113astr.addAttribute(TextAttribute.BIDI_EMBEDDING,114new Integer(-3),115start,116limit);117118Bidi bidi = new Bidi(astr.getIterator());119120for (int i = 0; i < bidi.getRunCount(); ++i) {121System.out.println("run " + i +122" from " + bidi.getRunStart(i) +123" to " + bidi.getRunLimit(i) +124" at level " + bidi.getRunLevel(i));125}126127System.out.println(bidi + "\n");128129if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1130throw new Error("Bidi embedding processing failed");131} else {132System.out.println("test2() passed.\n");133}134}135}136137138