Instantiate pref controllers from xml if it's defined.

- If a <preference> tag also defines a controller, we will try to
  instantiate it before displaying the UI. The same logic is shared by
  BaseSearchIndexProvider so it also drives search suppression.

- If user also defines a list of controllers programatically, the
  programatically created ones takes precedence.

Bug: 73668763
Test: WIP
Change-Id: I7aecec270bcd3af261e012ef1f6995d2a523cfa1
This commit is contained in:
Fan Zhang
2018-02-21 15:22:25 -08:00
parent fc520ee38a
commit 917f101899
28 changed files with 496 additions and 267 deletions

View File

@@ -19,6 +19,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -44,11 +46,10 @@ import com.android.settingslib.drawer.TileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
@@ -58,24 +59,25 @@ import java.util.List;
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DashboardFragmentTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private FakeFeatureFactory mFakeFeatureFactory;
private DashboardCategory mDashboardCategory;
private Context mContext;
private TestFragment mTestFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mDashboardCategory = new DashboardCategory();
mDashboardCategory.addTile(new Tile());
mTestFragment = new TestFragment(ShadowApplication.getInstance().getApplicationContext());
mTestFragment = new TestFragment(mContext);
when(mFakeFeatureFactory.dashboardFeatureProvider
.getTilesForCategory(nullable(String.class)))
.thenReturn(mDashboardCategory);
mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
mTestFragment.onAttach(mContext);
when(mContext.getPackageName()).thenReturn("TestPackage");
}
@@ -146,16 +148,18 @@ public class DashboardFragmentTest {
mock(AbstractPreferenceController.class);
final AbstractPreferenceController mockController2 =
mock(AbstractPreferenceController.class);
when(mockController1.getPreferenceKey()).thenReturn("key1");
when(mockController2.getPreferenceKey()).thenReturn("key2");
preferenceControllers.add(mockController1);
preferenceControllers.add(mockController2);
when(mockController1.isAvailable()).thenReturn(false);
when(mockController2.isAvailable()).thenReturn(true);
mTestFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
mTestFragment.onAttach(mContext);
mTestFragment.onResume();
verify(mockController1, never()).getPreferenceKey();
verify(mockController2).getPreferenceKey();
verify(mockController1).getPreferenceKey();
verify(mockController2, times(2)).getPreferenceKey();
}
@Test
@@ -205,9 +209,7 @@ public class DashboardFragmentTest {
final Intent intent = new Intent();
tile.intent = intent;
intent.setComponent(new ComponentName(
ShadowApplication.getInstance().getApplicationContext().getPackageName(),
"TestClass"));
intent.setComponent(new ComponentName(mContext.getPackageName(), "TestClass"));
assertThat(mTestFragment.tintTileIcon(tile)).isFalse();
intent.setComponent(new ComponentName("OtherPackage", "TestClass"));