summaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/src/com/android')
-rw-r--r--tests/unit/src/com/android/car/notification/CarHeadsUpNotificationManagerTest.java34
-rw-r--r--tests/unit/src/com/android/car/notification/CarHeadsUpNotificationQueueTest.java108
-rw-r--r--tests/unit/src/com/android/car/notification/CarNotificationListenerTest.java53
-rw-r--r--tests/unit/src/com/android/car/notification/HeadsUpNotificationOnTouchListenerTest.java398
4 files changed, 496 insertions, 97 deletions
diff --git a/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationManagerTest.java b/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationManagerTest.java
index dd84822a..0c09c006 100644
--- a/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationManagerTest.java
+++ b/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationManagerTest.java
@@ -24,6 +24,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -37,11 +38,13 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
+import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.testing.TestableContext;
+import android.view.View;
import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -54,6 +57,8 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -106,7 +111,9 @@ public class CarHeadsUpNotificationManagerTest {
@Mock
KeyguardManager mKeyguardManager;
@Mock
- HeadsUpEntry mHeadsUpEntry;
+ Handler mHandlerMock;
+ @Captor
+ ArgumentCaptor<View> mViewCaptor;
private CarHeadsUpNotificationManager mManager;
private AlertEntry mAlertEntryMessageHeadsUp;
private AlertEntry mAlertEntryNavigationHeadsUp;
@@ -460,6 +467,22 @@ public class CarHeadsUpNotificationManagerTest {
CarHeadsUpNotificationManager.HeadsUpState.REMOVED_FROM_QUEUE);
}
+ @Test
+ public void clearCache_viewsRemovedFromCarHeadsUpNotificationContainer() {
+ CarHeadsUpNotificationContainer container = mManager.mHunContainer;
+ HeadsUpEntry notification1 = createMockHeadsUpEntry("key1");
+ HeadsUpEntry notification2 = createMockHeadsUpEntry("key2");
+ mManager.addActiveHeadsUpNotification(notification1);
+ mManager.addActiveHeadsUpNotification(notification2);
+
+ mManager.clearCache();
+
+ verify(container, times(2)).removeNotification(mViewCaptor.capture());
+ assertThat(mViewCaptor.getAllValues().containsAll(List.of(new View[]{
+ notification1.getNotificationView(), notification2.getNotificationView()})))
+ .isTrue();
+ }
+
private void createCarHeadsUpNotificationManager() {
createCarHeadsUpNotificationManager(mCarHeadsUpNotificationQueue);
}
@@ -491,4 +514,13 @@ public class CarHeadsUpNotificationManagerTest {
when(mPackageManager.getPackageInfoAsUser(eq(packageName), anyInt(), anyInt())).thenReturn(
packageInfo);
}
+
+ private HeadsUpEntry createMockHeadsUpEntry(String key) {
+ HeadsUpEntry headsUpEntry = mock(HeadsUpEntry.class);
+ when(headsUpEntry.getKey()).thenReturn(key);
+ when(headsUpEntry.getHandler()).thenReturn(mHandlerMock);
+ View headsUpNotificationView = mock(View.class);
+ when(headsUpEntry.getNotificationView()).thenReturn(headsUpNotificationView);
+ return headsUpEntry;
+ }
} \ No newline at end of file
diff --git a/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationQueueTest.java b/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationQueueTest.java
index ce87e896..1a465399 100644
--- a/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationQueueTest.java
+++ b/tests/unit/src/com/android/car/notification/CarHeadsUpNotificationQueueTest.java
@@ -43,7 +43,6 @@ import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.testing.TestableContext;
-import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -68,7 +67,6 @@ import java.util.Collections;
import java.util.PriorityQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@RunWith(AndroidJUnit4.class)
@@ -90,7 +88,6 @@ public class CarHeadsUpNotificationQueueTest {
@Mock
private NotificationManager mNotificationManager;
-
@Captor
private ArgumentCaptor<TaskStackListener> mTaskStackListenerArg;
@Captor
@@ -118,7 +115,6 @@ public class CarHeadsUpNotificationQueueTest {
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>(Collections.singletonList(mock(AlertEntry.class))));
ExtendedMockito.doReturn(USER_ID).when(() -> NotificationUtils.getCurrentUser(any()));
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
}
@After
@@ -130,30 +126,13 @@ public class CarHeadsUpNotificationQueueTest {
}
private CarHeadsUpNotificationQueue createCarHeadsUpNotificationQueue() {
- return createCarHeadsUpNotificationQueue(
- /* activityTaskManager= */ null,
- /* notificationManager= */ null,
- /* scheduledExecutorService= */ null,
- /* callback= */ null);
- }
-
- private CarHeadsUpNotificationQueue createCarHeadsUpNotificationQueue(
- @Nullable ActivityTaskManager activityTaskManager,
- @Nullable NotificationManager notificationManager,
- @Nullable ScheduledExecutorService scheduledExecutorService,
- @Nullable CarHeadsUpNotificationQueue.CarHeadsUpNotificationQueueCallback callback) {
- return new CarHeadsUpNotificationQueue(mContext,
- activityTaskManager != null ? activityTaskManager
- : ActivityTaskManager.getInstance(),
- notificationManager != null ? notificationManager
- : mContext.getSystemService(NotificationManager.class),
- scheduledExecutorService != null ? scheduledExecutorService
- : new ScheduledThreadPoolExecutor(/* corePoolSize= */ 1),
- callback != null ? callback : mCarHeadsUpNotificationQueueCallback);
+ return new CarHeadsUpNotificationQueue(mContext, mActivityTaskManager, mNotificationManager,
+ mScheduledExecutorService, mCarHeadsUpNotificationQueueCallback);
}
@Test
public void addToQueue_prioritises_postTimeOfHeadsUp() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 4000);
AlertEntry alertEntry2 = new AlertEntry(generateMockStatusBarNotification(
@@ -245,6 +224,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void addToQueue_merges_newHeadsUpWithSameKey() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry alertEntry2 = new AlertEntry(generateMockStatusBarNotification(
@@ -289,6 +269,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void addToQueue_handles_notificationWithNoCategory() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", /* category= */ null), 4000);
@@ -493,11 +474,9 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void triggerCallback_sendsNotificationToCurrentUser_onHunExpiredAndEmptyQueue() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>());
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- /* activityTaskManager= */ null, mNotificationManager,
- /* scheduledExecutorService= */ null, mCarHeadsUpNotificationQueueCallback);
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
mCarHeadsUpNotificationQueue.addToPriorityQueue(alertEntry1);
@@ -550,10 +529,7 @@ public class CarHeadsUpNotificationQueueTest {
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
mContext.getOrCreateTestableResources().addOverride(
R.array.headsup_throttled_foreground_packages, /* value= */ new String[]{PKG_1});
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager,
- /* notificationManager= */ null, /* scheduledExecutorService= */ null,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
verify(mActivityTaskManager).registerTaskStackListener(mTaskStackListenerArg.capture());
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
@@ -577,10 +553,7 @@ public class CarHeadsUpNotificationQueueTest {
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
mContext.getOrCreateTestableResources().addOverride(
R.array.headsup_throttled_foreground_packages, /* value= */ new String[]{PKG_1});
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager,
- /* notificationManager= */ null, /* scheduledExecutorService= */ null,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
verify(mActivityTaskManager).registerTaskStackListener(mTaskStackListenerArg.capture());
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
@@ -605,9 +578,7 @@ public class CarHeadsUpNotificationQueueTest {
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
mContext.getOrCreateTestableResources().addOverride(
R.array.headsup_throttled_foreground_packages, /* value= */ new String[]{PKG_1});
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager, /* notificationManager= */ null, mScheduledExecutorService,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
verify(mActivityTaskManager).registerTaskStackListener(mTaskStackListenerArg.capture());
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
@@ -633,9 +604,7 @@ public class CarHeadsUpNotificationQueueTest {
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
mContext.getOrCreateTestableResources().addOverride(
R.array.headsup_throttled_foreground_packages, /* value= */ new String[]{PKG_1});
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager, /* notificationManager= */ null, mScheduledExecutorService,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
verify(mActivityTaskManager).registerTaskStackListener(mTaskStackListenerArg.capture());
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
@@ -656,6 +625,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void removeFromQueue_returnsFalse_whenNotificationNotInQueue() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry alertEntry2 = new AlertEntry(generateMockStatusBarNotification(
@@ -672,6 +642,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void removeFromQueue_returnsTrue_whenNotificationInQueue() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry alertEntry2 = new AlertEntry(generateMockStatusBarNotification(
@@ -688,10 +659,7 @@ public class CarHeadsUpNotificationQueueTest {
public void releaseQueue_removes_notificationsFromQueue() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_dismissHeadsUpWhenNotificationCenterOpens, /* value= */ false);
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager,
- /* notificationManager= */ null, /* scheduledExecutorService= */ null,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
AlertEntry alertEntry2 = new AlertEntry(generateMockStatusBarNotification(
@@ -710,14 +678,11 @@ public class CarHeadsUpNotificationQueueTest {
public void releaseQueue_dismiss_activeHUNs() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_dismissHeadsUpWhenNotificationCenterOpens, /* value= */ true);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>(Collections.singletonList(alertEntry)));
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager,
- /* notificationManager= */ null, /* scheduledExecutorService= */ null,
- mCarHeadsUpNotificationQueueCallback);
mCarHeadsUpNotificationQueue.releaseQueue();
@@ -729,16 +694,13 @@ public class CarHeadsUpNotificationQueueTest {
public void releaseQueue_doesNot_dismiss_nonDismissibleHUNs() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_dismissHeadsUpWhenNotificationCenterOpens, /* value= */ true);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(
generateMockStatusBarNotification("key1", Notification.CATEGORY_CALL,
/* isOngoing= */ true, /* hasFullScreenIntent= */ true),
/* postTime= */ 1000);
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>(Collections.singletonList(alertEntry)));
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager,
- /* notificationManager= */ null, /* scheduledExecutorService= */ null,
- mCarHeadsUpNotificationQueueCallback);
mCarHeadsUpNotificationQueue.releaseQueue();
@@ -747,6 +709,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void onStateChange_internalCategory_hunRemovalFlagTrue_setHunRemovalFlagToFalse() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "HUN_QUEUE_INTERNAL"), 1000);
mCarHeadsUpNotificationQueue.mCancelInternalNotificationOnStateChange = true;
@@ -759,6 +722,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void onStateChange_internalCategory_hunRemovalFlagTrue_setHunExpiredToFalse() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "HUN_QUEUE_INTERNAL"), 1000);
mCarHeadsUpNotificationQueue.mCancelInternalNotificationOnStateChange = true;
@@ -771,9 +735,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void onStateChange_internalCategory_hunRemovalFlagTrue_cancelHunForCurrentUser() {
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- /* activityTaskManager= */ null, mNotificationManager,
- /* scheduledExecutorService= */ null, mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "HUN_QUEUE_INTERNAL"), 1000);
mCarHeadsUpNotificationQueue.mCancelInternalNotificationOnStateChange = true;
@@ -787,9 +749,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void onStateChange_notInternalCategory_hunRemovalFlagTrue_notCancelHunForCurrentUser() {
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- /* activityTaskManager= */ null, mNotificationManager,
- /* scheduledExecutorService= */ null, mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
mCarHeadsUpNotificationQueue.mCancelInternalNotificationOnStateChange = true;
@@ -803,6 +763,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void onStateChange_notInternalCategory_hunRemovalFlagTrue_doesNotsetHunRemovalFlag() {
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
mCarHeadsUpNotificationQueue.mCancelInternalNotificationOnStateChange = true;
@@ -817,11 +778,9 @@ public class CarHeadsUpNotificationQueueTest {
public void onStateChange_dismissed_callbackScheduled() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>());
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- /* activityTaskManager= */ null, /* notificationManager= */ null,
- mScheduledExecutorService, mCarHeadsUpNotificationQueueCallback);
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry nextAlertEntry = new AlertEntry(generateMockStatusBarNotification(
@@ -839,11 +798,9 @@ public class CarHeadsUpNotificationQueueTest {
public void onStateChange_removedBySender_callbackScheduled() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>());
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- /* activityTaskManager= */ null, /* notificationManager= */ null,
- mScheduledExecutorService, mCarHeadsUpNotificationQueueCallback);
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry nextAlertEntry = new AlertEntry(generateMockStatusBarNotification(
@@ -861,9 +818,9 @@ public class CarHeadsUpNotificationQueueTest {
public void onStateChange_shown_doesNot_showNextNotificationInQueue() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>());
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry nextAlertEntry = new AlertEntry(generateMockStatusBarNotification(
@@ -881,9 +838,9 @@ public class CarHeadsUpNotificationQueueTest {
public void onStateChange_removedFromQueue_does_not_showNextNotificationInQueue() {
mContext.getOrCreateTestableResources().addOverride(
R.bool.config_expireHeadsUpWhenParked, /* value= */ false);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
when(mCarHeadsUpNotificationQueueCallback.getActiveHeadsUpNotifications()).thenReturn(
new ArrayList<>());
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), 1000);
AlertEntry nextAlertEntry = new AlertEntry(generateMockStatusBarNotification(
@@ -926,9 +883,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void scheduleCallback_schedulesNewTask_whenNoTaskScheduled() {
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager, /* notificationManager= */ null, mScheduledExecutorService,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
mCarHeadsUpNotificationQueue.addToPriorityQueue(alertEntry1);
@@ -945,9 +900,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void scheduleCallback_schedulesNewTask_whenShorterTaskScheduled() {
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager, /* notificationManager= */ null, mScheduledExecutorService,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
mCarHeadsUpNotificationQueue.addToPriorityQueue(alertEntry1);
@@ -966,9 +919,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void scheduleCallback_cancelsFutureTask_whenShorterTaskScheduled() {
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager, /* notificationManager= */ null, mScheduledExecutorService,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
mCarHeadsUpNotificationQueue.addToPriorityQueue(alertEntry1);
@@ -986,9 +937,7 @@ public class CarHeadsUpNotificationQueueTest {
@Test
public void scheduleCallback_doesNotScheduleNewTask_whenLongerTaskScheduled() {
- mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue(
- mActivityTaskManager, /* notificationManager= */ null, mScheduledExecutorService,
- mCarHeadsUpNotificationQueueCallback);
+ mCarHeadsUpNotificationQueue = createCarHeadsUpNotificationQueue();
AlertEntry alertEntry1 = new AlertEntry(generateMockStatusBarNotification(
"key1", "msg"), /* postTime= */ 1000);
mCarHeadsUpNotificationQueue.addToPriorityQueue(alertEntry1);
@@ -1034,5 +983,4 @@ public class CarHeadsUpNotificationQueueTest {
mockRunningTaskInfo.displayAreaFeatureId = displayAreaFeatureId;
return mockRunningTaskInfo;
}
-
}
diff --git a/tests/unit/src/com/android/car/notification/CarNotificationListenerTest.java b/tests/unit/src/com/android/car/notification/CarNotificationListenerTest.java
index 380d883e..85f49056 100644
--- a/tests/unit/src/com/android/car/notification/CarNotificationListenerTest.java
+++ b/tests/unit/src/com/android/car/notification/CarNotificationListenerTest.java
@@ -20,8 +20,10 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -30,6 +32,7 @@ import android.app.NotificationManager;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
+import android.os.RemoteException;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
@@ -37,8 +40,7 @@ import android.service.notification.StatusBarNotification;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
-import com.android.internal.statusbar.IStatusBarService;
-
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -52,16 +54,15 @@ public class CarNotificationListenerTest {
private static final int CURRENT_USER_ID = 10;
private static final String TEST_KEY = "TEST_KEY";
- private static final String TEST_OVERRIDE_GROUP_KEY = "TEST_OVERRIDE_GROUP_KEY";
+ private static final String TEST_OLD_OVERRIDE_GROUP_KEY = "TEST_OLD_OVERRIDE_GROUP_KEY";
+ private static final String TEST_NEW_OVERRIDE_GROUP_KEY = "TEST_NEW_OVERRIDE_GROUP_KEY";
- private Context mContext;
+ private final Object mLock = new Object();
private CarNotificationListener mCarNotificationListener;
private NotificationListenerService.RankingMap mRankingMap;
@Mock
private CarHeadsUpNotificationManager mCarHeadsUpNotificationManager;
@Mock
- private IStatusBarService mBarService;
- @Mock
private Handler mHandler;
@Mock
private StatusBarNotification mStatusBarNotification;
@@ -71,23 +72,43 @@ public class CarNotificationListenerTest {
private CarUxRestrictionManagerWrapper mCarUxRestrictionManagerWrapper;
@Before
- public void setup() {
+ public void setup() throws InterruptedException {
MockitoAnnotations.initMocks(this);
- mContext = ApplicationProvider.getApplicationContext();
- mCarNotificationListener = new CarNotificationListener();
+ Context context = ApplicationProvider.getApplicationContext();
+ mCarNotificationListener = spy(new CarNotificationListener());
mCarNotificationListener.setHandler(mHandler);
- mCarNotificationListener.registerAsSystemService(mContext, mCarUxRestrictionManagerWrapper,
+ // wait for the listener to be connected before going further
+ doAnswer(a -> {
+ a.callRealMethod();
+ synchronized (mLock) {
+ mLock.notify();
+ }
+ return null;
+ }).when(mCarNotificationListener).onListenerConnected();
+ mCarNotificationListener.registerAsSystemService(context, mCarUxRestrictionManagerWrapper,
mCarHeadsUpNotificationManager);
+ synchronized (mLock) {
+ while (!mCarNotificationListener.getIsListenerConnected()) {
+ mLock.wait();
+ }
+ }
mCarNotificationListener.setNotificationDataManager(mNotificationDataManager);
createMockRankingMap(NotificationManager.IMPORTANCE_DEFAULT);
when(mStatusBarNotification.getKey()).thenReturn(TEST_KEY);
- when(mStatusBarNotification.getOverrideGroupKey()).thenReturn(TEST_OVERRIDE_GROUP_KEY);
+ when(mStatusBarNotification.getOverrideGroupKey()).thenReturn(TEST_OLD_OVERRIDE_GROUP_KEY);
when(mStatusBarNotification.getUser()).thenReturn(new UserHandle(CURRENT_USER_ID));
}
+ @After
+ public void tearDown() throws RemoteException {
+ if (mCarNotificationListener != null) {
+ mCarNotificationListener.unregisterAsSystemService();
+ }
+ }
+
@Test
public void onNotificationPosted_isHun_notForCurrentUser_ignoresTheEvent() {
UserHandle userHandle = new UserHandle(UserHandle.USER_NULL);
@@ -166,7 +187,7 @@ public class CarNotificationListenerTest {
mCarNotificationListener.onNotificationPosted(mStatusBarNotification, mRankingMap);
- verify(mStatusBarNotification).setOverrideGroupKey(eq(null));
+ verify(mStatusBarNotification).setOverrideGroupKey(eq(TEST_NEW_OVERRIDE_GROUP_KEY));
}
@Test
@@ -306,7 +327,7 @@ public class CarNotificationListenerTest {
mCarNotificationListener.onStateChange(alertEntry,
CarHeadsUpNotificationManager.HeadsUpState.DISMISSED);
- verify(mStatusBarNotification).setOverrideGroupKey(eq(null));
+ verify(mStatusBarNotification).setOverrideGroupKey(eq(TEST_NEW_OVERRIDE_GROUP_KEY));
}
@Test
@@ -338,7 +359,7 @@ public class CarNotificationListenerTest {
testingHeadsUpNotification(false);
UserHandle userHandle = new UserHandle(CURRENT_USER_ID);
when(mStatusBarNotification.getUser()).thenReturn(userHandle);
- when(mStatusBarNotification.getOverrideGroupKey()).thenReturn(null);
+ when(mStatusBarNotification.getOverrideGroupKey()).thenReturn(TEST_NEW_OVERRIDE_GROUP_KEY);
mCarNotificationListener.onNotificationPosted(mStatusBarNotification, mRankingMap);
reset(mHandler);
@@ -354,7 +375,7 @@ public class CarNotificationListenerTest {
when(mStatusBarNotification.getUser()).thenReturn(userHandle);
mCarNotificationListener.onNotificationPosted(mStatusBarNotification, mRankingMap);
when(mStatusBarNotification.getOverrideGroupKey())
- .thenReturn(TEST_OVERRIDE_GROUP_KEY + "A");
+ .thenReturn(TEST_OLD_OVERRIDE_GROUP_KEY + "A");
reset(mHandler);
mCarNotificationListener.onNotificationRankingUpdate(mRankingMap);
@@ -384,7 +405,7 @@ public class CarNotificationListenerTest {
/* suppressedVisualEffects= */ 0,
/* importance= */ importance,
/* explanation= */ null,
- /* overrideGroupKey= */ null,
+ TEST_NEW_OVERRIDE_GROUP_KEY,
/* channel= */ null,
/* overridePeople= */ new ArrayList<>(),
/* snoozeCriteria= */ new ArrayList<>(),
diff --git a/tests/unit/src/com/android/car/notification/HeadsUpNotificationOnTouchListenerTest.java b/tests/unit/src/com/android/car/notification/HeadsUpNotificationOnTouchListenerTest.java
new file mode 100644
index 00000000..e2cd469b
--- /dev/null
+++ b/tests/unit/src/com/android/car/notification/HeadsUpNotificationOnTouchListenerTest.java
@@ -0,0 +1,398 @@
+/*
+ * Copyright (C) 2023 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.car.notification;
+
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyFloat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.animation.Animator;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.testing.TestableContext;
+import android.testing.TestableResources;
+import android.view.MotionEvent;
+import android.view.VelocityTracker;
+import android.view.View;
+import android.view.ViewParent;
+import android.view.ViewPropertyAnimator;
+import android.view.ViewTreeObserver;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.stubbing.Answer;
+
+@RunWith(AndroidJUnit4.class)
+public class HeadsUpNotificationOnTouchListenerTest {
+ private static final int TRANSLATION = 5;
+ private static final int VELOCITY = 5;
+ private static final int VIEW_SIZE = 20;
+
+ @Rule
+ public final TestableContext mContext = new TestableContext(
+ InstrumentationRegistry.getInstrumentation().getTargetContext()) {
+ @Override
+ public Context createApplicationContext(ApplicationInfo application, int flags) {
+ return this;
+ }
+ };
+ private TestableResources mTestableResources;
+ private HeadsUpNotificationOnTouchListener mHeadsUpNotificationOnTouchListener;
+ @Mock
+ private View mView;
+ @Mock
+ private ViewParent mViewParent;
+ @Mock
+ private ViewPropertyAnimator mViewPropertyAnimator;
+ @Mock
+ private ViewTreeObserver mViewTreeObserver;
+ @Mock
+ private MotionEvent mDownMotionEvent;
+ @Mock
+ private MotionEvent mMoveMotionEventLessThanTouchSlop;
+ @Mock
+ private MotionEvent mMoveMotionEventMoreThanTouchSlop;
+ @Mock
+ private MotionEvent mCancelMotionEvent;
+ @Mock
+ private MotionEvent mUpMotionEventMoreThanThreshold;
+ @Mock
+ private MotionEvent mUpMotionEventLessThanThreshold;
+ @Mock
+ private MotionEvent mNewMotionEvent;
+ @Mock
+ private VelocityTracker mVelocityTracker;
+ @Mock
+ private HeadsUpNotificationOnTouchListener.DismissCallbacks mDismissCallbacks;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mTestableResources = mContext.getOrCreateTestableResources();
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, false);
+ mTestableResources.addOverride(R.dimen.max_translation_headsup, 0f);
+ setUpView();
+ createHeadsUpNotificationOnTouchListener();
+ setUpMotionEvents();
+ }
+
+ @Test
+ public void onTouch_actionCancel_viewTranslationReset() {
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mCancelMotionEvent);
+
+ verify(mView).animate();
+ verify(mViewPropertyAnimator).translationX(0);
+ verify(mViewPropertyAnimator).translationY(0);
+ }
+
+ @Test
+ public void onTouch_actionCancel_viewAlphaReset() {
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mCancelMotionEvent);
+
+ verify(mView).animate();
+ verify(mViewPropertyAnimator).alpha(1);
+ }
+
+ @Test
+ public void onTouch_actionCancel_callbackNotSent() {
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mCancelMotionEvent);
+
+ verify(mDismissCallbacks, times(0)).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionMove_moveLessThanTouchSlop_horizontal_returnsFalse() {
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ boolean returnValue = mHeadsUpNotificationOnTouchListener
+ .onTouch(mView, mMoveMotionEventLessThanTouchSlop);
+
+ assertThat(returnValue).isFalse();
+ }
+
+ @Test
+ public void onTouch_actionMove_moveLessThanTouchSlop_vertical_returnsFalse() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ boolean returnValue = mHeadsUpNotificationOnTouchListener
+ .onTouch(mView, mMoveMotionEventLessThanTouchSlop);
+
+ assertThat(returnValue).isFalse();
+ }
+
+ @Test
+ public void onTouch_actionMove_moveLessThanTouchSlop_horizontal_viewNotMoved() {
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventLessThanTouchSlop);
+
+ verify(mView, times(0)).setTranslationX(anyFloat());
+ }
+
+ @Test
+ public void onTouch_actionMove_moveLessThanTouchSlop_vertical_viewNotMoved() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventLessThanTouchSlop);
+
+ verify(mView, times(0)).setTranslationY(anyFloat());
+ }
+
+ @Test
+ public void onTouch_actionMove_moveMoreThanTouchSlop_horizontal_viewTranslationSet() {
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ verify(mView, times(1)).setTranslationX(TRANSLATION);
+ verify(mView, times(0)).setTranslationY(anyFloat());
+
+ }
+
+ @Test
+ public void onTouch_actionMove_moveMoreThanTouchSlop_vertical_viewTranslationSet() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ verify(mView, times(1)).setTranslationY(TRANSLATION);
+ verify(mView, times(0)).setTranslationX(anyFloat());
+ }
+
+ @Test
+ public void onTouch_actionUp_moveMoreThanMin_horizontal_callbackSent() {
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventMoreThanThreshold);
+
+ verify(mDismissCallbacks, times(1)).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_moveMoreThanMin_vertical_callbackSent() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventMoreThanThreshold);
+
+ verify(mDismissCallbacks, times(1)).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_flingMoreThanThreshold_horizontal_callbackSent() {
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() + VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventLessThanThreshold);
+
+ verify(mDismissCallbacks, times(1)).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_flingMoreThanThreshold_vertical_callbackSent() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() + VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventLessThanThreshold);
+
+ verify(mDismissCallbacks, times(1)).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_flingEqualInBothDirection_horizontal_callbackNotSent() {
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() + VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() + VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventLessThanThreshold);
+
+ verify(mDismissCallbacks, never()).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_flingEqualInBothDirection_vertical_callbackNotSent() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() + VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() + VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventLessThanThreshold);
+
+ verify(mDismissCallbacks, never()).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_moveLessThanMin_velocityLessThanMin_horizontal_callbackNotSent() {
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventLessThanThreshold);
+
+ verify(mDismissCallbacks, never()).onDismiss();
+ }
+
+ @Test
+ public void onTouch_actionUp_moveLessThanMin_velocityLessThanMin_vertical_callbackNotSent() {
+ mTestableResources
+ .addOverride(R.bool.config_isHeadsUpNotificationDismissibleVertically, true);
+ createHeadsUpNotificationOnTouchListener();
+ when(mVelocityTracker.getXVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ when(mVelocityTracker.getYVelocity()).thenReturn(
+ (float) mHeadsUpNotificationOnTouchListener.getMinimumFlingVelocity() - VELOCITY);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mDownMotionEvent);
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mMoveMotionEventMoreThanTouchSlop);
+
+ mHeadsUpNotificationOnTouchListener.onTouch(mView, mUpMotionEventLessThanThreshold);
+
+ verify(mDismissCallbacks, never()).onDismiss();
+ }
+
+ private void setUpView() {
+ when(mView.getContext()).thenReturn(mContext);
+ when(mView.getViewTreeObserver()).thenReturn(mViewTreeObserver);
+ doAnswer((Answer<Void>) invocation -> {
+ ViewTreeObserver.OnGlobalLayoutListener listener = invocation.getArgument(0);
+ listener.onGlobalLayout();
+ return null;
+ }).when(mViewTreeObserver).addOnGlobalLayoutListener(
+ any(ViewTreeObserver.OnGlobalLayoutListener.class));
+ when(mView.animate()).thenReturn(mViewPropertyAnimator);
+ when(mViewPropertyAnimator.alpha(anyFloat())).thenReturn(mViewPropertyAnimator);
+ when(mViewPropertyAnimator.translationX(anyFloat())).thenReturn(mViewPropertyAnimator);
+ when(mViewPropertyAnimator.translationY(anyFloat())).thenReturn(mViewPropertyAnimator);
+ when(mViewPropertyAnimator.setListener(nullable(Animator.AnimatorListener.class)))
+ .thenReturn(mViewPropertyAnimator);
+ when(mView.getHeight()).thenReturn(VIEW_SIZE);
+ when(mView.getWidth()).thenReturn(VIEW_SIZE);
+ when(mView.getParent()).thenReturn(mViewParent);
+ }
+
+ private void setUpMotionEvents() {
+ float touchSlop = (float) mHeadsUpNotificationOnTouchListener.getTouchSlop();
+ float percentageOfMaxTransaltionToDismiss = (float) mHeadsUpNotificationOnTouchListener
+ .getPercentageOfMaxTransaltionToDismiss();
+ when(mDownMotionEvent.getActionMasked()).thenReturn(MotionEvent.ACTION_DOWN);
+ when(mDownMotionEvent.getRawX()).thenReturn((float) 0);
+ when(mDownMotionEvent.getRawY()).thenReturn((float) 0);
+
+ when(mCancelMotionEvent.getActionMasked()).thenReturn(MotionEvent.ACTION_CANCEL);
+
+ when(mMoveMotionEventLessThanTouchSlop.getActionMasked())
+ .thenReturn(MotionEvent.ACTION_MOVE);
+ when(mMoveMotionEventLessThanTouchSlop.getRawX()).thenReturn(touchSlop - TRANSLATION);
+ when(mMoveMotionEventLessThanTouchSlop.getRawY()).thenReturn(touchSlop - TRANSLATION);
+
+
+ when(mMoveMotionEventMoreThanTouchSlop.getActionMasked())
+ .thenReturn(MotionEvent.ACTION_MOVE);
+ when(mMoveMotionEventMoreThanTouchSlop.getRawX()).thenReturn(touchSlop + TRANSLATION);
+ when(mMoveMotionEventMoreThanTouchSlop.getRawY()).thenReturn(touchSlop + TRANSLATION);
+
+ when(mUpMotionEventMoreThanThreshold.getActionMasked())
+ .thenReturn(MotionEvent.ACTION_UP);
+ when(mUpMotionEventMoreThanThreshold.getRawX()).thenReturn(
+ percentageOfMaxTransaltionToDismiss * VIEW_SIZE + TRANSLATION);
+ when(mUpMotionEventMoreThanThreshold.getRawY()).thenReturn(
+ percentageOfMaxTransaltionToDismiss * VIEW_SIZE + TRANSLATION);
+
+ when(mUpMotionEventLessThanThreshold.getActionMasked())
+ .thenReturn(MotionEvent.ACTION_UP);
+ when(mUpMotionEventLessThanThreshold.getRawX()).thenReturn(
+ percentageOfMaxTransaltionToDismiss * VIEW_SIZE - TRANSLATION);
+ when(mUpMotionEventLessThanThreshold.getRawY()).thenReturn(
+ percentageOfMaxTransaltionToDismiss * VIEW_SIZE - TRANSLATION);
+
+ }
+
+ private void createHeadsUpNotificationOnTouchListener() {
+ mHeadsUpNotificationOnTouchListener = new HeadsUpNotificationOnTouchListener(mView,
+ /* dismissOnSwipe= */ true, mDismissCallbacks) {
+ @Override
+ MotionEvent obtainMotionEvent(MotionEvent motionEvent) {
+ return mNewMotionEvent;
+ }
+
+ @Override
+ VelocityTracker obtainVelocityTracker() {
+ return mVelocityTracker;
+ }
+ };
+ }
+}