Fix Dialer tests

* Empty geocode is now " " instead of "-" per UX request
* DialpadFragment now throws IllegalArgumentException instead
of Log.wtf so that it can be tested
* Added contact id column to contactsprovider query
* Modified PhoneNumberDisplayHelper to take an instance of
PhoneNumberUtilsWrapper so that it can be mocked out
Fix label-related tests that were failing due to a change in how we
treat empty labels

Bug: 9111164

Change-Id: If2244586b9d09fa2839fa0ddfc9f369f9dc66e51
(cherry picked from commit 3671725b58a9768016e141c77424dedb5fd2c55a)
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3723fd7..a0376a6 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -456,8 +456,9 @@
     <!-- String describing the icon used to start a voice search -->
     <string name="description_start_voice_search">Start voice search</string>
 
-    <!-- The string used to represent an unknown location for a phone number in the call log [CHAR LIMIT=3] -->
-    <string name="call_log_empty_gecode">\u0020</string>
+    <!-- The string used to represent an unknown location for a phone number in the call log
+        Do not translate. -->
+    <string name="call_log_empty_geocode"></string>
 
     <!-- Menu item used to call a contact, containing the number of the contact to call -->
     <string name="menu_callNumber">Call <xliff:g id="number">%s</xliff:g></string>
diff --git a/src/com/android/dialer/PhoneCallDetailsHelper.java b/src/com/android/dialer/PhoneCallDetailsHelper.java
index 8e6c943..446df02 100644
--- a/src/com/android/dialer/PhoneCallDetailsHelper.java
+++ b/src/com/android/dialer/PhoneCallDetailsHelper.java
@@ -62,8 +62,8 @@
             PhoneNumberUtilsWrapper phoneUtils) {
         mResources = resources;
         mCallTypeHelper = callTypeHelper;
-        mPhoneNumberHelper = new PhoneNumberDisplayHelper(resources);
         mPhoneNumberUtilsWrapper = phoneUtils;
+        mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
     }
 
     /** Fills the call details views with content. */
@@ -121,7 +121,7 @@
             nameText = displayNumber;
             if (TextUtils.isEmpty(details.geocode)
                     || mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
-                numberText = mResources.getString(R.string.call_log_empty_gecode);
+                numberText = mResources.getString(R.string.call_log_empty_geocode);
             } else {
                 numberText = details.geocode;
             }
@@ -136,7 +136,6 @@
         }
 
         views.nameView.setText(nameText);
-
         views.labelView.setText(labelText);
         views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
     }
diff --git a/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java b/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java
index 81f3ca1..5d7ce7e 100644
--- a/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java
+++ b/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java
@@ -18,8 +18,8 @@
 
 import android.content.res.Resources;
 import android.provider.CallLog.Calls;
-import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
+import android.util.Log;
 
 import com.android.dialer.R;
 
@@ -27,10 +27,17 @@
  * Helper for formatting and managing the display of phone numbers.
  */
 public class PhoneNumberDisplayHelper {
+    private final PhoneNumberUtilsWrapper mPhoneNumberUtils;
     private final Resources mResources;
 
     public PhoneNumberDisplayHelper(Resources resources) {
         mResources = resources;
+        mPhoneNumberUtils = new PhoneNumberUtilsWrapper();
+    }
+
+    public PhoneNumberDisplayHelper(PhoneNumberUtilsWrapper phoneNumberUtils, Resources resources) {
+        mPhoneNumberUtils = phoneNumberUtils;
+        mResources = resources;
     }
 
     /* package */ CharSequence getDisplayName(CharSequence number, int presentation) {
@@ -43,7 +50,7 @@
         if (presentation == Calls.PRESENTATION_PAYPHONE) {
             return mResources.getString(R.string.payphone);
         }
-        if (new PhoneNumberUtilsWrapper().isVoicemailNumber(number)) {
+        if (mPhoneNumberUtils.isVoicemailNumber(number)) {
             return mResources.getString(R.string.voicemail);
         }
         if (PhoneNumberUtilsWrapper.isLegacyUnknownNumbers(number)) {
@@ -62,7 +69,6 @@
             int presentation, CharSequence formattedNumber) {
 
         final CharSequence displayName = getDisplayName(number, presentation);
-
         if (!TextUtils.isEmpty(displayName)) {
             return displayName;
         }
diff --git a/src/com/android/dialer/dialpad/DialpadFragment.java b/src/com/android/dialer/dialpad/DialpadFragment.java
index 00b8281..093cde2 100644
--- a/src/com/android/dialer/dialpad/DialpadFragment.java
+++ b/src/com/android/dialer/dialpad/DialpadFragment.java
@@ -1560,9 +1560,9 @@
      * or Wait character (;).
      */
     private void updateDialString(char newDigit) {
-        if(newDigit != WAIT && newDigit != PAUSE) {
-            Log.wtf(TAG, "Not expected for anything other than PAUSE & WAIT");
-            return;
+        if (newDigit != WAIT && newDigit != PAUSE) {
+            throw new IllegalArgumentException(
+                    "Not expected for anything other than PAUSE & WAIT");
         }
 
         int selectionStart;
@@ -1642,8 +1642,8 @@
     /* package */ static boolean canAddDigit(CharSequence digits, int start, int end,
                                              char newDigit) {
         if(newDigit != WAIT && newDigit != PAUSE) {
-            Log.wtf(TAG, "Should not be called for anything other than PAUSE & WAIT");
-            return false;
+            throw new IllegalArgumentException(
+                    "Should not be called for anything other than PAUSE & WAIT");
         }
 
         // False if no selection, or selection is reversed (end < start)
diff --git a/tests/src/com/android/dialer/PhoneCallDetailsHelperTest.java b/tests/src/com/android/dialer/PhoneCallDetailsHelperTest.java
index 6a9817f..6f5a986 100644
--- a/tests/src/com/android/dialer/PhoneCallDetailsHelperTest.java
+++ b/tests/src/com/android/dialer/PhoneCallDetailsHelperTest.java
@@ -51,6 +51,8 @@
     private static final String TEST_COUNTRY_ISO = "US";
     /** The geocoded location used in the tests. */
     private static final String TEST_GEOCODE = "United States";
+    /** Empty geocode label */
+    private static final String EMPTY_GEOCODE = "";
 
     /** The object under test. */
     private PhoneCallDetailsHelper mHelper;
@@ -183,18 +185,18 @@
     public void testSetPhoneCallDetails_NoGeocode() {
         setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", null);
         assertNameEquals("1-412-555-5555");  // The phone number is shown as the name.
-        assertLabelEquals("-"); // The empty geocode is shown as the label.
+        assertLabelEquals(EMPTY_GEOCODE); // The empty geocode is shown as the label.
     }
 
     public void testSetPhoneCallDetails_EmptyGeocode() {
         setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", "");
         assertNameEquals("1-412-555-5555");  // The phone number is shown as the name.
-        assertLabelEquals("-"); // The empty geocode is shown as the label.
+        assertLabelEquals(EMPTY_GEOCODE); // The empty geocode is shown as the label.
     }
 
     public void testSetPhoneCallDetails_NoGeocodeForVoicemail() {
         setPhoneCallDetailsWithNumberAndGeocode(TEST_VOICEMAIL_NUMBER, "", "United States");
-        assertLabelEquals("-"); // The empty geocode is shown as the label.
+        assertLabelEquals(EMPTY_GEOCODE); // The empty geocode is shown as the label.
     }
 
     public void testSetPhoneCallDetails_Highlighted() {
diff --git a/tests/src/com/android/dialer/calllog/CallLogFragmentTest.java b/tests/src/com/android/dialer/calllog/CallLogFragmentTest.java
index 7ceec8f..4ccdaaf 100644
--- a/tests/src/com/android/dialer/calllog/CallLogFragmentTest.java
+++ b/tests/src/com/android/dialer/calllog/CallLogFragmentTest.java
@@ -70,6 +70,8 @@
     /** The formatted version of {@link #TEST_NUMBER}. */
     private static final String TEST_FORMATTED_NUMBER = "1 212-555-1000";
 
+    private static final String TEST_DEFAULT_CUSTOM_LABEL = "myLabel";
+
     /** The activity in which we are hosting the fragment. */
     private FragmentTestActivity mActivity;
     private CallLogFragment mFragment;
@@ -218,8 +220,10 @@
     @MediumTest
     public void testBindView_WithCachedName() {
         mCursor.moveToFirst();
+        // provide a default custom label instead of an empty string, which corresponds to
+        // {@value com.android.dialer.calllog.ContactInfo#GEOCODE_AS_LABEL}
         insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
-                "John Doe", Phone.TYPE_HOME, "");
+                "John Doe", Phone.TYPE_HOME, TEST_DEFAULT_CUSTOM_LABEL);
         View view = mAdapter.newStandAloneView(getActivity(), mParentView);
         mAdapter.bindStandAloneView(view, getActivity(), mCursor);
 
@@ -232,20 +236,22 @@
     public void testBindView_UriNumber() {
         mCursor.moveToFirst();
         insertWithCachedValues("sip:johndoe@gmail.com", NOW, 0, Calls.INCOMING_TYPE,
-                "John Doe", Phone.TYPE_HOME, "");
+                "John Doe", Phone.TYPE_HOME, TEST_DEFAULT_CUSTOM_LABEL);
         View view = mAdapter.newStandAloneView(getActivity(), mParentView);
         mAdapter.bindStandAloneView(view, getActivity(), mCursor);
 
         CallLogListItemViews views = (CallLogListItemViews) view.getTag();
         assertNameIs(views, "John Doe");
-        assertLabel(views, "sip:johndoe@gmail.com", null);
+        assertLabel(views, "sip:johndoe@gmail.com", "sip:johndoe@gmail.com");
     }
 
     @MediumTest
     public void testBindView_HomeLabel() {
         mCursor.moveToFirst();
+        // provide a default custom label instead of an empty string, which corresponds to
+        // {@value com.android.dialer.calllog.ContactInfo#GEOCODE_AS_LABEL}
         insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
-                "John Doe", Phone.TYPE_HOME, "");
+                "John Doe", Phone.TYPE_HOME, TEST_DEFAULT_CUSTOM_LABEL);
         View view = mAdapter.newStandAloneView(getActivity(), mParentView);
         mAdapter.bindStandAloneView(view, getActivity(), mCursor);
 
@@ -257,8 +263,10 @@
     @MediumTest
     public void testBindView_WorkLabel() {
         mCursor.moveToFirst();
+        // provide a default custom label instead of an empty string, which corresponds to
+        // {@link com.android.dialer.calllog.ContactInfo#GEOCODE_AS_LABEL}
         insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
-                "John Doe", Phone.TYPE_WORK, "");
+                "John Doe", Phone.TYPE_WORK, TEST_DEFAULT_CUSTOM_LABEL);
         View view = mAdapter.newStandAloneView(getActivity(), mParentView);
         mAdapter.bindStandAloneView(view, getActivity(), mCursor);
 
@@ -607,7 +615,9 @@
                 privateOrUnknownOrVm[2] = true;
             } else {
                 int inout = mRnd.nextBoolean() ? Calls.OUTGOING_TYPE :  Calls.INCOMING_TYPE;
-                String number = new Formatter().format("1800123%04d", i).toString();
+                final Formatter formatter = new Formatter();
+                String number = formatter.format("1800123%04d", i).toString();
+                formatter.close();
                 insert(number, Calls.PRESENTATION_ALLOWED, NOW, RAND_DURATION, inout);
             }
         }
diff --git a/tests/src/com/android/dialer/calllog/CallLogListItemHelperTest.java b/tests/src/com/android/dialer/calllog/CallLogListItemHelperTest.java
index 662afa0..a10dec9 100644
--- a/tests/src/com/android/dialer/calllog/CallLogListItemHelperTest.java
+++ b/tests/src/com/android/dialer/calllog/CallLogListItemHelperTest.java
@@ -50,6 +50,7 @@
     /** The views used in the tests. */
     private CallLogListItemViews mViews;
     private PhoneNumberDisplayHelper mPhoneNumberHelper;
+    private PhoneNumberDisplayHelper mPhoneNumberDisplayHelper;
 
     @Override
     protected void setUp() throws Exception {
@@ -61,7 +62,9 @@
                 TEST_VOICEMAIL_NUMBER);
         PhoneCallDetailsHelper phoneCallDetailsHelper = new PhoneCallDetailsHelper(
                 resources, callTypeHelper, phoneUtils);
-        mHelper = new CallLogListItemHelper(phoneCallDetailsHelper, mPhoneNumberHelper, resources);
+        mPhoneNumberDisplayHelper = new PhoneNumberDisplayHelper(resources);
+        mHelper = new CallLogListItemHelper(phoneCallDetailsHelper, mPhoneNumberDisplayHelper,
+                resources);
         mViews = CallLogListItemViews.createForTest(context);
     }
 
diff --git a/tests/src/com/android/dialer/calllog/TestPhoneNumberUtilsWrapper.java b/tests/src/com/android/dialer/calllog/TestPhoneNumberUtilsWrapper.java
index 95f3cc1..7266d88 100644
--- a/tests/src/com/android/dialer/calllog/TestPhoneNumberUtilsWrapper.java
+++ b/tests/src/com/android/dialer/calllog/TestPhoneNumberUtilsWrapper.java
@@ -16,8 +16,6 @@
 
 package com.android.dialer.calllog;
 
-import android.content.res.Resources;
-
 /**
  * Modified version of {@link com.android.dialer.calllog.PhoneNumberDisplayHelper} to be used in tests
  * that allows injecting the voicemail number.
diff --git a/tests/src/com/android/dialer/dialpad/DialpadFragmentTest.java b/tests/src/com/android/dialer/dialpad/DialpadFragmentTest.java
index a123e74..6f18fe6 100644
--- a/tests/src/com/android/dialer/dialpad/DialpadFragmentTest.java
+++ b/tests/src/com/android/dialer/dialpad/DialpadFragmentTest.java
@@ -34,7 +34,11 @@
 
     public void testCanAddDigit_InvalidCharacter() {
         // only handles wait/pause
-        assertFalse(DialpadFragment.canAddDigit("123", 1, 1, '5'));
+        try {
+            DialpadFragment.canAddDigit("123", 1, 1, '5');
+            fail("Calling canAddDigit with invalid character should throw an exception");
+        } catch (IllegalArgumentException e) {
+        }
     }
 
     public void testCanAddDigit_BadOrNoSelection() {
diff --git a/tests/src/com/android/dialer/interactions/PhoneNumberInteractionTest.java b/tests/src/com/android/dialer/interactions/PhoneNumberInteractionTest.java
index f86675e..fbc64cd 100644
--- a/tests/src/com/android/dialer/interactions/PhoneNumberInteractionTest.java
+++ b/tests/src/com/android/dialer/interactions/PhoneNumberInteractionTest.java
@@ -91,7 +91,7 @@
         Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
         expectQuery(contactUri)
                 .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null,
-                        Phone.CONTENT_ITEM_TYPE);
+                        Phone.CONTENT_ITEM_TYPE, 13);
 
         TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                 mContext, ContactDisplayUtils.INTERACTION_SMS, null);
@@ -110,7 +110,7 @@
         Uri dataUri = ContentUris.withAppendedId(Data.CONTENT_URI, 1);
         expectQuery(dataUri, true /* isDataUri */ )
                 .returnRow(1, "987", 0, null, null, Phone.TYPE_HOME, null,
-                        Phone.CONTENT_ITEM_TYPE);
+                        Phone.CONTENT_ITEM_TYPE, 1);
 
         TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                 mContext, ContactDisplayUtils.INTERACTION_SMS, null);
@@ -128,10 +128,10 @@
     public void testSendSmsWhenThereIsPrimaryNumber() {
         Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
         expectQuery(contactUri)
-                .returnRow(
-                        1, "123", 0, null, null, Phone.TYPE_HOME, null, Phone.CONTENT_ITEM_TYPE)
-                .returnRow(
-                        2, "456", 1, null, null, Phone.TYPE_HOME, null, Phone.CONTENT_ITEM_TYPE);
+                .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null,
+                        Phone.CONTENT_ITEM_TYPE, 13)
+                .returnRow(2, "456", 1, null, null, Phone.TYPE_HOME, null,
+                        Phone.CONTENT_ITEM_TYPE, 13);
 
         TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                 mContext, ContactDisplayUtils.INTERACTION_SMS, null);
@@ -170,9 +170,9 @@
         Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
         expectQuery(contactUri)
                 .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null,
-                        Phone.CONTENT_ITEM_TYPE)
+                        Phone.CONTENT_ITEM_TYPE, 13)
                 .returnRow(2, "123", 0, null, null, Phone.TYPE_WORK, null,
-                        Phone.CONTENT_ITEM_TYPE);
+                        Phone.CONTENT_ITEM_TYPE, 13);
 
         TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                 mContext, ContactDisplayUtils.INTERACTION_CALL, null);
@@ -191,7 +191,7 @@
         Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
         expectQuery(contactUri)
                 .returnRow(1, "example@example.com", 0, null, null, Phone.TYPE_HOME, null,
-                        SipAddress.CONTENT_ITEM_TYPE);
+                        SipAddress.CONTENT_ITEM_TYPE, 13);
         TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                 mContext, ContactDisplayUtils.INTERACTION_CALL, null);
 
@@ -209,9 +209,9 @@
         Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
         expectQuery(contactUri)
                 .returnRow(1, "123", 0, "account", null, Phone.TYPE_HOME, "label",
-                        Phone.CONTENT_ITEM_TYPE)
+                        Phone.CONTENT_ITEM_TYPE, 13)
                 .returnRow(2, "456", 0, null, null, Phone.TYPE_WORK, null,
-                        Phone.CONTENT_ITEM_TYPE);
+                        Phone.CONTENT_ITEM_TYPE, 13);
 
         TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                 mContext, ContactDisplayUtils.INTERACTION_CALL, null);
@@ -252,7 +252,8 @@
                         RawContacts.DATA_SET,
                         Phone.TYPE,
                         Phone.LABEL,
-                        Phone.MIMETYPE)
+                        Phone.MIMETYPE,
+                        Phone.CONTACT_ID)
                 .withSelection("mimetype IN ('vnd.android.cursor.item/phone_v2',"
                         + " 'vnd.android.cursor.item/sip_address') AND data1 NOT NULL");
     }