/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.emergency.preferences;

import android.app.Instrumentation;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.provider.ContactsContract;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;

import com.android.emergency.ContactTestUtils;
import com.android.emergency.edit.EditInfoActivity;

/**
 * Tests for {@link ContactPreference}.
 */
@MediumTest
public class ContactPreferenceTest extends ActivityInstrumentationTestCase2<EditInfoActivity> {
    private static final String NAME = "Jake";
    private static final String PHONE_NUMBER = "123456";
    private ContactPreference mContactPreference;
    private Uri mContactUri;

    public ContactPreferenceTest() {
        super(EditInfoActivity.class);
    }
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mContactUri =
                ContactTestUtils.createContact(getActivity().getContentResolver(),
                        NAME,
                        PHONE_NUMBER);
        mContactPreference = new ContactPreference(getActivity(), mContactUri);
    }

    @Override
    protected void tearDown() throws Exception {
        assertTrue(ContactTestUtils.deleteContact(getActivity().getContentResolver(),
                NAME,
                PHONE_NUMBER));
        super.tearDown();
    }

    public void testContactPreference() {
        assertEquals(mContactUri, mContactPreference.getContactUri());
        assertEquals(NAME, mContactPreference.getContact().getName());
        assertEquals(PHONE_NUMBER, mContactPreference.getContact().getPhoneNumber());

        assertNull(mContactPreference.getRemoveContactDialog());
        mContactPreference.setRemoveContactPreferenceListener(
                new ContactPreference.RemoveContactPreferenceListener() {
                    @Override
                    public void onRemoveContactPreference(ContactPreference preference) {
                        // Do nothing
                    }
                });
        assertNotNull(mContactPreference.getRemoveContactDialog());
    }


    public void testDisplayContact() throws Throwable {
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
        intentFilter.addDataType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
        Instrumentation.ActivityMonitor activityMonitor =
                getInstrumentation().addMonitor(intentFilter, null, true /* block */);
        mContactPreference.displayContact();

        assertEquals(true, getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */));
    }

    public void testCallContact() throws Throwable {
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CALL);
        intentFilter.addDataScheme("tel");
        Instrumentation.ActivityMonitor activityMonitor =
                getInstrumentation().addMonitor(intentFilter, null, true /* block */);
        mContactPreference.callContact();

        assertEquals(true, getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */));
    }
}
