1 /*
2  * Copyright (C) 2013 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 android.provider.cts.contacts;
18 
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.provider.ContactsContract;
23 import android.test.AndroidTestCase;
24 
25 import com.android.compatibility.common.util.CddTest;
26 
27 import java.util.List;
28 
29 /**
30  * Tests to verify that common actions on {@link ContactsContract} content are
31  * available.
32  */
33 public class ContactsContractIntentsTest extends AndroidTestCase {
assertCanBeHandled(Intent intent)34     public void assertCanBeHandled(Intent intent) {
35         List<ResolveInfo> resolveInfoList = getContext()
36                 .getPackageManager().queryIntentActivities(intent, 0);
37         assertNotNull("Missing ResolveInfo", resolveInfoList);
38         assertTrue("No ResolveInfo found for " + intent.toString(),
39                 resolveInfoList.size() > 0);
40     }
41 
testViewContactDir()42     public void testViewContactDir() {
43         Intent intent = new Intent(Intent.ACTION_VIEW);
44         intent.setData(ContactsContract.Contacts.CONTENT_URI);
45         assertCanBeHandled(intent);
46     }
47 
testPickContactDir()48     public void testPickContactDir() {
49         Intent intent = new Intent(Intent.ACTION_PICK);
50         intent.setData(ContactsContract.Contacts.CONTENT_URI);
51         assertCanBeHandled(intent);
52     }
53 
testGetContentContactDir()54     public void testGetContentContactDir() {
55         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
56         intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
57         assertCanBeHandled(intent);
58     }
59 
60     @CddTest(requirements={"3.18/C-2-1"})
testSetDefaultAccount()61     public void testSetDefaultAccount() {
62         PackageManager packageManager = getContext().getPackageManager();
63         if (
64                 packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)
65                 || packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)
66         ) {
67             return; // Skip test on watch and automotive since the intent is not required.
68         }
69 
70         Intent intent = new Intent(ContactsContract.Settings.ACTION_SET_DEFAULT_ACCOUNT);
71         List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
72         assertNotNull("Missing ResolveInfo", resolveInfoList);
73         int handlerCount = 0;
74         for (ResolveInfo resolveInfo : resolveInfoList) {
75             String packageName = resolveInfo.activityInfo.packageName;
76             if (packageManager.checkPermission(
77                     android.Manifest.permission.SET_DEFAULT_ACCOUNT_FOR_CONTACTS, packageName)
78                     == PackageManager.PERMISSION_GRANTED) {
79                 handlerCount++;
80             }
81         }
82         assertEquals(1, handlerCount);
83     }
84 }
85