01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.x.swing;
19:
20: import java.awt.Font;
21: import java.awt.FontMetrics;
22: import javax.swing.SwingTestCase;
23:
24: public class UtilitiesTest extends SwingTestCase {
25: public void testClipString() {
26: FontMetrics metrics;
27: String clippedStr;
28: String initialString = "Long enough text for this label, can you see that it is clipped now?";
29: metrics = getFontMetrics(new Font("fixed", Font.PLAIN, 12));
30: clippedStr = Utilities.clipString(metrics, initialString, 350);
31: assertEquals("clipped string ",
32: "Long enough text for this ...", clippedStr);
33: clippedStr = Utilities.clipString(metrics, initialString, 100);
34: assertEquals("clipped string ", "Long ...", clippedStr);
35: clippedStr = Utilities
36: .clipString(metrics, initialString, 10000);
37: assertEquals("clipped string ", initialString, clippedStr);
38: metrics = getFontMetrics(new Font("fixed", Font.PLAIN, 60));
39: clippedStr = Utilities.clipString(metrics, initialString, 1500);
40: assertEquals("clipped string ", "Long enough text for t...",
41: clippedStr);
42: metrics = getFontMetrics(new Font("fixed", Font.PLAIN, 50));
43: clippedStr = Utilities.clipString(metrics, initialString, 500);
44: assertEquals("clipped string ", "Long en...", clippedStr);
45: metrics = getFontMetrics(new Font("fixed", Font.PLAIN, 60));
46: clippedStr = Utilities.clipString(metrics, initialString, 5);
47: assertEquals("clipped string ", "...", clippedStr);
48: metrics = getFontMetrics(new Font("fixed", Font.PLAIN, 2));
49: clippedStr = Utilities.clipString(metrics, initialString, 5);
50: assertEquals("clipped string ", "...", clippedStr);
51: metrics = getFontMetrics(new Font("fixed", Font.PLAIN, 3));
52: clippedStr = Utilities.clipString(metrics, initialString, 5);
53: assertEquals("clipped string ", "...", clippedStr);
54: }
55:
56: public void testIsStringEmpty() {
57: assertTrue(Utilities.isEmptyString(null));
58: assertTrue(Utilities.isEmptyString(""));
59: assertFalse(Utilities.isEmptyString(" "));
60: assertFalse(Utilities.isEmptyString("\t"));
61: assertFalse(Utilities.isEmptyString("\n"));
62: assertFalse(Utilities.isEmptyString("\r"));
63: }
64:
65: // Regression for HARMONY-2253
66: public void testSafeIntSum() {
67: assertEquals(0, Utilities.safeIntSum(9, -9));
68: assertEquals(0, Utilities.safeIntSum(-9, 9));
69: assertEquals(-18, Utilities.safeIntSum(-9, -9));
70: assertEquals(Integer.MIN_VALUE + 1, Utilities.safeIntSum(
71: Integer.MIN_VALUE, 1));
72: // assertEquals(Integer.MIN_VALUE, Utilities.safeIntSum(Integer.MIN_VALUE, -1));
73: // assertEquals(Integer.MIN_VALUE, Utilities.safeIntSum(Integer.MIN_VALUE + 2, -2));
74: }
75: }
|