Show proper label for phone only accounts

I broke the account label for phone only contacts
when I extracted the logic to set the account label
on the full editor to a utility method so that it
could also be used on the compact editor in ag/712716

Bug 22859536

Change-Id: I3e64f7f6834a1da83078cc783e04b3c2faa0702f
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 6b7df67..5bd474f 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -357,15 +357,9 @@
         // Get the account information for the default account RawContactDelta
         final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(),
                 isUserProfile, defaultAccountRawContactDelta.getAccountName(), accountType);
-
-        // Set the account information already
-        if (accountInfo == null) {
-            vlog("Account info hidden because no account info could be composed");
-            mAccountContainer.setVisibility(View.GONE);
-            return;
-        }
         vlog("Account info loaded");
         if (accountInfo.first == null) {
+             // Hide this view so the other text view will be centered vertically
             mAccountNameView.setVisibility(View.GONE);
         } else {
             mAccountNameView.setVisibility(View.VISIBLE);
diff --git a/src/com/android/contacts/editor/EditorUiUtils.java b/src/com/android/contacts/editor/EditorUiUtils.java
index 78f7a42..56be3c0 100644
--- a/src/com/android/contacts/editor/EditorUiUtils.java
+++ b/src/com/android/contacts/editor/EditorUiUtils.java
@@ -83,13 +83,13 @@
     }
 
     /**
-     * Returns a Pair of the account name and type to display for the given arguments or null
-     * in no account information should be displayed. The account name may also be null.
+     * Returns the account name and account type labels to display for the given account type.
+     *
+     * @param isProfile True to get the labels for the "me" profile or a phone only (local) contact
+     *         and false otherwise.
      */
-    public static Pair<String,String> getAccountInfo(Context context, boolean isProfile,
-            String accountName, AccountType accountType) {
-        CharSequence accountTypeDisplayLabel = accountType.getDisplayLabel(context);
-
+    public static Pair<String,String> getAccountInfo(Context context,
+            boolean isProfile, String accountName, AccountType accountType) {
         if (isProfile) {
             if (TextUtils.isEmpty(accountName)) {
                 return new Pair<>(
@@ -98,28 +98,33 @@
             }
             return new Pair<>(
                     accountName,
-                    context.getString(R.string.external_profile_title, accountTypeDisplayLabel));
+                    context.getString(R.string.external_profile_title,
+                            accountType.getDisplayLabel(context)));
         }
-        if (!TextUtils.isEmpty(accountName)) {
-            final String accountNameDisplayLabel =
-                    context.getString(R.string.from_account_format, accountName);
 
-            if (TextUtils.isEmpty(accountTypeDisplayLabel)) {
-                accountTypeDisplayLabel = context.getString(R.string.account_phone);
-            }
+        CharSequence accountTypeDisplayLabel = accountType.getDisplayLabel(context);
+        if (TextUtils.isEmpty(accountTypeDisplayLabel)) {
+            accountTypeDisplayLabel = context.getString(R.string.account_phone);
+        }
 
-            if (GoogleAccountType.ACCOUNT_TYPE.equals(accountType.accountType)
-                    && accountType.dataSet == null) {
-                return new Pair<>(
-                        accountNameDisplayLabel,
-                        context.getString(R.string.google_account_type_format,
-                                accountTypeDisplayLabel));
-            }
+        if (TextUtils.isEmpty(accountName)) {
             return new Pair<>(
-                    accountNameDisplayLabel,
+                    /* accountName =*/ null,
                     context.getString(R.string.account_type_format, accountTypeDisplayLabel));
         }
-        return null;
+
+        final String accountNameDisplayLabel =
+                context.getString(R.string.from_account_format, accountName);
+
+        if (GoogleAccountType.ACCOUNT_TYPE.equals(accountType.accountType)
+                && accountType.dataSet == null) {
+            return new Pair<>(
+                    accountNameDisplayLabel,
+                    context.getString(R.string.google_account_type_format, accountTypeDisplayLabel));
+        }
+        return new Pair<>(
+                accountNameDisplayLabel,
+                context.getString(R.string.account_type_format, accountTypeDisplayLabel));
     }
 
     /**
diff --git a/src/com/android/contacts/editor/RawContactEditorView.java b/src/com/android/contacts/editor/RawContactEditorView.java
index eeba401..f021ec7 100644
--- a/src/com/android/contacts/editor/RawContactEditorView.java
+++ b/src/com/android/contacts/editor/RawContactEditorView.java
@@ -26,7 +26,6 @@
 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
-import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.Pair;
 import android.view.LayoutInflater;
@@ -192,18 +191,14 @@
         // Fill in the account info
         final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(),
                 isProfile, state.getAccountName(), type);
-        if (accountInfo == null) {
+        if (accountInfo.first == null) {
             // Hide this view so the other text view will be centered vertically
             mAccountHeaderNameTextView.setVisibility(View.GONE);
         } else {
-            if (accountInfo.first == null) {
-                mAccountHeaderNameTextView.setVisibility(View.GONE);
-            } else {
-                mAccountHeaderNameTextView.setVisibility(View.VISIBLE);
-                mAccountHeaderNameTextView.setText(accountInfo.first);
-            }
-            mAccountHeaderTypeTextView.setText(accountInfo.second);
+            mAccountHeaderNameTextView.setVisibility(View.VISIBLE);
+            mAccountHeaderNameTextView.setText(accountInfo.first);
         }
+        mAccountHeaderTypeTextView.setText(accountInfo.second);
         updateAccountHeaderContentDescription();
 
         // The account selector and header are both used to display the same information.
diff --git a/src/com/android/contacts/editor/RawContactReadOnlyEditorView.java b/src/com/android/contacts/editor/RawContactReadOnlyEditorView.java
index d7b3dd0..fd99ddc 100644
--- a/src/com/android/contacts/editor/RawContactReadOnlyEditorView.java
+++ b/src/com/android/contacts/editor/RawContactReadOnlyEditorView.java
@@ -117,18 +117,14 @@
 
         final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(),
                 isProfile, state.getAccountName(), type);
-        if (accountInfo == null) {
+        if (accountInfo.first == null) {
             // Hide this view so the other text view will be centered vertically
             mAccountHeaderNameTextView.setVisibility(View.GONE);
         } else {
-            if (accountInfo.first == null) {
-                mAccountHeaderNameTextView.setVisibility(View.GONE);
-            } else {
-                mAccountHeaderNameTextView.setVisibility(View.VISIBLE);
-                mAccountHeaderNameTextView.setText(accountInfo.first);
-            }
-            mAccountHeaderTypeTextView.setText(accountInfo.second);
+            mAccountHeaderNameTextView.setVisibility(View.VISIBLE);
+            mAccountHeaderNameTextView.setText(accountInfo.first);
         }
+        mAccountHeaderTypeTextView.setText(accountInfo.second);
         updateAccountHeaderContentDescription();
 
         // TODO: Expose data set in the UI somehow?
diff --git a/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java b/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java
new file mode 100644
index 0000000..58e6f26
--- /dev/null
+++ b/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2015 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.contacts.editor;
+
+import com.android.contacts.R;
+import com.android.contacts.common.model.account.AccountType;
+import com.android.contacts.common.model.account.GoogleAccountType;
+
+import android.content.Context;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Pair;
+
+/**
+ * Tests {@link EditorUiUtils}.
+ */
+@SmallTest
+public class EditorUiUtilsTest extends AndroidTestCase {
+
+    private static final String ACCOUNT_NAME = "somebody@lunkedin.com";
+    private static final String DISPLAY_LABEL = "LunkedIn";
+
+    private static final String GOOGLE_ACCOUNT_NAME = "somebody@gmail.com";
+    private static final String GOOGLE_DISPLAY_LABEL = "Google";
+
+    private static final class MockAccountType extends AccountType {
+
+        private final String mDisplayLabel;
+
+        private MockAccountType(String displayLabel) {
+            mDisplayLabel = displayLabel;
+        }
+
+        @Override
+        public boolean areContactsWritable() {
+            return false;
+        }
+
+        @Override
+        public boolean isGroupMembershipEditable() {
+            return false;
+        }
+
+        @Override
+        public CharSequence getDisplayLabel(Context context) {
+            return mDisplayLabel;
+        }
+    }
+
+    public void testGetProfileAccountInfo_AccountName() {
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ true,
+                ACCOUNT_NAME, new MockAccountType(DISPLAY_LABEL));
+
+        assertNotNull(pair);
+        assertEquals(ACCOUNT_NAME, pair.first);
+        assertEquals(getContext().getString(R.string.external_profile_title, DISPLAY_LABEL),
+                pair.second); // My LunkedIn profile
+    }
+
+    public void testGetProfileAccountInfo_NoAccountName() {
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ true,
+                /* accountName =*/ null, new MockAccountType(DISPLAY_LABEL));
+
+        assertNotNull(pair);
+        assertNull(pair.first);
+        assertEquals(getContext().getString(R.string.local_profile_title),
+                pair.second); // "My local profile
+    }
+
+    public void testGetAccountInfo_AccountName_DisplayLabel() {
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ false,
+                ACCOUNT_NAME, new MockAccountType(DISPLAY_LABEL));
+
+        assertNotNull(pair);
+        assertEquals(getContext().getString(R.string.from_account_format, ACCOUNT_NAME),
+                pair.first); // somebody@lunkedin.com
+        assertEquals(getContext().getString(R.string.account_type_format, DISPLAY_LABEL),
+                pair.second); // LunkedIn Contact
+    }
+
+    public void testGetAccountInfo_AccountName_DisplayLabel_GoogleAccountType() {
+        final AccountType accountType = new MockAccountType(GOOGLE_DISPLAY_LABEL);
+        accountType.accountType = GoogleAccountType.ACCOUNT_TYPE;
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ false,
+                GOOGLE_ACCOUNT_NAME, accountType);
+
+        assertNotNull(pair);
+        assertEquals(getContext().getString(R.string.from_account_format, GOOGLE_ACCOUNT_NAME),
+                pair.first); // somebody@gmail.com
+        assertEquals(
+                getContext().getString(R.string.google_account_type_format, GOOGLE_DISPLAY_LABEL),
+                pair.second); // Google Account
+    }
+
+    public void testGetAccountInfo_AccountName_NoDisplayLabel() {
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ false,
+                ACCOUNT_NAME, new MockAccountType(/* displayLabel =*/ null));
+
+        assertNotNull(pair);
+        assertEquals(getContext().getString(R.string.from_account_format, ACCOUNT_NAME),
+                pair.first); // somebody@lunkedin.com
+        assertEquals(
+                getContext().getString(R.string.account_type_format,
+                        getContext().getString(R.string.account_phone)),
+                pair.second); // "Phone-only, unsynced contact"
+    }
+
+    public void testGetAccountInfo_NoAccountName_DisplayLabel() {
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ false,
+                /* accountName =*/ null, new MockAccountType(DISPLAY_LABEL));
+
+        assertNotNull(pair);
+        assertNull(pair.first);
+        assertEquals(getContext().getString(R.string.account_type_format, DISPLAY_LABEL),
+                pair.second); // LunkedIn contact
+    }
+
+    public void testGetAccountInfo_NoAccountName_NoDisplayLabel() {
+        final Pair pair = EditorUiUtils.getAccountInfo(getContext(), /* isProfile =*/ false,
+                /* accountName =*/ null, new MockAccountType(/* displayLabel =*/ null));
+
+        assertNotNull(pair);
+        assertNull(pair.first);
+        assertEquals(
+                getContext().getString(R.string.account_type_format,
+                        getContext().getString(R.string.account_phone)),
+                pair.second); // "Phone-only, unsynced contact"
+    }
+}