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:
@@ -18,7 +18,6 @@ package com.android.settings.search;
|
||||
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
@@ -26,15 +25,15 @@ import android.content.Context;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
@@ -50,13 +49,13 @@ public class BaseSearchIndexProviderTest {
|
||||
|
||||
private static final String TEST_PREF_KEY = "test_pref_key";
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
private BaseSearchIndexProvider mIndexProvider;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mIndexProvider = spy(BaseSearchIndexProvider.class);
|
||||
}
|
||||
|
||||
@@ -91,6 +90,38 @@ public class BaseSearchIndexProviderTest {
|
||||
assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void getAllPreferenceControllers_shouldCreateControllerFromCodeAndXml() {
|
||||
|
||||
final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
|
||||
boolean enabled) {
|
||||
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||
sir.xmlResId = R.xml.location_settings;
|
||||
return Arrays.asList(sir);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||
final List<AbstractPreferenceController> controllersFromCode = new ArrayList<>();
|
||||
controllersFromCode.add(new BasePreferenceController(mContext, "TEST_KEY") {
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
});
|
||||
return controllersFromCode;
|
||||
}
|
||||
};
|
||||
|
||||
final List<AbstractPreferenceController> controllers =
|
||||
provider.getAllPreferenceControllers(mContext);
|
||||
|
||||
assertThat(controllers).hasSize(3);
|
||||
}
|
||||
|
||||
public static class NotAvailablePreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
public NotAvailablePreferenceController(Context context) {
|
||||
|
||||
@@ -21,7 +21,6 @@ import android.content.Context;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -48,12 +47,6 @@ public class FakeIndexProvider implements Indexable {
|
||||
result.add(KEY);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractPreferenceController> getPreferenceControllers(
|
||||
Context context) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.XmlResourceParser;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Xml;
|
||||
|
||||
@@ -32,9 +33,13 @@ import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* These tests use a series of preferences that have specific attributes which are sometimes
|
||||
@@ -45,13 +50,13 @@ import org.xmlpull.v1.XmlPullParser;
|
||||
*/
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class XmlParserUtilTest {
|
||||
public class PreferenceXmlParserUtilTest {
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ShadowApplication.getInstance().getApplicationContext();
|
||||
mContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,7 +96,6 @@ public class XmlParserUtilTest {
|
||||
String summary = PreferenceXmlParserUtils.getDataSummary(mContext, attrs);
|
||||
String expSummary = mContext.getString(R.string.summary_placeholder);
|
||||
assertThat(summary).isEqualTo(expSummary);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,6 +167,20 @@ public class XmlParserUtilTest {
|
||||
assertThat(entries).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_shouldContainKeyAndControllerName()
|
||||
throws IOException, XmlPullParserException {
|
||||
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings);
|
||||
|
||||
assertThat(metadata).isNotEmpty();
|
||||
for (Bundle bundle : metadata) {
|
||||
assertThat(bundle.getString(PreferenceXmlParserUtils.METADATA_KEY)).isNotNull();
|
||||
assertThat(bundle.getString(PreferenceXmlParserUtils.METADATA_CONTROLLER)).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resId the ID for the XML preference
|
||||
* @return an XML resource parser that points to the start tag
|
||||
@@ -18,9 +18,8 @@ package com.android.settings.search;
|
||||
|
||||
import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
@@ -36,6 +35,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@@ -67,7 +67,7 @@ public class SearchIndexableResourcesTest {
|
||||
final int beforeCount =
|
||||
mSearchProvider.getSearchIndexableResources().getProviderValues().size();
|
||||
|
||||
( (SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
|
||||
((SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
|
||||
.addIndex(java.lang.String.class);
|
||||
|
||||
assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
|
||||
@@ -86,11 +86,13 @@ public class SearchIndexableResourcesTest {
|
||||
@Test
|
||||
public void testNonIndexableKeys_GetsKeyFromProvider() {
|
||||
mSearchProvider.getSearchIndexableResources().getProviderValues().clear();
|
||||
( (SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
|
||||
((SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
|
||||
.addIndex(FakeIndexProvider.class);
|
||||
|
||||
SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
|
||||
|
||||
doReturn(RuntimeEnvironment.application).when(provider).getContext();
|
||||
|
||||
Cursor cursor = provider.queryNonIndexableKeys(null);
|
||||
boolean hasTestKey = false;
|
||||
while (cursor.moveToNext()) {
|
||||
@@ -106,8 +108,8 @@ public class SearchIndexableResourcesTest {
|
||||
|
||||
@Test
|
||||
public void testAllClassNamesHaveProviders() {
|
||||
for (Class clazz: mSearchProvider.getSearchIndexableResources().getProviderValues()) {
|
||||
if(DatabaseIndexingUtils.getSearchIndexProvider(clazz) == null) {
|
||||
for (Class clazz : mSearchProvider.getSearchIndexableResources().getProviderValues()) {
|
||||
if (DatabaseIndexingUtils.getSearchIndexProvider(clazz) == null) {
|
||||
fail(clazz.getName() + "is not an index provider");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +104,7 @@ public class SettingsSearchIndexablesProviderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void testNonIndexablesColumnFetched() {
|
||||
Uri rawUri = Uri.parse("content://" + BASE_AUTHORITY + "/" +
|
||||
SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH);
|
||||
|
||||
Reference in New Issue
Block a user