Migrate robolectric tests to junit tests

This change do the 2 things:

1. Add new junit tests files which replace robolectric
   RobolectricTestRunner & RuntimeEnvironment with
   AndroidX objects without problem.
2. Remove the robolectric test files which have it's new junit files.

This change migrate 103 files, there are still 1209
files to go.

Bug: 174728471
Test: atest
      make RunSettingsRoboTests
Change-Id: I15ed3f4745b85862f720aabbf710ce1475aced93
This commit is contained in:
Arc Wang
2020-12-09 17:03:43 +08:00
parent f705f7933e
commit 62c78ff3fa
108 changed files with 3082 additions and 338 deletions

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2020 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 android.content.Context;
import com.android.settings.core.BasePreferenceController;
public class FakeContextOnlyPreferenceController extends BasePreferenceController {
public static final String KEY = "fakeController2";
public FakeContextOnlyPreferenceController(Context context) {
super(context, KEY);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2020 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 android.content.Context;
import com.android.settings.core.BasePreferenceController;
public class FakePreferenceController extends BasePreferenceController {
public FakePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public int getSliceType() {
return SliceData.SliceType.SLIDER;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public boolean isPublicSlice() {
return true;
}
@Override
public boolean useDynamicSliceSummary() {
return true;
}
}

View File

@@ -0,0 +1,174 @@
/*
* Copyright (C) 2020 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 com.android.settings.core.PreferenceXmlParserUtils.METADATA_CONTROLLER;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import android.content.Context;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.text.TextUtils;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.PreferenceXmlParserUtils;
import com.android.settings.core.SliderPreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.core.codeinspection.CodeInspector;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settings.search.SearchFeatureProviderImpl;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.search.SearchIndexableData;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class SliceControllerInXmlCodeInspector extends CodeInspector {
private static final List<Class> sSliceControllerClasses = Arrays.asList(
TogglePreferenceController.class,
SliderPreferenceController.class
);
private final List<String> mXmlDeclaredControllers = new ArrayList<>();
private final List<String> mExemptedClasses = new ArrayList<>();
private static final String ERROR_MISSING_CONTROLLER =
"The following controllers were expected to be declared by "
+ "'settings:controller=Controller_Class_Name' in their corresponding Xml. "
+ "If it should not appear in XML, add the controller's classname to "
+ "exempt_slice_controller_not_in_xml. Controllers:\n";
private final Context mContext;
private final SearchFeatureProvider mSearchProvider;
private final FakeFeatureFactory mFakeFeatureFactory;
public SliceControllerInXmlCodeInspector(List<Class<?>> classes) throws Exception {
super(classes);
mContext = ApplicationProvider.getApplicationContext();
mSearchProvider = new SearchFeatureProviderImpl();
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFakeFeatureFactory.searchFeatureProvider = mSearchProvider;
CodeInspector.initializeExemptList(mExemptedClasses,
"exempt_slice_controller_not_in_xml");
initDeclaredControllers();
}
private void initDeclaredControllers() throws IOException, XmlPullParserException {
final List<Integer> xmlResources = getIndexableXml();
for (int xmlResId : xmlResources) {
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
xmlResId, PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_PREF_CONTROLLER);
for (Bundle bundle : metadata) {
final String controllerClassName = bundle.getString(METADATA_CONTROLLER);
if (TextUtils.isEmpty(controllerClassName)) {
continue;
}
mXmlDeclaredControllers.add(controllerClassName);
}
}
// We definitely have some controllers in xml, so assert not-empty here as a proxy to
// make sure the parser didn't fail
assertThat(mXmlDeclaredControllers).isNotEmpty();
}
@Override
public void run() {
final List<String> missingControllersInXml = new ArrayList<>();
for (Class<?> clazz : mClasses) {
if (!isConcreteSettingsClass(clazz)) {
// Only care about non-abstract classes.
continue;
}
if (!isInlineSliceClass(clazz)) {
// Only care about inline-slice controller classes.
continue;
}
if (!mXmlDeclaredControllers.contains(clazz.getName())) {
// Class clazz should have been declared in XML (unless allowlisted).
missingControllersInXml.add(clazz.getName());
}
}
// Removed allowlisted classes
missingControllersInXml.removeAll(mExemptedClasses);
final String missingControllerError =
buildErrorMessage(ERROR_MISSING_CONTROLLER, missingControllersInXml);
assertWithMessage(missingControllerError).that(missingControllersInXml).isEmpty();
}
private boolean isInlineSliceClass(Class clazz) {
while (clazz != null) {
clazz = clazz.getSuperclass();
if (sSliceControllerClasses.contains(clazz)) {
return true;
}
}
return false;
}
private String buildErrorMessage(String errorSummary, List<String> errorClasses) {
final StringBuilder error = new StringBuilder(errorSummary);
for (String c : errorClasses) {
error.append(c).append("\n");
}
return error.toString();
}
private List<Integer> getIndexableXml() {
final List<Integer> xmlResSet = new ArrayList<>();
final Collection<SearchIndexableData> bundles = FeatureFactory.getFactory(
mContext).getSearchFeatureProvider().getSearchIndexableResources()
.getProviderValues();
for (SearchIndexableData bundle : bundles) {
Indexable.SearchIndexProvider provider = bundle.getSearchIndexProvider();
if (provider == null) {
continue;
}
List<SearchIndexableResource> resources = provider.getXmlResourcesToIndex(mContext,
true);
if (resources == null) {
continue;
}
for (SearchIndexableResource resource : resources) {
// Add '0's anyway. It won't break the test.
xmlResSet.add(resource.xmlResId);
}
}
return xmlResSet;
}
}

View File

@@ -0,0 +1,287 @@
/*
* Copyright (C) 2020 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 com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class SliceDataTest {
private static final String KEY = "KEY";
private static final String TITLE = "title";
private static final String SUMMARY = "summary";
private static final String SCREEN_TITLE = "screen title";
private static final String KEYWORDS = "a, b, c";
private static final String FRAGMENT_NAME = "fragment name";
private static final int ICON = 1234; // I declare a thumb war
private static final Uri URI = Uri.parse("content://com.android.settings.slices/test");
private static final String PREF_CONTROLLER = "com.android.settings.slices.tester";
private static final int SLICE_TYPE = SliceData.SliceType.SWITCH;
private static final String UNAVAILABLE_SLICE_SUBTITLE = "subtitleOfUnavailableSlice";
@Test
public void testBuilder_buildsMatchingObject() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setKeywords(KEYWORDS)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER)
.setSliceType(SLICE_TYPE)
.setUnavailableSliceSubtitle(UNAVAILABLE_SLICE_SUBTITLE)
.setIsPublicSlice(true);
SliceData data = builder.build();
assertThat(data.getKey()).isEqualTo(KEY);
assertThat(data.getTitle()).isEqualTo(TITLE);
assertThat(data.getSummary()).isEqualTo(SUMMARY);
assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
assertThat(data.getKeywords()).isEqualTo(KEYWORDS);
assertThat(data.getIconResource()).isEqualTo(ICON);
assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
assertThat(data.getUri()).isEqualTo(URI);
assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
assertThat(data.getSliceType()).isEqualTo(SLICE_TYPE);
assertThat(data.getUnavailableSliceSubtitle()).isEqualTo(UNAVAILABLE_SLICE_SUBTITLE);
assertThat(data.isPublicSlice()).isEqualTo(true);
}
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noKey_throwsIllegalStateException() {
new SliceData.Builder()
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER)
.build();
}
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noTitle_throwsIllegalStateException() {
new SliceData.Builder()
.setKey(KEY)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER)
.build();
}
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noFragment_throwsIllegalStateException() {
new SliceData.Builder()
.setKey(KEY)
.setFragmentName(FRAGMENT_NAME)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER)
.build();
}
@Test(expected = SliceData.InvalidSliceDataException.class)
public void testBuilder_noPrefController_throwsIllegalStateException() {
new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setUri(URI)
.setFragmentName(FRAGMENT_NAME)
.build();
}
@Test
public void testBuilder_noSubtitle_buildsMatchingObject() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData data = builder.build();
assertThat(data.getKey()).isEqualTo(KEY);
assertThat(data.getTitle()).isEqualTo(TITLE);
assertThat(data.getSummary()).isNull();
assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
assertThat(data.getIconResource()).isEqualTo(ICON);
assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
assertThat(data.getUri()).isEqualTo(URI);
assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
}
@Test
public void testBuilder_noScreenTitle_buildsMatchingObject() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData data = builder.build();
assertThat(data.getKey()).isEqualTo(KEY);
assertThat(data.getTitle()).isEqualTo(TITLE);
assertThat(data.getSummary()).isEqualTo(SUMMARY);
assertThat(data.getScreenTitle()).isNull();
assertThat(data.getIconResource()).isEqualTo(ICON);
assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
assertThat(data.getUri()).isEqualTo(URI);
assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
}
@Test
public void testBuilder_noIcon_buildsMatchingObject() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData data = builder.build();
assertThat(data.getKey()).isEqualTo(KEY);
assertThat(data.getTitle()).isEqualTo(TITLE);
assertThat(data.getSummary()).isEqualTo(SUMMARY);
assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
assertThat(data.getIconResource()).isEqualTo(0);
assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
assertThat(data.getUri()).isEqualTo(URI);
assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
}
@Test
public void testBuilder_noUri_buildsMatchingObject() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(null)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData data = builder.build();
assertThat(data.getKey()).isEqualTo(KEY);
assertThat(data.getTitle()).isEqualTo(TITLE);
assertThat(data.getSummary()).isEqualTo(SUMMARY);
assertThat(data.getScreenTitle()).isEqualTo(SCREEN_TITLE);
assertThat(data.getIconResource()).isEqualTo(ICON);
assertThat(data.getFragmentClassName()).isEqualTo(FRAGMENT_NAME);
assertThat(data.getUri()).isNull();
assertThat(data.getPreferenceController()).isEqualTo(PREF_CONTROLLER);
}
@Test
public void testEquality_identicalObjects() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData dataOne = builder.build();
SliceData dataTwo = builder.build();
assertThat(dataOne.hashCode()).isEqualTo(dataTwo.hashCode());
assertThat(dataOne).isEqualTo(dataTwo);
}
@Test
public void testEquality_matchingKey_EqualObjects() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData dataOne = builder.build();
builder.setTitle(TITLE + " diff")
.setSummary(SUMMARY + " diff")
.setScreenTitle(SCREEN_TITLE + " diff")
.setIcon(ICON + 1)
.setFragmentName(FRAGMENT_NAME + " diff")
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER + " diff");
SliceData dataTwo = builder.build();
assertThat(dataOne.hashCode()).isEqualTo(dataTwo.hashCode());
assertThat(dataOne).isEqualTo(dataTwo);
}
@Test
public void testEquality_differentKey_differentObjects() {
SliceData.Builder builder = new SliceData.Builder()
.setKey(KEY)
.setTitle(TITLE)
.setSummary(SUMMARY)
.setScreenTitle(SCREEN_TITLE)
.setIcon(ICON)
.setFragmentName(FRAGMENT_NAME)
.setUri(URI)
.setPreferenceControllerClassName(PREF_CONTROLLER);
SliceData dataOne = builder.build();
builder.setKey("not key");
SliceData dataTwo = builder.build();
assertThat(dataOne.hashCode()).isNotEqualTo(dataTwo.hashCode());
assertThat(dataOne).isNotEqualTo(dataTwo);
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2020 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 android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.SettingsSlicesContract;
import com.android.settings.testutils.FakeIndexProvider;
import com.android.settings.testutils.FakeToggleController;
class SliceTestUtils {
public static final String FAKE_TITLE = "title";
public static final String FAKE_SUMMARY = "summary";
public static final String FAKE_SCREEN_TITLE = "screen_title";
public static final String FAKE_KEYWORDS = "a, b, c";
public static final int FAKE_ICON = 1234;
public static final String FAKE_FRAGMENT_NAME = FakeIndexProvider.class.getName();
public static final String FAKE_CONTROLLER_NAME = FakeToggleController.class.getName();
public static void insertSliceToDb(Context context, String key) {
insertSliceToDb(context, key, true /* isPlatformSlice */);
}
public static void insertSliceToDb(Context context, String key, boolean isPlatformSlice) {
insertSliceToDb(context, key, isPlatformSlice, null /*customizedUnavailableSliceSubtitle*/);
}
public static void insertSliceToDb(Context context, String key, boolean isPlatformSlice,
String customizedUnavailableSliceSubtitle) {
insertSliceToDb(context, key, isPlatformSlice, customizedUnavailableSliceSubtitle, false);
}
public static void insertSliceToDb(Context context, String key, boolean isPlatformSlice,
String customizedUnavailableSliceSubtitle, boolean isPublicSlice) {
final SQLiteDatabase db = SlicesDatabaseHelper.getInstance(context).getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SlicesDatabaseHelper.IndexColumns.KEY, key);
values.put(SlicesDatabaseHelper.IndexColumns.SLICE_URI,
new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(isPlatformSlice
? SettingsSlicesContract.AUTHORITY
: SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(key)
.build().toSafeString());
values.put(SlicesDatabaseHelper.IndexColumns.TITLE, FAKE_TITLE);
values.put(SlicesDatabaseHelper.IndexColumns.SUMMARY, FAKE_SUMMARY);
values.put(SlicesDatabaseHelper.IndexColumns.SCREENTITLE, FAKE_SCREEN_TITLE);
values.put(SlicesDatabaseHelper.IndexColumns.KEYWORDS, FAKE_KEYWORDS);
values.put(SlicesDatabaseHelper.IndexColumns.ICON_RESOURCE, FAKE_ICON);
values.put(SlicesDatabaseHelper.IndexColumns.FRAGMENT, FAKE_FRAGMENT_NAME);
values.put(SlicesDatabaseHelper.IndexColumns.CONTROLLER, FAKE_CONTROLLER_NAME);
values.put(SlicesDatabaseHelper.IndexColumns.SLICE_TYPE, SliceData.SliceType.INTENT);
values.put(SlicesDatabaseHelper.IndexColumns.UNAVAILABLE_SLICE_SUBTITLE,
customizedUnavailableSliceSubtitle);
values.put(SlicesDatabaseHelper.IndexColumns.PUBLIC_SLICE, isPublicSlice);
db.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, values);
db.close();
}
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright (C) 2020 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 com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.provider.SettingsSlicesContract;
import androidx.slice.Slice;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class SpecialCaseSliceManagerTest {
private static final String FAKE_PARAMETER_KEY = "fake_parameter_key";
private static final String FAKE_PARAMETER_VALUE = "fake_value";
private Context mContext;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
CustomSliceRegistry.sUriToSlice.clear();
CustomSliceRegistry.sUriToSlice.put(FakeSliceable.URI, FakeSliceable.class);
}
@Test
public void getSliceableFromUri_returnsCorrectObject() {
final CustomSliceable sliceable = CustomSliceable.createInstance(
mContext, CustomSliceRegistry.getSliceClassByUri(FakeSliceable.URI));
assertThat(sliceable).isInstanceOf(FakeSliceable.class);
}
@Test
public void getSliceableFromUriWithParameter_returnsCorrectObject() {
final Uri parameterUri = FakeSliceable.URI
.buildUpon()
.clearQuery()
.appendQueryParameter(FAKE_PARAMETER_KEY, FAKE_PARAMETER_VALUE)
.build();
final CustomSliceable sliceable = CustomSliceable.createInstance(
mContext, CustomSliceRegistry.getSliceClassByUri(parameterUri));
assertThat(sliceable).isInstanceOf(FakeSliceable.class);
}
@Test
public void isValidUri_validUri_returnsTrue() {
final boolean isValidUri = CustomSliceRegistry.isValidUri(FakeSliceable.URI);
assertThat(isValidUri).isTrue();
}
@Test
public void isValidUri_invalidUri_returnsFalse() {
final boolean isValidUri = CustomSliceRegistry.isValidUri(null);
assertThat(isValidUri).isFalse();
}
@Test
public void isValidAction_validActions_returnsTrue() {
final boolean isValidAction =
CustomSliceRegistry.isValidAction(FakeSliceable.URI.toString());
assertThat(isValidAction).isTrue();
}
@Test
public void isValidAction_invalidAction_returnsFalse() {
final boolean isValidAction = CustomSliceRegistry.isValidAction("action");
assertThat(isValidAction).isFalse();
}
static class FakeSliceable implements CustomSliceable {
static final String KEY = "magic key of khazad dum";
static final Uri URI = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(KEY)
.build();
static final Slice SLICE = new Slice.Builder(URI).build();
static boolean sBackingData = false;
final Context mContext;
public FakeSliceable(Context context) {
mContext = context;
}
@Override
public Slice getSlice() {
return SLICE;
}
@Override
public Uri getUri() {
return URI;
}
@Override
public void onNotifyChange(Intent intent) {
sBackingData = !sBackingData;
}
@Override
public IntentFilter getIntentFilter() {
return new IntentFilter();
}
@Override
public Intent getIntent() {
return null;
}
}
}