Home | History | Annotate | Download | only in lnotifications
      1 package com.example.android.lnotifications;
      2 
      3 import android.app.Activity;
      4 import android.app.Notification;
      5 import android.content.Intent;
      6 import android.net.Uri;
      7 import android.test.ActivityInstrumentationTestCase2;
      8 import android.util.Log;
      9 
     10 import java.util.ArrayList;
     11 import java.util.Arrays;
     12 import java.util.Collections;
     13 
     14 /**
     15  * Unit tests for {@link OtherMetadataFragment}.
     16  */
     17 public class OtherMetadataFragmentTest extends
     18         ActivityInstrumentationTestCase2<LNotificationActivity> {
     19 
     20     private LNotificationActivity mActivity;
     21     private OtherMetadataFragment mFragment;
     22 
     23     public OtherMetadataFragmentTest() {
     24         super(LNotificationActivity.class);
     25     }
     26 
     27     @Override
     28     protected void setUp() throws Exception {
     29         super.setUp();
     30         mActivity = getActivity();
     31         // The third tab should be {@link OtherMetadataFragment}, that is tested in the
     32         // {@link LNotificationActivityTest}.
     33         mActivity.getActionBar().setSelectedNavigationItem(2);
     34         getInstrumentation().waitForIdleSync();
     35         mFragment = (OtherMetadataFragment) mActivity.getFragmentManager()
     36                 .findFragmentById(R.id.container);
     37     }
     38 
     39     public void testPreconditions() {
     40         assertNotNull(mActivity);
     41         assertNotNull(mFragment);
     42         assertNull(mFragment.mContactUri);
     43         assertNotNull(mActivity.findViewById(R.id.attach_person));
     44         assertNotNull(mActivity.findViewById(R.id.category_spinner));
     45         assertNotNull(mActivity.findViewById(R.id.priority_spinner));
     46         assertNotNull(mActivity.findViewById(R.id.show_notification_button));
     47     }
     48 
     49     public void testCreateNotification_verifyPriorityAndCategoryWithoutPerson() {
     50         Notification notification = mFragment.createNotification(OtherMetadataFragment.Priority
     51                 .HIGH, OtherMetadataFragment.Category.CALL, null);
     52         assertEquals(Notification.PRIORITY_HIGH, notification.priority);
     53         assertEquals(Notification.CATEGORY_CALL, notification.category);
     54     }
     55 
     56     public void testCreateNotification_verifyPriorityAndCategoryWithPerson() {
     57         String tel = "81 (90) 555-1212";
     58         Uri dummyContactUri = Uri.fromParts("tel", tel, null);
     59         Notification notification = mFragment.createNotification(OtherMetadataFragment.Priority
     60                 .DEFAULT, OtherMetadataFragment.Category.MESSAGE, dummyContactUri);
     61         assertEquals(Notification.PRIORITY_DEFAULT, notification.priority);
     62         assertEquals(Notification.CATEGORY_MESSAGE, notification.category);
     63 
     64         String[] peopleArray = notification.extras.getStringArray(Notification.EXTRA_PEOPLE);
     65         assertNotNull(peopleArray);
     66         assertEquals(1, peopleArray.length);
     67     }
     68 
     69     public void testActionPickResultUpdatesContactInstanceField() {
     70         getInstrumentation().runOnMainSync(new Runnable() {
     71             @Override
     72             public void run() {
     73                 String tel = "81 (90) 555-1212";
     74                 Uri dummyContactUri = Uri.fromParts("tel", tel, null);
     75                 Intent intent = new Intent(Intent.ACTION_PICK);
     76                 intent.setData(dummyContactUri);
     77                 mFragment.onActivityResult(mFragment.REQUEST_CODE_PICK_CONTACT,
     78                         Activity.RESULT_OK, intent);
     79             }
     80         });
     81         getInstrumentation().waitForIdleSync();
     82         assertNotNull(mFragment.mContactUri);
     83     }
     84 }
     85