Home | History | Annotate | Download | only in contacts
      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 
     17 package com.android.contacts;
     18 
     19 import android.content.ContentValues;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 import android.provider.ContactsContract.CommonDataKinds.Email;
     23 import android.provider.ContactsContract.CommonDataKinds.Im;
     24 import android.test.AndroidTestCase;
     25 import android.test.suitebuilder.annotation.SmallTest;
     26 import android.util.Pair;
     27 
     28 import com.android.contacts.model.dataitem.DataItem;
     29 import com.android.contacts.model.dataitem.EmailDataItem;
     30 import com.android.contacts.model.dataitem.ImDataItem;
     31 
     32 /**
     33  * Tests for {@link ContactsUtils}.
     34  */
     35 @SmallTest
     36 public class ContactsUtilsTests extends AndroidTestCase {
     37 
     38     private static final String TEST_ADDRESS = "user (at) example.org";
     39     private static final String TEST_PROTOCOL = "prot%col";
     40 
     41     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     }
     77 
     78     public void testImIntentCustom() throws Exception {
     79         // 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);
     85         values.put(Im.CUSTOM_PROTOCOL, TEST_PROTOCOL);
     86         values.put(Im.DATA, TEST_ADDRESS);
     87         final ImDataItem im = (ImDataItem) DataItem.createFrom(values);
     88 
     89         final Pair<Intent, Intent> intents = ContactsUtils.buildImIntent(getContext(), im);
     90         final Intent imIntent = intents.first;
     91 
     92         assertEquals(Intent.ACTION_SENDTO, imIntent.getAction());
     93 
     94         final Uri data = imIntent.getData();
     95         assertEquals("imto", data.getScheme());
     96         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());
    186     }
    187 }
    188