Add slice uri to slice index db

And slightly refactored the SliceDataConverter to remove 1 reflection.

Bug: 126222433
Test: robotest
Change-Id: Ic5782bdd71f5c9cb77879a35de81dc61c01d1912
This commit is contained in:
Fan Zhang
2019-05-29 13:19:15 -07:00
parent 88e3adce68
commit f36ca50ec7
8 changed files with 86 additions and 130 deletions

View File

@@ -23,7 +23,6 @@ import static com.android.settings.slices.SettingsSliceProvider.EXTRA_SLICE_PLAT
import android.annotation.ColorInt; import android.annotation.ColorInt;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@@ -110,17 +109,6 @@ public class SliceBuilderUtils {
} }
} }
/**
* @return the {@link SliceData.SliceType} for the {@param controllerClassName} and key.
*/
@SliceData.SliceType
public static int getSliceType(Context context, String controllerClassName,
String controllerKey) {
BasePreferenceController controller = getPreferenceController(context, controllerClassName,
controllerKey);
return controller.getSliceType();
}
/** /**
* Splits the Settings Slice Uri path into its two expected components: * Splits the Settings Slice Uri path into its two expected components:
* - intent/action * - intent/action
@@ -214,17 +202,6 @@ public class SliceBuilderUtils {
return ""; return "";
} }
public static Uri getUri(String path, boolean isPlatformSlice) {
final String authority = isPlatformSlice
? SettingsSlicesContract.AUTHORITY
: SettingsSliceProvider.SLICE_AUTHORITY;
return new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
.appendPath(path)
.build();
}
public static Intent buildSearchResultPageIntent(Context context, String className, String key, public static Intent buildSearchResultPageIntent(Context context, String className, String key,
String screenTitle, int sourceMetricsCategory) { String screenTitle, int sourceMetricsCategory) {
final Bundle args = new Bundle(); final Bundle args = new Bundle();
@@ -350,7 +327,7 @@ public class SliceBuilderUtils {
.build(); .build();
} }
private static BasePreferenceController getPreferenceController(Context context, static BasePreferenceController getPreferenceController(Context context,
String controllerClassName, String controllerKey) { String controllerClassName, String controllerKey) {
try { try {
return BasePreferenceController.createInstance(context, controllerClassName); return BasePreferenceController.createInstance(context, controllerClassName);

View File

@@ -27,14 +27,17 @@ import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_UNAVAI
import android.accessibilityservice.AccessibilityServiceInfo; import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.provider.SettingsSlicesContract;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
@@ -199,17 +202,24 @@ class SliceDataConverter {
} }
final String key = bundle.getString(METADATA_KEY); final String key = bundle.getString(METADATA_KEY);
final BasePreferenceController controller = SliceBuilderUtils
.getPreferenceController(mContext, controllerClassName, key);
// Only add pre-approved Slices available on the device.
if (!controller.isSliceable() || !controller.isAvailable()) {
continue;
}
final String title = bundle.getString(METADATA_TITLE); final String title = bundle.getString(METADATA_TITLE);
final String summary = bundle.getString(METADATA_SUMMARY); final String summary = bundle.getString(METADATA_SUMMARY);
final int iconResId = bundle.getInt(METADATA_ICON); final int iconResId = bundle.getInt(METADATA_ICON);
final int sliceType = SliceBuilderUtils.getSliceType(mContext, controllerClassName,
key); final int sliceType = controller.getSliceType();
final boolean isPlatformSlice = bundle.getBoolean(METADATA_PLATFORM_SLICE_FLAG); final boolean isPlatformSlice = bundle.getBoolean(METADATA_PLATFORM_SLICE_FLAG);
final String unavailableSliceSubtitle = bundle.getString( final String unavailableSliceSubtitle = bundle.getString(
METADATA_UNAVAILABLE_SLICE_SUBTITLE); METADATA_UNAVAILABLE_SLICE_SUBTITLE);
final SliceData xmlSlice = new SliceData.Builder() final SliceData xmlSlice = new SliceData.Builder()
.setKey(key) .setKey(key)
.setUri(controller.getSliceUri())
.setTitle(title) .setTitle(title)
.setSummary(summary) .setSummary(summary)
.setIcon(iconResId) .setIcon(iconResId)
@@ -221,13 +231,7 @@ class SliceDataConverter {
.setUnavailableSliceSubtitle(unavailableSliceSubtitle) .setUnavailableSliceSubtitle(unavailableSliceSubtitle)
.build(); .build();
final BasePreferenceController controller = xmlSliceData.add(xmlSlice);
SliceBuilderUtils.getPreferenceController(mContext, xmlSlice);
// Only add pre-approved Slices available on the device.
if (controller.isSliceable() && controller.isAvailable()) {
xmlSliceData.add(xmlSlice);
}
} }
} catch (SliceData.InvalidSliceDataException e) { } catch (SliceData.InvalidSliceDataException e) {
Log.w(TAG, "Invalid data when building SliceData for " + fragmentName, e); Log.w(TAG, "Invalid data when building SliceData for " + fragmentName, e);
@@ -294,6 +298,12 @@ class SliceDataConverter {
sliceDataBuilder.setKey(flattenedName) sliceDataBuilder.setKey(flattenedName)
.setTitle(title) .setTitle(title)
.setUri(new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(flattenedName)
.build())
.setIcon(iconResource) .setIcon(iconResource)
.setSliceType(SliceData.SliceType.SWITCH); .setSliceType(SliceData.SliceType.SWITCH);
try { try {

View File

@@ -101,6 +101,7 @@ class SlicesIndexer implements Runnable {
for (SliceData dataRow : indexData) { for (SliceData dataRow : indexData) {
values = new ContentValues(); values = new ContentValues();
values.put(IndexColumns.KEY, dataRow.getKey()); values.put(IndexColumns.KEY, dataRow.getKey());
values.put(IndexColumns.SLICE_URI, dataRow.getUri().toSafeString());
values.put(IndexColumns.TITLE, dataRow.getTitle()); values.put(IndexColumns.TITLE, dataRow.getTitle());
values.put(IndexColumns.SUMMARY, dataRow.getSummary()); values.put(IndexColumns.SUMMARY, dataRow.getSummary());
values.put(IndexColumns.SCREENTITLE, dataRow.getScreenTitle().toString()); values.put(IndexColumns.SCREENTITLE, dataRow.getScreenTitle().toString());

View File

@@ -163,6 +163,7 @@ public class NfcPreferenceControllerTest {
assertThat(keys).hasSize(1); assertThat(keys).hasSize(1);
} }
@Test @Test
public void setChecked_True_nfcShouldEnable() { public void setChecked_True_nfcShouldEnable() {
mNfcController.setChecked(true); mNfcController.setChecked(true);
@@ -209,7 +210,7 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void shouldTurnOffNFCInAirplaneMode_airplaneModeRadiosContainsNfc_shouldReturnTrue() { public void shouldTurnOffNFCInAirplaneMode_airplaneModeRadiosContainsNfc_shouldReturnTrue() {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC); Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC);
assertThat(NfcPreferenceController.shouldTurnOffNFCInAirplaneMode(mContext)).isTrue(); assertThat(NfcPreferenceController.shouldTurnOffNFCInAirplaneMode(mContext)).isTrue();
} }
@@ -217,7 +218,7 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void shouldTurnOffNFCInAirplaneMode_airplaneModeRadiosWithoutNfc_shouldReturnFalse() { public void shouldTurnOffNFCInAirplaneMode_airplaneModeRadiosWithoutNfc_shouldReturnFalse() {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_RADIOS, ""); Settings.Global.AIRPLANE_MODE_RADIOS, "");
assertThat(NfcPreferenceController.shouldTurnOffNFCInAirplaneMode(mContext)).isFalse(); assertThat(NfcPreferenceController.shouldTurnOffNFCInAirplaneMode(mContext)).isFalse();
} }
@@ -225,7 +226,7 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void displayPreference_airplaneModeRadiosContainsNfc_shouldCreateAirplaneModeObserver() { public void displayPreference_airplaneModeRadiosContainsNfc_shouldCreateAirplaneModeObserver() {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC); Settings.Global.AIRPLANE_MODE_RADIOS, Settings.Global.RADIO_NFC);
mNfcController.displayPreference(mScreen); mNfcController.displayPreference(mScreen);
@@ -235,7 +236,7 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void displayPreference_nfcToggleableInAirplaneMode_shouldCreateAirplaneModeObserver() { public void displayPreference_nfcToggleableInAirplaneMode_shouldCreateAirplaneModeObserver() {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, Settings.Global.RADIO_NFC); Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, Settings.Global.RADIO_NFC);
mNfcController.displayPreference(mScreen); mNfcController.displayPreference(mScreen);
@@ -245,9 +246,9 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void displayPreference_nfcNotAffectByAirplaneMode_shouldNotCreateAirplaneModeObserver() { public void displayPreference_nfcNotAffectByAirplaneMode_shouldNotCreateAirplaneModeObserver() {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, ""); Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS, "");
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_RADIOS, ""); Settings.Global.AIRPLANE_MODE_RADIOS, "");
mNfcController.displayPreference(mScreen); mNfcController.displayPreference(mScreen);
@@ -256,7 +257,8 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void ncfSliceWorker_nfcBroadcast_noExtra_sliceDoesntUpdate() { public void ncfSliceWorker_nfcBroadcast_noExtra_sliceDoesntUpdate() {
final NfcSliceWorker worker = spy(new NfcSliceWorker(mContext, getDummyUri())); final NfcSliceWorker worker = spy(
new NfcSliceWorker(mContext, mNfcController.getSliceUri()));
final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker); final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker);
final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
@@ -267,7 +269,8 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void ncfSliceWorker_nfcBroadcast_turningOn_sliceDoesntUpdate() { public void ncfSliceWorker_nfcBroadcast_turningOn_sliceDoesntUpdate() {
final NfcSliceWorker worker = spy(new NfcSliceWorker(mContext, getDummyUri())); final NfcSliceWorker worker = spy(
new NfcSliceWorker(mContext, mNfcController.getSliceUri()));
final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker); final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker);
final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_TURNING_ON); triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_TURNING_ON);
@@ -279,7 +282,8 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void ncfSliceWorker_nfcBroadcast_turningOff_sliceDoesntUpdate() { public void ncfSliceWorker_nfcBroadcast_turningOff_sliceDoesntUpdate() {
final NfcSliceWorker worker = spy(new NfcSliceWorker(mContext, getDummyUri())); final NfcSliceWorker worker = spy(
new NfcSliceWorker(mContext, mNfcController.getSliceUri()));
final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker); final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker);
final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_TURNING_OFF); triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_TURNING_OFF);
@@ -291,7 +295,8 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void ncfSliceWorker_nfcBroadcast_nfcOn_sliceUpdates() { public void ncfSliceWorker_nfcBroadcast_nfcOn_sliceUpdates() {
final NfcSliceWorker worker = spy(new NfcSliceWorker(mContext, getDummyUri())); final NfcSliceWorker worker = spy(
new NfcSliceWorker(mContext, mNfcController.getSliceUri()));
final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker); final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker);
final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_ON); triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_ON);
@@ -303,7 +308,8 @@ public class NfcPreferenceControllerTest {
@Test @Test
public void ncfSliceWorker_nfcBroadcast_nfcOff_sliceUpdates() { public void ncfSliceWorker_nfcBroadcast_nfcOff_sliceUpdates() {
final NfcSliceWorker worker = spy(new NfcSliceWorker(mContext, getDummyUri())); final NfcSliceWorker worker = spy(
new NfcSliceWorker(mContext, mNfcController.getSliceUri()));
final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker); final NfcUpdateReceiver receiver = worker.new NfcUpdateReceiver(worker);
final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); final Intent triggerIntent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_OFF); triggerIntent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_OFF);
@@ -312,8 +318,4 @@ public class NfcPreferenceControllerTest {
verify(worker).updateSlice(); verify(worker).updateSlice();
} }
private Uri getDummyUri() {
return SliceBuilderUtils.getUri("action/nfc", false);
}
} }

View File

@@ -96,8 +96,18 @@ import java.util.Set;
public class SettingsSliceProviderTest { public class SettingsSliceProviderTest {
private static final String KEY = "KEY"; private static final String KEY = "KEY";
private static final String INTENT_PATH = private static final Uri INTENT_SLICE_URI =
SettingsSlicesContract.PATH_SETTING_INTENT + "/" + KEY; new Uri.Builder().scheme(SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_INTENT)
.appendPath(KEY)
.build();
private static final Uri ACTION_SLICE_URI =
new Uri.Builder().scheme(SCHEME_CONTENT)
.authority(SettingsSlicesContract.AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(KEY)
.build();
private static final String TITLE = "title"; private static final String TITLE = "title";
private static final String SUMMARY = "summary"; private static final String SUMMARY = "summary";
private static final String SCREEN_TITLE = "screen title"; private static final String SCREEN_TITLE = "screen title";
@@ -155,20 +165,18 @@ public class SettingsSliceProviderTest {
@Test @Test
public void testInitialSliceReturned_emptySlice() { public void testInitialSliceReturned_emptySlice() {
insertSpecialCase(KEY); insertSpecialCase(KEY);
final Uri uri = SliceBuilderUtils.getUri(INTENT_PATH, false); Slice slice = mProvider.onBindSlice(INTENT_SLICE_URI);
Slice slice = mProvider.onBindSlice(uri);
assertThat(slice.getUri()).isEqualTo(uri); assertThat(slice.getUri()).isEqualTo(INTENT_SLICE_URI);
assertThat(slice.getItems()).isEmpty(); assertThat(slice.getItems()).isEmpty();
} }
@Test @Test
public void testLoadSlice_returnsSliceFromAccessor() { public void testLoadSlice_returnsSliceFromAccessor() {
insertSpecialCase(KEY); insertSpecialCase(KEY);
final Uri uri = SliceBuilderUtils.getUri(INTENT_PATH, false);
mProvider.loadSlice(uri); mProvider.loadSlice(INTENT_SLICE_URI);
SliceData data = mProvider.mSliceWeakDataCache.get(uri); SliceData data = mProvider.mSliceWeakDataCache.get(INTENT_SLICE_URI);
assertThat(data.getKey()).isEqualTo(KEY); assertThat(data.getKey()).isEqualTo(KEY);
assertThat(data.getTitle()).isEqualTo(TITLE); assertThat(data.getTitle()).isEqualTo(TITLE);
@@ -177,24 +185,23 @@ public class SettingsSliceProviderTest {
@Test @Test
public void loadSlice_registersIntentFilter() { public void loadSlice_registersIntentFilter() {
insertSpecialCase(KEY); insertSpecialCase(KEY);
final Uri uri = SliceBuilderUtils.getUri(INTENT_PATH, false);
mProvider.loadSlice(uri); mProvider.loadSlice(INTENT_SLICE_URI);
verify(mProvider).registerIntentToUri(eq(FakeToggleController.INTENT_FILTER), eq(uri)); verify(mProvider)
.registerIntentToUri(eq(FakeToggleController.INTENT_FILTER), eq(INTENT_SLICE_URI));
} }
@Test @Test
public void loadSlice_registersBackgroundListener() { public void loadSlice_registersBackgroundListener() {
insertSpecialCase(KEY); insertSpecialCase(KEY);
final Uri uri = SliceBuilderUtils.getUri(INTENT_PATH, false);
mProvider.loadSlice(uri); mProvider.loadSlice(INTENT_SLICE_URI);
Robolectric.flushForegroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler();
Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushBackgroundThreadScheduler();
assertThat(mProvider.mPinnedWorkers.get(uri).getClass()) assertThat(mProvider.mPinnedWorkers.get(INTENT_SLICE_URI).getClass())
.isEqualTo(FakeToggleController.TestWorker.class); .isEqualTo(FakeToggleController.TestWorker.class);
} }
@@ -255,27 +262,26 @@ public class SettingsSliceProviderTest {
@Test @Test
public void getDescendantUris_fullActionUri_returnsSelf() { public void getDescendantUris_fullActionUri_returnsSelf() {
final Uri uri = SliceBuilderUtils.getUri( final Collection<Uri> descendants = mProvider.onGetSliceDescendants(ACTION_SLICE_URI);
SettingsSlicesContract.PATH_SETTING_ACTION + "/key", true);
final Collection<Uri> descendants = mProvider.onGetSliceDescendants(uri); assertThat(descendants).containsExactly(ACTION_SLICE_URI);
assertThat(descendants).containsExactly(uri);
} }
@Test @Test
public void getDescendantUris_fullIntentUri_returnsSelf() { public void getDescendantUris_fullIntentUri_returnsSelf() {
final Uri uri = SliceBuilderUtils.getUri(
SettingsSlicesContract.PATH_SETTING_ACTION + "/key", true);
final Collection<Uri> descendants = mProvider.onGetSliceDescendants(uri); final Collection<Uri> descendants = mProvider.onGetSliceDescendants(ACTION_SLICE_URI);
assertThat(descendants).containsExactly(uri); assertThat(descendants).containsExactly(ACTION_SLICE_URI);
} }
@Test @Test
public void getDescendantUris_wrongPath_returnsEmpty() { public void getDescendantUris_wrongPath_returnsEmpty() {
final Uri uri = SliceBuilderUtils.getUri("invalid_path", true); final Uri uri = new Uri.Builder()
.scheme(SCHEME_CONTENT)
.authority(SettingsSlicesContract.AUTHORITY)
.appendPath("invalid_path")
.build();
final Collection<Uri> descendants = mProvider.onGetSliceDescendants(uri); final Collection<Uri> descendants = mProvider.onGetSliceDescendants(uri);

View File

@@ -136,58 +136,6 @@ public class SliceBuilderUtilsTest {
SliceTester.testSettingsCopyableSlice(mContext, slice, dummyData); SliceTester.testSettingsCopyableSlice(mContext, slice, dummyData);
} }
@Test
public void testUriBuilder_oemAuthority_intentPath_returnsValidSliceUri() {
final Uri expectedUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(INTENT_PATH)
.build();
final Uri actualUri = SliceBuilderUtils.getUri(INTENT_PATH, false);
assertThat(actualUri).isEqualTo(expectedUri);
}
@Test
public void testUriBuilder_oemAuthority_actionPath_returnsValidSliceUri() {
final Uri expectedUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(ACTION_PATH)
.build();
final Uri actualUri = SliceBuilderUtils.getUri(ACTION_PATH, false);
assertThat(actualUri).isEqualTo(expectedUri);
}
@Test
public void testUriBuilder_platformAuthority_intentPath_returnsValidSliceUri() {
final Uri expectedUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSlicesContract.AUTHORITY)
.appendPath(ACTION_PATH)
.build();
final Uri actualUri = SliceBuilderUtils.getUri(ACTION_PATH, true);
assertThat(actualUri).isEqualTo(expectedUri);
}
@Test
public void testUriBuilder_platformAuthority_actionPath_returnsValidSliceUri() {
final Uri expectedUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSlicesContract.AUTHORITY)
.appendPath(ACTION_PATH)
.build();
final Uri actualUri = SliceBuilderUtils.getUri(ACTION_PATH, true);
assertThat(actualUri).isEqualTo(expectedUri);
}
@Test @Test
public void testGetPreferenceController_buildsMatchingController() { public void testGetPreferenceController_buildsMatchingController() {
final BasePreferenceController controller = final BasePreferenceController controller =

View File

@@ -119,7 +119,8 @@ public class SliceDataConverterTest {
assertThat(fakeSlice.getScreenTitle()).isEqualTo(FAKE_SCREEN_TITLE); assertThat(fakeSlice.getScreenTitle()).isEqualTo(FAKE_SCREEN_TITLE);
assertThat(fakeSlice.getKeywords()).isNull(); assertThat(fakeSlice.getKeywords()).isNull();
assertThat(fakeSlice.getIconResource()).isNotNull(); assertThat(fakeSlice.getIconResource()).isNotNull();
assertThat(fakeSlice.getUri()).isNull(); assertThat(fakeSlice.getUri().toSafeString())
.isEqualTo("content://com.android.settings.slices/action/key");
assertThat(fakeSlice.getFragmentClassName()).isEqualTo(FAKE_FRAGMENT_CLASSNAME); assertThat(fakeSlice.getFragmentClassName()).isEqualTo(FAKE_FRAGMENT_CLASSNAME);
assertThat(fakeSlice.getPreferenceController()).isEqualTo(FAKE_CONTROLLER_NAME); assertThat(fakeSlice.getPreferenceController()).isEqualTo(FAKE_CONTROLLER_NAME);
assertThat(fakeSlice.getSliceType()).isEqualTo(SliceData.SliceType.SLIDER); assertThat(fakeSlice.getSliceType()).isEqualTo(SliceData.SliceType.SLIDER);
@@ -135,7 +136,7 @@ public class SliceDataConverterTest {
assertThat(fakeSlice.getScreenTitle()).isEqualTo( assertThat(fakeSlice.getScreenTitle()).isEqualTo(
mContext.getString(R.string.accessibility_settings)); mContext.getString(R.string.accessibility_settings));
assertThat(fakeSlice.getIconResource()).isEqualTo(FAKE_ICON); assertThat(fakeSlice.getIconResource()).isEqualTo(FAKE_ICON);
assertThat(fakeSlice.getUri()).isNull(); assertThat(fakeSlice.getUri()).isNotNull();
assertThat(fakeSlice.getFragmentClassName()).isEqualTo(ACCESSIBILITY_FRAGMENT); assertThat(fakeSlice.getFragmentClassName()).isEqualTo(ACCESSIBILITY_FRAGMENT);
assertThat(fakeSlice.getPreferenceController()).isEqualTo(A11Y_CONTROLLER_NAME); assertThat(fakeSlice.getPreferenceController()).isEqualTo(A11Y_CONTROLLER_NAME);
} }

View File

@@ -23,6 +23,7 @@ import static org.mockito.Mockito.spy;
import android.app.ApplicationPackageManager; import android.app.ApplicationPackageManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
@@ -139,10 +140,15 @@ public class SlicesDatabaseAccessorTest {
@Test @Test
public void testGetSliceFromUri_validUri_validSliceReturned() { public void testGetSliceFromUri_validUri_validSliceReturned() {
String key = "key"; final String key = "key";
String path = "intent/" + key;
insertSpecialCase(key); insertSpecialCase(key);
Uri uri = SliceBuilderUtils.getUri(path, false);
final Uri uri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath("action")
.appendPath(key)
.build();
SliceData data = mAccessor.getSliceDataFromUri(uri); SliceData data = mAccessor.getSliceDataFromUri(uri);
@@ -159,7 +165,12 @@ public class SlicesDatabaseAccessorTest {
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void testGetSliceFromUri_invalidUri_errorThrown() { public void testGetSliceFromUri_invalidUri_errorThrown() {
Uri uri = SliceBuilderUtils.getUri("intent/durr", false); final Uri uri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath("intent")
.appendPath("durr")
.build();
mAccessor.getSliceDataFromUri(uri); mAccessor.getSliceDataFromUri(uri);
} }