1/*
2 * Copyright (C) 2016 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 */
16package com.android.emergency.preferences;
17
18import android.app.Instrumentation;
19import android.content.Intent;
20import android.content.IntentFilter;
21import android.net.Uri;
22import android.provider.ContactsContract;
23import android.test.ActivityInstrumentationTestCase2;
24import android.test.suitebuilder.annotation.MediumTest;
25
26import com.android.emergency.ContactTestUtils;
27import com.android.emergency.edit.EditInfoActivity;
28
29/**
30 * Tests for {@link ContactPreference}.
31 */
32@MediumTest
33public class ContactPreferenceTest extends ActivityInstrumentationTestCase2<EditInfoActivity> {
34    private static final String NAME = "Jake";
35    private static final String PHONE_NUMBER = "123456";
36    private ContactPreference mContactPreference;
37    private Uri mContactUri;
38
39    public ContactPreferenceTest() {
40        super(EditInfoActivity.class);
41    }
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45        mContactUri =
46                ContactTestUtils.createContact(getActivity().getContentResolver(),
47                        NAME,
48                        PHONE_NUMBER);
49        mContactPreference = new ContactPreference(getActivity(), mContactUri);
50    }
51
52    @Override
53    protected void tearDown() throws Exception {
54        assertTrue(ContactTestUtils.deleteContact(getActivity().getContentResolver(),
55                NAME,
56                PHONE_NUMBER));
57        super.tearDown();
58    }
59
60    public void testContactPreference() {
61        assertEquals(mContactUri, mContactPreference.getContactUri());
62        assertEquals(NAME, mContactPreference.getContact().getName());
63        assertEquals(PHONE_NUMBER, mContactPreference.getContact().getPhoneNumber());
64
65        assertNull(mContactPreference.getRemoveContactDialog());
66        mContactPreference.setRemoveContactPreferenceListener(
67                new ContactPreference.RemoveContactPreferenceListener() {
68                    @Override
69                    public void onRemoveContactPreference(ContactPreference preference) {
70                        // Do nothing
71                    }
72                });
73        assertNotNull(mContactPreference.getRemoveContactDialog());
74    }
75
76
77    public void testDisplayContact() throws Throwable {
78        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
79        intentFilter.addDataType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
80        Instrumentation.ActivityMonitor activityMonitor =
81                getInstrumentation().addMonitor(intentFilter, null, true /* block */);
82        mContactPreference.displayContact();
83
84        assertEquals(true, getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */));
85    }
86
87    public void testCallContact() throws Throwable {
88        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CALL);
89        intentFilter.addDataScheme("tel");
90        Instrumentation.ActivityMonitor activityMonitor =
91                getInstrumentation().addMonitor(intentFilter, null, true /* block */);
92        mContactPreference.callContact();
93
94        assertEquals(true, getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */));
95    }
96}
97