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,45 @@
/*
* 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.testutils;
import android.content.pm.ApplicationInfo;
/**
* Helper for mocking installed applications.
*/
public class ApplicationTestUtils {
/**
* Create and populate an {@link android.content.pm.ApplicationInfo} object that describes an
* installed app.
*
* @param uid The app's uid
* @param packageName The app's package name.
* @param flags Flags describing the app. See {@link android.content.pm.ApplicationInfo#flags}
* for possible values.
* @param targetSdkVersion The app's target SDK version
*
* @see android.content.pm.ApplicationInfo
*/
public static ApplicationInfo buildInfo(int uid, String packageName, int flags,
int targetSdkVersion) {
final ApplicationInfo info = new ApplicationInfo();
info.uid = uid;
info.packageName = packageName;
info.flags = flags;
info.targetSdkVersion = targetSdkVersion;
return info;
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.testutils;
import android.content.Intent;
import android.os.BatteryManager;
public class BatteryTestUtils {
public static Intent getChargingIntent() {
return getCustomBatteryIntent(
BatteryManager.BATTERY_PLUGGED_AC,
50 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_CHARGING);
}
public static Intent getDischargingIntent() {
return getCustomBatteryIntent(
0 /* plugged */,
10 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_DISCHARGING);
}
private static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
Intent intent = new Intent();
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);
intent.putExtra(BatteryManager.EXTRA_LEVEL, level);
intent.putExtra(BatteryManager.EXTRA_SCALE, scale);
intent.putExtra(BatteryManager.EXTRA_STATUS, status);
return intent;
}
}

View File

@@ -0,0 +1,26 @@
/*
* 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.testutils;
import android.app.Activity;
import android.content.Intent;
import android.os.UserHandle;
public class CustomActivity extends Activity {
@Override
public void startActivityAsUser(Intent intent, UserHandle user) {}
}

View File

@@ -0,0 +1,43 @@
/*
* 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.testutils;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
public class FakeCopyableController extends BasePreferenceController {
public FakeCopyableController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public boolean isCopyableSlice() {
return true;
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.testutils;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.Indexable;
import java.util.List;
public class FakeIndexProvider implements Indexable {
public static final String KEY = "TestKey";
/**
* The fake SearchIndexProvider. Note that the use of location_settings below implies that tests
* using this should be using the res/xml-mcc999/location_settings.xml or
* res/xml-mcc998/location_settings.xml. Annotate tests with
* {@code @Config(qualifiers = "mcc999")}.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_settings) {
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> result = super.getNonIndexableKeys(context);
result.add(KEY);
return result;
}
};
}

View File

@@ -0,0 +1,31 @@
/*
* 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.testutils;
import android.content.Context;
public class FakeInvalidSliderController extends FakeSliderController {
public FakeInvalidSliderController(Context context, String key) {
super(context, key);
}
@Override
public int getMax() {
// Return 0 to make it invalid
return 0;
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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.testutils;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.core.SliderPreferenceController;
public class FakeSliderController extends SliderPreferenceController {
public static final String AVAILABILITY_KEY = "fake_slider_availability_key";
public static final int MAX_VALUE = 9;
private static final String SETTING_KEY = "fake_slider_key";
public FakeSliderController(Context context, String key) {
super(context, key);
}
@Override
public int getSliderPosition() {
return Settings.System.getInt(mContext.getContentResolver(), SETTING_KEY, 0);
}
@Override
public boolean setSliderPosition(int position) {
return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY, position);
}
@Override
public int getMax() {
return MAX_VALUE;
}
@Override
public int getMin() {
return 0;
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(), AVAILABILITY_KEY, AVAILABLE);
}
@Override
public boolean isSliceable() {
return true;
}
}

View File

@@ -0,0 +1,105 @@
/*
* 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.testutils;
import android.content.Context;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.slices.SliceBackgroundWorker;
public class FakeToggleController extends TogglePreferenceController {
public static final String AVAILABILITY_KEY = "fake_toggle_availability_key";
public static final IntentFilter INTENT_FILTER = new IntentFilter(
WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
private static final String SETTING_KEY = "toggle_key";
private static final int ON = 1;
private static final int OFF = 0;
private boolean mIsAsyncUpdate = false;
public FakeToggleController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY, OFF) == ON;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY,
isChecked ? ON : OFF);
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(),
AVAILABILITY_KEY, AVAILABLE);
}
@Override
public IntentFilter getIntentFilter() {
return INTENT_FILTER;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() {
return TestWorker.class;
}
@Override
public boolean hasAsyncUpdate() {
return mIsAsyncUpdate;
}
public void setAsyncUpdate(boolean isAsyncUpdate) {
mIsAsyncUpdate = isAsyncUpdate;
}
public static class TestWorker extends SliceBackgroundWorker<Void> {
public TestWorker(Context context, Uri uri) {
super(context, uri);
}
@Override
protected void onSlicePinned() {
}
@Override
protected void onSliceUnpinned() {
}
@Override
public void close() {
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.testutils;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.core.BasePreferenceController;
public class FakeUnavailablePreferenceController extends BasePreferenceController {
public static final String AVAILABILITY_KEY = "fake_availability_key";
public FakeUnavailablePreferenceController(Context context) {
super(context, "key");
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(), AVAILABILITY_KEY, 0);
}
@Override
public boolean isSliceable() {
return true;
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.testutils;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import com.google.common.base.Preconditions;
/**
* Helper for building {@link ResolveInfo}s to be used in Robolectric tests.
*
* <p>The resulting {@link PackageInfo}s should typically be added to {@link
* org.robolectric.shadows.ShadowPackageManager#addResolveInfoForIntent(Intent, ResolveInfo)}.
*/
public final class ResolveInfoBuilder {
private final String mPackageName;
private ActivityInfo mActivityInfo;
private ProviderInfo mProviderInfo;
public ResolveInfoBuilder(String packageName) {
this.mPackageName = Preconditions.checkNotNull(packageName);
}
public ResolveInfoBuilder setActivity(String packageName, String className) {
mActivityInfo = new ActivityInfo();
mActivityInfo.packageName = packageName;
mActivityInfo.name = className;
return this;
}
public ResolveInfoBuilder setProvider(
String packageName, String className, String authority, boolean isSystemApp) {
mProviderInfo = new ProviderInfo();
mProviderInfo.authority = authority;
mProviderInfo.applicationInfo = new ApplicationInfo();
if (isSystemApp) {
mProviderInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
}
mProviderInfo.packageName = mPackageName;
mProviderInfo.applicationInfo.packageName = mPackageName;
mProviderInfo.name = className;
return this;
}
public ResolveInfo build() {
ResolveInfo info = new ResolveInfo();
info.activityInfo = mActivityInfo;
info.resolvePackageName = mPackageName;
info.providerInfo = mProviderInfo;
return info;
}
}

View File

@@ -0,0 +1,324 @@
/*
* 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.testutils;
import static android.app.slice.Slice.HINT_TITLE;
import static android.app.slice.Slice.SUBTYPE_COLOR;
import static android.app.slice.SliceItem.FORMAT_IMAGE;
import static android.app.slice.SliceItem.FORMAT_INT;
import static android.app.slice.SliceItem.FORMAT_TEXT;
import static com.google.common.truth.Truth.assertThat;
import android.app.PendingIntent;
import android.content.Context;
import android.text.TextUtils;
import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata;
import androidx.slice.builders.ListBuilder;
import androidx.slice.core.SliceAction;
import androidx.slice.core.SliceQuery;
import androidx.slice.widget.EventInfo;
import com.android.settings.Utils;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settings.slices.SliceBuilderUtils;
import com.android.settings.slices.SliceData;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Testing utility class to verify the contents of the different Settings Slices.
*
* TODO (77712944) check Summary, range (metadata.getRange()), toggle icons.
*/
public class SliceTester {
/**
* Test the contents of an intent based slice, including:
* - No toggles
* - Correct intent
* - Correct title
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsIntentSlice(Context context, Slice slice, SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the contents of an toggle based slice, including:
* - Contains one toggle
* - Correct toggle intent
* - Correct content intent
* - Correct title
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsToggleSlice(Context context, Slice slice, SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).hasSize(1);
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final SliceAction mainToggleAction = toggles.get(0);
assertThat(mainToggleAction.getIcon()).isNull();
// Check intent in Toggle Action
final PendingIntent togglePendingIntent = mainToggleAction.getAction();
assertThat(togglePendingIntent).isEqualTo(SliceBuilderUtils.getActionIntent(context,
SettingsSliceProvider.ACTION_TOGGLE_CHANGED, sliceData));
// Check primary intent
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the contents of an slider based slice, including:
* - No intent
* - Correct title
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsSliderSlice(Context context, Slice slice, SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final SliceAction primaryAction = metadata.getPrimaryAction();
final IconCompat icon = primaryAction.getIcon();
if (icon == null) {
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
} else {
final IconCompat expectedIcon = IconCompat.createWithResource(context,
sliceData.getIconResource());
assertThat(expectedIcon.toString()).isEqualTo(icon.toString());
}
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final int headerType = metadata.getHeaderType();
assertThat(headerType).isEqualTo(EventInfo.ROW_TYPE_SLIDER);
// Check primary intent
final PendingIntent primaryPendingIntent = primaryAction.getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the copyable slice, including:
* - No intent
* - Correct title
* - Correct intent
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsCopyableSlice(Context context, Slice slice,
SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final SliceAction primaryAction = metadata.getPrimaryAction();
final IconCompat expectedIcon = IconCompat.createWithResource(context,
sliceData.getIconResource());
assertThat(expectedIcon.toString()).isEqualTo(primaryAction.getIcon().toString());
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
// Check primary intent
final PendingIntent primaryPendingIntent = primaryAction.getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the contents of an unavailable slice, including:
* - No toggles
* - Correct title
* - Correct intent
* - Correct keywords
* - Color
* - TTL
*/
public static void testSettingsUnavailableSlice(Context context, Slice slice,
SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(SliceBuilderUtils.getContentPendingIntent(
context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Assert any slice item contains title.
*
* @param sliceItems All slice items of a Slice.
* @param title Title for asserting.
*/
public static void assertAnySliceItemContainsTitle(List<SliceItem> sliceItems, String title) {
assertThat(hasText(sliceItems, title, HINT_TITLE)).isTrue();
}
/**
* Assert any slice item contains subtitle.
*
* @param sliceItems All slice items of a Slice.
* @param subtitle Subtitle for asserting.
*/
public static void assertAnySliceItemContainsSubtitle(List<SliceItem> sliceItems,
String subtitle) {
// Subtitle has no hints
assertThat(hasText(sliceItems, subtitle, null /* hints */)).isTrue();
}
/**
* Assert no slice item contains subtitle.
*
* @param sliceItems All slice items of a Slice.
* @param subtitle Subtitle for asserting.
*/
public static void assertNoSliceItemContainsSubtitle(List<SliceItem> sliceItems,
String subtitle) {
// Subtitle has no hints
assertThat(hasText(sliceItems, subtitle, null /* hints */)).isFalse();
}
private static boolean hasText(List<SliceItem> sliceItems, String text, String hints) {
boolean hasText = false;
for (SliceItem item : sliceItems) {
List<SliceItem> textItems = SliceQuery.findAll(item, FORMAT_TEXT, hints,
null /* non-hints */);
if (textItems == null) {
continue;
}
for (SliceItem textItem : textItems) {
if (TextUtils.equals(textItem.getText(), text)) {
hasText = true;
break;
}
}
}
return hasText;
}
/**
* Assert any slice item contains icon.
*
* @param sliceItems All slice items of a Slice.
* @param icon Icon for asserting.
*/
public static void assertAnySliceItemContainsIcon(List<SliceItem> sliceItems, IconCompat icon) {
boolean hasIcon = false;
for (SliceItem item : sliceItems) {
List<SliceItem> iconItems = SliceQuery.findAll(item, FORMAT_IMAGE,
(String) null /* hints */, null /* non-hints */);
if (iconItems == null) {
continue;
}
for (SliceItem iconItem : iconItems) {
if (icon.toString().equals(iconItem.getIcon().toString())) {
hasIcon = true;
break;
}
}
}
assertThat(hasIcon).isTrue();
}
private static void assertKeywords(SliceMetadata metadata, SliceData data) {
final List<String> keywords = metadata.getSliceKeywords();
final Set<String> expectedKeywords = Arrays.stream(data.getKeywords().split(","))
.map(s -> s = s.trim())
.collect(Collectors.toSet());
expectedKeywords.add(data.getTitle());
expectedKeywords.add(data.getScreenTitle().toString());
assertThat(keywords).containsExactlyElementsIn(expectedKeywords);
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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.testutils;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag
.FLAG_INCLUDE_PREF_SCREEN;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.android.settings.core.PreferenceXmlParserUtils;
import org.xmlpull.v1.XmlPullParserException;
import java.util.ArrayList;
import java.util.List;
/**
* Util class for parsing XML
*/
public class XmlTestUtils {
/**
* Parses a preference screen's xml, collects and returns all keys used by preferences
* on the screen.
*
* @param context of the preference screen.
* @param xmlId of the Preference Xml to be parsed.
* @return List of all keys in the preference Xml
*/
public static List<String> getKeysFromPreferenceXml(Context context, int xmlId) {
final List<String> keys = new ArrayList<>();
try {
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(context, xmlId,
FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN);
for (Bundle bundle : metadata) {
final String key = bundle.getString(METADATA_KEY);
if (!TextUtils.isEmpty(key)) {
keys.add(key);
}
}
} catch (java.io.IOException | XmlPullParserException e) {
return null;
}
return keys;
}
}