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