Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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.widget.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.content.ContentUris;
     23 import android.content.Context;
     24 import android.content.ContextWrapper;
     25 import android.content.Intent;
     26 import android.net.Uri;
     27 import android.os.Bundle;
     28 import android.os.UserHandle;
     29 import android.provider.ContactsContract;
     30 import android.provider.ContactsContract.Contacts;
     31 import android.support.test.InstrumentationRegistry;
     32 import android.support.test.annotation.UiThreadTest;
     33 import android.support.test.filters.SmallTest;
     34 import android.support.test.runner.AndroidJUnit4;
     35 import android.widget.QuickContactBadge;
     36 
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 
     40 import java.util.concurrent.CountDownLatch;
     41 import java.util.concurrent.TimeUnit;
     42 
     43 @SmallTest
     44 @RunWith(AndroidJUnit4.class)
     45 public class QuickContactBadgeTest {
     46     @UiThreadTest
     47     @Test
     48     public void testPrioritizedMimetype() throws InterruptedException {
     49         final String plainMimeType = "text/plain";
     50         final Uri nonExistentContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 0);
     51         final CountDownLatch latch = new CountDownLatch(1);
     52         final Context context = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
     53             @Override
     54             public void startActivity(Intent intent) {
     55                 testCallback(intent);
     56             }
     57 
     58             // @Override
     59             public void startActivityAsUser(Intent intent, UserHandle user) {
     60                 testCallback(intent);
     61             }
     62 
     63             // @Override
     64             public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
     65                 testCallback(intent);
     66             }
     67 
     68             private void testCallback(Intent intent) {
     69                 assertEquals(plainMimeType, intent.getStringExtra(
     70                         ContactsContract.QuickContact.EXTRA_PRIORITIZED_MIMETYPE));
     71                 latch.countDown();
     72             }
     73         };
     74 
     75         // Execute: create QuickContactBadge with a prioritized mimetype and click on it
     76         QuickContactBadge badge = new QuickContactBadge(context);
     77         badge.setPrioritizedMimeType(plainMimeType);
     78         badge.assignContactUri(nonExistentContactUri);
     79         badge.onClick(badge);
     80 
     81         // Verify: the QuickContactBadge attempts to start an activity, and sets the
     82         // prioritized mimetype. We don't know which method will be used to start the activity,
     83         // so we check all options.
     84         assertTrue(latch.await(1, TimeUnit.SECONDS));
     85     }
     86 }
     87 
     88