1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package com.android.contacts;
18
19import android.content.Intent;
20import android.provider.ContactsContract.CommonDataKinds.Phone;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23
24/**
25 * Tests for {@link ContactsUtils}.
26 */
27@SmallTest
28public class ContactsUtilsTests extends AndroidTestCase {
29
30    public void testIsGraphicNull() throws Exception {
31        assertFalse(ContactsUtils.isGraphic(null));
32    }
33
34    public void testIsGraphicEmpty() throws Exception {
35        assertFalse(ContactsUtils.isGraphic(""));
36    }
37
38    public void testIsGraphicSpaces() throws Exception {
39        assertFalse(ContactsUtils.isGraphic("  "));
40    }
41
42    public void testIsGraphicPunctuation() throws Exception {
43        assertTrue(ContactsUtils.isGraphic("."));
44    }
45
46    public void testAreObjectsEqual() throws Exception {
47        assertTrue("null:null", ContactsUtils.areObjectsEqual(null, null));
48        assertTrue("1:1", ContactsUtils.areObjectsEqual(1, 1));
49
50        assertFalse("null:1", ContactsUtils.areObjectsEqual(null, 1));
51        assertFalse("1:null", ContactsUtils.areObjectsEqual(1, null));
52        assertFalse("1:2", ContactsUtils.areObjectsEqual(1, 2));
53    }
54
55    public void testShouldCollapse() throws Exception {
56        assertCollapses("1", true, null, null, null, null);
57        assertCollapses("2", true, "a", "b", "a", "b");
58
59        assertCollapses("11", false, "a", null, null, null);
60        assertCollapses("12", false, null, "a", null, null);
61        assertCollapses("13", false, null, null, "a", null);
62        assertCollapses("14", false, null, null, null, "a");
63
64        assertCollapses("21", false, "a", "b", null, null);
65        assertCollapses("22", false, "a", "b", "a", null);
66        assertCollapses("23", false, "a", "b", null, "b");
67        assertCollapses("24", false, "a", "b", "a", "x");
68        assertCollapses("25", false, "a", "b", "x", "b");
69
70        assertCollapses("31", false, null, null, "a", "b");
71        assertCollapses("32", false, "a", null, "a", "b");
72        assertCollapses("33", false, null, "b", "a", "b");
73        assertCollapses("34", false, "a", "x", "a", "b");
74        assertCollapses("35", false, "x", "b", "a", "b");
75
76        assertCollapses("41", true, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE,
77                null);
78        assertCollapses("42", true, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, "1");
79
80        assertCollapses("51", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE,
81                "2");
82        assertCollapses("52", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE,
83                null);
84        assertCollapses("53", false, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE,
85                "2");
86
87        // Test phone numbers
88        assertCollapses("60", true,
89                Phone.CONTENT_ITEM_TYPE, "1234567",
90                Phone.CONTENT_ITEM_TYPE, "1234567");
91        assertCollapses("61", false,
92                Phone.CONTENT_ITEM_TYPE, "1234567",
93                Phone.CONTENT_ITEM_TYPE, "1234568");
94        assertCollapses("62", true,
95                Phone.CONTENT_ITEM_TYPE, "1234567;0",
96                Phone.CONTENT_ITEM_TYPE, "1234567;0");
97        assertCollapses("63", false,
98                Phone.CONTENT_ITEM_TYPE, "1234567;89321",
99                Phone.CONTENT_ITEM_TYPE, "1234567;89322");
100        assertCollapses("64", true,
101                Phone.CONTENT_ITEM_TYPE, "1234567;89321",
102                Phone.CONTENT_ITEM_TYPE, "1234567;89321");
103        assertCollapses("65", false,
104                Phone.CONTENT_ITEM_TYPE, "1234567;0111111111",
105                Phone.CONTENT_ITEM_TYPE, "1234567;");
106        assertCollapses("66", false,
107                Phone.CONTENT_ITEM_TYPE, "12345675426;91970xxxxx",
108                Phone.CONTENT_ITEM_TYPE, "12345675426");
109        assertCollapses("67", false,
110                Phone.CONTENT_ITEM_TYPE, "12345675426;23456xxxxx",
111                Phone.CONTENT_ITEM_TYPE, "12345675426;234567xxxx");
112        assertCollapses("68", true,
113                Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567",
114                Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567");
115        assertCollapses("69", false,
116                Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567",
117                Phone.CONTENT_ITEM_TYPE, "1234567;1234567");
118
119        // test some numbers with country and area code
120        assertCollapses("70", true,
121                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678",
122                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678");
123        assertCollapses("71", true,
124                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678",
125                Phone.CONTENT_ITEM_TYPE, "+49 (89)12345678");
126        assertCollapses("72", true,
127                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234",
128                Phone.CONTENT_ITEM_TYPE, "+49 (8092)1234");
129        assertCollapses("73", false,
130                Phone.CONTENT_ITEM_TYPE, "0049 (8092) 1234",
131                Phone.CONTENT_ITEM_TYPE, "+49/80921234");
132        assertCollapses("74", false,
133                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678",
134                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345679");
135
136        // test some numbers with wait symbol and area code
137        assertCollapses("80", true,
138                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321",
139                Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321");
140        assertCollapses("81", false,
141                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321",
142                Phone.CONTENT_ITEM_TYPE, "+49/80921235;89321");
143        assertCollapses("82", false,
144                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89322",
145                Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321");
146        assertCollapses("83", true,
147                Phone.CONTENT_ITEM_TYPE, "1234567;+49 (8092) 1234",
148                Phone.CONTENT_ITEM_TYPE, "1234567;+49/80921234");
149
150        assertCollapses("86", true,
151                Phone.CONTENT_ITEM_TYPE, "",
152                Phone.CONTENT_ITEM_TYPE, "");
153
154        assertCollapses("87", false,
155                Phone.CONTENT_ITEM_TYPE, "1",
156                Phone.CONTENT_ITEM_TYPE, "");
157
158        assertCollapses("88", false,
159                Phone.CONTENT_ITEM_TYPE, "",
160                Phone.CONTENT_ITEM_TYPE, "1");
161
162        assertCollapses("89", true,
163                Phone.CONTENT_ITEM_TYPE, "---",
164                Phone.CONTENT_ITEM_TYPE, "---");
165
166        assertCollapses("90", true,
167                Phone.CONTENT_ITEM_TYPE, "1-/().",
168                Phone.CONTENT_ITEM_TYPE, "--$%1");
169
170        assertCollapses("91", true,
171                Phone.CONTENT_ITEM_TYPE, "abcdefghijklmnopqrstuvwxyz",
172                Phone.CONTENT_ITEM_TYPE, "22233344455566677778889999");
173
174        assertCollapses("92", false,
175                Phone.CONTENT_ITEM_TYPE, "1;2",
176                Phone.CONTENT_ITEM_TYPE, "12");
177
178        assertCollapses("93", false,
179                Phone.CONTENT_ITEM_TYPE, "1,2",
180                Phone.CONTENT_ITEM_TYPE, "12");
181    }
182
183    private void assertCollapses(String message, boolean expected, CharSequence mimetype1,
184            CharSequence data1, CharSequence mimetype2, CharSequence data2) {
185        assertEquals(message, expected,
186                ContactsUtils.shouldCollapse(mimetype1, data1, mimetype2, data2));
187        assertEquals(message, expected,
188                ContactsUtils.shouldCollapse(mimetype2, data2, mimetype1, data1));
189
190        // If data1 and data2 are the same instance, make sure the same test passes with different
191        // instances.
192        if (data1 == data2 && data1 != null) {
193            // Create a different instance
194            final CharSequence data2_newref = new StringBuilder(data2).append("").toString();
195
196            if (data1 == data2_newref) {
197                // In some cases no matter what we do the runtime reuses the same instance, so
198                // we can't do the "different instance" test.
199                return;
200            }
201
202            // we have two different instances, now make sure we get the same result as before
203            assertEquals(message, expected,
204                    ContactsUtils.shouldCollapse(mimetype1, data1, mimetype2,
205                    data2_newref));
206            assertEquals(message, expected,
207                    ContactsUtils.shouldCollapse(mimetype2, data2_newref, mimetype1,
208                    data1));
209        }
210    }
211
212    public void testAreIntentActionEqual() throws Exception {
213        assertTrue("1", ContactsUtils.areIntentActionEqual(null, null));
214        assertTrue("1", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("a")));
215
216        assertFalse("11", ContactsUtils.areIntentActionEqual(new Intent("a"), null));
217        assertFalse("12", ContactsUtils.areIntentActionEqual(null, new Intent("a")));
218
219        assertFalse("21", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent()));
220        assertFalse("22", ContactsUtils.areIntentActionEqual(new Intent(), new Intent("b")));
221        assertFalse("23", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("b")));
222    }
223}
224