import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.ActivityManager;
+import android.app.IActivityManager;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationChannel;
import android.metrics.LogMaker;
import android.net.Uri;
import android.os.Bundle;
+import android.os.RemoteException;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.Adjustment;
import android.service.notification.StatusBarNotification;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Slog;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.server.UiServiceTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.util.Objects;
@RunWith(AndroidJUnit4.class)
public class NotificationRecordTest extends UiServiceTestCase {
- private final Context mMockContext = Mockito.mock(Context.class);
+ private final Context mMockContext = mock(Context.class);
@Mock PackageManager mPm;
private final String pkg = PKG_N_MR1;
new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test",
NotificationManager.IMPORTANCE_UNSPECIFIED);
private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
- final ApplicationInfo legacy = new ApplicationInfo();
- final ApplicationInfo upgrade = new ApplicationInfo();
private static final long[] CUSTOM_VIBRATION = new long[] {
300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
when(mMockContext.getResources()).thenReturn(getContext().getResources());
when(mMockContext.getPackageManager()).thenReturn(mPm);
+ when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
}
- private StatusBarNotification getNotification(boolean preO, boolean noisy, boolean defaultSound,
+ private StatusBarNotification getNotification(String pkg, boolean noisy, boolean defaultSound,
boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights,
String group) {
- when(mMockContext.getApplicationInfo()).thenReturn(preO ? legacy : upgrade);
final Builder builder = new Builder(mMockContext)
.setContentTitle("foo")
.setSmallIcon(android.R.drawable.sym_def_app_icon)
}
builder.setDefaults(defaults);
- if (!preO) {
- builder.setChannelId(channelId);
- }
+ builder.setChannelId(channelId);
if(group != null) {
builder.setGroup(group);
}
Notification n = builder.build();
- if (preO) {
- return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n,
- mUser, null, uid);
- } else {
- return new StatusBarNotification(pkg2, pkg2, id2, tag2, uid2, uid2, n,
- mUser, null, uid2);
- }
+ return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid);
}
//
public void testSound_default_preUpgradeUsesNotification() throws Exception {
defaultChannel.setSound(null, null);
// pre upgrade, default sound.
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
public void testSound_custom_preUpgradeUsesNotification() throws Exception {
defaultChannel.setSound(null, null);
// pre upgrade, custom sound.
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
// pre upgrade, default sound.
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
@Test
public void testSound_noSound_preUpgrade() throws Exception {
// pre upgrade, default sound.
- StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
public void testSound_default_upgradeUsesChannel() throws Exception {
channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
// post upgrade, default sound.
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
public void testVibration_default_preUpgradeUsesNotification() throws Exception {
defaultChannel.enableVibration(false);
// pre upgrade, default vibration.
- StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
public void testVibration_custom_preUpgradeUsesNotification() throws Exception {
defaultChannel.enableVibration(false);
// pre upgrade, custom vibration.
- StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
defaultChannel.enableVibration(true);
defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
// pre upgrade, custom vibration.
- StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
public void testVibration_custom_upgradeUsesChannel() throws Exception {
channel.enableVibration(true);
// post upgrade, custom vibration.
- StatusBarNotification sbn = getNotification(false /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
@Test
public void testImportance_preUpgrade() throws Exception {
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
public void testImportance_locked_preUpgrade() throws Exception {
defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
public void testImportance_locked_unspecified_preUpgrade() throws Exception {
defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
@Test
public void testImportance_upgrade() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testLights_preUpgrade_noLight() throws Exception {
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
@Test
public void testLights_preUpgrade() throws Exception {
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
true /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
public void testLights_locked_preUpgrade() throws Exception {
defaultChannel.enableLights(true);
defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
- StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
true /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord.Light expected = new NotificationRecord.Light(
defaultLightColor, defaultLightOn, defaultLightOff);
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
true /* lights */, true /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
NotificationRecord.Light expected = new NotificationRecord.Light(
Color.BLUE, defaultLightOn, defaultLightOff);
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
true /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testLights_upgrade_noLight() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
@Test
public void testLogmakerShortChannel() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testLogmakerLongChannel() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /*defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channelLongId);
@Test
public void testLogmakerNoGroup() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /*defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testLogmakerShortGroup() throws Exception {
- StatusBarNotification sbn = getNotification(false /*reO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testLogmakerLongGroup() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupIdLong /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testLogmakerOverrideGroup() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testNotificationStats() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testUserSentiment() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testUserSentiment_appImportanceUpdatesSentiment() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testUserSentiment_appImportanceBlocksNegativeSentimentUpdate() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testUserSentiment_userLocked() throws Exception {
channel.lockFields(USER_LOCKED_IMPORTANCE);
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testAppImportance_returnsCorrectly() throws Exception {
- StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, groupId /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testIsInterruptive_textChanged_notSeen() {
- StatusBarNotification sbn = getNotification(false /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testIsInterruptive_textChanged_seen() {
- StatusBarNotification sbn = getNotification(false /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
@Test
public void testIsInterruptive_textNotChanged_seen() {
- StatusBarNotification sbn = getNotification(false /*preO */, false /* noisy */,
+ StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
false /* lights */, false /* defaultLights */, null /* group */);
NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
record.setSeen();
assertEquals(false, record.isInterruptive());
}
+
+ @Test
+ public void testCalculateGrantableUris_PappProvided() throws RemoteException {
+ IActivityManager am = mock(IActivityManager.class);
+ when(am.checkGrantUriPermission(anyInt(), eq(null), any(),
+ anyInt(), anyInt())).thenThrow(new SecurityException());
+
+ Notification n = mock(Notification.class);
+ when(n.getChannelId()).thenReturn(channel.getId());
+ StatusBarNotification sbn =
+ new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
+ NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
+ record.mAm = am;
+
+ try {
+ record.calculateGrantableUris();
+ fail("App provided uri for p targeting app should throw exception");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testCalculateGrantableUris_PuserOverridden() throws RemoteException {
+ IActivityManager am = mock(IActivityManager.class);
+ when(am.checkGrantUriPermission(anyInt(), eq(null), any(),
+ anyInt(), anyInt())).thenThrow(SecurityException.class);
+
+ channel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
+ Notification n = mock(Notification.class);
+ when(n.getChannelId()).thenReturn(channel.getId());
+ StatusBarNotification sbn =
+ new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
+ NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
+ record.mAm = am;
+
+ record.calculateGrantableUris();
+ }
+
+ @Test
+ public void testCalculateGrantableUris_prePappProvided() throws RemoteException {
+ IActivityManager am = mock(IActivityManager.class);
+ when(am.checkGrantUriPermission(anyInt(), eq(null), any(),
+ anyInt(), anyInt())).thenThrow(SecurityException.class);
+
+ Notification n = mock(Notification.class);
+ when(n.getChannelId()).thenReturn(channel.getId());
+ StatusBarNotification sbn =
+ new StatusBarNotification(PKG_O, PKG_O, id1, tag1, uid, uid, n, mUser, null, uid);
+ NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
+ record.mAm = am;
+
+ record.calculateGrantableUris();
+ // should not throw
+ }
}