1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt;
17 
18 import junit.framework.TestCase;
19 
20 import java.util.Locale;
21 
22 @SuppressWarnings("javadoc")
23 public class AdtUtilsTest extends TestCase {
testExtractClassName()24     public void testExtractClassName() {
25         assertEquals("Foo", AdtUtils.extractClassName("foo"));
26         assertEquals("Foobar", AdtUtils.extractClassName("foo bar"));
27         assertEquals("JavasTypeSystem", AdtUtils.extractClassName("Java's Type System"));
28         assertEquals("Foo", AdtUtils.extractClassName("1foo "));
29     }
30 
testStripAllExtensions()31     public void testStripAllExtensions() {
32         assertEquals("", AdtUtils.stripAllExtensions(""));
33         assertEquals("foobar", AdtUtils.stripAllExtensions("foobar"));
34         assertEquals("foobar", AdtUtils.stripAllExtensions("foobar.png"));
35         assertEquals("foobar", AdtUtils.stripAllExtensions("foobar.9.png"));
36         assertEquals(".profile", AdtUtils.stripAllExtensions(".profile"));
37     }
38 
testStripLastExtension()39     public void testStripLastExtension() {
40         assertEquals("", AdtUtils.stripLastExtension(""));
41         assertEquals("foobar", AdtUtils.stripLastExtension("foobar"));
42         assertEquals("foobar", AdtUtils.stripLastExtension("foobar.png"));
43         assertEquals("foobar.9", AdtUtils.stripLastExtension("foobar.9.png"));
44         assertEquals(".profile", AdtUtils.stripLastExtension(".profile"));
45     }
46 
testCapitalize()47     public void testCapitalize() {
48         assertEquals("UPPER", AdtUtils.capitalize("UPPER"));
49         assertEquals("Lower", AdtUtils.capitalize("lower"));
50         assertEquals("Capital", AdtUtils.capitalize("Capital"));
51         assertEquals("CamelCase", AdtUtils.capitalize("camelCase"));
52         assertEquals("", AdtUtils.capitalize(""));
53         assertSame("Foo", AdtUtils.capitalize("Foo"));
54         assertNull(null, AdtUtils.capitalize(null));
55     }
56 
testCamelCaseToUnderlines()57     public void testCamelCaseToUnderlines() {
58         assertEquals("", AdtUtils.camelCaseToUnderlines(""));
59         assertEquals("foo", AdtUtils.camelCaseToUnderlines("foo"));
60         assertEquals("foo", AdtUtils.camelCaseToUnderlines("Foo"));
61         assertEquals("foo_bar", AdtUtils.camelCaseToUnderlines("FooBar"));
62         assertEquals("test_xml", AdtUtils.camelCaseToUnderlines("testXML"));
63         assertEquals("test_foo", AdtUtils.camelCaseToUnderlines("testFoo"));
64     }
65 
testUnderlinesToCamelCase()66     public void testUnderlinesToCamelCase() {
67         assertEquals("", AdtUtils.underlinesToCamelCase(""));
68         assertEquals("", AdtUtils.underlinesToCamelCase("_"));
69         assertEquals("Foo", AdtUtils.underlinesToCamelCase("foo"));
70         assertEquals("FooBar", AdtUtils.underlinesToCamelCase("foo_bar"));
71         assertEquals("FooBar", AdtUtils.underlinesToCamelCase("foo__bar"));
72         assertEquals("Foo", AdtUtils.underlinesToCamelCase("foo_"));
73     }
74 
testStripSuffix()75     public void testStripSuffix() {
76         assertEquals("Foo", AdtUtils.stripSuffix("Foo", ""));
77         assertEquals("Fo", AdtUtils.stripSuffix("Foo", "o"));
78         assertEquals("F", AdtUtils.stripSuffix("Fo", "o"));
79         assertEquals("", AdtUtils.stripSuffix("Foo", "Foo"));
80         assertEquals("LinearLayout_Layout",
81                 AdtUtils.stripSuffix("LinearLayout_LayoutParams", "Params"));
82         assertEquals("Foo", AdtUtils.stripSuffix("Foo", "Bar"));
83     }
84 
testFormatFloatValue()85     public void testFormatFloatValue() throws Exception {
86         assertEquals("1", AdtUtils.formatFloatAttribute(1.0f));
87         assertEquals("2", AdtUtils.formatFloatAttribute(2.0f));
88         assertEquals("1.50", AdtUtils.formatFloatAttribute(1.5f));
89         assertEquals("1.50", AdtUtils.formatFloatAttribute(1.50f));
90         assertEquals("1.51", AdtUtils.formatFloatAttribute(1.51f));
91         assertEquals("1.51", AdtUtils.formatFloatAttribute(1.514542f));
92         assertEquals("1.52", AdtUtils.formatFloatAttribute(1.516542f));
93         assertEquals("-1.51", AdtUtils.formatFloatAttribute(-1.51f));
94         assertEquals("-1", AdtUtils.formatFloatAttribute(-1f));
95     }
96 
testFormatFloatValueLocale()97     public void testFormatFloatValueLocale() throws Exception {
98         // Ensure that the layout float values aren't affected by
99         // locale settings, like using commas instead of of periods
100         Locale originalDefaultLocale = Locale.getDefault();
101 
102         try {
103             Locale.setDefault(Locale.FRENCH);
104 
105             // Ensure that this is a locale which uses a comma instead of a period:
106             assertEquals("5,24", String.format("%.2f", 5.236f));
107 
108             // Ensure that the formatFloatAttribute is immune
109             assertEquals("1.50", AdtUtils.formatFloatAttribute(1.5f));
110         } finally {
111             Locale.setDefault(originalDefaultLocale);
112         }
113     }
114 
testEscapeUnicodeChars()115     public void testEscapeUnicodeChars() throws Exception {
116         assertEquals("", AdtUtils.replaceUnicodeEscapes(""));
117         assertEquals("foo bar", AdtUtils.replaceUnicodeEscapes("foo bar"));
118         assertEquals("\u25C0", AdtUtils.replaceUnicodeEscapes("\\u25C0"));
119         assertEquals("!\u25C0\u25C1!", AdtUtils.replaceUnicodeEscapes("!\\u25C0\\u25C1!"));
120         assertEquals("\u1234\\", AdtUtils.replaceUnicodeEscapes("\\u1234\\"));
121 
122         assertEquals("\\U25C0", AdtUtils.replaceUnicodeEscapes("\\U25C0")); // no unicode expand
123         assertEquals("\\u25C", AdtUtils.replaceUnicodeEscapes("\\u25C")); // no unicode expand
124         assertEquals("\\\\u25C0", AdtUtils.replaceUnicodeEscapes("\\\\u25C0")); // escaped
125         assertEquals("\\u123\\", AdtUtils.replaceUnicodeEscapes("\\u123\\")); // broken
126     }
127 }
128