1 /*
2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4396492 4396496 4778510 6850113
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  * @modules java.desktop
32  */
33 
34 import java.awt.Color;
35 import java.awt.Frame;
36 import java.awt.font.TextAttribute;
37 import java.text.AttributedString;
38 import java.text.Bidi;
39 
40 public class BidiEmbeddingTest {
main(String[] args)41     public static void main(String[] args) {
42         // to regress embedding test against old fix, call with an arg.  A window will pop
43         // up causing awt lib to be loaded so the vm won't die with the unsatisfied link error.
44         if (args.length > 0) {
45             Frame f = new Frame();
46             f.setSize(300, 300);
47             f.setBackground(Color.white);
48             f.show();
49         }
50 
51         test1();
52         test2();
53     }
54 
test1()55     static void test1() {
56         String target = "BACK WARDS";
57         String str = "If this text is >" + target + "< the test passed.";
58         int start = str.indexOf(target);
59         int limit = start + target.length();
60 
61         System.out.println("start: " + start + " limit: " + limit);
62 
63         AttributedString astr = new AttributedString(str);
64         astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
65                          new Integer(-1),
66                          start,
67                          limit);
68 
69         Bidi bidi = new Bidi(astr.getIterator());
70 
71         for (int i = 0; i < bidi.getRunCount(); ++i) {
72             System.out.println("run " + i +
73                                " from " + bidi.getRunStart(i) +
74                                " to " + bidi.getRunLimit(i) +
75                                " at level " + bidi.getRunLevel(i));
76         }
77 
78         System.out.println(bidi);
79 
80         byte[] embs = new byte[str.length() + 3];
81         for (int i = start + 1; i < limit + 1; ++i) {
82             embs[i] = -1;
83         }
84 
85         Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
86         for (int i = 0; i < bidi2.getRunCount(); ++i) {
87             System.out.println("run " + i +
88                                " from " + bidi2.getRunStart(i) +
89                                " to " + bidi2.getRunLimit(i) +
90                                " at level " + bidi2.getRunLevel(i));
91         }
92 
93         System.out.println(bidi2 + "\n");
94 
95         if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
96             throw new Error("Bidi run count incorrect");
97         } else {
98             System.out.println("test1() passed.\n");
99         }
100     }
101 
102     // make sure BIDI_EMBEDDING values of 0 are mapped to base run direction, instead of flagging an error.
test2()103     static void test2() {
104         String target = "BACK WARDS";
105         String str = "If this text is >" + target + "< the test passed.";
106         int length = str.length();
107         int start = str.indexOf(target);
108         int limit = start + target.length();
109 
110         System.out.println("start: " + start + " limit: " + limit);
111 
112         AttributedString astr = new AttributedString(str);
113         astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);
114 
115         astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
116                          new Integer(-3),
117                          start,
118                          limit);
119 
120         Bidi bidi = new Bidi(astr.getIterator());
121 
122         for (int i = 0; i < bidi.getRunCount(); ++i) {
123             System.out.println("run " + i +
124                                " from " + bidi.getRunStart(i) +
125                                " to " + bidi.getRunLimit(i) +
126                                " at level " + bidi.getRunLevel(i));
127         }
128 
129         System.out.println(bidi + "\n");
130 
131         if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1
132             throw new Error("Bidi embedding processing failed");
133         } else {
134             System.out.println("test2() passed.\n");
135         }
136     }
137 }
138