Merge "Add search providers for Connected Devices in new IA"

This commit is contained in:
TreeHugger Robot
2016-12-02 02:27:34 +00:00
committed by Android (Google) Code Review
11 changed files with 152 additions and 77 deletions

View File

@@ -0,0 +1,3 @@
com.android.settings.display.ScreenZoomPreferenceFragmentForSetupWizard
com.android.settings.wifi.WifiSettingsForSetupWizard
com.android.settings.print.PrintServiceSettingsFragment

View File

@@ -16,10 +16,13 @@
package com.android.settings.connecteddevice;
import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.drawer.CategoryKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -27,28 +30,45 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ConnectedDeviceDashboardFragmentTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private ConnectedDeviceDashboardFragment mFragment;
private ConnectedDeviceDashboardFragment mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFragment = new ConnectedDeviceDashboardFragment();
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
final FakeFeatureFactory factory =
(FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
when(factory.dashboardFeatureProvider.isEnabled()).thenReturn(true);
mFragment = new ConnectedDeviceDashboardFragment();
}
@Test
public void testCategory_isConnectedDevice() {
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
}
@Test
public void testCategory_isConnectedDevice() {
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
}
@Test
public void testSearchIndexProvider_shouldIndexResource() {
final List<SearchIndexableResource> indexRes =
ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
ShadowApplication.getInstance().getApplicationContext(),
true /* enabled */);
assertThat(indexRes).isNotNull();
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
}
}

View File

@@ -32,12 +32,12 @@ import static com.google.common.truth.Truth.assertWithMessage;
*/
public class InstrumentableFragmentCodeInspector extends CodeInspector {
private final List<String> grandfather_notImplmentingInstrumentable;
private final List<String> grandfather_notImplementingInstrumentable;
public InstrumentableFragmentCodeInspector(List<Class<?>> classes) {
super(classes);
grandfather_notImplmentingInstrumentable = new ArrayList<>();
initializeGrandfatherList(grandfather_notImplmentingInstrumentable,
grandfather_notImplementingInstrumentable = new ArrayList<>();
initializeGrandfatherList(grandfather_notImplementingInstrumentable,
"grandfather_not_implementing_instrumentable");
}
@@ -53,7 +53,7 @@ public class InstrumentableFragmentCodeInspector extends CodeInspector {
// If it's a fragment, it must also be instrumentable.
if (Fragment.class.isAssignableFrom(clazz)
&& !Instrumentable.class.isAssignableFrom(clazz)
&& !grandfather_notImplmentingInstrumentable.contains(className)) {
&& !grandfather_notImplementingInstrumentable.contains(className)) {
broken.add(className);
}
}

View File

@@ -17,16 +17,18 @@ package com.android.settings.core.instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.testutils.FakeFeatureFactory;
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.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
@@ -41,23 +43,21 @@ public class SharedPreferenceLoggerTest {
private static final String TEST_TAG = "tag";
private static final String TEST_KEY = "key";
@Mock
private LogWriter mLogWriter;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFactory;
private MetricsFeatureProvider mMetricsFeature;
private ShadowApplication mApplication;
private SharedPreferencesLogger mSharedPrefLogger;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mApplication = ShadowApplication.getInstance();
Context context = mApplication.getApplicationContext();
mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
((MetricsFeatureProviderImpl) mMetricsFeature).addLogWriter(mLogWriter);
FakeFeatureFactory.setupForTest(mContext);
mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mMetricsFeature = mFactory.metricsFeatureProvider;
mSharedPrefLogger = new SharedPreferencesLogger(
mApplication.getApplicationContext(), TEST_TAG);
mSharedPrefLogger = new SharedPreferencesLogger(mContext, TEST_TAG);
}
@Test
@@ -71,7 +71,7 @@ public class SharedPreferenceLoggerTest {
editor.putInt(TEST_KEY, 2);
editor.putInt(TEST_KEY, 2);
verify(mLogWriter, times(6)).count(any(Context.class), anyString(), anyInt());
verify(mMetricsFeature, times(6)).count(any(Context.class), anyString(), anyInt());
}
@Test
@@ -83,7 +83,7 @@ public class SharedPreferenceLoggerTest {
editor.putBoolean(TEST_KEY, false);
editor.putBoolean(TEST_KEY, false);
verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt());
verify(mMetricsFeature, times(4)).count(any(Context.class), anyString(), anyInt());
}
@Test
@@ -95,7 +95,7 @@ public class SharedPreferenceLoggerTest {
editor.putLong(TEST_KEY, 1);
editor.putLong(TEST_KEY, 2);
verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt());
verify(mMetricsFeature, times(4)).count(any(Context.class), anyString(), anyInt());
}
@Test
@@ -107,7 +107,7 @@ public class SharedPreferenceLoggerTest {
editor.putFloat(TEST_KEY, 1);
editor.putFloat(TEST_KEY, 2);
verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt());
verify(mMetricsFeature, times(4)).count(any(Context.class), anyString(), anyInt());
}
}

View File

@@ -35,23 +35,37 @@ import static com.google.common.truth.Truth.assertWithMessage;
public class SearchIndexProviderCodeInspector extends CodeInspector {
private static final String TAG = "SearchCodeInspector";
private final List<String> notImplementingIndexableWhitelist;
private final List<String> notImplementingIndexProviderWhitelist;
private static final String NOT_IMPLEMENTING_INDEXABLE_ERROR =
"SettingsPreferenceFragment should implement Indexable, but these are not:\n";
private static final String NOT_CONTAINING_PROVIDER_OBJECT_ERROR =
"Indexable should have public field " + Index.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER
+ " but these are not:\n";
private static final String NOT_IN_INDEXABLE_PROVIDER_REGISTRY =
"Class containing " + Index.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + " must be added to "
+ SearchIndexableResources.class.getName() + " but these are not: \n";
private final List<String> notImplementingIndexableGrandfatherList;
private final List<String> notImplementingIndexProviderGrandfatherList;
private final List<String> notInSearchIndexableRegistryGrandfatherList;
public SearchIndexProviderCodeInspector(List<Class<?>> classes) {
super(classes);
notImplementingIndexableWhitelist = new ArrayList<>();
notImplementingIndexProviderWhitelist = new ArrayList<>();
initializeGrandfatherList(notImplementingIndexableWhitelist,
notImplementingIndexableGrandfatherList = new ArrayList<>();
notImplementingIndexProviderGrandfatherList = new ArrayList<>();
notInSearchIndexableRegistryGrandfatherList = new ArrayList<>();
initializeGrandfatherList(notImplementingIndexableGrandfatherList,
"grandfather_not_implementing_indexable");
initializeGrandfatherList(notImplementingIndexProviderWhitelist,
initializeGrandfatherList(notImplementingIndexProviderGrandfatherList,
"grandfather_not_implementing_index_provider");
initializeGrandfatherList(notInSearchIndexableRegistryGrandfatherList,
"grandfather_not_in_search_index_provider_registry");
}
@Override
public void run() {
final Set<String> notImplementingIndexable = new ArraySet<>();
final Set<String> notImplementingIndexProvider = new ArraySet<>();
final Set<String> notInSearchProviderRegistry = new ArraySet<>();
for (Class clazz : mClasses) {
if (!isConcreteSettingsClass(clazz)) {
@@ -64,44 +78,59 @@ public class SearchIndexProviderCodeInspector extends CodeInspector {
}
// If it's a SettingsPreferenceFragment, it must also be Indexable.
final boolean implementsIndexable = Indexable.class.isAssignableFrom(clazz);
if (!implementsIndexable && !notImplementingIndexableWhitelist.contains(className)) {
if (!implementsIndexable
&& !notImplementingIndexableGrandfatherList.contains(className)) {
notImplementingIndexable.add(className);
}
final boolean hasSearchIndexProvider = hasSearchIndexProvider(clazz);
// If it implements Indexable, it must also implement the index provider field.
if (implementsIndexable && !hasSearchIndexProvider(clazz)
&& !notImplementingIndexProviderWhitelist.contains(className)) {
if (implementsIndexable && !hasSearchIndexProvider
&& !notImplementingIndexProviderGrandfatherList.contains(className)) {
notImplementingIndexProvider.add(className);
}
if (hasSearchIndexProvider
&& SearchIndexableResources.getResourceByName(className) == null
&& !notInSearchIndexableRegistryGrandfatherList.contains(className)) {
notInSearchProviderRegistry.add(className);
}
}
// Build error messages
final StringBuilder indexableError = new StringBuilder(
"SettingsPreferenceFragment should implement Indexable, but these are not:\n");
for (String c : notImplementingIndexable) {
indexableError.append(c).append("\n");
}
final StringBuilder indexProviderError = new StringBuilder(
"Indexable should have public field " + Index.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER
+ " but these are not:\n");
for (String c : notImplementingIndexProvider) {
indexProviderError.append(c).append("\n");
}
assertWithMessage(indexableError.toString())
.that(notImplementingIndexable.isEmpty())
.isTrue();
final String indexableError = buildErrorMessage(NOT_IMPLEMENTING_INDEXABLE_ERROR,
notImplementingIndexable);
final String indexProviderError = buildErrorMessage(NOT_CONTAINING_PROVIDER_OBJECT_ERROR,
notImplementingIndexProvider);
final String notInProviderRegistryError =
buildErrorMessage(NOT_IN_INDEXABLE_PROVIDER_REGISTRY, notInSearchProviderRegistry);
assertWithMessage(indexableError)
.that(notImplementingIndexable)
.isEmpty();
assertWithMessage(indexProviderError.toString())
.that(notImplementingIndexProvider.isEmpty())
.isTrue();
.that(notImplementingIndexProvider)
.isEmpty();
assertWithMessage(notInProviderRegistryError.toString())
.that(notInSearchProviderRegistry)
.isEmpty();
}
private boolean hasSearchIndexProvider(Class clazz) {
try {
final Field f = clazz.getField(Index.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER);
return f != null;
} catch (NoClassDefFoundError e) {
// Cannot find class def, ignore
return true;
} catch (NoSuchFieldException e) {
Log.e(TAG, "error fetching search provider from class " + clazz.getName());
return false;
}
}
private String buildErrorMessage(String errorSummary, Set<String> errorClasses) {
final StringBuilder error = new StringBuilder(errorSummary);
for (String c : errorClasses) {
error.append(c).append("\n");
}
return error.toString();
}
}