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