Path: blob/master/test/jdk/java/text/Bidi/BidiEmbeddingTest.java
41149 views
/*1* Copyright (c) 2008, 2016, 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* @modules java.desktop31*/3233import java.awt.Color;34import java.awt.Frame;35import java.awt.font.TextAttribute;36import java.text.AttributedString;37import java.text.Bidi;3839public class BidiEmbeddingTest {40public static void main(String[] args) {41// to regress embedding test against old fix, call with an arg. A window will pop42// up causing awt lib to be loaded so the vm won't die with the unsatisfied link error.43if (args.length > 0) {44Frame f = new Frame();45f.setSize(300, 300);46f.setBackground(Color.white);47f.show();48}4950test1();51test2();52}5354static void test1() {55String target = "BACK WARDS";56String str = "If this text is >" + target + "< the test passed.";57int start = str.indexOf(target);58int limit = start + target.length();5960System.out.println("start: " + start + " limit: " + limit);6162AttributedString astr = new AttributedString(str);63astr.addAttribute(TextAttribute.BIDI_EMBEDDING,64new Integer(-1),65start,66limit);6768Bidi bidi = new Bidi(astr.getIterator());6970for (int i = 0; i < bidi.getRunCount(); ++i) {71System.out.println("run " + i +72" from " + bidi.getRunStart(i) +73" to " + bidi.getRunLimit(i) +74" at level " + bidi.getRunLevel(i));75}7677System.out.println(bidi);7879byte[] embs = new byte[str.length() + 3];80for (int i = start + 1; i < limit + 1; ++i) {81embs[i] = -1;82}8384Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);85for (int i = 0; i < bidi2.getRunCount(); ++i) {86System.out.println("run " + i +87" from " + bidi2.getRunStart(i) +88" to " + bidi2.getRunLimit(i) +89" at level " + bidi2.getRunLevel(i));90}9192System.out.println(bidi2 + "\n");9394if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {95throw new Error("Bidi run count incorrect");96} else {97System.out.println("test1() passed.\n");98}99}100101// make sure BIDI_EMBEDDING values of 0 are mapped to base run direction, instead of flagging an error.102static void test2() {103String target = "BACK WARDS";104String str = "If this text is >" + target + "< the test passed.";105int length = str.length();106int start = str.indexOf(target);107int limit = start + target.length();108109System.out.println("start: " + start + " limit: " + limit);110111AttributedString astr = new AttributedString(str);112astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);113114astr.addAttribute(TextAttribute.BIDI_EMBEDDING,115new Integer(-3),116start,117limit);118119Bidi bidi = new Bidi(astr.getIterator());120121for (int i = 0; i < bidi.getRunCount(); ++i) {122System.out.println("run " + i +123" from " + bidi.getRunStart(i) +124" to " + bidi.getRunLimit(i) +125" at level " + bidi.getRunLevel(i));126}127128System.out.println(bidi + "\n");129130if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1131throw new Error("Bidi embedding processing failed");132} else {133System.out.println("test2() passed.\n");134}135}136}137138139