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.common;
18
19import android.content.ContentValues;
20import android.content.Intent;
21import android.net.Uri;
22import android.provider.ContactsContract.CommonDataKinds.Email;
23import android.provider.ContactsContract.CommonDataKinds.Im;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
26import android.util.Pair;
27
28import com.android.contacts.common.ContactsUtils;
29import com.android.contacts.common.model.dataitem.DataItem;
30import com.android.contacts.common.model.dataitem.EmailDataItem;
31import com.android.contacts.common.model.dataitem.ImDataItem;
32
33/**
34 * Tests for {@link ContactsUtils}.
35 */
36@SmallTest
37public class ContactsUtilsTests extends AndroidTestCase {
38
39    private static final String TEST_ADDRESS = "user@example.org";
40    private static final String TEST_PROTOCOL = "prot%col";
41
42    public void testIsGraphicNull() throws Exception {
43        assertFalse(ContactsUtils.isGraphic(null));
44    }
45
46    public void testIsGraphicEmpty() throws Exception {
47        assertFalse(ContactsUtils.isGraphic(""));
48    }
49
50    public void testIsGraphicSpaces() throws Exception {
51        assertFalse(ContactsUtils.isGraphic("  "));
52    }
53
54    public void testIsGraphicPunctuation() throws Exception {
55        assertTrue(ContactsUtils.isGraphic("."));
56    }
57
58    public void testAreObjectsEqual() throws Exception {
59        assertTrue("null:null", ContactsUtils.areObjectsEqual(null, null));
60        assertTrue("1:1", ContactsUtils.areObjectsEqual(1, 1));
61
62        assertFalse("null:1", ContactsUtils.areObjectsEqual(null, 1));
63        assertFalse("1:null", ContactsUtils.areObjectsEqual(1, null));
64        assertFalse("1:2", ContactsUtils.areObjectsEqual(1, 2));
65    }
66
67    public void testAreIntentActionEqual() throws Exception {
68        assertTrue("1", ContactsUtils.areIntentActionEqual(null, null));
69        assertTrue("1", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("a")));
70
71        assertFalse("11", ContactsUtils.areIntentActionEqual(new Intent("a"), null));
72        assertFalse("12", ContactsUtils.areIntentActionEqual(null, new Intent("a")));
73
74        assertFalse("21", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent()));
75        assertFalse("22", ContactsUtils.areIntentActionEqual(new Intent(), new Intent("b")));
76        assertFalse("23", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("b")));
77    }
78
79    public void testImIntentCustom() throws Exception {
80        // Custom IM types have encoded authority. We send the imto Intent here, because
81        // legacy third party apps might not accept xmpp yet
82        final ContentValues values = new ContentValues();
83        values.put(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE);
84        values.put(Im.TYPE, Im.TYPE_HOME);
85        values.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM);
86        values.put(Im.CUSTOM_PROTOCOL, TEST_PROTOCOL);
87        values.put(Im.DATA, TEST_ADDRESS);
88        final ImDataItem im = (ImDataItem) DataItem.createFrom(values);
89
90        final Pair<Intent, Intent> intents = ContactsUtils.buildImIntent(getContext(), im);
91        final Intent imIntent = intents.first;
92
93        assertEquals(Intent.ACTION_SENDTO, imIntent.getAction());
94
95        final Uri data = imIntent.getData();
96        assertEquals("imto", data.getScheme());
97        assertEquals(TEST_PROTOCOL, data.getAuthority());
98        assertEquals(TEST_ADDRESS, data.getPathSegments().get(0));
99
100        assertNull(intents.second);
101    }
102
103    public void testImIntent() throws Exception {
104        // Test GTalk XMPP URI. No chat capabilities provided
105        final ContentValues values = new ContentValues();
106        values.put(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE);
107        values.put(Im.TYPE, Im.TYPE_HOME);
108        values.put(Im.PROTOCOL, Im.PROTOCOL_GOOGLE_TALK);
109        values.put(Im.DATA, TEST_ADDRESS);
110        final ImDataItem im = (ImDataItem) DataItem.createFrom(values);
111
112        final Pair<Intent, Intent> intents = ContactsUtils.buildImIntent(getContext(), im);
113        final Intent imIntent = intents.first;
114
115        assertEquals(Intent.ACTION_SENDTO, imIntent.getAction());
116        assertEquals("xmpp:" + TEST_ADDRESS + "?message", imIntent.getData().toString());
117
118        assertNull(intents.second);
119    }
120
121    public void testImIntentWithAudio() throws Exception {
122        // Test GTalk XMPP URI. Audio chat capabilities provided
123        final ContentValues values = new ContentValues();
124        values.put(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE);
125        values.put(Im.TYPE, Im.TYPE_HOME);
126        values.put(Im.PROTOCOL, Im.PROTOCOL_GOOGLE_TALK);
127        values.put(Im.DATA, TEST_ADDRESS);
128        values.put(Im.CHAT_CAPABILITY, Im.CAPABILITY_HAS_VOICE | Im.CAPABILITY_HAS_VIDEO);
129        final ImDataItem im = (ImDataItem) DataItem.createFrom(values);
130
131        final Pair<Intent, Intent> intents = ContactsUtils.buildImIntent(getContext(), im);
132        final Intent imIntent = intents.first;
133
134        assertEquals(Intent.ACTION_SENDTO, imIntent.getAction());
135        assertEquals("xmpp:" + TEST_ADDRESS + "?message", imIntent.getData().toString());
136
137        final Intent secondaryIntent = intents.second;
138        assertEquals(Intent.ACTION_SENDTO, secondaryIntent.getAction());
139        assertEquals("xmpp:" + TEST_ADDRESS + "?call", secondaryIntent.getData().toString());
140    }
141
142    public void testImIntentWithVideo() throws Exception {
143        // Test GTalk XMPP URI. Video chat capabilities provided
144        final ContentValues values = new ContentValues();
145        values.put(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE);
146        values.put(Im.TYPE, Im.TYPE_HOME);
147        values.put(Im.PROTOCOL, Im.PROTOCOL_GOOGLE_TALK);
148        values.put(Im.DATA, TEST_ADDRESS);
149        values.put(Im.CHAT_CAPABILITY, Im.CAPABILITY_HAS_VOICE | Im.CAPABILITY_HAS_VIDEO |
150                Im.CAPABILITY_HAS_VOICE);
151        final ImDataItem im = (ImDataItem) DataItem.createFrom(values);
152
153        final Pair<Intent, Intent> intents = ContactsUtils.buildImIntent(getContext(), im);
154        final Intent imIntent = intents.first;
155
156        assertEquals(Intent.ACTION_SENDTO, imIntent.getAction());
157        assertEquals("xmpp:" + TEST_ADDRESS + "?message", imIntent.getData().toString());
158
159        final Intent secondaryIntent = intents.second;
160        assertEquals(Intent.ACTION_SENDTO, secondaryIntent.getAction());
161        assertEquals("xmpp:" + TEST_ADDRESS + "?call", secondaryIntent.getData().toString());
162    }
163
164
165    public void testImEmailIntent() throws Exception {
166        // Email addresses are treated as Google Talk entries
167        // This test only tests the VIDEO+CAMERA case. The other cases have been addressed by the
168        // Im tests
169        final ContentValues values = new ContentValues();
170        values.put(Email.MIMETYPE, Email.CONTENT_ITEM_TYPE);
171        values.put(Email.TYPE, Email.TYPE_HOME);
172        values.put(Email.DATA, TEST_ADDRESS);
173        values.put(Email.CHAT_CAPABILITY, Im.CAPABILITY_HAS_VOICE | Im.CAPABILITY_HAS_VIDEO |
174                Im.CAPABILITY_HAS_VOICE);
175        final ImDataItem im = ImDataItem.createFromEmail(
176                (EmailDataItem) DataItem.createFrom(values));
177
178        final Pair<Intent, Intent> intents = ContactsUtils.buildImIntent(getContext(), im);
179        final Intent imIntent = intents.first;
180
181        assertEquals(Intent.ACTION_SENDTO, imIntent.getAction());
182        assertEquals("xmpp:" + TEST_ADDRESS + "?message", imIntent.getData().toString());
183
184        final Intent secondaryIntent = intents.second;
185        assertEquals(Intent.ACTION_SENDTO, secondaryIntent.getAction());
186        assertEquals("xmpp:" + TEST_ADDRESS + "?call", secondaryIntent.getData().toString());
187    }
188}
189