AppNameHelper { public static String getAppName(Context context) { ApplicationInfo applicationInfo = context.getApplicationInfo(); int labelRes = applicationInfo.labelRes; if (labelRes == 0) { return String.valueOf(applicationInfo.nonLocalizedLabel); } else { return context.getString(labelRes); } }  static String getAppName(Context context);  }
@Test public void localizedAppName() { String appName = "localized-app-name"; int resId = 42; Context context = mock(Context.class); ApplicationInfo applicationInfo = mock(ApplicationInfo.class); when(context.getApplicationInfo()).thenReturn(applicationInfo); Whitebox.setInternalState(applicationInfo, "labelRes", resId); when(context.getString(resId)).thenReturn(appName); String retrievedAppName = AppNameHelper.getAppName(context); assertEquals(appName, retrievedAppName); }
@Test public void nonLocalizedAppName() { String appName = "non-localized-app-name"; Context context = mock(Context.class); ApplicationInfo applicationInfo = mock(ApplicationInfo.class); when(context.getApplicationInfo()).thenReturn(applicationInfo); Whitebox.setInternalState(applicationInfo, "labelRes", 0); Whitebox.setInternalState(applicationInfo, "nonLocalizedLabel", appName); String retrievedAppName = AppNameHelper.getAppName(context); assertEquals(appName, retrievedAppName); }