Catch exception when failing to create slice

- Created a non-generic exception type when failing SliceData.build()
- Catch exception and log error instead of crash.
- Added a presubmit test

Fixes: 78347031
Test: robotests, atest
Change-Id: I06e528cb5e1cd328f82f9561553f3c4b5b0bed26
This commit is contained in:
Fan Zhang
2018-04-20 12:53:25 -07:00
parent e0069d332d
commit 2a7fcc4d6f
4 changed files with 147 additions and 13 deletions

View File

@@ -67,7 +67,7 @@ public class SliceDataTest {
assertThat(data.isPlatformDefined()).isEqualTo(IS_PLATFORM_DEFINED);
}
@Test(expected = IllegalStateException.class)
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noKey_throwsIllegalStateException() {
new SliceData.Builder()
.setTitle(TITLE)
@@ -80,7 +80,7 @@ public class SliceDataTest {
.build();
}
@Test(expected = IllegalStateException.class)
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noTitle_throwsIllegalStateException() {
new SliceData.Builder()
.setKey(KEY)
@@ -93,7 +93,7 @@ public class SliceDataTest {
.build();
}
@Test(expected = IllegalStateException.class)
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noFragment_throwsIllegalStateException() {
new SliceData.Builder()
.setKey(KEY)
@@ -106,7 +106,7 @@ public class SliceDataTest {
.build();
}
@Test(expected = IllegalStateException.class)
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noPrefController_throwsIllegalStateException() {
new SliceData.Builder()
.setKey(KEY)

View File

@@ -0,0 +1,122 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.slices;
import static junit.framework.Assert.fail;
import android.content.Context;
import android.os.Bundle;
import android.platform.test.annotations.Presubmit;
import android.provider.SearchIndexableResource;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;
import android.util.Log;
import com.android.settings.core.PreferenceXmlParserUtils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.DatabaseIndexingUtils;
import com.android.settings.search.Indexable;
import com.android.settingslib.search.SearchIndexableResources;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@RunWith(AndroidJUnit4.class)
@MediumTest
public class SliceDataContractTest {
private static final String TAG = "SliceDataContractTest";
private Context mContext;
@Before
public void setUp() {
mContext = InstrumentationRegistry.getTargetContext();
}
@Test
@Presubmit
public void preferenceWithControllerMustHaveNonEmptyTitle()
throws IOException, XmlPullParserException {
final Set<String> nullTitleFragments = new HashSet<>();
final SearchIndexableResources resources =
FeatureFactory.getFactory(mContext).getSearchFeatureProvider()
.getSearchIndexableResources();
for (Class<?> clazz : resources.getProviderValues()) {
verifyPreferenceTitle(nullTitleFragments, clazz);
}
if (!nullTitleFragments.isEmpty()) {
final StringBuilder error = new StringBuilder(
"All preferences with a controller must have a non-empty title by default, "
+ "found empty title in the following fragments\n");
for (String c : nullTitleFragments) {
error.append(c).append("\n");
}
fail(error.toString());
}
}
private void verifyPreferenceTitle(Set<String> nullTitleFragments, Class<?> clazz)
throws IOException, XmlPullParserException {
if (clazz == null) {
return;
}
final String className = clazz.getName();
final Indexable.SearchIndexProvider provider =
DatabaseIndexingUtils.getSearchIndexProvider(clazz);
final List<SearchIndexableResource> resourcesToIndex =
provider.getXmlResourcesToIndex(mContext, true);
if (resourcesToIndex == null) {
Log.d(TAG, className + "is not providing SearchIndexableResource, skipping");
return;
}
for (SearchIndexableResource sir : resourcesToIndex) {
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
sir.xmlResId,
PreferenceXmlParserUtils.MetadataFlag.FLAG_INCLUDE_PREF_SCREEN
| PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_PREF_TITLE
| PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_PREF_CONTROLLER);
for (Bundle bundle : metadata) {
final String controller = bundle.getString(
PreferenceXmlParserUtils.METADATA_CONTROLLER);
if (TextUtils.isEmpty(controller)) {
continue;
}
final String title = bundle.getString(PreferenceXmlParserUtils.METADATA_TITLE);
if (TextUtils.isEmpty(title)) {
nullTitleFragments.add(className);
}
}
}
}
}