Fix null pointer in dashboard fragment test.

- use context instead of activity to retrieve resource details.
- revert the change previously made in getActivity() calls.
- add null checking in package name and tile intent.

Fix: 34396855
Test: make RunSettingsRoboTests

Change-Id: Ic853939fee3c381b663c0320354da51d3b2a0e11
This commit is contained in:
Doris Ling
2017-01-18 17:03:26 -08:00
parent 1d0e6c8cee
commit fd83b2eb52
2 changed files with 10 additions and 8 deletions

View File

@@ -299,15 +299,15 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
if (mSummaryLoader != null) { if (mSummaryLoader != null) {
mSummaryLoader.release(); mSummaryLoader.release();
} }
final Activity activity = getActivity(); final Context context = getContext();
mSummaryLoader = new SummaryLoader(activity, getCategoryKey()); mSummaryLoader = new SummaryLoader(getActivity(), getCategoryKey());
mSummaryLoader.setSummaryConsumer(this); mSummaryLoader.setSummaryConsumer(this);
final TypedArray a = activity.obtainStyledAttributes(new int[] { final TypedArray a = context.obtainStyledAttributes(new int[] {
mDashboardFeatureProvider.isEnabled() ? android.R.attr.colorControlNormal mDashboardFeatureProvider.isEnabled() ? android.R.attr.colorControlNormal
: android.R.attr.colorAccent}); : android.R.attr.colorAccent});
final int tintColor = a.getColor(0, activity.getColor(android.R.color.white)); final int tintColor = a.getColor(0, context.getColor(android.R.color.white));
a.recycle(); a.recycle();
final String pkgName = activity.getPackageName(); final String pkgName = context.getPackageName();
// Install dashboard tiles. // Install dashboard tiles.
for (Tile tile : tiles) { for (Tile tile : tiles) {
final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile); final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile);
@@ -318,7 +318,8 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
if (!displayTile(tile)) { if (!displayTile(tile)) {
continue; continue;
} }
if (!pkgName.equals(tile.intent.getComponent().getPackageName())) { if (pkgName != null && tile.intent != null
&& !pkgName.equals(tile.intent.getComponent().getPackageName())) {
// If this drawable is coming from outside Settings, tint it to match the color. // If this drawable is coming from outside Settings, tint it to match the color.
tile.icon.setTint(tintColor); tile.icon.setTint(tintColor);
} }
@@ -326,12 +327,12 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
// Have the key already, will rebind. // Have the key already, will rebind.
final Preference preference = mProgressiveDisclosureMixin.findPreference( final Preference preference = mProgressiveDisclosureMixin.findPreference(
screen, key); screen, key);
mDashboardFeatureProvider.bindPreferenceToTile(activity, preference, tile, key, mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), preference, tile, key,
mPlaceholderPreferenceController.getOrder()); mPlaceholderPreferenceController.getOrder());
} else { } else {
// Don't have this key, add it. // Don't have this key, add it.
final Preference pref = new Preference(getPrefContext()); final Preference pref = new Preference(getPrefContext());
mDashboardFeatureProvider.bindPreferenceToTile(activity, pref, tile, key, mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), pref, tile, key,
mPlaceholderPreferenceController.getOrder()); mPlaceholderPreferenceController.getOrder());
mProgressiveDisclosureMixin.addPreference(screen, pref); mProgressiveDisclosureMixin.addPreference(screen, pref);
mDashboardTilePrefKeys.add(key); mDashboardTilePrefKeys.add(key);

View File

@@ -79,6 +79,7 @@ public class DashboardFragmentTest {
when(mFakeFeatureFactory.dashboardFeatureProvider.getTilesForCategory(anyString())) when(mFakeFeatureFactory.dashboardFeatureProvider.getTilesForCategory(anyString()))
.thenReturn(mDashboardCategory); .thenReturn(mDashboardCategory);
mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext()); mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
when(mContext.getPackageName()).thenReturn("TestPackage");
} }
@Test @Test