Refactor DashboardFragment.

Merged refreshAllPreferences into DashboardFragment. This hopefully
makes it more modular to manage preference display logic in each
dashboardFragment, and makes it more efficient to monitor category
changes.

Now subclasses needs to implement 2 methods:
- displayResourceTiles(): for 'static' preferences from xml
- getDashboardTiles(): returns a list of dashboard tiles and superclass
  will wire it up to preference screen.

If getDashboardTiles() return null (aka no dashboardCategory available),
the fragment will not attempt to monitor category change. The edge case
is that if a package starts to provide a tile for this category, we will
not be notified. I have not seen this case coming up. If we indeed need
to handle this case, the category listener needs to have a way to
monitor specific category rather than globally.

Bug: 31781480
Test: make RunSettingsRoboTests -j40
Change-Id: Ia9f9541b95816214df0d0bb27e3e41078c36c5ca
This commit is contained in:
Fan Zhang
2016-10-07 08:38:48 -07:00
parent cb054fff54
commit 36924659f5
5 changed files with 117 additions and 61 deletions

View File

@@ -16,6 +16,7 @@
package com.android.settings.dashboard;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
@@ -41,6 +42,7 @@ import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -55,8 +57,6 @@ public class DashboardFragmentTest {
private DashboardCategory mDashboardCategory;
@Mock
private FakeFeatureFactory mFakeFeatureFactory;
@Mock
private PreferenceScreen mScreen;
private TestFragment mTestFragment;
@Before
@@ -68,6 +68,7 @@ public class DashboardFragmentTest {
mDashboardCategory.tiles.add(new Tile());
mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
mTestFragment.onAttach(mContext);
mTestFragment.mCategory = mDashboardCategory;
}
@Test
@@ -85,24 +86,24 @@ public class DashboardFragmentTest {
public void displayTilesAsPreference_shouldAddTilesWithIntent() {
when(mFakeFeatureFactory.dashboardFeatureProvider.getDashboardKeyForTile(any(Tile.class)))
.thenReturn("test_key");
mTestFragment.displayTilesAsPreference("TEST_FRAGMENT", mScreen, mDashboardCategory);
mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
verify(mScreen).addPreference(any(DashboardTilePreference.class));
verify(mTestFragment.mScreen).addPreference(any(DashboardTilePreference.class));
}
@Test
public void displayTilesAsPreference_shouldNotAddTilesWithoutIntent() {
mTestFragment.displayTilesAsPreference("TEST_FRAGMENT", mScreen, mDashboardCategory);
mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
verify(mScreen, never()).addPreference(any(DashboardTilePreference.class));
verify(mTestFragment.mScreen, never()).addPreference(any(DashboardTilePreference.class));
}
@Test
public void displayTilesAsPreference_withEmptyCategory_shouldNotAddTiles() {
mDashboardCategory.tiles = null;
mTestFragment.displayTilesAsPreference("TEST_FRAGMENT", mScreen, mDashboardCategory);
mTestFragment.mCategory.tiles = null;
mTestFragment.onCreatePreferences(new Bundle(), "rootKey");
verify(mScreen, never()).addPreference(any(DashboardTilePreference.class));
verify(mTestFragment.mScreen, never()).addPreference(any(DashboardTilePreference.class));
}
public static class TestPreferenceController extends PreferenceController {
@@ -130,9 +131,14 @@ public class DashboardFragmentTest {
public static class TestFragment extends DashboardFragment {
private final Context mContext;
@Mock
public PreferenceScreen mScreen;
public DashboardCategory mCategory;
public TestFragment(Context context) {
mContext = context;
mScreen = mock(PreferenceScreen.class);
}
@Override
@@ -146,8 +152,22 @@ public class DashboardFragmentTest {
}
@Override
public void onCategoriesChanged() {
protected DashboardCategory getDashboardTiles() {
return mCategory;
}
@Override
protected void displayResourceTiles() {
}
@Override
public PreferenceScreen getPreferenceScreen() {
return mScreen;
}
@Override
protected String getLogTag() {
return "TEST_FRAG";
}
}