1 /*
2  * Copyright (c) 1998, 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 4151160
27  * @summary Make sure to return correct run start and limit values
28  * when the iterator has been created with begin and end index values.
29  * @modules java.desktop
30  */
31 
32 import java.awt.font.TextAttribute;
33 import java.text.AttributedCharacterIterator;
34 import java.text.AttributedString;
35 import java.text.Annotation;
36 
37 public class getRunStartLimitTest {
38 
main(String[] args)39     public static void main(String[] args) throws Exception {
40 
41         String text = "Hello world";
42         AttributedString as = new AttributedString(text);
43 
44         // add non-Annotation attributes
45         as.addAttribute(TextAttribute.WEIGHT,
46                         TextAttribute.WEIGHT_LIGHT,
47                         0,
48                         3);
49         as.addAttribute(TextAttribute.WEIGHT,
50                         TextAttribute.WEIGHT_BOLD,
51                         3,
52                         5);
53         as.addAttribute(TextAttribute.WEIGHT,
54                         TextAttribute.WEIGHT_EXTRABOLD,
55                         5,
56                         text.length());
57 
58         // add Annotation attributes
59         as.addAttribute(TextAttribute.WIDTH,
60                         new Annotation(TextAttribute.WIDTH_EXTENDED),
61                         0,
62                         3);
63         as.addAttribute(TextAttribute.WIDTH,
64                         new Annotation(TextAttribute.WIDTH_CONDENSED),
65                         3,
66                         4);
67 
68         AttributedCharacterIterator aci = as.getIterator(null, 2, 4);
69 
70         aci.first();
71         int runStart = aci.getRunStart();
72         if (runStart != 2) {
73             throw new Exception("1st run start is wrong. ("+runStart+" should be 2.)");
74         }
75 
76         int runLimit = aci.getRunLimit();
77         if (runLimit != 3) {
78             throw new Exception("1st run limit is wrong. ("+runLimit+" should be 3.)");
79         }
80 
81         Object value = aci.getAttribute(TextAttribute.WEIGHT);
82         if (value != TextAttribute.WEIGHT_LIGHT) {
83             throw new Exception("1st run attribute is wrong. ("
84                                 +value+" should be "+TextAttribute.WEIGHT_LIGHT+".)");
85         }
86 
87         value = aci.getAttribute(TextAttribute.WIDTH);
88         if (value != null) {
89             throw new Exception("1st run annotation is wrong. ("
90                                 +value+" should be null.)");
91         }
92 
93         aci.setIndex(runLimit);
94         runStart = aci.getRunStart();
95         if (runStart != 3) {
96             throw new Exception("2nd run start is wrong. ("+runStart+" should be 3.)");
97         }
98 
99         runLimit = aci.getRunLimit();
100         if (runLimit != 4) {
101             throw new Exception("2nd run limit is wrong. ("+runLimit+" should be 4.)");
102         }
103         value = aci.getAttribute(TextAttribute.WEIGHT);
104         if (value != TextAttribute.WEIGHT_BOLD) {
105             throw new Exception("2nd run attribute is wrong. ("
106                                 +value+" should be "+TextAttribute.WEIGHT_BOLD+".)");
107         }
108 
109         value = aci.getAttribute(TextAttribute.WIDTH);
110         if (!(value instanceof Annotation)
111             || (((Annotation)value).getValue() !=  TextAttribute.WIDTH_CONDENSED)) {
112             throw new Exception("2nd run annotation is wrong. (" + value + " should be "
113                                 + new Annotation(TextAttribute.WIDTH_CONDENSED)+".)");
114         }
115     }
116 }
117