Use ListFormatter to join strings

Currently in Settings we are using R.string.join_many_items_first, R.string.join_many_items_middle and R.string.join_many_items_last to manually join strings. The join code is messy and the joined string is incorrect in some languages, so we migrate all string join to just use ListFormatter.getInstance().format().

Bug: b/78248791
Test: robotests
Change-Id: I898339978e6e2027587e28994b0280fa46821fd6
This commit is contained in:
jyhshiangwang
2018-04-24 16:05:57 +08:00
parent 7c45f59478
commit 4d015b17b3
12 changed files with 210 additions and 212 deletions

View File

@@ -15,9 +15,13 @@
*/
package com.android.settings.accounts;
import static com.android.settings.accounts.AccountDashboardFragmentTest.ShadowAuthenticationHelper.LABELS;
import static com.android.settings.accounts.AccountDashboardFragmentTest.ShadowAuthenticationHelper.TYPES;
import static com.android.settings.accounts.AccountDashboardFragmentTest
.ShadowAuthenticationHelper.LABELS;
import static com.android.settings.accounts.AccountDashboardFragmentTest
.ShadowAuthenticationHelper.TYPES;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -49,116 +53,119 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class AccountDashboardFragmentTest {
private AccountDashboardFragment mFragment;
private AccountDashboardFragment mFragment;
@Before
public void setUp() {
mFragment = new AccountDashboardFragment();
}
@After
public void tearDown() {
ShadowAuthenticationHelper.reset();
}
@Test
public void testCategory_isAccount() {
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT);
}
@Test
@Config(shadows = {
ShadowAuthenticationHelper.class
})
public void updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes() {
final SummaryLoader loader = mock(SummaryLoader.class);
final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
final SummaryLoader.SummaryProvider provider =
AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity, loader);
provider.setListening(true);
verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1] + ", " + LABELS[2]);
}
@Test
@Config(shadows = ShadowAuthenticationHelper.class)
public void updateSummary_noAccount_shouldDisplayDefaultSummary() {
ShadowAuthenticationHelper.setEnabledAccount(null);
final SummaryLoader loader = mock(SummaryLoader.class);
final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
final SummaryLoader.SummaryProvider provider =
AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity, loader);
provider.setListening(true);
verify(loader).setSummary(provider,
activity.getString(R.string.account_dashboard_default_summary));
}
@Test
@Config(shadows = ShadowAuthenticationHelper.class)
public void updateSummary_noAccountTypeLabel_shouldNotDisplayNullEntry() {
final SummaryLoader loader = mock(SummaryLoader.class);
final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
final String[] enabledAccounts = {TYPES[0], "unlabeled_account_type", TYPES[1]};
ShadowAuthenticationHelper.setEnabledAccount(enabledAccounts);
final SummaryLoader.SummaryProvider provider =
AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity, loader);
provider.setListening(true);
// should only show the 2 accounts with labels
verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1]);
}
@Test
public void testSearchIndexProvider_shouldIndexResource() {
final List<SearchIndexableResource> indexRes =
AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
assertThat(indexRes).isNotNull();
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
}
@Implements(AuthenticatorHelper.class)
public static class ShadowAuthenticationHelper {
static final String[] TYPES = {"type1", "type2", "type3", "type4"};
static final String[] LABELS = {"LABEL1", "LABEL2", "LABEL3", "LABEL4"};
private static String[] sEnabledAccount = TYPES;
public void __constructor__(Context context, UserHandle userHandle,
AuthenticatorHelper.OnAccountsUpdateListener listener) {
@Before
public void setUp() {
mFragment = new AccountDashboardFragment();
}
private static void setEnabledAccount(String[] enabledAccount) {
sEnabledAccount = enabledAccount;
@After
public void tearDown() {
ShadowAuthenticationHelper.reset();
}
@Resetter
public static void reset() {
sEnabledAccount = TYPES;
@Test
public void testCategory_isAccount() {
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT);
}
@Implementation
public String[] getEnabledAccountTypes() {
return sEnabledAccount;
@Test
@Config(shadows = {
ShadowAuthenticationHelper.class
})
public void updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes() {
final SummaryLoader loader = mock(SummaryLoader.class);
final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
final SummaryLoader.SummaryProvider provider =
AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity,
loader);
provider.setListening(true);
verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1] + ", and " + LABELS[2]);
}
@Implementation
public CharSequence getLabelForType(Context context, final String accountType) {
if (TextUtils.equals(accountType, TYPES[0])) {
return LABELS[0];
} else if (TextUtils.equals(accountType, TYPES[1])) {
return LABELS[1];
} else if (TextUtils.equals(accountType, TYPES[2])) {
return LABELS[2];
} else if (TextUtils.equals(accountType, TYPES[3])) {
return LABELS[3];
}
return null;
@Test
@Config(shadows = ShadowAuthenticationHelper.class)
public void updateSummary_noAccount_shouldDisplayDefaultSummary() {
ShadowAuthenticationHelper.setEnabledAccount(null);
final SummaryLoader loader = mock(SummaryLoader.class);
final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
final SummaryLoader.SummaryProvider provider =
AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity,
loader);
provider.setListening(true);
verify(loader).setSummary(provider,
activity.getString(R.string.account_dashboard_default_summary));
}
@Test
@Config(shadows = ShadowAuthenticationHelper.class)
public void updateSummary_noAccountTypeLabel_shouldNotDisplayNullEntry() {
final SummaryLoader loader = mock(SummaryLoader.class);
final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
final String[] enabledAccounts = {TYPES[0], "unlabeled_account_type", TYPES[1]};
ShadowAuthenticationHelper.setEnabledAccount(enabledAccounts);
final SummaryLoader.SummaryProvider provider =
AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity,
loader);
provider.setListening(true);
// should only show the 2 accounts with labels
verify(loader).setSummary(provider, LABELS[0] + " and " + LABELS[1]);
}
@Test
public void testSearchIndexProvider_shouldIndexResource() {
final List<SearchIndexableResource> indexRes =
AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
assertThat(indexRes).isNotNull();
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
}
@Implements(AuthenticatorHelper.class)
public static class ShadowAuthenticationHelper {
static final String[] TYPES = {"type1", "type2", "type3", "type4"};
static final String[] LABELS = {"LABEL1", "LABEL2", "LABEL3", "LABEL4"};
private static String[] sEnabledAccount = TYPES;
public void __constructor__(Context context, UserHandle userHandle,
AuthenticatorHelper.OnAccountsUpdateListener listener) {
}
private static void setEnabledAccount(String[] enabledAccount) {
sEnabledAccount = enabledAccount;
}
@Resetter
public static void reset() {
sEnabledAccount = TYPES;
}
@Implementation
public String[] getEnabledAccountTypes() {
return sEnabledAccount;
}
@Implementation
public CharSequence getLabelForType(Context context, final String accountType) {
if (TextUtils.equals(accountType, TYPES[0])) {
return LABELS[0];
} else if (TextUtils.equals(accountType, TYPES[1])) {
return LABELS[1];
} else if (TextUtils.equals(accountType, TYPES[2])) {
return LABELS[2];
} else if (TextUtils.equals(accountType, TYPES[3])) {
return LABELS[3];
}
return null;
}
}
}
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.applications;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
@@ -31,6 +32,7 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import androidx.preference.Preference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -95,21 +97,21 @@ public class AppPermissionsPreferenceControllerTest {
// create permission groups
when(mPackageManager.getPermissionGroupInfo(eq(PERM_LOCATION), anyInt()))
.thenReturn(mGroupLocation);
.thenReturn(mGroupLocation);
when(mGroupLocation.loadLabel(mPackageManager)).thenReturn(LABEL_LOCATION);
when(mPackageManager.getPermissionGroupInfo(eq(PERM_MICROPHONE), anyInt()))
.thenReturn(mGroupMic);
.thenReturn(mGroupMic);
when(mGroupMic.loadLabel(mPackageManager)).thenReturn(LABEL_MICROPHONE);
when(mPackageManager.getPermissionGroupInfo(eq(PERM_CAMERA), anyInt()))
.thenReturn(mGroupCamera);
.thenReturn(mGroupCamera);
when(mGroupCamera.loadLabel(mPackageManager)).thenReturn(LABEL_CAMERA);
when(mPackageManager.getPermissionGroupInfo(eq(PERM_SMS), anyInt())).thenReturn(mGroupSms);
when(mGroupSms.loadLabel(mPackageManager)).thenReturn(LABEL_SMS);
when(mPackageManager.getPermissionGroupInfo(eq(PERM_CONTACTS), anyInt()))
.thenReturn(mGroupContacts);
.thenReturn(mGroupContacts);
when(mGroupContacts.loadLabel(mPackageManager)).thenReturn(LABEL_CONTACTS);
when(mPackageManager.getPermissionGroupInfo(eq(PERM_PHONE), anyInt()))
.thenReturn(mGroupPhone);
.thenReturn(mGroupPhone);
when(mGroupPhone.loadLabel(mPackageManager)).thenReturn(LABEL_PHONE);
// create permissions
@@ -139,7 +141,7 @@ public class AppPermissionsPreferenceControllerTest {
permissions.add(mPermContacts);
permissions.add(mPermPhone);
when(mPackageManager.queryPermissionsByGroup(anyString(), anyInt()))
.thenReturn(permissions);
.thenReturn(permissions);
mController = spy(new AppPermissionsPreferenceController(mContext));
}
@@ -155,7 +157,7 @@ public class AppPermissionsPreferenceControllerTest {
final PackageInfo info = new PackageInfo();
installedPackages.add(info);
when(mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS))
.thenReturn(installedPackages);
.thenReturn(installedPackages);
mController.updateState(mPreference);
@@ -168,7 +170,7 @@ public class AppPermissionsPreferenceControllerTest {
final PackageInfo info = new PackageInfo();
installedPackages.add(info);
when(mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS))
.thenReturn(installedPackages);
.thenReturn(installedPackages);
PermissionInfo[] permissions = new PermissionInfo[4];
info.permissions = permissions;
@@ -177,33 +179,38 @@ public class AppPermissionsPreferenceControllerTest {
permissions[2] = mPermCamera;
permissions[3] = mPermSms;
mController.updateState(mPreference);
verify(mPreference).setSummary("Apps using location, microphone, camera");
verify(mPreference).setSummary("Apps using location, microphone, and camera");
permissions[0] = mPermPhone;
permissions[1] = mPermMic;
permissions[2] = mPermCamera;
permissions[3] = mPermSms;
mController.updateState(mPreference);
verify(mPreference).setSummary("Apps using microphone, camera, sms");
verify(mPreference).setSummary("Apps using microphone, camera, and sms");
permissions[0] = mPermPhone;
permissions[1] = mPermMic;
permissions[2] = mPermContacts;
permissions[3] = mPermSms;
mController.updateState(mPreference);
verify(mPreference).setSummary("Apps using microphone, sms, contacts");
verify(mPreference).setSummary("Apps using microphone, sms, and contacts");
permissions = new PermissionInfo[2];
info.permissions = permissions;
permissions[0] = mPermLocation;
permissions[1] = mPermCamera;
mController.updateState(mPreference);
verify(mPreference).setSummary("Apps using location, camera");
verify(mPreference).setSummary("Apps using location and camera");
permissions = new PermissionInfo[1];
info.permissions = permissions;
permissions[0] = mPermCamera;
mController.updateState(mPreference);
verify(mPreference).setSummary("Apps using camera");
}
}

View File

@@ -91,44 +91,51 @@ public class DefaultAppSettingsTest {
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1, Sms1");
verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1, and Sms1");
// 2 available
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1");
verify(summaryLoader).setSummary(summaryProvider, "Browser1 and Phone1");
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Phone1, Sms1");
verify(summaryLoader).setSummary(summaryProvider, "Phone1 and Sms1");
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Phone1, Sms1");
verify(summaryLoader).setSummary(summaryProvider, "Browser1 and Sms1");
// 1 available
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Browser1");
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Sms1");
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
summaryProvider.setListening(true);
verify(summaryLoader).setSummary(summaryProvider, "Phone1");
// None available
@@ -136,6 +143,7 @@ public class DefaultAppSettingsTest {
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
summaryProvider.setListening(true);
verify(summaryLoader, never()).setSummary(summaryProvider, eq(anyString()));
}

View File

@@ -17,8 +17,7 @@
package com.android.settings.inputmethod;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -28,11 +27,12 @@ import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.text.BidiFormatter;
import androidx.preference.Preference;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import androidx.core.text.BidiFormatter;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -125,18 +125,9 @@ public class VirtualKeyboardPreferenceControllerTest {
when(imis.get(0).loadLabel(mPm)).thenReturn(label1);
when(imis.get(1).getPackageName()).thenReturn(componentName.getPackageName());
when(imis.get(1).loadLabel(mPm)).thenReturn(label2);
when(mContext.getString(eq(R.string.join_many_items_middle), anyString(), anyString()))
.thenAnswer(invocation -> {
final Object[] args = invocation.getArguments();
final String str1 = (String) args[1];
final String str2 = (String) args[2];
return RuntimeEnvironment.application.getString(R.string.join_many_items_middle,
str1, str2);
});
mController.updateState(mPreference);
verify(mPreference).setSummary(
formatter.unicodeWrap(label1) + ", " + formatter.unicodeWrap(label2));
verify(mPreference).setSummary(formatter.unicodeWrap(label1) + " and " + formatter.unicodeWrap(label2));
}
}