Merge "Cleanup notification channel slice" into rvc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
1e0bb9f528
@@ -16,40 +16,17 @@
|
||||
|
||||
package com.android.settings.homepage.contextualcards;
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice.PREFS;
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice.PREF_KEY_INTERACTED_PACKAGES;
|
||||
import static com.android.settings.slices.CustomSliceRegistry.CONTEXTUAL_NOTIFICATION_CHANNEL_SLICE_URI;
|
||||
import static com.android.settings.slices.CustomSliceRegistry.FLASHLIGHT_SLICE_URI;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
import androidx.slice.Slice;
|
||||
import androidx.slice.SliceProvider;
|
||||
import androidx.slice.builders.ListBuilder;
|
||||
import androidx.slice.builders.SliceAction;
|
||||
import androidx.slice.widget.SliceLiveData;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.applications.AppInfoBase;
|
||||
import com.android.settings.intelligence.ContextualCardProto;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -61,14 +38,12 @@ import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ContextualCardFeatureProviderImplTest {
|
||||
|
||||
private Context mContext;
|
||||
private ContextualCardFeatureProviderImpl mImpl;
|
||||
private SharedPreferences mSharedPreferences;
|
||||
private CardDatabaseHelper mCardDatabaseHelper;
|
||||
private SQLiteDatabase mDatabase;
|
||||
|
||||
@@ -76,7 +51,6 @@ public class ContextualCardFeatureProviderImplTest {
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mImpl = new ContextualCardFeatureProviderImpl(mContext);
|
||||
mSharedPreferences = mContext.getSharedPreferences(PREFS, MODE_PRIVATE);
|
||||
// Set-up specs for SliceMetadata.
|
||||
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
|
||||
mCardDatabaseHelper = CardDatabaseHelper.getInstance(mContext);
|
||||
@@ -87,7 +61,6 @@ public class ContextualCardFeatureProviderImplTest {
|
||||
public void tearDown() {
|
||||
CardDatabaseHelper.getInstance(mContext).close();
|
||||
CardDatabaseHelper.sCardDatabaseHelper = null;
|
||||
removeInteractedPackageFromSharedPreference();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,37 +101,12 @@ public class ContextualCardFeatureProviderImplTest {
|
||||
assertThat(rowsUpdated).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logNotificationPackage_isContextualNotificationChannel_shouldLogPackage() {
|
||||
final String packageName = "com.android.test.app";
|
||||
final Slice slice = buildSlice(CONTEXTUAL_NOTIFICATION_CHANNEL_SLICE_URI, packageName);
|
||||
|
||||
mImpl.logNotificationPackage(slice);
|
||||
|
||||
final Set<String> interactedPackages = mSharedPreferences.getStringSet(
|
||||
PREF_KEY_INTERACTED_PACKAGES, new ArraySet<>());
|
||||
assertThat(interactedPackages.contains(packageName)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logNotificationPackage_isNotContextualNotificationChannel_shouldNotLogPackage() {
|
||||
final String packageName = "com.android.test.app";
|
||||
final Slice slice = buildSlice(FLASHLIGHT_SLICE_URI, packageName);
|
||||
|
||||
mImpl.logNotificationPackage(slice);
|
||||
|
||||
final Set<String> interactedPackages = mSharedPreferences.getStringSet(
|
||||
PREF_KEY_INTERACTED_PACKAGES, new ArraySet<>());
|
||||
assertThat(interactedPackages.contains(packageName)).isFalse();
|
||||
}
|
||||
|
||||
private static void insertFakeCard(
|
||||
SQLiteDatabase db, String name, double score, String uri, @Nullable Long time) {
|
||||
final ContentValues value = new ContentValues();
|
||||
value.put(CardDatabaseHelper.CardColumns.NAME, name);
|
||||
value.put(CardDatabaseHelper.CardColumns.SCORE, score);
|
||||
value.put(CardDatabaseHelper.CardColumns.SLICE_URI, uri);
|
||||
|
||||
value.put(CardDatabaseHelper.CardColumns.TYPE, ContextualCard.CardType.SLICE);
|
||||
value.put(CardDatabaseHelper.CardColumns.CATEGORY,
|
||||
ContextualCardProto.ContextualCard.Category.DEFAULT.getNumber());
|
||||
@@ -173,31 +121,4 @@ public class ContextualCardFeatureProviderImplTest {
|
||||
|
||||
db.insert(CardDatabaseHelper.CARD_TABLE, null, value);
|
||||
}
|
||||
|
||||
private Slice buildSlice(Uri sliceUri, String packageName) {
|
||||
final Bundle args = new Bundle();
|
||||
args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
|
||||
final Intent intent = new Intent("action");
|
||||
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
|
||||
|
||||
final PendingIntent pendingIntent = spy(
|
||||
PendingIntent.getActivity(mContext, 0 /* requestCode */, intent, 0 /* flags */));
|
||||
doReturn(intent).when(pendingIntent).getIntent();
|
||||
final IconCompat icon = IconCompat.createWithResource(mContext, R.drawable.empty_icon);
|
||||
final SliceAction action = SliceAction.createDeeplink(pendingIntent, icon,
|
||||
ListBuilder.SMALL_IMAGE, "title");
|
||||
|
||||
return new ListBuilder(mContext, sliceUri, ListBuilder.INFINITY)
|
||||
.addRow(new ListBuilder.RowBuilder()
|
||||
.addEndItem(icon, ListBuilder.ICON_IMAGE)
|
||||
.setTitle("title")
|
||||
.setPrimaryAction(action))
|
||||
.build();
|
||||
}
|
||||
|
||||
private void removeInteractedPackageFromSharedPreference() {
|
||||
if (mSharedPreferences.contains(PREF_KEY_INTERACTED_PACKAGES)) {
|
||||
mSharedPreferences.edit().remove(PREF_KEY_INTERACTED_PACKAGES).apply();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,107 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.homepage.contextualcards.slices;
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice.PREFS;
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice.PREF_KEY_INTERACTED_PACKAGES;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.slices.CustomSliceRegistry;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ContextualNotificationChannelSliceTest {
|
||||
|
||||
private static final String PACKAGE_NAME = "package_name";
|
||||
|
||||
private Context mContext;
|
||||
private ContextualNotificationChannelSlice mNotificationChannelSlice;
|
||||
private SharedPreferences mSharedPreferences;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mNotificationChannelSlice = new ContextualNotificationChannelSlice(mContext);
|
||||
mSharedPreferences = mContext.getSharedPreferences(PREFS, MODE_PRIVATE);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
removeInteractedPackageFromSharedPreference();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUri_shouldBeContextualNotificationChannelSliceUri() {
|
||||
final Uri uri = mNotificationChannelSlice.getUri();
|
||||
|
||||
assertThat(uri).isEqualTo(CustomSliceRegistry.CONTEXTUAL_NOTIFICATION_CHANNEL_SLICE_URI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSubTitle_shouldBeRecentlyInstalledApp() {
|
||||
final CharSequence subTitle = mNotificationChannelSlice.getSubTitle("com.test.package", 0);
|
||||
|
||||
assertThat(subTitle).isEqualTo(mContext.getText(R.string.recently_installed_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUserInteracted_hasInteractedPackage_shouldBeTrue() {
|
||||
addInteractedPackageToSharedPreference();
|
||||
|
||||
final boolean isInteracted = mNotificationChannelSlice.isUserInteracted(PACKAGE_NAME);
|
||||
|
||||
assertThat(isInteracted).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUserInteracted_noInteractedPackage_shouldBeFalse() {
|
||||
final boolean isInteracted = mNotificationChannelSlice.isUserInteracted(PACKAGE_NAME);
|
||||
|
||||
assertThat(isInteracted).isFalse();
|
||||
}
|
||||
|
||||
private void addInteractedPackageToSharedPreference() {
|
||||
final Set<String> interactedPackages = new ArraySet<>();
|
||||
interactedPackages.add(PACKAGE_NAME);
|
||||
|
||||
mSharedPreferences.edit().putStringSet(PREF_KEY_INTERACTED_PACKAGES,
|
||||
interactedPackages).apply();
|
||||
}
|
||||
|
||||
private void removeInteractedPackageFromSharedPreference() {
|
||||
if (mSharedPreferences.contains(PREF_KEY_INTERACTED_PACKAGES)) {
|
||||
mSharedPreferences.edit().remove(PREF_KEY_INTERACTED_PACKAGES).apply();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,420 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.homepage.contextualcards.slices;
|
||||
|
||||
import static android.app.NotificationManager.IMPORTANCE_LOW;
|
||||
import static android.app.NotificationManager.IMPORTANCE_NONE;
|
||||
import static android.app.slice.Slice.HINT_LIST_ITEM;
|
||||
import static android.app.slice.SliceItem.FORMAT_SLICE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationChannelGroup;
|
||||
import android.app.role.RoleManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ParceledListSlice;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
import androidx.slice.Slice;
|
||||
import androidx.slice.SliceItem;
|
||||
import androidx.slice.SliceMetadata;
|
||||
import androidx.slice.SliceProvider;
|
||||
import androidx.slice.core.SliceQuery;
|
||||
import androidx.slice.widget.SliceLiveData;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settings.notification.NotificationBackend.AppRow;
|
||||
import com.android.settings.notification.NotificationBackend.NotificationsSentState;
|
||||
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowPackageManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NotificationChannelSliceTest {
|
||||
private static final String APP_LABEL = "Example App";
|
||||
private static final int CHANNEL_COUNT = 3;
|
||||
private static final String CHANNEL_NAME_PREFIX = "channel";
|
||||
private static final int NOTIFICATION_COUNT =
|
||||
NotificationChannelSlice.MIN_NOTIFICATION_SENT_COUNT + 1;
|
||||
private static final String PACKAGE_NAME = "com.test.notification.channel.slice";
|
||||
private static final int UID = 2019;
|
||||
|
||||
@Mock
|
||||
private NotificationBackend mNotificationBackend;
|
||||
private Context mContext;
|
||||
private IconCompat mIcon;
|
||||
private NotificationChannelSlice mNotificationChannelSlice;
|
||||
private ShadowPackageManager mPackageManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
|
||||
// Shadow PackageManager to add mock package.
|
||||
mPackageManager = shadowOf(mContext.getPackageManager());
|
||||
|
||||
// Set-up specs for SliceMetadata.
|
||||
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
|
||||
|
||||
mNotificationChannelSlice = spy(new NotificationChannelSlice(mContext));
|
||||
|
||||
doReturn(UID).when(mNotificationChannelSlice).getApplicationUid(any(String.class));
|
||||
mIcon = IconCompat.createWithResource(mContext, R.drawable.ic_settings_24dp);
|
||||
doReturn(mIcon).when(mNotificationChannelSlice).getApplicationIcon(any(String.class));
|
||||
|
||||
// Assign mock NotificationBackend to build notification related data.
|
||||
mNotificationChannelSlice.mNotificationBackend = mNotificationBackend;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mPackageManager.removePackage(PACKAGE_NAME);
|
||||
ShadowRestrictedLockUtilsInternal.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_hasSuggestedApp_shouldHaveNotificationChannelTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(
|
||||
mContext.getString(R.string.manage_app_notification, APP_LABEL));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_hasSuggestedApp_shouldSortByNotificationSentCount() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
// Get all RowBuilders from Slice.
|
||||
final List<SliceItem> rowItems = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM,
|
||||
null /* nonHints */);
|
||||
|
||||
// Ensure the total size of rows is equal to the notification channel count with header.
|
||||
assertThat(rowItems).isNotNull();
|
||||
assertThat(rowItems.size()).isEqualTo(CHANNEL_COUNT + 1);
|
||||
|
||||
// Remove the header of slice.
|
||||
rowItems.remove(0);
|
||||
|
||||
// Test the rows of slice are sorted with notification sent count by descending.
|
||||
for (int i = 0; i < rowItems.size(); i++) {
|
||||
// Assert the summary text is the same as expectation.
|
||||
assertThat(getSummaryFromSliceItem(rowItems.get(i))).isEqualTo(
|
||||
mContext.getResources().getQuantityString(R.plurals.notifications_sent_weekly,
|
||||
CHANNEL_COUNT - i, CHANNEL_COUNT - i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSlice_noRecentlyInstalledApp_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(false /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSlice_noMultiChannelApp_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(1 /* channelCount */, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_insufficientNotificationSentCount_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, 1 /* notificationCount */, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSlice_isSystemApp_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */, ApplicationInfo.FLAG_SYSTEM);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSlice_isNotificationBanned_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, true /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_exceedDefaultRowCount_shouldOnlyShowDefaultRows() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(NotificationChannelSlice.DEFAULT_EXPANDED_ROW_COUNT * 2,
|
||||
NOTIFICATION_COUNT, false /* banned */, false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
// Get the number of RowBuilders from Slice.
|
||||
final int rows = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM,
|
||||
null /* nonHints */).size();
|
||||
// The header of this slice is built by RowBuilder. Hence, the row count will contain it.
|
||||
assertThat(rows).isEqualTo(NotificationChannelSlice.DEFAULT_EXPANDED_ROW_COUNT + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_channelCountIsLessThanDefaultRows_subTitleShouldNotHaveTapToManagerAll() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT - 1, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getSubtitle()).isEqualTo(mContext.getResources().getQuantityString(
|
||||
R.plurals.notification_few_channel_count_summary, CHANNEL_COUNT - 1,
|
||||
CHANNEL_COUNT - 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_channelCountIsEqualToDefaultRows_subTitleShouldNotHaveTapToManagerAll() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getSubtitle()).isEqualTo(mContext.getResources().getQuantityString(
|
||||
R.plurals.notification_few_channel_count_summary, CHANNEL_COUNT, CHANNEL_COUNT));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_channelCountIsMoreThanDefaultRows_subTitleShouldHaveTapToManagerAll() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT + 1, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getSubtitle()).isEqualTo(
|
||||
mContext.getString(R.string.notification_many_channel_count_summary,
|
||||
CHANNEL_COUNT + 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_isAllDisplayableChannelBlocked_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
true /* isChannelBlocked */);
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowRestrictedLockUtilsInternal.class)
|
||||
public void getSlice_isInteractedPackage_shouldHaveNoSuggestedAppTitle() {
|
||||
addMockPackageToPackageManager(true /* isRecentlyInstalled */,
|
||||
ApplicationInfo.FLAG_INSTALLED);
|
||||
mockNotificationBackend(CHANNEL_COUNT, NOTIFICATION_COUNT, false /* banned */,
|
||||
false /* isChannelBlocked */);
|
||||
doReturn(true).when(mNotificationChannelSlice).isUserInteracted(any(String.class));
|
||||
|
||||
final Slice slice = mNotificationChannelSlice.getSlice();
|
||||
|
||||
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.no_suggested_app));
|
||||
}
|
||||
|
||||
private void addMockPackageToPackageManager(boolean isRecentlyInstalled, int flags) {
|
||||
final ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
applicationInfo.name = APP_LABEL;
|
||||
applicationInfo.uid = UID;
|
||||
applicationInfo.flags = flags;
|
||||
applicationInfo.packageName = PACKAGE_NAME;
|
||||
|
||||
final PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.packageName = PACKAGE_NAME;
|
||||
packageInfo.applicationInfo = applicationInfo;
|
||||
packageInfo.firstInstallTime = createAppInstallTime(isRecentlyInstalled);
|
||||
mPackageManager.addPackage(packageInfo);
|
||||
}
|
||||
|
||||
private long createAppInstallTime(boolean isRecentlyInstalled) {
|
||||
if (isRecentlyInstalled) {
|
||||
return System.currentTimeMillis() - NotificationChannelSlice.DURATION_END_DAYS;
|
||||
}
|
||||
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private void mockNotificationBackend(int channelCount, int notificationCount, boolean banned,
|
||||
boolean isChannelBlocked) {
|
||||
final List<NotificationChannel> channels = buildNotificationChannel(channelCount,
|
||||
isChannelBlocked);
|
||||
final AppRow appRow = buildAppRow(channelCount, notificationCount, banned);
|
||||
|
||||
doReturn(buildNotificationChannelGroups(channels)).when(mNotificationBackend).getGroups(
|
||||
any(String.class), any(int.class));
|
||||
doReturn(appRow).when(mNotificationBackend).loadAppRow(any(Context.class),
|
||||
any(PackageManager.class), any(RoleManager.class), any(PackageInfo.class));
|
||||
doReturn(channelCount).when(mNotificationBackend).getChannelCount(
|
||||
any(String.class), any(int.class));
|
||||
}
|
||||
|
||||
private AppRow buildAppRow(int channelCount, int sentCount, boolean banned) {
|
||||
final AppRow appRow = new AppRow();
|
||||
appRow.pkg = PACKAGE_NAME;
|
||||
appRow.uid = UID;
|
||||
appRow.banned = banned;
|
||||
appRow.channelCount = channelCount;
|
||||
appRow.sentByApp = new NotificationsSentState();
|
||||
appRow.sentByApp.sentCount = sentCount;
|
||||
appRow.sentByChannel = buildNotificationSentStates(channelCount, sentCount);
|
||||
|
||||
return appRow;
|
||||
}
|
||||
|
||||
private List<NotificationChannel> buildNotificationChannel(int channelCount,
|
||||
boolean isChannelBlock) {
|
||||
final List<NotificationChannel> channels = new ArrayList<>();
|
||||
for (int i = 0; i < channelCount; i++) {
|
||||
channels.add(new NotificationChannel(CHANNEL_NAME_PREFIX + i, CHANNEL_NAME_PREFIX + i,
|
||||
isChannelBlock ? IMPORTANCE_NONE : IMPORTANCE_LOW));
|
||||
}
|
||||
|
||||
return channels;
|
||||
}
|
||||
|
||||
private ParceledListSlice<NotificationChannelGroup> buildNotificationChannelGroups(
|
||||
List<NotificationChannel> channels) {
|
||||
final NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup(
|
||||
"group", "group");
|
||||
notificationChannelGroup.setBlocked(false);
|
||||
notificationChannelGroup.setChannels(channels);
|
||||
|
||||
return new ParceledListSlice(Arrays.asList(notificationChannelGroup));
|
||||
}
|
||||
|
||||
private Map<String, NotificationsSentState> buildNotificationSentStates(int channelCount,
|
||||
int sentCount) {
|
||||
final Map<String, NotificationBackend.NotificationsSentState> states = new ArrayMap<>();
|
||||
for (int i = 0; i < channelCount; i++) {
|
||||
final NotificationsSentState state = new NotificationsSentState();
|
||||
// Set the avgSentWeekly for each channel: channel0 is 1, channel1: 2, channel2: 3.
|
||||
state.avgSentWeekly = i + 1;
|
||||
state.sentCount = sentCount;
|
||||
states.put(CHANNEL_NAME_PREFIX + i, state);
|
||||
}
|
||||
|
||||
return states;
|
||||
}
|
||||
|
||||
private CharSequence getSummaryFromSliceItem(SliceItem rowItem) {
|
||||
if (rowItem == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Slice rowSlice = rowItem.getSlice();
|
||||
if (rowSlice == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<SliceItem> rowSliceItems = rowSlice.getItems();
|
||||
if (rowSliceItems == null || rowSliceItems.size() < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Index 0: title; Index 1: summary.
|
||||
return rowSliceItems.get(1).getText();
|
||||
}
|
||||
}
|
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.homepage.contextualcards.slices;
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice.PREFS;
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice.PREF_KEY_INTERACTED_PACKAGES;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.net.Uri;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadows.ShadowPackageManager;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NotificationChannelWorkerTest {
|
||||
private static final Uri URI = Uri.parse("content://com.android.settings.slices/test");
|
||||
private static final String PACKAGE_NAME = "com.test.notification.channel.slice";
|
||||
|
||||
private Context mContext;
|
||||
private NotificationChannelWorker mNotificationChannelWorker;
|
||||
private ShadowPackageManager mPackageManager;
|
||||
private SharedPreferences mSharedPreferences;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mNotificationChannelWorker = new NotificationChannelWorker(mContext, URI);
|
||||
|
||||
// Shadow PackageManager to add mock package.
|
||||
mPackageManager = shadowOf(mContext.getPackageManager());
|
||||
|
||||
mSharedPreferences = mContext.getSharedPreferences(PREFS, MODE_PRIVATE);
|
||||
addInteractedPackageToSharedPreference();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mPackageManager.removePackage(PACKAGE_NAME);
|
||||
removeInteractedPackageFromSharedPreference();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSliceUnpinned_interactedPackageIsUninstalled_shouldRemovePackage() {
|
||||
mNotificationChannelWorker.onSliceUnpinned();
|
||||
|
||||
final Set<String> interactedPackages = mSharedPreferences.getStringSet(
|
||||
PREF_KEY_INTERACTED_PACKAGES, new ArraySet<>());
|
||||
assertThat(interactedPackages.contains(PACKAGE_NAME)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSliceUnpinned_interactedPackageIsInstalled_shouldKeepPackage() {
|
||||
mockInteractedPackageAsInstalled();
|
||||
|
||||
mNotificationChannelWorker.onSliceUnpinned();
|
||||
|
||||
final Set<String> interactedPackages = mSharedPreferences.getStringSet(
|
||||
PREF_KEY_INTERACTED_PACKAGES, new ArraySet<>());
|
||||
assertThat(interactedPackages.contains(PACKAGE_NAME)).isTrue();
|
||||
}
|
||||
|
||||
private void mockInteractedPackageAsInstalled() {
|
||||
final PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.packageName = PACKAGE_NAME;
|
||||
mPackageManager.addPackage(packageInfo);
|
||||
}
|
||||
|
||||
private void addInteractedPackageToSharedPreference() {
|
||||
final Set<String> interactedPackages = new ArraySet<>();
|
||||
interactedPackages.add(PACKAGE_NAME);
|
||||
|
||||
mSharedPreferences.edit().putStringSet(PREF_KEY_INTERACTED_PACKAGES,
|
||||
interactedPackages).apply();
|
||||
}
|
||||
|
||||
private void removeInteractedPackageFromSharedPreference() {
|
||||
if (mSharedPreferences.contains(PREF_KEY_INTERACTED_PACKAGES)) {
|
||||
mSharedPreferences.edit().remove(PREF_KEY_INTERACTED_PACKAGES).apply();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.homepage.contextualcards.slices;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.app.role.RoleManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NotificationMultiChannelAppRowTest {
|
||||
|
||||
@Mock
|
||||
private NotificationBackend mNotificationBackend;
|
||||
private Context mContext;
|
||||
private NotificationMultiChannelAppRow mNotificationMultiChannelAppRow;
|
||||
private PackageInfo mPackageInfo;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mPackageInfo = new PackageInfo();
|
||||
mPackageInfo.applicationInfo = new ApplicationInfo();
|
||||
mPackageInfo.applicationInfo.packageName = "com.android.test";
|
||||
mNotificationMultiChannelAppRow = new NotificationMultiChannelAppRow(mContext,
|
||||
mNotificationBackend, mPackageInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void call_isMultiChannel_shouldLoadAppRow() throws Exception {
|
||||
doReturn(3).when(mNotificationBackend).getChannelCount(any(String.class),
|
||||
any(int.class));
|
||||
|
||||
mNotificationMultiChannelAppRow.call();
|
||||
|
||||
verify(mNotificationBackend).loadAppRow(any(Context.class), any(PackageManager.class),
|
||||
any(RoleManager.class), any(PackageInfo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void call_isNotMultiChannel_shouldNotLoadAppRow() throws Exception {
|
||||
doReturn(1).when(mNotificationBackend).getChannelCount(any(String.class),
|
||||
any(int.class));
|
||||
|
||||
mNotificationMultiChannelAppRow.call();
|
||||
|
||||
verify(mNotificationBackend, never()).loadAppRow(any(Context.class),
|
||||
any(PackageManager.class), any(RoleManager.class), any(PackageInfo.class));
|
||||
}
|
||||
}
|
@@ -37,7 +37,6 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
private static DevicePolicyManager sDevicePolicyManager;
|
||||
private static String[] sDisabledTypes;
|
||||
private static int sKeyguardDisabledFeatures;
|
||||
private static boolean sIsSuspended;
|
||||
|
||||
@Resetter
|
||||
public static void reset() {
|
||||
@@ -46,7 +45,6 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
sKeyguardDisabledFeatures = 0;
|
||||
sDisabledTypes = new String[0];
|
||||
sMaximumTimeToLockIsSet = false;
|
||||
sIsSuspended = false;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
@@ -103,12 +101,6 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
return sMaximumTimeToLockIsSet ? new EnforcedAdmin() : null;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
protected static EnforcedAdmin checkIfApplicationIsSuspended(Context context,
|
||||
String packageName, int userId) {
|
||||
return sIsSuspended ? new EnforcedAdmin() : null;
|
||||
}
|
||||
|
||||
public static void setRestricted(boolean restricted) {
|
||||
sIsRestricted = restricted;
|
||||
}
|
||||
@@ -140,8 +132,4 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
public static void setMaximumTimeToLockIsSet(boolean isSet) {
|
||||
sMaximumTimeToLockIsSet = isSet;
|
||||
}
|
||||
|
||||
public static void setSuspended(boolean suspended) {
|
||||
sIsRestricted = suspended;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user